Пример #1
0
        public override T GetValue(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var weakReference = _weakReference;

            if (weakReference == null || !weakReference.TryGetTarget(out var instance))
            {
                Task?saveTask = null;
                using (Gate.DisposableWait(cancellationToken))
                {
                    if (_weakReference == null || !_weakReference.TryGetTarget(out instance))
                    {
                        instance = _recoverySource.GetValue(cancellationToken);
                        saveTask = EnsureInstanceIsSaved(instance);
                    }
                }

                if (saveTask != null)
                {
                    ResetRecoverySource(saveTask, instance);
                }
            }

            return(instance);
        }
Пример #2
0
        public AnalyzerConfigOptionsResult?GetAnalyzerConfigOptions()
        {
            // We need to find the analyzer config options at the root of the project.
            // Currently, there is no compiler API to query analyzer config options for a directory in a language agnostic fashion.
            // So, we use a dummy language-specific file name appended to the project directory to query analyzer config options.

            var projectDirectory = PathUtilities.GetDirectoryName(_projectInfo.FilePath);

            if (!PathUtilities.IsAbsolute(projectDirectory))
            {
                return(null);
            }

            var    fileName = Guid.NewGuid().ToString();
            string sourceFilePath;

            switch (_projectInfo.Language)
            {
            case LanguageNames.CSharp:
                // Suppression should be removed or addressed https://github.com/dotnet/roslyn/issues/41636
                sourceFilePath = PathUtilities.CombineAbsoluteAndRelativePaths(projectDirectory, $"{fileName}.cs") !;
                break;

            case LanguageNames.VisualBasic:
                // Suppression should be removed or addressed https://github.com/dotnet/roslyn/issues/41636
                sourceFilePath = PathUtilities.CombineAbsoluteAndRelativePaths(projectDirectory, $"{fileName}.vb") !;
                break;

            default:
                return(null);
            }

            return(_lazyAnalyzerConfigSet.GetValue(CancellationToken.None).GetOptionsForSourcePath(sourceFilePath));
        }
Пример #3
0
        private static TextAndVersion GetTextAndVersion(
            ValueSource <TreeAndVersion> newTreeSource, VersionStamp version, Encoding encoding, string filePath, CancellationToken cancellationToken)
        {
            var treeAndVersion = newTreeSource.GetValue(cancellationToken);
            var text           = treeAndVersion.Tree.GetRoot(cancellationToken).GetText(encoding);

            return(TextAndVersion.Create(text, version, filePath));
        }
Пример #4
0
        internal SyntaxTree GetSyntaxTree(CancellationToken cancellationToken)
        {
            var treeAndVersion = _treeSource.GetValue(cancellationToken);

            // make sure there is an association between this tree and this doc id before handing it out
            BindSyntaxTreeToId(treeAndVersion.Tree, this.Id);
            return(treeAndVersion.Tree);
        }
