22
22
import com .sonar .sslr .api .AstNode ;
23
23
import com .sonar .sslr .api .RecognitionException ;
24
24
import java .io .File ;
25
- import java .io .IOException ;
26
25
import java .util .ArrayDeque ;
27
26
import java .util .ArrayList ;
28
27
import java .util .Deque ;
29
- import java .util .HashMap ;
30
28
import java .util .List ;
31
- import java .util .Map ;
32
29
import java .util .Set ;
33
30
import javax .annotation .CheckForNull ;
34
31
import org .sonar .api .SonarProduct ;
57
54
import org .sonar .python .metrics .FileLinesVisitor ;
58
55
import org .sonar .python .metrics .FileMetrics ;
59
56
import org .sonar .python .parser .PythonParser ;
60
- import org .sonar .python .semantic .ProjectLevelSymbolTable ;
61
57
import org .sonar .python .tree .PythonTreeMaker ;
62
58
63
- import static org .sonar .python .semantic .SymbolUtils .pythonPackageName ;
64
-
65
59
public class PythonScanner extends Scanner {
66
60
67
61
private static final Logger LOG = Loggers .get (PythonScanner .class );
68
62
69
63
private final PythonParser parser ;
70
- private final Map <InputFile , String > packageNames = new HashMap <>();
71
64
private final PythonChecks checks ;
72
65
private final FileLinesContextFactory fileLinesContextFactory ;
73
66
private final NoSonarFilter noSonarFilter ;
74
67
private final PythonCpdAnalyzer cpdAnalyzer ;
75
- private final ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable () ;
68
+ private final PythonIndexer indexer ;
76
69
77
70
78
71
public PythonScanner (
@@ -85,13 +78,8 @@ public PythonScanner(
85
78
this .noSonarFilter = noSonarFilter ;
86
79
this .cpdAnalyzer = new PythonCpdAnalyzer (context );
87
80
this .parser = PythonParser .create ();
88
-
89
- // computes "globalSymbolsByModuleName"
90
- long startTime = System .currentTimeMillis ();
91
- GlobalSymbolsScanner globalSymbolsStep = new GlobalSymbolsScanner (context );
92
- globalSymbolsStep .execute (files , context );
93
- long stopTime = System .currentTimeMillis () - startTime ;
94
- LOG .debug ("Time to build the project level symbol table: " + stopTime + "ms" );
81
+ this .indexer = new PythonIndexer ();
82
+ this .indexer .buildOnce (context , files );
95
83
}
96
84
97
85
@ Override
@@ -106,7 +94,7 @@ protected void scanFile(InputFile inputFile) {
106
94
try {
107
95
AstNode astNode = parser .parse (pythonFile .content ());
108
96
FileInput parse = new PythonTreeMaker ().fileInput (astNode );
109
- visitorContext = new PythonVisitorContext (parse , pythonFile , getWorkingDirectory (context ), packageNames . get (inputFile ), projectLevelSymbolTable );
97
+ visitorContext = new PythonVisitorContext (parse , pythonFile , getWorkingDirectory (context ), indexer . packageName (inputFile . uri ()), indexer . projectLevelSymbolTable () );
110
98
saveMeasures (inputFile , visitorContext );
111
99
} catch (RecognitionException e ) {
112
100
visitorContext = new PythonVisitorContext (pythonFile , e );
@@ -245,32 +233,4 @@ private void saveMetricOnFile(InputFile inputFile, Metric<Integer> metric, Integ
245
233
.on (inputFile )
246
234
.save ();
247
235
}
248
-
249
- private class GlobalSymbolsScanner extends Scanner {
250
-
251
- private GlobalSymbolsScanner (SensorContext context ) {
252
- super (context );
253
- }
254
-
255
- @ Override
256
- protected String name () {
257
- return "global symbols computation" ;
258
- }
259
-
260
- @ Override
261
- protected void scanFile (InputFile inputFile ) throws IOException {
262
- AstNode astNode = parser .parse (inputFile .contents ());
263
- FileInput astRoot = new PythonTreeMaker ().fileInput (astNode );
264
- String packageName = pythonPackageName (inputFile .file (), context .fileSystem ().baseDir ());
265
- packageNames .put (inputFile , packageName );
266
- PythonFile pythonFile = SonarQubePythonFile .create (inputFile );
267
- projectLevelSymbolTable .addModule (astRoot , packageName , pythonFile );
268
- }
269
-
270
- @ Override
271
- protected void processException (Exception e , InputFile file ) {
272
- LOG .debug ("Unable to construct project-level symbol table for file: " + file .toString ());
273
- LOG .debug (e .getMessage ());
274
- }
275
- }
276
236
}
0 commit comments