Getting info about local variables.

How would you get information about variables local to a method? I thought looping over a method's instructions would get me there but it appears there is no opcode generated for local variable declaration. Instead there is a .locals init ( ) line. Is there a way to access that information? So far Method.Body.HasLocals is the only thing I've found to be of any use and I thought the Block class would lead me to my goal but so far it hasn't. Any advice?
[464 byte] By [RSX-S] at [2007-12-20]
# 1

Warning: this rules development API is likely to change in a future version of FxCop.

Currently, the locals of a method are stored in a pseudo-instruction in the method InstructionList. We plan to change this in a future version of the tool, the locals should hang directly off the method.

Here's a sample code pattern:



LocalList locals = null;
if (method.Instructions.Length > 0)
{
locals = method.Instructions[0].Value as LocalList;
}

if (locals != null)
{
foreach(Local local in locals)
{
// do something
}
}

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

I'm trying to check local variables too, but I'm using the VS2005 IDE integrated version of FxCop.

The post above works in the stand-alone version 1.35 but doesn't work when using the IDE integrated version.

Can you tell me how I check local variables in the integrated version?

Also is there any way I can debug my rules using the integrated version as obviously the FxCop reference DLLs differ between the two versions (Program Files | FxCop and Program Files | Visual Studio 8 | Team Tools etc) which means that I cannot rely on any debugging done using the stand-alone front end :(

Thanks

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

The mechanism for checking locals is the same, what's the problem you're seeing?

As long as you stick with 1.35, your rules debugging should be valid between the two. There's always the possibility, though, that we've fixed an issue in 1.35 or made some other change that alters behavior.

To debug your rules within the IDE, you would have to configure a reg key (AEDebug) that specifies the debugger should be launched anytime FxCopCmd.exe is invoked. Here's a link providing some information.

http://support.microsoft.com/default.aspx?scid=kb;en-us;103861&sd=tech

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

Sara,

For integration with the Visual Studio version, you will need to change:



foreach (Local local in locals)
{

}

to:



for (int i = 0; i < locals.Length; i++)
{
Local local = locals[i ];
}

The above code should work with both FxCop 1.35 and Visual Studio versions.

You might to check out the following post by Claudio, who is doing something similar to you: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=299899&SiteID=1

DavidM.Kean-MSFT at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 5

Thanks Michael & David,

As it happens I am actually using VB - not that that should make any difference - and I am already using a For ... Next loop rather than a For Each ... Next loop as shown below:


Dim memberMethod As Method = CType(member, Method)
Dim methodLocalsList As LocalList = CType(memberMethod.Instructions(0).Value, LocalList)
Dim methodLocal As Local

' check each local in the list
For i As Int32 = 0 To methodLocalsList.Length - 1
methodLocal = methodLocalsList.Item(i)

' perform check here

Next

The problem that I'm seeing is that when I run the test in stand-alone FxCop I see the rule failure messages I expected to see, since I've purposefully added local variables to a method which fail the rule to test it, and when I run the exact same rule in the Studio integrated version I don't see any failure messages.

I am performing the rule check within a Check(member) override and I'm specifically looking for a NodeType of Method before attempting to retrieve the locals - if this information helps.

I'll have a look at the debugging information and see if that can get me any further too, thanks :)

PS

Once I have this issue licked, is there any way I can tell if a local is a constant or not since that's another item I need to be able to check.

Sara_H at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 6

Um, Has anyone had any further thoughts on this?

It's been a couple of weeks now and I'm still getting the problem...

Thanks

Sara_H at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 7

"To debug your rules within the IDE, you would have to configure a reg key (AEDebug) that specifies the debugger should be launched anytime FxCopCmd.exe is invoked. Here's a link providing some information."

Can you please explain the procedure in detail?

Thanks a lot.

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

Sara,

Sorry because this thread was already marked as an answer, it wasn't on my radar. Can you send me a repro binary and I'll investigate (see my profile for my email)?

Cheers

David

DavidM.Kean-MSFT at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 9

Is there a way to debug custom rules in vs2005 without using the stand-alone fxcop?

Following is the comment from Michael in this thread -

"To debug your rules within the IDE, you would have to configure a reg key (AEDebug) that specifies the debugger should be launched anytime FxCopCmd.exe is invoked. Here's a link providing some information."

I would appreciate if any one can explain this procedure in detail?

Thanks in advance.

mahendra_nath at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 10

Morning David,

I actually managed to get this rule check mostly going Ok eventually. Looking at my code I'm not sure quite what I was doing wrong to be honest.

Is there a way I can tell if a local variable is defined as a constant though?

I haven't managed to get the rule debugging to work other than by changing my references and using the stand alone version so a bit more info on how to get that going would be really useful if you can provide it.

Thanks,
Sara

Sara_H at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 12
the answer is not complete;I have the same problem when I'm making a rule for check if a constant has a prefix:Is there a way I can tell if a local variable is defined as a constant though?
ahurtado at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...

Visual Studio Team System

Site Classified