Пример #5
0
        internal DocumentState UpdateTree(SyntaxNode newRoot, PreservationMode mode)
        {
            if (newRoot == null)
            {
                throw new ArgumentNullException(nameof(newRoot));
            }

            var newTextVersion = GetNewerVersion();
            var newTreeVersion = GetNewTreeVersionForUpdatedTree(newRoot, newTextVersion, mode);

            // determine encoding
            Encoding?encoding;
            ImmutableDictionary <string, ReportDiagnostic>?treeDiagnosticReportingOptions = null;

            if (TryGetSyntaxTree(out var priorTree))
            {
                // this is most likely available since UpdateTree is normally called after modifying the existing tree.
                encoding = priorTree.Encoding;
                treeDiagnosticReportingOptions = priorTree.DiagnosticOptions;
            }
            else if (TryGetText(out var priorText))
            {
                encoding = priorText.Encoding;
            }
            else
            {
                // the existing encoding was never observed so is unknown.
                encoding = null;
            }

            var syntaxTreeFactory = _languageServices.GetRequiredService <ISyntaxTreeFactoryService>();

            var filePath = GetSyntaxTreeFilePath(Attributes);

            if (treeDiagnosticReportingOptions == null)
            {
                // Ideally we'd pass a cancellation token here but we don't have one to pass as the operation previously didn't take a cancellation token.
                // In practice, I don't suspect it will matter: GetValue will only do work if we haven't already computed the AnalyzerConfigSet for this project,
                // which would only happen if no tree was observed for any file in this project. Arbitrarily replacing trees without ever looking at the
                // original one is possible but unlikely.
                treeDiagnosticReportingOptions = _analyzerConfigSetSource.GetValue(CancellationToken.None).GetOptionsForSourcePath(filePath).TreeOptions;
            }

            Contract.ThrowIfNull(_options);
            var(text, tree) = CreateRecoverableTextAndTree(newRoot, filePath, newTextVersion, newTreeVersion, encoding, Attributes, _options, treeDiagnosticReportingOptions, syntaxTreeFactory, mode);

            return(new DocumentState(
                       LanguageServices,
                       solutionServices,
                       Services,
                       Attributes,
                       _options,
                       _analyzerConfigSetSource,
                       sourceText: null,
                       textSource: text,
                       treeSource: new ConstantValueSource <TreeAndVersion>(tree)));
        }
Пример #6
0
        internal SyntaxTree GetSyntaxTree(CancellationToken cancellationToken)
        {
            // operation should only be performed on documents that support syntax trees
            RoslynDebug.Assert(_treeSource != null);

            var treeAndVersion = _treeSource.GetValue(cancellationToken);

            // make sure there is an association between this tree and this doc id before handing it out
            BindSyntaxTreeToId(treeAndVersion.Tree, this.Id);
            return(treeAndVersion.Tree);
        }
            /// <summary>
            /// Creates a new instance of the compilation info, retaining any already built
            /// compilation state as the now 'old' state
            /// </summary>
            public CompilationTracker Fork(
                ProjectState newProject,
                CompilationTranslationAction translate = null,
                CancellationToken cancellationToken    = default(CancellationToken),
                bool clone = false)
            {
                var state = this.ReadState();

                ValueSource <Compilation> baseCompilationSource = state.Compilation;
                var baseCompilation = baseCompilationSource.GetValue(cancellationToken);

                if (baseCompilation != null)
                {
                    // We have some pre-calculated state to incrementally update
                    var newInProgressCompilationSource = clone
                        ? new WeakConstantValueSource <Compilation>(baseCompilation.Clone())
                        : baseCompilationSource;

                    var intermediateProjects = state is InProgressState
                        ? ((InProgressState)state).IntermediateProjects
                        : ImmutableArray.Create <ValueTuple <ProjectState, CompilationTranslationAction> >();

                    var newIntermediateProjects = translate == null
                         ? intermediateProjects
                         : intermediateProjects.Add(ValueTuple.Create(this.ProjectState, translate));

                    var newState = State.Create(newInProgressCompilationSource, newIntermediateProjects);

                    return(new CompilationTracker(newProject, newState));
                }

                var declarationOnlyCompilation = state.DeclarationOnlyCompilation;

                if (declarationOnlyCompilation != null)
                {
                    if (translate != null)
                    {
                        var compilationSource = clone
                            ? (ValueSource <Compilation>) new WeakConstantValueSource <Compilation>(declarationOnlyCompilation)
                            : (ValueSource <Compilation>) new ConstantValueSource <Compilation>(declarationOnlyCompilation);

                        var intermediateProjects =
                            ImmutableArray.Create <ValueTuple <ProjectState, CompilationTranslationAction> >(ValueTuple.Create(this.ProjectState, translate));

                        return(new CompilationTracker(newProject, new InProgressState(compilationSource, intermediateProjects)));
                    }

                    return(new CompilationTracker(newProject, new LightDeclarationState(declarationOnlyCompilation)));
                }

                // We have nothing.  Just make a tracker that only points to the new project.  We'll have
                // to rebuild its compilation from scratch if anyone asks for it.
                return(new CompilationTracker(newProject));
            }
                public InProgressState(
                    ValueSource <Compilation> inProgressCompilationSource,
                    ImmutableList <ValueTuple <ProjectState, CompilationTranslationAction> > intermediateProjects)
                    : base(inProgressCompilationSource)
                {
                    Contract.ThrowIfNull(inProgressCompilationSource);
                    Contract.ThrowIfNull(inProgressCompilationSource.GetValue());
                    Contract.ThrowIfNull(intermediateProjects);
                    Contract.ThrowIfFalse(intermediateProjects.Count > 0);

                    this.IntermediateProjects = intermediateProjects;
                }
                public InProgressState(
                    ValueSource <Compilation> inProgressCompilationSource,
                    ImmutableArray <ValueTuple <ProjectState, CompilationTranslationAction> > intermediateProjects)
                    : base(inProgressCompilationSource)
                {
                    Contract.ThrowIfNull(inProgressCompilationSource);
                    Contract.ThrowIfNull(inProgressCompilationSource.GetValue());
                    Contract.ThrowIfTrue(intermediateProjects.IsDefault);
                    Contract.ThrowIfFalse(intermediateProjects.Length > 0);

                    this.IntermediateProjects = intermediateProjects;
                }
                public static State Create(
                    ValueSource <Compilation> compilationSource,
                    ImmutableList <ValueTuple <ProjectState, CompilationTranslationAction> > intermediateProjects)
                {
                    Contract.ThrowIfNull(compilationSource);
                    Contract.ThrowIfNull(compilationSource.GetValue());
                    Contract.ThrowIfNull(intermediateProjects);

                    // If we don't have any intermediate projects to process, just initialize our
                    // DeclarationState now.
                    return(intermediateProjects.Count == 0
                        ? (State) new FullDeclarationState(compilationSource)
                        : (State) new InProgressState(compilationSource, intermediateProjects));
                }
                public static State Create(
                    ValueSource<Compilation> compilationSource,
                    ImmutableArray<ValueTuple<ProjectState, CompilationTranslationAction>> intermediateProjects)
                {
                    Contract.ThrowIfNull(compilationSource);
                    Contract.ThrowIfNull(compilationSource.GetValue());
                    Contract.ThrowIfTrue(intermediateProjects.IsDefault);

                    // If we don't have any intermediate projects to process, just initialize our
                    // DeclarationState now.
                    return intermediateProjects.Length == 0
                        ? (State)new FullDeclarationState(compilationSource)
                        : (State)new InProgressState(compilationSource, intermediateProjects);
                }
