private RecoverableSyntaxTree(RecoverableSyntaxTree original, SyntaxTreeInfo info)
 {
     this.recoverableRoot     = original.recoverableRoot.WithSyntaxTree(this);
     this.info                = info;
     this.projectCacheService = original.projectCacheService;
     this.cacheKey            = original.cacheKey;
 }
예제 #2
0
            private async Task AddDocumentsWithPotentialConflicts(IEnumerable <Document> documents)
            {
                foreach (var document in documents)
                {
                    if (_documentsIdsToBeCheckedForConflict.Contains(document.Id))
                    {
                        continue;
                    }

                    var info = await SyntaxTreeInfo.GetIdentifierInfoAsync(document, CancellationToken.None).ConfigureAwait(false);

                    if (info.ProbablyContainsEscapedIdentifier(_originalText))
                    {
                        _documentsIdsToBeCheckedForConflict.Add(document.Id);
                        continue;
                    }

                    if (info.ProbablyContainsIdentifier(_replacementText))
                    {
                        _documentsIdsToBeCheckedForConflict.Add(document.Id);
                        continue;
                    }

                    foreach (var replacementName in _possibleNameConflicts)
                    {
                        if (info.ProbablyContainsIdentifier(replacementName))
                        {
                            _documentsIdsToBeCheckedForConflict.Add(document.Id);
                            break;
                        }
                    }
                }
            }
예제 #3
0
 private RecoverableSyntaxTree(RecoverableSyntaxTree original, SyntaxTreeInfo info)
 {
     _recoverableRoot     = original._recoverableRoot.WithSyntaxTree(this);
     _info                = info;
     _projectCacheService = original._projectCacheService;
     _cacheKey            = original._cacheKey;
 }
예제 #4
0
        protected override Task <IEnumerable <Document> > DetermineDocumentsToSearchAsync(
            IMethodSymbol symbol,
            Project project,
            IImmutableSet <Document> documents,
            CancellationToken cancellationToken)
        {
            return(FindDocumentsAsync(project, documents, async(d, c) =>
            {
                var contextInfo = await SyntaxTreeInfo.GetContextInfoAsync(d, c).ConfigureAwait(false);
                if (contextInfo.ContainsBaseConstructorInitializer)
                {
                    return true;
                }

                var identifierInfo = await SyntaxTreeInfo.GetIdentifierInfoAsync(d, c).ConfigureAwait(false);
                if (identifierInfo.ProbablyContainsIdentifier(symbol.ContainingType.Name))
                {
                    if (contextInfo.ContainsThisConstructorInitializer)
                    {
                        return true;
                    }
                    else if (project.Language == LanguageNames.VisualBasic && identifierInfo.ProbablyContainsIdentifier("New"))
                    {
                        // "New" can be explicitly accessed in xml doc comments to reference a constructor.
                        return true;
                    }
                }

                return false;
            }, cancellationToken));
        }
예제 #5
0
 private RecoverableSyntaxTree(AbstractSyntaxTreeFactoryService service, ProjectId cacheKey, CompilationUnitSyntax root, SyntaxTreeInfo info)
 {
     _recoverableRoot     = new RecoverableSyntaxRoot <CompilationUnitSyntax>(service, root, this);
     _info                = info;
     _projectCacheService = service.LanguageServices.WorkspaceServices.GetService <IProjectCacheHostService>();
     _cacheKey            = cacheKey;
 }
예제 #6
0
 protected Task <IEnumerable <Document> > FindDocumentsWithForEachStatementsAsync(Project project, IImmutableSet <Document> documents, CancellationToken cancellationToken)
 {
     return(FindDocumentsAsync(project, documents, async(d, c) =>
     {
         var info = await SyntaxTreeInfo.GetContextInfoAsync(d, c).ConfigureAwait(false);
         return info.ContainsForEachStatement;
     }, cancellationToken));
 }
예제 #7
0
 private Task <IEnumerable <Document> > FindDocumentWithElementAccessExpressionsAsync(
     Project project, IImmutableSet <Document> documents, CancellationToken cancellationToken)
 {
     return(FindDocumentsAsync(project, documents, async(d, c) =>
     {
         var info = await SyntaxTreeInfo.GetContextInfoAsync(d, c).ConfigureAwait(false);
         return info.ContainsElementAccessExpression;
     }, cancellationToken));
 }
예제 #8
0
 private Task <ImmutableArray <Document> > FindDocumentWithIndexerMemberCrefAsync(
     Project project, IImmutableSet <Document> documents, CancellationToken cancellationToken)
 {
     return(FindDocumentsAsync(project, documents, async(d, c) =>
     {
         var info = await SyntaxTreeInfo.GetContextInfoAsync(d, c).ConfigureAwait(false);
         return info.ContainsIndexerMemberCref;
     }, cancellationToken));
 }
