[Mono-dev] [PROPOSAL] gendarme - support for displaying the source code of violations

Christian Birkl christian.birkl at gmail.com
Wed Sep 27 13:37:33 EDT 2006


Hi,

currently gendarme just displays TypeName::MethodName::Offset of violations.
This results for example in the following output:

[snip]

1. NewLineLiteralRule

Problem: The method 'System.Void Mono.Xml.MiniParser::Parse(
Mono.Xml.MiniParser/IReader,Mono.Xml.MiniParser/IHandler)' use some literal
values for new lines (e.g. \r\n) which aren't portable across operating
systems.

Details:
  Mono.Xml.MiniParser::Parse:00b5

[/snip]

Thanks to Jb Mono.Cecil has support of reading debug files of
MS.NET<http://ms.net/>(pdb) and mono (mdb). With just a few lines of
code we can embed this
feature into gendarme. I've "hacked" together a sample implementation which
produces the following output:

[snip]

1. NewLineLiteralRule

Problem: The method 'System.Void Mono.Xml.MiniParser::Parse(
Mono.Xml.MiniParser/IReader,Mono.Xml.MiniParser/IHandler)' use some literal
values for new lines (e.g. \r\n) which aren't portable across operating
systems.

Details:
  Mono.Xml.MiniParser::Parse:00b5: Found string: "<>/?=&'"![ ]  \r\n"
  Source Text:

                                int charCode = "<>/?=&'\"![
]\t\r\n".IndexOf((char)currCh) & 0xF;
--------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

                                if (charCode == (int)CharKind.CR) continue;
// ignore

[/snip]

The changes for supporting this stuff aren't a big deal, but since I haven't
worked with Mono.Cecil.Mdb and/or Mono.Cecil.Pdb I'd like to hear what other
might think about this implementation:

1) Add Runner::LoadDebugInfos (AssemblyDefinition, string fileName):

  private Hashtable symbolReaders = new Hashtable ();

  public void LoadDebugInfos (AssemblyDefinition ad, string fileName) {
   symbolReaders.Clear ();

   ISymbolStoreFactory factory = GetSymbolStoreFactory (fileName);
   if (factory == null)
    return;

   foreach (ModuleDefinition md in ad.Modules) {
    symbolReaders [md] = factory.CreateReader (md, fileName);
   }
  }

  private ISymbolStoreFactory GetSymbolStoreFactory (string fileName) {
   string pdbFileName = Path.ChangeExtension (fileName, "pdb");
   if (File.Exists (pdbFileName)) {
    return new Mono.Cecil.Pdb.PdbFactory ();
   }

   string mdbFileName = Path.ChangeExtension (fileName, "mdb");
   if (File.Exists (mdbFileName)) {
    return new Mono.Cecil.Mdb.MdbFactory ();
   }

   return null;
  }

3) Add reading code to the foreach loop in Runner::Process to read debugging
infos (if available)

4) Change Location.cs and add a new Constructor (MethodDeclaration,
Instruction) to load the source text out of the SequencePoint of the given
instruction. Read in the file name specified in the document and extract the
source text beginning at StartLine.

5) Modify Runner(s) to display the new Source Text.

Any ideas how this can be easier/better/... done? Should there be a command
line flag to specifiy where to search for debug files or to enable/disable
debug symbol resolving? Does this really belong into the framework or is it
something each runner need to implement on its own?

BTW - It seems like "Mono.Cecil.Pdb" and "Mono.Cecil.Mdb" aren't part of the
"official" Mono.Cecil package - at least they aren't download able from the
cecil homepage. Are they intended to be used by other applications yet?

Christian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20060927/2d30b0c4/attachment.html 


More information about the Mono-devel-list mailing list