protected override void AnnotateFile(AnalysisServices services, RepoFile file, BoundSourceFileBuilder binder)
        {
            string solutionDirectory = Path.GetDirectoryName(file.FilePath);

            try
            {
                var defaultRepoProject = file.PrimaryProject.Repo.DefaultRepoProject;
                var repoRoot           = defaultRepoProject.ProjectDirectory;
                var solutionFile       = SolutionFile.Parse(new SourceTextReader(binder.SourceText));
                foreach (var projectBlock in solutionFile.ProjectBlocks)
                {
                    var projectPath = projectBlock.ParsedProjectPath.Value;
                    if (string.IsNullOrEmpty(projectPath))
                    {
                        continue;
                    }

                    var fullProjectPath = Path.GetFullPath(Path.Combine(solutionDirectory, projectPath));

                    var projectFileReference = BoundSourceFileBuilder.CreateFileReferenceSymbol(
                        defaultRepoProject.GetLogicalPath(fullProjectPath),
                        defaultRepoProject.ProjectId);

                    var span = projectBlock.ParsedProjectName.Span;
                    AnnotateReference(binder, projectFileReference, span);

                    span = projectBlock.ParsedProjectPath.Span;
                    AnnotateReference(binder, projectFileReference, span);
                }
            }
            catch (Exception ex)
            {
                services.Logger.LogExceptionError("AnnotateSolution", ex);
            }
        }
示例#2
0
        public static ReferenceSpan ToReference(CodexSemanticStore store, BoundSourceFileBuilder builder, CodexSpan span)
        {
            if (!span.ReferenceKind.IsValid || !span.Symbol.IsValid)
            {
                return(null);
            }

            var      symbol   = store.Symbols[span.Symbol];
            SymbolId symbolId = GetSpanSymbolId(symbol);

            var projectId = symbol.Project.IsValid ? store.Projects.Get(symbol.Project).Name : builder.ProjectId;

            return(new ReferenceSpan()
            {
                Start = span.Start,
                Length = span.Length,
                Reference = new ReferenceSymbol()
                {
                    Id = symbolId,
                    ReferenceKind = store.ReferenceKinds[span.ReferenceKind].Name,
                    Kind = "symbol",
                    ProjectId = projectId,
                },
            });
        }
示例#3
0
        public static DefinitionSpan ToDefinition(CodexSemanticStore store, BoundSourceFileBuilder builder, CodexSpan span)
        {
            if ((span.ReferenceKind.IsValid && !string.Equals(nameof(ReferenceKind.Definition), store.ReferenceKinds[span.ReferenceKind].Name,
                                                              StringComparison.OrdinalIgnoreCase)) || !span.Symbol.IsValid)
            {
                return(null);
            }

            var      symbol   = store.Symbols[span.Symbol];
            SymbolId symbolId = GetSpanSymbolId(symbol);

            var projectId = symbol.Project.IsValid ? store.Projects.Get(symbol.Project).Name : builder.ProjectId;

            return(new DefinitionSpan()
            {
                Start = span.Start,
                Length = span.Length,
                Definition = new DefinitionSymbol()
                {
                    Id = symbolId,
                    ShortName = symbol.ShortName,
                    DisplayName = symbol.DisplayName,
                    ContainerQualifiedName = symbol.ContainerQualifiedName,
                    SymbolDepth = symbol.SymbolDepth,
                    ReferenceKind = nameof(ReferenceKind.Definition),
                    Kind = symbol.Kind,
                    ProjectId = projectId,
                },
            });
        }
示例#4
0
        private void AnalyzePropertiesAndItems(BoundSourceFileBuilder binder, IXmlElement parent)
        {
            foreach (var group in parent.Elements("PropertyGroup"))
            {
                binder.AddReferences(group.Attribute("Condition").GetAttributeValueExpressionSpans());
                foreach (var element in group.Elements)
                {
                    binder.AddElementNameReferences(element,
                                                    CreatePropertyReference(element.Name));

                    ExpressionProcessor propertyValueProcessor = null;
                    if (element.Name.EndsWith("DependsOn"))
                    {
                        propertyValueProcessor = ProcessSemicolonSeparatedTargetList;
                    }

                    binder.AddReferences(element.Attribute("Condition").GetAttributeValueExpressionSpans());
                    binder.AddReferences(element.GetElementValueExpressionSpans(propertyValueProcessor));
                }
            }

            foreach (var group in parent.Elements("ItemGroup"))
            {
                AnalyzeItemGroup(binder, group);
            }

            foreach (var group in parent.Elements("ItemDefinitionGroup"))
            {
                AnalyzeItemGroup(binder, group, isDefinition: true);
            }
        }
示例#5
0
        protected virtual void AnnotateFile(BoundSourceFileBuilder binder)
        {
            var text = binder.SourceFile.Content;

            if (XmlAnalyzer.IsXml(text))
            {
                XmlAnalyzer.Analyze(binder);
            }
        }
