Getting info about local variables.  
Author Message
RSX-S





PostPosted: Visual Studio Code Analysis and Code Metrics, Getting info about local variables. Top

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


Visual Studio Team System5  
 
 
Michael Fanning - MS





PostPosted: Visual Studio Code Analysis and Code Metrics, Getting info about local variables. Top

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
        }
      }

 



 
 
Sara_H





PostPosted: Visual Studio Code Analysis and Code Metrics, Getting info about local variables. Top

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


 
 
Michael Fanning - MS





PostPosted: Visual Studio Code Analysis and Code Metrics, Getting info about local variables. Top

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 de**** should be launched anytime FxCopCmd.exe is invoked. Here's a link providing some information.

http://www.hide-link.com/ ;en-us;103861&sd=tech



 
 
David M. Kean - MSFT





PostPosted: Visual Studio Code Analysis and Code Metrics, Getting info about local variables. Top

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



 
 
Sara_H





PostPosted: Visual Studio Code Analysis and Code Metrics, Getting info about local variables. Top

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****ed, 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





PostPosted: Visual Studio Code Analysis and Code Metrics, Getting info about local variables. Top

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


 
 
mahendra_nath





PostPosted: Visual Studio Code Analysis and Code Metrics, Getting info about local variables. Top

"To debug your rules within the IDE, you would have to configure a reg key (AEDebug) that specifies the de**** 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.


 
 
David M. Kean - MSFT





PostPosted: Visual Studio Code Analysis and Code Metrics, Getting info about local variables. Top

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



 
 
mahendra_nath





PostPosted: Visual Studio Code Analysis and Code Metrics, Getting info about local variables. Top

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 de**** 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.


 
 
Sara_H





PostPosted: Visual Studio Code Analysis and Code Metrics, Getting info about local variables. Top

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


 
 
mahendra_nath





PostPosted: Visual Studio Code Analysis and Code Metrics, Getting info about local variables. Top

See my post -

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1090172&SiteID=1

-Mahendra.


 
 
ahurtado





PostPosted: Visual Studio Code Analysis and Code Metrics, Getting info about local variables. Top

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