コード例 #1
0
            public static async Task <HashSet <IFieldSymbol> > Scan(
                Document document,
                CancellationToken cancellationToken)
            {
                var scanner = new WritableFieldScanner(
                    await document.GetSemanticModelAsync(cancellationToken));

                scanner.Visit(await document.GetSyntaxRootAsync(cancellationToken));
                return(scanner.m_fields);
            }
コード例 #2
0
        public async Task <Solution> ProcessAsync(
            Document document,
            SyntaxNode syntaxRoot,
            CancellationToken cancellationToken)
        {
            if (m_unwrittenWritableFields == null)
            {
                using (await SemaphoreLock.GetAsync(m_processUsagesLock))
                {
                    // A global analysis must be run before we can do any actual processing, because a field might
                    // be written in a different file than it is declared (even private ones may be split between
                    // partial classes).

                    // It's also quite expensive, which is why it's being done inside the lock, so
                    // that the entire solution is not processed for each input file individually
                    if (m_unwrittenWritableFields == null)
                    {
                        List <Document> allDocuments =
                            document.Project.Solution.Projects.SelectMany(p => p.Documents).ToList();
                        HashSet <IFieldSymbol>[] fields = await Task.WhenAll(
                            allDocuments
                            .AsParallel()
                            .Select(
                                doc => WritableFieldScanner.Scan(doc, cancellationToken)));

                        var writableFields = new ConcurrentDictionary <IFieldSymbol, bool>(
                            fields.SelectMany(s => s).Select(f => new KeyValuePair <IFieldSymbol, bool>(f, true)));

                        await Task.WhenAll(
                            allDocuments.AsParallel()
                            .Select(
                                doc => WriteUsagesScanner.RemoveWrittenFields(
                                    doc,
                                    writableFields,
                                    cancellationToken)));

                        m_unwrittenWritableFields = writableFields;
                    }
                }
            }

            if (m_unwrittenWritableFields.Count == 0)
            {
                // If there are no unwritten writable fields, skip all the rewriting
                return(document.Project.Solution);
            }

            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken);

            var application = new ReadonlyRewriter(
                m_unwrittenWritableFields,
                await document.GetSemanticModelAsync(cancellationToken));

            return(document.Project.Solution.WithDocumentSyntaxRoot(document.Id, application.Visit(root)));
        }
コード例 #3
0
            public static async Task <HashSet <IFieldSymbol> > Scan(
                Document document,
                CancellationToken cancellationToken)
            {
                if (document.Project.Language != LanguageNames.CSharp)
                {
                    return(new HashSet <IFieldSymbol>());
                }

                var scanner = new WritableFieldScanner(
                    await document.GetSemanticModelAsync(cancellationToken));

                scanner.Visit(await document.GetSyntaxRootAsync(cancellationToken));
                return(scanner._fields);
            }
 public static async Task<HashSet<IFieldSymbol>> Scan(
     Document document,
     CancellationToken cancellationToken)
 {
     var scanner = new WritableFieldScanner(
         await document.GetSemanticModelAsync(cancellationToken));
     scanner.Visit(await document.GetSyntaxRootAsync(cancellationToken));
     return scanner._fields;
 }