示例#6
0
        private async void AnnotateAndUpload(
            AnalysisServices services,
            RepoFile file,
            BoundSourceFileBuilder binder)
        {
            await AnnotateFileAsync(services, file, binder);

            var boundSourceFile = binder.Build();

            UploadSourceFile(services, file, boundSourceFile);
        }
示例#7
0
            protected override void Analyze(AnalysisServices services, RepoFile file)
            {
                ProjectAnalyzer.DocumentAnalysisTasks.Add(services.TaskDispatcher.Invoke(async() =>
                {
                    try
                    {
                        ReportStartAnalyze(file);

                        var project = ProjectAnalyzer.Project;
                        if (project == null)
                        {
                            file.PrimaryProject.Repo.AnalysisServices.Logger.LogError("Project is null");
                            return(null);
                        }

                        var document = project.GetDocument(DocumentInfo.Id);
                        var text     = await document.GetTextAsync();

                        SourceFile sourceFile = new SourceFile()
                        {
                            Info = AugmentSourceFileInfo(new SourceFileInfo()
                            {
                                Language         = project.Language,
                                Path             = file.LogicalPath,
                                RepoRelativePath = file.RepoRelativePath
                            }),
                        };

                        BoundSourceFileBuilder binder = CreateBuilder(sourceFile, file, file.PrimaryProject.ProjectId);
                        binder.SourceText             = text;

                        DocumentAnalyzer analyzer = new DocumentAnalyzer(
                            ProjectAnalyzer.semanticServices,
                            document,
                            ProjectAnalyzer.CompilationServices,
                            file.LogicalPath,
                            ProjectAnalyzer.ProjectContext,
                            binder);

                        var boundSourceFile = await analyzer.CreateBoundSourceFile();

                        ProjectAnalyzer.ProjectContext.ReportDocument(boundSourceFile, file);

                        UploadSourceFile(services, file, boundSourceFile);

                        return(boundSourceFile);
                    }
                    finally
                    {
                        file.Analyzer   = RepoFileAnalyzer.Null;
                        ProjectAnalyzer = null;
                    }
                }));
            }
示例#8
0
        protected override void AnnotateFile(BoundSourceFileBuilder binder)
        {
            CodexFile file;

            if (m_fileToStoreMap.TryGetValue(binder.SourceFile.Info.RepoRelativePath, out file))
            {
                file.Load();
                CodexTypeAdapter.Annotate(binder, file);
            }

            base.AnnotateFile(binder);
        }
示例#9
0
        public static void Annotate(BoundSourceFileBuilder builder, CodexFile file)
        {
            builder.AddClassifications(FilterSpans(file.Spans.Select(span => ToClassification(file.Store, span)).Where(c => c != null)));

            foreach (var reference in FilterSpans(file.Spans.Select(span => ToReference(file.Store, builder, span)).Where(c => c != null)))
            {
                builder.AnnotateReferences(reference.Start, reference.Length, reference.Reference);
            }

            foreach (var definition in FilterSpans(file.Spans.Select(span => ToDefinition(file.Store, builder, span)).Where(c => c != null)))
            {
                builder.AnnotateDefinition(definition.Start, definition.Length, definition.Definition);
            }
        }
示例#10
0
        protected BoundSourceFileBuilder CreateBuilder(SourceFile sourceFile, RepoFile repoFile, string projectId)
        {
            var builder = CreateBuilderCore(sourceFile, projectId);

            if (sourceFile.Info.RepoRelativePath != null && repoFile.PrimaryProject != repoFile.PrimaryProject.Repo.DefaultRepoProject)
            {
                // Add a file definition for the repo relative path in the default repo project
                builder.AnnotateReferences(0, 0, BoundSourceFileBuilder.CreateFileReferenceSymbol(
                                               sourceFile.Info.RepoRelativePath,
                                               repoFile.PrimaryProject.Repo.DefaultRepoProject.ProjectId,
                                               isDefinition: true));
            }

            return(builder);
        }
示例#11
0
        private void AnalyzeChoose(BoundSourceFileBuilder binder, IXmlElement parent)
        {
            foreach (var choose in parent.Elements("Choose"))
            {
                foreach (var element in parent.Elements("When"))
                {
                    AnalyzePropertiesAndItems(binder, element);
                    AnalyzeChoose(binder, element);
                }

                foreach (var element in parent.Elements("Otherwise"))
                {
                    AnalyzePropertiesAndItems(binder, element);
                    AnalyzeChoose(binder, element);
                }
            }
        }