Пример #12
0
 private static TreeAndVersion FullyParseTree(
     ValueSource <TextAndVersion> newTextSource,
     ProjectId cacheKey,
     string?filePath,
     ParseOptions options,
     HostLanguageServices languageServices,
     PreservationMode mode,
     CancellationToken cancellationToken)
 {
     using (Logger.LogBlock(FunctionId.Workspace_Document_State_FullyParseSyntaxTree, s_fullParseLog, filePath, mode, cancellationToken))
     {
         var textAndVersion = newTextSource.GetValue(cancellationToken);
         return(CreateTreeAndVersion(newTextSource, cacheKey, filePath, options, languageServices, mode, textAndVersion, cancellationToken));
     }
 }
Пример #13
0
        public override T GetValue(CancellationToken cancellationToken = default(CancellationToken))
        {
            if (!_reference.TryGetTarget(out var value))
            {
                using (Gate.DisposableWait(cancellationToken))
                {
                    if (!_reference.TryGetTarget(out value))
                    {
                        value      = _source.GetValue(cancellationToken);
                        _reference = new WeakReference <T>(value);
                    }
                }
            }

            return(value);
        }
            public bool TryGetOrAddMetadata(FileKey key, ValueSource <AssemblyMetadata> newMetadata, out AssemblyMetadata metadata)
            {
                lock (_gate) {
                    if (TryGetMetadata_NoLock(key, out metadata))
                    {
                        return(false);
                    }

                    EnsureCapacity_NoLock();

                    metadata = newMetadata.GetValue();
                    Contract.ThrowIfNull(metadata);

                    // don't use "Add" since key might already exist with already released metadata
                    _metadataCache [key] = newMetadata;
                    return(true);
                }
            }
            public bool TryGetOrAddMetadata(FileKey key, ValueSource<AssemblyMetadata> newMetadata, out AssemblyMetadata metadata)
            {
                lock (_gate)
                {
                    if (TryGetMetadata_NoLock(key, out metadata))
                    {
                        return false;
                    }

                    EnsureCapacity_NoLock();

                    metadata = newMetadata.GetValue();
                    Contract.ThrowIfNull(metadata);

                    // don't use "Add" since key might already exist with already released metadata
                    _metadataCache[key] = newMetadata;
                    return true;
                }
            }