예제 #9
0
        /// <summary>
        /// Generate test class.
        /// </summary>
        /// <param name="sourceCode">Some class code.</param>
        /// <returns>Generated test.</returns>
        private GeneratedTestClass Produce(string sourceCode)
        {
            SyntaxTreeInfoBuilder syntaxTreeInfoBuilder = new SyntaxTreeInfoBuilder(sourceCode);
            SyntaxTreeInfo        syntaxTreeInfo        = syntaxTreeInfoBuilder.GetSyntaxTreeInfo();

            TestClassTemplateGenerator testTemplatesGenerator = new TestClassTemplateGenerator(syntaxTreeInfo);
            List <GeneratedTestClass>  testTemplates          = testTemplatesGenerator.GetTestTemplates().ToList();

            return(new GeneratedTestClass(testTemplates.First().TestClassName, testTemplates.First().TestClassData));
        }
예제 #10
0
 protected Task <ImmutableArray <Document> > FindDocumentsAsync(
     Project project,
     IImmutableSet <Document> documents,
     PredefinedType predefinedType,
     CancellationToken cancellationToken)
 {
     return(FindDocumentsAsync(project, documents, async(d, c) =>
     {
         var info = await SyntaxTreeInfo.GetContextInfoAsync(d, c).ConfigureAwait(false);
         return info.ContainsPredefinedType(predefinedType);
     }, cancellationToken));
 }
        private static void VerifyRoundTrip <TCompilation>(TCompilation original)
            where TCompilation : Compilation
        {
            Assert.True(original.SyntaxTrees.All(x => !string.IsNullOrEmpty(x.FilePath)));
            Assert.True(original.Options.Deterministic);

            original.VerifyDiagnostics();
            var originalBytes     = original.EmitToArray(new EmitOptions(debugInformationFormat: DebugInformationFormat.Embedded));
            var originalReader    = new PEReader(originalBytes);
            var originalPdbReader = originalReader.GetEmbeddedPdbMetadataReader();

            var factory          = LoggerFactory.Create(configure => { });
            var logger           = factory.CreateLogger("RoundTripVerification");
            var buildConstructor = new BuildConstructor(logger);
            var optionsReader    = new CompilationOptionsReader(logger, originalPdbReader, originalReader);
            var assemblyFileName = original.AssemblyName !;

            if (typeof(TCompilation) == typeof(CSharpCompilation))
            {
                var assemblyFileExtension = original.Options.OutputKind switch
                {
                    OutputKind.DynamicallyLinkedLibrary => ".dll",
                    OutputKind.ConsoleApplication => ".exe",
                    _ => throw new InvalidOperationException(),
                };
                assemblyFileName += assemblyFileExtension;
            }

            var rebuild = buildConstructor.CreateCompilation(
                assemblyFileName,
                optionsReader,
                original.SyntaxTrees.Select(x => SyntaxTreeInfo.Create(x)).ToImmutableArray(),
                metadataReferences: original.References.ToImmutableArray());

            Assert.IsType <TCompilation>(rebuild);
            VerifyCompilationOptions(original.Options, rebuild.Options);

            using var rebuildStream = new MemoryStream();
            var result = BuildConstructor.Emit(
                rebuildStream,
                optionsReader,
                rebuild,
                embeddedTexts: ImmutableArray <EmbeddedText> .Empty,
                CancellationToken.None);

            Assert.Empty(result.Diagnostics);
            Assert.True(result.Success);
            Assert.Equal(originalBytes.ToArray(), rebuildStream.ToArray());
        }
예제 #12
0
        /// <summary>
        /// Finds all the documents in the provided project that contain the requested string
        /// values
        /// </summary>
        protected Task <IEnumerable <Document> > FindDocumentsAsync(Project project, IImmutableSet <Document> documents, CancellationToken cancellationToken, params string[] values)
        {
            return(FindDocumentsAsync(project, documents, async(d, c) =>
            {
                var info = await SyntaxTreeInfo.GetIdentifierInfoAsync(d, c).ConfigureAwait(false);
                foreach (var value in values)
                {
                    if (!info.ProbablyContainsIdentifier(value))
                    {
                        return false;
                    }
                }

                return true;
            }, cancellationToken));
        }
예제 #13
0
        protected async Task <IEnumerable <Document> > FindDocumentsAsync(
            Project project,
            IImmutableSet <Document> documents,
            PredefinedOperator op,
            CancellationToken cancellationToken)
        {
            if (op == PredefinedOperator.None)
            {
                return(SpecializedCollections.EmptyEnumerable <Document>());
            }

            return(await FindDocumentsAsync(project, documents, async (d, c) =>
            {
                var info = await SyntaxTreeInfo.GetContextInfoAsync(d, c).ConfigureAwait(false);
                return info.ContainsPredefinedOperator(op);
            }, cancellationToken).ConfigureAwait(false));
        }
        private TestClassInMemoryInfo Produce(string sourceCode)
        {
            SyntaxTreeInfoBuilder syntaxTreeInfoBuilder = new SyntaxTreeInfoBuilder(sourceCode);
            SyntaxTreeInfo        syntaxTreeInfo        = syntaxTreeInfoBuilder.GetSyntaxTreeInfo();

            TestTemplatesGenerator       testTemplatesGenerator = new TestTemplatesGenerator(syntaxTreeInfo);
            List <TestClassInMemoryInfo> testTemplates          = testTemplatesGenerator.GetTestTemplates().ToList();

            if (testTemplates.Count > 1)
            {
                for (int i = 1; i < testTemplates.Count; ++i)
                {
                    _additionalProducerBuffer.Post(new TestClassInMemoryInfo(testTemplates.ElementAt(i).TestClassName, testTemplates.ElementAt(i).TestClassData));
                }
            }

            return(new TestClassInMemoryInfo(testTemplates.First().TestClassName, testTemplates.First().TestClassData));
        }
