77using Rubberduck . Parsing . VBA ;
88using Rubberduck . Settings ;
99using Rubberduck . UI ;
10+ using Antlr4 . Runtime . Tree ;
11+ using Rubberduck . Parsing ;
1012
1113namespace Rubberduck . Inspections
1214{
@@ -46,7 +48,7 @@ private void UpdateInspectionSeverity()
4648 }
4749 }
4850
49- public async Task < IList < ICodeInspectionResult > > FindIssuesAsync ( RubberduckParserState state , CancellationToken token )
51+ public async Task < IEnumerable < ICodeInspectionResult > > FindIssuesAsync ( RubberduckParserState state , CancellationToken token )
5052 {
5153 if ( state == null || ! state . AllUserDeclarations . Any ( ) )
5254 {
@@ -61,13 +63,20 @@ public async Task<IList<ICodeInspectionResult>> FindIssuesAsync(RubberduckParser
6163
6264 var allIssues = new ConcurrentBag < ICodeInspectionResult > ( ) ;
6365
66+ // Prepare ParseTreeWalker based inspections
67+ var parseTreeWalkResults = GetParseTreeResults ( state ) ;
68+ foreach ( var parseTreeInspection in _inspections . Where ( inspection => inspection . Severity != CodeInspectionSeverity . DoNotShow && inspection is IParseTreeInspection ) )
69+ {
70+ ( parseTreeInspection as IParseTreeInspection ) . ParseTreeResults = parseTreeWalkResults ;
71+ }
72+
6473 var inspections = _inspections . Where ( inspection => inspection . Severity != CodeInspectionSeverity . DoNotShow )
6574 . Select ( inspection =>
6675 new Task ( ( ) =>
6776 {
6877 token . ThrowIfCancellationRequested ( ) ;
6978 var inspectionResults = inspection . GetInspectionResults ( ) ;
70- var results = inspectionResults as IList < InspectionResultBase > ?? inspectionResults . ToList ( ) ;
79+ var results = inspectionResults as IEnumerable < InspectionResultBase > ?? inspectionResults ;
7180
7281 if ( results . Any ( ) )
7382 {
@@ -88,7 +97,39 @@ public async Task<IList<ICodeInspectionResult>> FindIssuesAsync(RubberduckParser
8897 Task . WaitAll ( inspections ) ;
8998 state . OnStatusMessageUpdate ( RubberduckUI . ResourceManager . GetString ( "ParserState_" + state . Status ) ) ; // should be "Ready"
9099
91- return allIssues . ToList ( ) ;
100+ return allIssues ;
101+ }
102+
103+ private ParseTreeResults GetParseTreeResults ( RubberduckParserState state )
104+ {
105+ var result = new ParseTreeResults ( ) ;
106+
107+ foreach ( var componentTreePair in state . ParseTrees )
108+ {
109+ /*
110+ Need to reinitialize these for each and every ParseTree we process, since the results are aggregated in the instances themselves
111+ before moving them into the ParseTreeResults after qualifying them
112+ */
113+ var obsoleteCallStatementListener = new ObsoleteCallStatementInspection . ObsoleteCallStatementListener ( ) ;
114+ var obsoleteLetStatementListener = new ObsoleteLetStatementInspection . ObsoleteLetStatementListener ( ) ;
115+ var emptyStringLiteralListener = new EmptyStringLiteralInspection . EmptyStringLiteralListener ( ) ;
116+ var argListWithOneByRefParamListener = new ProcedureCanBeWrittenAsFunctionInspection . ArgListWithOneByRefParamListener ( ) ;
117+
118+ var combinedListener = new CombinedParseTreeListener ( new IParseTreeListener [ ] {
119+ obsoleteCallStatementListener ,
120+ obsoleteLetStatementListener ,
121+ emptyStringLiteralListener ,
122+ argListWithOneByRefParamListener ,
123+ } ) ;
124+
125+ ParseTreeWalker . Default . Walk ( combinedListener , componentTreePair . Value ) ;
126+
127+ result . ArgListsWithOneByRefParam . Concat ( argListWithOneByRefParamListener . Contexts . Select ( context => new QualifiedContext ( componentTreePair . Key , context ) ) ) ;
128+ result . EmptyStringLiterals . Concat ( emptyStringLiteralListener . Contexts . Select ( context => new QualifiedContext ( componentTreePair . Key , context ) ) ) ;
129+ result . ObsoleteLetContexts . Concat ( obsoleteLetStatementListener . Contexts . Select ( context => new QualifiedContext ( componentTreePair . Key , context ) ) ) ;
130+ result . ObsoleteCallContexts . Concat ( obsoleteCallStatementListener . Contexts . Select ( context => new QualifiedContext ( componentTreePair . Key , context ) ) ) ;
131+ }
132+ return result ;
92133 }
93134
94135 public void Dispose ( )
0 commit comments