Пример #16
0
        private static TreeAndVersion IncrementallyParseTree(
            ValueSource <TreeAndVersion> oldTreeSource,
            ValueSource <TextAndVersion> newTextSource,
            CancellationToken cancellationToken)
        {
            try
            {
                using (Logger.LogBlock(FunctionId.Workspace_Document_State_IncrementallyParseSyntaxTree, cancellationToken))
                {
                    var newTextAndVersion = newTextSource.GetValue(cancellationToken);
                    var oldTreeAndVersion = oldTreeSource.GetValue(cancellationToken);

                    return(IncrementallyParse(newTextAndVersion, oldTreeAndVersion, cancellationToken));
                }
            }
            catch (Exception e) when(FatalError.ReportUnlessCanceled(e))
            {
                throw ExceptionUtilities.Unreachable;
            }
        }
Пример #17
0
        public override T GetValue(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (!_weakInstance.TryGetTarget(out var instance))
            {
                Task saveTask = null;
                using (this.Gate.DisposableWait(cancellationToken))
                {
                    if (!_weakInstance.TryGetTarget(out instance))
                    {
                        instance = _recoverySource.GetValue(cancellationToken);
                        saveTask = EnsureInstanceIsSaved(instance);
                    }
                }

                ResetRecoverySource(saveTask, instance);
            }

            return(instance);
        }
Пример #18
0
        private static TreeAndVersion IncrementallyParseTree(
            ValueSource<TreeAndVersion> oldTreeSource,
            ValueSource<TextAndVersion> newTextSource,
            CancellationToken cancellationToken)
        {
            try
            {
                using (Logger.LogBlock(FunctionId.Workspace_Document_State_IncrementallyParseSyntaxTree, cancellationToken))
                {
                    var newTextAndVersion = newTextSource.GetValue(cancellationToken);
                    var oldTreeAndVersion = oldTreeSource.GetValue(cancellationToken);

                    return IncrementallyParse(newTextAndVersion, oldTreeAndVersion, cancellationToken);
                }
            }
            catch (Exception e) when (FatalError.ReportUnlessCanceled(e))
            {
                throw ExceptionUtilities.Unreachable;
            }
        }
 public FinalState(ValueSource<Compilation> finalCompilationSource)
     : base(finalCompilationSource, finalCompilationSource.GetValue().Clone().RemoveAllReferences())
 {
 }
 public FinalState(ValueSource <Compilation> finalCompilationSource)
     : base(finalCompilationSource, finalCompilationSource.GetValue().Clone().RemoveAllReferences())
 {
 }
 public FullDeclarationState(ValueSource <Compilation> declarationCompilation)
     : base(declarationCompilation, declarationCompilation.GetValue().RemoveAllReferences())
 {
 }
Пример #22
0
 public SourceText GetText(CancellationToken cancellationToken)
 {
     return(TextSource.GetValue(cancellationToken).Text);
 }
Пример #23
0
 public FinalState(ValueSource <Compilation> finalCompilationSource, bool hasSuccessfullyLoaded)
     : base(finalCompilationSource, finalCompilationSource.GetValue().Clone().RemoveAllReferences())
 {
     _hasSuccessfullyLoaded = hasSuccessfullyLoaded;
 }