示例#12
0
        protected virtual void Analyze(AnalysisServices services, RepoFile file)
        {
            try
            {
                services.TaskDispatcher.QueueInvoke(() =>
                {
                    ReportStartAnalyze(file);

                    SourceFile sourceFile = file.InMemorySourceFileBuilder?.SourceFile ?? CreateSourceFile(services, file);

                    BoundSourceFileBuilder binder = file.InMemorySourceFileBuilder ?? CreateBuilder(sourceFile, file, file.PrimaryProject.ProjectId);

                    AnnotateAndUpload(services, file, binder);
                });
            }
            catch (Exception ex)
            {
                services.Logger.LogExceptionError($"Analyzing file: {file.FilePath}", ex);
            }
        }
示例#13
0
        private void AnalyzeItemGroup(BoundSourceFileBuilder binder, IXmlElement group, bool isDefinition = false)
        {
            binder.AddReferences(group.Attribute("Condition").GetAttributeValueExpressionSpans());
            foreach (var element in group.Elements)
            {
                var itemName = element.Name;
                binder.AddElementNameReferences(element,
                                                CreateItemReference(itemName));

                binder.AddReferences(element.Attribute("Condition").GetAttributeValueExpressionSpans(referencingItemName: itemName));
                binder.AddReferences(element.Attribute("Include").GetAttributeValueExpressionSpans());
                binder.AddReferences(element.Attribute("Exclude").GetAttributeValueExpressionSpans());
                binder.AddReferences(element.Attribute("Remove").GetAttributeValueExpressionSpans());

                foreach (var metadataElement in element.Elements)
                {
                    binder.AddReferences(metadataElement.Attribute("Condition").GetAttributeValueExpressionSpans(referencingItemName: itemName));
                    binder.AddElementNameReferences(metadataElement,
                                                    CreateItemMetadataReference(element.Name, metadataElement.Name));

                    binder.AddReferences(metadataElement.GetElementValueExpressionSpans(referencingItemName: itemName));
                }
            }
        }
示例#14
0
 protected virtual void AnnotateFile(AnalysisServices services, RepoFile file, BoundSourceFileBuilder binder)
 {
     AnnotateFile(binder);
 }
示例#15
0
        private void AnalyzeEvaluation(
            AnalysisServices services,
            RepoFile file,
            XmlSourceFileBuilder binder,
            XmlDocumentSyntax document)
        {
            var repo = file.PrimaryProject.Repo;

            using (var scope = GetProjectCollectionScope())
            {
                try
                {
                    Project project = new Project(file.FilePath, null, null, scope.Collection,
                                                  ProjectLoadSettings.IgnoreMissingImports | ProjectLoadSettings.RecordDuplicateButNotCircularImports);

                    foreach (var item in project.ItemsIgnoringCondition)
                    {
                        if (item.IsImported || item.Xml?.IncludeLocation == null)
                        {
                            continue;
                        }

                        RepoFile otherRepoFile = null;

                        try
                        {
                            var fullPath = item.GetMetadataValue("FullPath");
                            otherRepoFile = repo.TryGetFile(fullPath);
                        }
                        catch
                        {
                        }

                        if (otherRepoFile == null)
                        {
                            continue;
                        }

                        var location = item.Xml.IncludeLocation;

                        var itemIncludeAttribute = binder
                                                   .TryGetElement(location.Line - 1, location.Column - 1)?
                                                   .Attribute("Include");

                        binder.AddAttributeNameReferences(itemIncludeAttribute,
                                                          BoundSourceFileBuilder.CreateFileReferenceSymbol(
                                                              otherRepoFile.LogicalPath,
                                                              otherRepoFile.PrimaryProject.ProjectId));
                    }

                    foreach (var import in project.ImportsIncludingDuplicates)
                    {
                        if (import.IsImported || import.ImportingElement == null)
                        {
                            continue;
                        }

                        var      fullPath      = import.ImportedProject.FullPath;
                        RepoFile otherRepoFile = repo.TryGetFile(fullPath);
                        if (otherRepoFile == null && fullPath != null)
                        {
                            if (!AddImports)
                            {
                                continue;
                            }

                            lock (MSBuildSharedProject)
                            {
                                otherRepoFile = MSBuildSharedProject.AddFile(fullPath);
                            }

                            otherRepoFile.IsSingleton = true;
                            otherRepoFile.Analyze();
                        }

                        var location = import.ImportingElement.ProjectLocation;

                        var importProjectAttribute = binder
                                                     .TryGetElement(location.Line - 1, location.Column - 1)?
                                                     .Attribute("Project");

                        binder.AddAttributeNameReferences(importProjectAttribute,
                                                          BoundSourceFileBuilder.CreateFileReferenceSymbol(
                                                              otherRepoFile.LogicalPath,
                                                              otherRepoFile.PrimaryProject.ProjectId));
                    }
                }
                catch (Exception ex)
                {
                    services.Logger.LogExceptionError($"Analyzing MSBuild file evaluation: {file.FilePath}", ex);
                }
            }
        }
