private void AnalyzeEntry(PythonAnalyzerEntry entry, IPythonModule module, PythonAst ast, int version) { // Now run the analysis. var analyzable = module as IAnalyzable; analyzable?.NotifyAnalysisBegins(); var walker = new ModuleWalker(_services, module, ast); ast.Walk(walker); _analyzerCancellationToken.ThrowIfCancellationRequested(); walker.Complete(); _analyzerCancellationToken.ThrowIfCancellationRequested(); var analysis = new DocumentAnalysis((IDocument)module, version, walker.GlobalScope, walker.Eval, walker.StarImportMemberNames); analyzable?.NotifyAnalysisComplete(analysis); entry.TrySetAnalysis(analysis, version); if (module.ModuleType == ModuleType.User) { var linterDiagnostics = _analyzer.LintModule(module); _diagnosticsService?.Replace(entry.Module.Uri, linterDiagnostics, DiagnosticSource.Linter); } }
/// <summary> /// Performs analysis of the document. Returns document global scope /// with declared variables and inner scopes. Does not analyze chain /// of dependencies, it is intended for the single file analysis. /// </summary> private async Task <IDocumentAnalysis> AnalyzeAsync(IDependencyChainNode node, CancellationToken cancellationToken) { var _startTime = DateTime.Now; _log?.Log(TraceEventType.Verbose, $"Analysis begins: {node.Document.Name}({node.Document.ModuleType})"); // Store current expected version so we can see if it still // the same at the time the analysis completes. var analysisVersion = node.Analyzable.ExpectedAnalysisVersion; // Make sure the file is parsed ans the AST is up to date. var ast = await node.Document.GetAstAsync(cancellationToken); _log?.Log(TraceEventType.Verbose, $"Parse of {node.Document.Name}({node.Document.ModuleType}) complete in {(DateTime.Now - _startTime).TotalMilliseconds} ms."); // Now run the analysis. var walker = new ModuleWalker(_services, node.Document, ast); await ast.WalkAsync(walker, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); // Note that we do not set the new analysis here and rather let // Python analyzer to call NotifyAnalysisComplete. await walker.CompleteAsync(cancellationToken); _log?.Log(TraceEventType.Verbose, $"Analysis of {node.Document.Name}({node.Document.ModuleType}) complete in {(DateTime.Now - _startTime).TotalMilliseconds} ms."); return(new DocumentAnalysis(node.Document, analysisVersion, walker.GlobalScope, walker.Ast)); }
private void AnalyzeEntry(PythonAnalyzerEntry entry, IPythonModule module, int version, bool isFinalPass) { if (entry.PreviousAnalysis is LibraryAnalysis) { _log?.Log(TraceEventType.Verbose, $"Request to re-analyze finalized {module.Name}."); } // Now run the analysis. var analyzable = module as IAnalyzable; analyzable?.NotifyAnalysisBegins(); var ast = module.GetAst(); var walker = new ModuleWalker(_services, module); ast.Walk(walker); _analyzerCancellationToken.ThrowIfCancellationRequested(); walker.Complete(); _analyzerCancellationToken.ThrowIfCancellationRequested(); analyzable?.NotifyAnalysisComplete(version, walker, isFinalPass); entry.TrySetAnalysis(module.Analysis, version); if (module.ModuleType == ModuleType.User) { var linterDiagnostics = _analyzer.LintModule(module); _diagnosticsService?.Replace(entry.Module.Uri, linterDiagnostics, DiagnosticSource.Linter); } }
private IDocumentAnalysis DoAnalyzeEntry(IDependencyChainNode <PythonAnalyzerEntry> node, IPythonModule module, PythonAst ast, int version) { var analysis = TryRestoreCachedAnalysis(node, module); if (analysis != null) { return(analysis); } var walker = new ModuleWalker(_services, module, ast, _analyzerCancellationToken); ast.Walk(walker); walker.Complete(); return(CreateAnalysis(node, (IDocument)module, ast, version, walker)); }
private void AnalyzeEntry(IDependencyChainNode <PythonAnalyzerEntry> node, PythonAnalyzerEntry entry, IPythonModule module, PythonAst ast, int version) { // Now run the analysis. var analyzable = module as IAnalyzable; analyzable?.NotifyAnalysisBegins(); var walker = new ModuleWalker(_services, module, ast); ast.Walk(walker); _analyzerCancellationToken.ThrowIfCancellationRequested(); walker.Complete(); _analyzerCancellationToken.ThrowIfCancellationRequested(); bool isCanceled; lock (_syncObj) { isCanceled = _isCanceled; } if (!isCanceled) { node?.MarkWalked(); } var analysis = CreateAnalysis(node, (IDocument)module, ast, version, walker, isCanceled); analyzable?.NotifyAnalysisComplete(analysis); entry.TrySetAnalysis(module.Analysis, version); if (module.ModuleType == ModuleType.User) { var linterDiagnostics = _analyzer.LintModule(module); _diagnosticsService?.Replace(entry.Module.Uri, linterDiagnostics, DiagnosticSource.Linter); } }
private IDocumentAnalysis CreateAnalysis(IDependencyChainNode <PythonAnalyzerEntry> node, IDocument document, PythonAst ast, int version, ModuleWalker walker, bool isCanceled) { var createLibraryAnalysis = !isCanceled && node != null && !node.HasMissingDependencies && document.ModuleType == ModuleType.Library && !document.IsOpen && node.HasOnlyWalkedDependencies && node.IsValidVersion; if (!createLibraryAnalysis) { return(new DocumentAnalysis(document, version, walker.GlobalScope, walker.Eval, walker.StarImportMemberNames)); } ast.Reduce(x => x is ImportStatement || x is FromImportStatement); document.SetAst(ast); var eval = new ExpressionEval(walker.Eval.Services, document, ast); return(new LibraryAnalysis(document, version, walker.GlobalScope, eval, walker.StarImportMemberNames)); }
private IDocumentAnalysis CreateAnalysis(IDependencyChainNode <PythonAnalyzerEntry> node, IDocument document, PythonAst ast, int version, ModuleWalker walker) { var canHaveLibraryAnalysis = false; // Don't try to drop builtins; it causes issues elsewhere. // We probably want the builtin module's AST and other info for evaluation. switch (document.ModuleType) { case ModuleType.Library: case ModuleType.Compiled: case ModuleType.CompiledBuiltin: canHaveLibraryAnalysis = true; break; } var isCanceled = MarkNodeWalked(node); var createLibraryAnalysis = !isCanceled && node != null && !node.HasMissingDependencies && canHaveLibraryAnalysis && !document.IsOpen && node.HasOnlyWalkedDependencies && node.IsValidVersion; if (!createLibraryAnalysis) { return(new DocumentAnalysis(document, version, walker.GlobalScope, walker.Eval, walker.StarImportMemberNames)); } ast.Reduce(x => x is ImportStatement || x is FromImportStatement); document.SetAst(ast); var eval = new ExpressionEval(walker.Eval.Services, document, ast); var analysis = new LibraryAnalysis(document, version, walker.GlobalScope, eval, walker.StarImportMemberNames); var dbs = _services.GetService <IModuleDatabaseService>(); dbs?.StoreModuleAnalysisAsync(analysis, CancellationToken.None).DoNotWait(); return(analysis); }