Пример #24
0
 private static TreeAndVersion FullyParseTree(
     ValueSource<TextAndVersion> newTextSource,
     ProjectId cacheKey,
     string filePath,
     ParseOptions options,
     HostLanguageServices languageServices,
     SolutionServices solutionServices,
     PreservationMode mode,
     CancellationToken cancellationToken)
 {
     using (Logger.LogBlock(FunctionId.Workspace_Document_State_FullyParseSyntaxTree, s_fullParseLog, filePath, mode, cancellationToken))
     {
         var textAndVersion = newTextSource.GetValue(cancellationToken);
         return CreateTreeAndVersion(newTextSource, cacheKey, filePath, options, languageServices, mode, textAndVersion, cancellationToken);
     }
 }
Пример #25
0
 private static TextAndVersion GetTextAndVersion(
     ValueSource<TreeAndVersion> newTreeSource, VersionStamp version, Encoding encoding, string filePath, CancellationToken cancellationToken)
 {
     var treeAndVersion = newTreeSource.GetValue(cancellationToken);
     var text = treeAndVersion.Tree.GetRoot(cancellationToken).GetText(encoding);
     return TextAndVersion.Create(text, version, filePath);
 }
Пример #26
0
 public AnalyzerConfig GetAnalyzerConfig(CancellationToken cancellationToken) => _analyzerConfigValueSource.GetValue(cancellationToken);
                public InProgressState(
                    ValueSource<Compilation> inProgressCompilationSource,
                    ImmutableArray<ValueTuple<ProjectState, CompilationTranslationAction>> intermediateProjects)
                    : base(inProgressCompilationSource)
                {
                    Contract.ThrowIfNull(inProgressCompilationSource);
                    Contract.ThrowIfNull(inProgressCompilationSource.GetValue());
                    Contract.ThrowIfTrue(intermediateProjects.IsDefault);
                    Contract.ThrowIfFalse(intermediateProjects.Length > 0);

                    this.IntermediateProjects = intermediateProjects;
                }
 public FullDeclarationState(ValueSource<Compilation> declarationCompilation)
     : base(declarationCompilation, declarationCompilation.GetValue().RemoveAllReferences())
 {
 }
 public FinalState(ValueSource<Compilation> finalCompilationSource, bool hasSuccessfullyLoadedTransitively)
     : base(finalCompilationSource, finalCompilationSource.GetValue().Clone().RemoveAllReferences())
 {
     _hasSuccessfullyLoadedTransitively = hasSuccessfullyLoadedTransitively;
 }
 public FinalState(ValueSource<Compilation> finalCompilationSource, bool hasCompleteReferences)
     : base(finalCompilationSource, finalCompilationSource.GetValue().Clone().RemoveAllReferences())
 {
     _hasCompleteReferences = hasCompleteReferences;
 }
            public override TextAndVersion GetValue(CancellationToken cancellationToken = default)
            {
                var text = _lazyText.GetValue(cancellationToken);

                return(TextAndVersion.Create(text, _version, _filePath));
            }
                public InProgressState(
                    ValueSource<Compilation> inProgressCompilationSource,
                    ImmutableList<ValueTuple<ProjectState, CompilationTranslationAction>> intermediateProjects)
                    : base(inProgressCompilationSource)
                {
                    Contract.ThrowIfNull(inProgressCompilationSource);
                    Contract.ThrowIfNull(inProgressCompilationSource.GetValue());
                    Contract.ThrowIfNull(intermediateProjects);
                    Contract.ThrowIfFalse(intermediateProjects.Count > 0);

                    this.IntermediateProjects = intermediateProjects;
                }
 public FinalState(ValueSource <Compilation> finalCompilationSource, bool hasCompleteReferences)
     : base(finalCompilationSource, finalCompilationSource.GetValue().Clone().RemoveAllReferences())
 {
     _hasCompleteReferences = hasCompleteReferences;
 }