示例#16
0
 protected override void AnnotateFile(BoundSourceFileBuilder binder)
 {
     XmlAnalyzer.Analyze(binder);
 }
 protected override void AnnotateFile(BoundSourceFileBuilder binder)
 {
 }
示例#18
0
 private static void AnalyzeImport(BoundSourceFileBuilder binder, IXmlElement element)
 {
     binder.AddReferences(element.Attribute("Condition").GetAttributeValueExpressionSpans());
     binder.AddReferences(element.Attribute("Project").GetAttributeValueExpressionSpans());
 }
 private static void AnnotateReference(BoundSourceFileBuilder binder, ReferenceSymbol projectFileReference, Microsoft.CodeAnalysis.Text.TextSpan span)
 {
     binder.AnnotateClassification(span.Start, span.Length, ClassificationTypeNames.XmlLiteralAttributeValue);
     binder.AnnotateReferences(span.Start, span.Length, projectFileReference);
 }
示例#20
0
 protected virtual Task AnnotateFileAsync(AnalysisServices services, RepoFile file, BoundSourceFileBuilder binder)
 {
     AnnotateFile(services, file, binder);
     return(Task.FromResult(true));
 }
示例#21
0
        protected override void AnnotateFile(AnalysisServices services, RepoFile file, BoundSourceFileBuilder binder)
        {
            var document = XmlAnalyzer.Analyze(binder);

            AnnotateFile(services, file, (XmlSourceFileBuilder)binder, document);
        }
示例#22
0
        private void AnalyzeEvaluation(
            AnalysisServices services,
            RepoFile file,
            XmlSourceFileBuilder binder,
            XmlDocumentSyntax document)
        {
            var repo = file.PrimaryProject.Repo;

            using (var scope = GetProjectCollectionScope())
            {
                try
                {
                    Project project = new Project(file.FilePath, null, "Current", scope.Collection,
                                                  ProjectLoadSettings.IgnoreMissingImports | ProjectLoadSettings.RecordDuplicateButNotCircularImports);

                    foreach (var item in project.ItemsIgnoringCondition)
                    {
                        if (item.IsImported || item.Xml?.IncludeLocation == null)
                        {
                            continue;
                        }

                        RepoFile otherRepoFile = null;

                        try
                        {
                            var fullPath = item.GetMetadataValue("FullPath");
                            otherRepoFile = repo.TryGetFile(fullPath);
                        }
                        catch
                        {
                        }

                        if (otherRepoFile == null)
                        {
                            continue;
                        }

                        var location = item.Xml.IncludeLocation;

                        var itemIncludeAttribute = binder
                                                   .TryGetElement(location.Line - 1, location.Column - 1)?
                                                   .Attribute("Include");

                        binder.AddAttributeNameReferences(itemIncludeAttribute,
                                                          BoundSourceFileBuilder.CreateFileReferenceSymbol(
                                                              otherRepoFile.LogicalPath,
                                                              otherRepoFile.PrimaryProject.ProjectId));
                    }

                    foreach (var import in project.ImportsIncludingDuplicates)
                    {
                        if (import.IsImported || import.ImportingElement == null)
                        {
                            continue;
                        }

                        var      fullPath      = import.ImportedProject.FullPath;
                        RepoFile otherRepoFile = repo.TryGetFile(fullPath);
                        if (otherRepoFile == null && fullPath != null)
                        {
                            if (!AddImports)
                            {
                                continue;
                            }

                            lock (MSBuildSharedProject)
                            {
                                otherRepoFile = MSBuildSharedProject.AddFile(fullPath);

                                // The file is marked with explicit in this lock so we can
                                // use that to know whether another thread has added the file already
                                // and we can skip redundant analysis here
                                if (otherRepoFile.HasExplicitAnalyzer)
                                {
                                    continue;
                                }

                                otherRepoFile.Analyzer            = this;
                                otherRepoFile.HasExplicitAnalyzer = true;

                                Placeholder.NotImplemented("Normalize imports to remove any references to repo specific files. Check if project is MSBuildSharedProject in order to determine if normalization is needed");
                            }

                            otherRepoFile.Analyze();
                        }

                        var location = import.ImportingElement.ProjectLocation;

                        var importProjectAttribute = binder
                                                     .TryGetElement(location.Line - 1, location.Column - 1)?
                                                     .Attribute("Project");

                        binder.AddAttributeNameReferences(importProjectAttribute,
                                                          BoundSourceFileBuilder.CreateFileReferenceSymbol(
                                                              otherRepoFile.LogicalPath,
                                                              otherRepoFile.PrimaryProject.ProjectId));
                    }
                }
                catch (Exception ex)
                {
                    services.Logger.LogExceptionError($"Analyzing MSBuild file evaluation: {file.FilePath}", ex);
                }
            }
        }