custom fxcop rule to check is default present in switch

Hi,

I wrote a custom fxcop rule to check Is Default present in FxCop. After some days when i checked it i found that it was not working properly.

Do give any any input on how to check is default present in a Switch statement or not.

I have attached below the code that i wrote previously;

publicoverride ProblemCollection Check(Member member)

{

if (member.NodeType == NodeType.Method)

{

Method method = memberas Method;

Instruction instruction =null;

InstructionList instructions = method.Instructions;

for(int i=0;i<instructions.Length;i++)

{

instruction = instructionsIdea;

if(instruction.OpCode == OpCode.Switch||instruction.OpCode == OpCode.Beq||instruction.OpCode == OpCode.Beq_S1)

{

if(IsDefault(instructions,i)==false)

{

Problems.Add(new Problem(GetResolution("Default is not present in switch statement")));

}

}

}

return Problems;

}

else

{

returnnull;

}

}

staticbool IsDefault(InstructionList instructions,int switchOffset)

{

Instruction instruction =null;

bool DefaultPresent =false;

string SwitchValue =null;

for(int i=switchOffset;i<instructions.Length;i++)

{

instruction = instructionsIdea;

if(instruction.OpCode == OpCode.Br_S)

{

SwitchValue = instruction.Value.ToString();

if(IsDefaultPresent(instructions,i,SwitchValue))

{

DefaultPresent =true;

}

}

}

return DefaultPresent;

}

staticbool IsDefaultPresent(InstructionList instructions,int BrInstruction,string BrValue)

{

Instruction instruction =null;

bool DefaultAvail =false;

string LdstrOffset =null;

for(int i = BrInstruction;i<instructions.Length;i++)

{

instruction = instructionsIdea;

if(instruction.OpCode == OpCode.Ldstr)

{

LdstrOffset = instruction.Offset.ToString(CultureInfo.InvariantCulture);

if(LdstrOffset.Equals(BrValue))

{

if(instruction.Value.Equals("Default"))

{

DefaultAvail =true;

}

}

}

}

return DefaultAvail;

}

[11088 byte] By [Guns] at [2007-12-22]
# 1

This analysis is not possible using FxCop. The issue is that switch statements compile to many different IL patterns, depending on the type of the case statements, or the values of the cases themselves. In many instances, the switch opcode itself is never emitted (as you've obviously noted). In C#, a switch statement against many string cases will result in a hashtable lookup.

You could attempt to write code that detected the lack of a default case by writing various IL patterns and observing patterns. But your analysis might not work for all language compilers (and versions). And it's likely your analysis would fire false positives on some code patterns that weren't generated by a switch statement.

Michael

MichaelFanning-MS at 2007-8-30 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 2

Hello,

@Guns

Does this really work ? :

.......

if(instruction.OpCode == OpCode.Ldstr)

{

LdstrOffset = instruction.Offset.ToString(CultureInfo.InvariantCulture);

if(LdstrOffset.Equals(BrValue))

{

if(instruction.Value.Equals("Default"))

{

DefaultAvail = true;

}

}

}

....

I've looked on IL generated for a switch-statement (with "default" instruction) and i couldn't see any ldstr

instruction with value "Default" (in my code there aren't any ldstr instrutions at all).

@FxCop Team.

I'm trying to get the scope of a switch statement in MSIL. A switch instruction in IL is looking something like

this:

...

IL_0006: switch ( IL_0019, IL_0019, IL_001e)

My question is, how can i get this labels (IL_0019, IL_0019, IL_001e) ? As i could see while debugging, they are declared as non-public members.

Thank You,

Anton.

Edit: Ok, i already got the answer. Just cast the Value of the switch instruction to Int32List Object and iterate

throw it.

AntonPapst at 2007-8-30 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 3

Hi Anton,

wheather this code work fine.....where to cast the value of switch instruction to int32List object.

thanks

Prakash

Prakashselvaraj at 2007-8-30 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...

Visual Studio Team System

Site Classified