예제 #15
0
        protected async Task <IEnumerable <ReferenceLocation> > FindReferencesInForEachStatementsAsync(
            ISymbol symbol,
            Document document,
            CancellationToken cancellationToken)
        {
            var syntaxTreeInfo = await SyntaxTreeInfo.GetContextInfoAsync(document, cancellationToken).ConfigureAwait(false);

            if (syntaxTreeInfo.ContainsForEachStatement)
            {
                var semanticFacts = document.Project.LanguageServices.GetService <ISemanticFactsService>();
                var syntaxRoot    = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

                var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

                var locations = new List <ReferenceLocation>();

                var originalUnreducedSymbolDefinition = symbol.GetOriginalUnreducedDefinition();

                foreach (var node in syntaxRoot.DescendantNodesAndSelf())
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    var info = semanticFacts.GetForEachSymbols(semanticModel, node);

                    if (Matches(info.GetEnumeratorMethod, originalUnreducedSymbolDefinition) ||
                        Matches(info.MoveNextMethod, originalUnreducedSymbolDefinition) ||
                        Matches(info.CurrentProperty, originalUnreducedSymbolDefinition) ||
                        Matches(info.DisposeMethod, originalUnreducedSymbolDefinition))
                    {
                        var location = node.GetFirstToken().GetLocation();
                        locations.Add(new ReferenceLocation(
                                          document, alias: null, location: location, isImplicit: true, candidateReason: CandidateReason.None));
                    }
                }

                return(locations);
            }
            else
            {
                return(SpecializedCollections.EmptyEnumerable <ReferenceLocation>());
            }
        }
            private async Task DetermineDocumentsAndConflictingNamesAsync(Project project)
            {
                var symbol = renameLocationSet.Symbol;
                var renameRewriterLanguageService = LanguageService.GetService <IRenameRewriterLanguageService>(project);

                renameRewriterLanguageService.TryAddPossibleNameConflicts(symbol, replacementText, possibleNameConflicts);

                foreach (var document in project.Documents)
                {
                    if (this.documentsIdsToBeCheckedForConflict.Contains(document.Id))
                    {
                        continue;
                    }

                    var info = await SyntaxTreeInfo.GetIdentifierInfoAsync(document, CancellationToken.None).ConfigureAwait(false);

                    if (info.ProbablyContainsEscapedIdentifier(originalText))
                    {
                        this.documentsIdsToBeCheckedForConflict.Add(document.Id);
                        continue;
                    }

                    if (info.ProbablyContainsIdentifier(replacementText))
                    {
                        this.documentsIdsToBeCheckedForConflict.Add(document.Id);
                        continue;
                    }

                    foreach (var replacementName in possibleNameConflicts)
                    {
                        if (info.ProbablyContainsIdentifier(replacementName))
                        {
                            this.documentsIdsToBeCheckedForConflict.Add(document.Id);
                            break;
                        }
                    }
                }
            }
예제 #17
0
 public override Task AnalyzeSyntaxAsync(Document document, InvocationReasons reasons, CancellationToken cancellationToken)
 {
     return(SyntaxTreeInfo.PrecalculateAsync(document, cancellationToken));
 }
예제 #18
0
 public static async Task <IDeclarationInfo> GetDeclarationInfoAsync(this Document document, CancellationToken cancellationToken)
 {
     return(await SyntaxTreeInfo.GetDeclarationInfoAsync(document, cancellationToken).ConfigureAwait(false));
 }
예제 #19
0
 public TestTemplatesGenerator(SyntaxTreeInfo syntaxTreeInfo)
 {
     _syntaxTreeInfo = syntaxTreeInfo;
 }
예제 #20
0
        public static async Task <IEnumerable <DeclaredSymbolInfo> > GetDeclaredSymbolInfosAsync(this Document document, CancellationToken cancellationToken)
        {
            var declarationInfo = await SyntaxTreeInfo.GetDeclarationInfoAsync(document, cancellationToken).ConfigureAwait(false);

            return(declarationInfo.DeclaredSymbolInfos);
        }
예제 #21
0
 /// <summary>
 /// Initializes a new instance of a <see cref="TestClassTemplateGenerator"/> class.
 /// </summary>
 /// <param name="syntaxTreeInfo"></param>
 public TestClassTemplateGenerator(SyntaxTreeInfo syntaxTreeInfo)
 {
     _syntaxTreeInfo = syntaxTreeInfo;
 }