public static void EvaluateComplexityMetrics(MetricsContext ctx, ProjectProperties project) { mctx = ctx; PrefixName = new Stack <string>(0); lock (mctx) { foreach (var file in project.Project.Files) { /*if(file.BuildAction != BuildAction.Compile) * continue;*/ // Files not set to compile are sometimes not accessible if (file.Name.Contains("svn-base")) { continue; } string text = ""; try { text = System.IO.File.ReadAllText(file.FilePath); } catch (System.UnauthorizedAccessException uae) { continue; } catch (System.IO.FileNotFoundException fnf) { // This exception arises in Nrefactory...WTF? 0_0 continue; } ProjProp = project; File = file; Mono.TextEditor.Document doc = new Mono.TextEditor.Document(); doc.Text = text; FileDoc = doc; FileText = new List <LineSegment>(); foreach (LineSegment segment in doc.Lines) { FileText.Add(segment); } using (ICSharpCode.OldNRefactory.IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(text))) { parser.Parse(); if (parser.Errors.Count > 0) { //Error handling TODO } else { foreach (var it in parser.CompilationUnit.Children) { ProcessNode(ctx, it); } } } } } }
static List <HashSet <string> > GetUsedDefineCombinations(ICSharpCode.OldNRefactory.IParser parser) { List <HashSet <string> > result = new List <HashSet <string> > (); foreach (ISpecial special in parser.Lexer.SpecialTracker.CurrentSpecials) { PreprocessingDirective directive = special as PreprocessingDirective; if (directive == null || (directive.Cmd != "#if" && directive.Cmd != "#elif")) { continue; } ExpressionVisitor visitor = new ExpressionVisitor(); directive.Expression.AcceptVisitor(visitor, null); ICSharpCode.OldNRefactory.Parser.CSharp.ConditionalCompilation cond = new ICSharpCode.OldNRefactory.Parser.CSharp.ConditionalCompilation(); bool nothingDefined = cond.Evaluate(directive.Expression); foreach (var combination in GetAllCombinations(visitor.Identifiers)) { cond = new ICSharpCode.OldNRefactory.Parser.CSharp.ConditionalCompilation(); HashSet <string> defines = new HashSet <string> (); foreach (string usedIdentifier in combination) { cond.Define(usedIdentifier); defines.Add(usedIdentifier); bool curDefineStatus = cond.Evaluate(directive.Expression); if (curDefineStatus != nothingDefined) { result.Add(defines); goto next; } } } next: ; } return(result); }