Пример #1
0
 public AnalyzerItem(AnalyzersFolderItem analyzersFolder, AnalyzerReference analyzerReference, IContextMenuController contextMenuController)
     : base(GetNameText(analyzerReference))
 {
     _analyzersFolder = analyzersFolder;
     _analyzerReference = analyzerReference;
     _contextMenuController = contextMenuController;
 }
Пример #2
0
        private static Checksum CreateChecksum(AnalyzerReference reference)
        {
            using (var stream = SerializableBytes.CreateWritableStream())
            using (var objectWriter = new StreamObjectWriter(stream))
            {
                objectWriter.WriteString(WellKnownSynchronizationKinds.AnalyzerReference);
                objectWriter.WriteString(reference.FullPath);

                return Checksum.Create(stream);
            }
        }
        public Checksum CreateChecksum(AnalyzerReference reference, CancellationToken cancellationToken)
        {
            using (var stream = SerializableBytes.CreateWritableStream())
            using (var writer = new ObjectWriter(stream, cancellationToken: cancellationToken))
            {
                WriteTo(reference, writer, cancellationToken);

                stream.Position = 0;
                return Checksum.Create(stream);
            }
        }
Пример #4
0
 private static string GetNameText(AnalyzerReference analyzerReference)
 {
     if (analyzerReference.IsUnresolved)
     {
         return analyzerReference.FullPath;
     }
     else
     {
         return analyzerReference.Display;
     }
 }
        public void SonarAnalyzerManager_HasNoCollidingAnalyzerReference_SameNameVersion()
        {
            IEnumerable<AnalyzerReference> references = new AnalyzerReference[]
            {
                new ConfigurableAnalyzerReference(
                    new AssemblyIdentity(SonarAnalyzerManager.AnalyzerName, SonarAnalyzerManager.AnalyzerVersion),
                    SonarAnalyzerManager.AnalyzerName)
            };

            Assert.IsFalse(
                SonarAnalyzerManager.HasConflictingAnalyzerReference(
                    SonarAnalyzerManager.GetProjectAnalyzerConflictStatus(references)),
                "Same named and versioned analyzers should not be reported as conflicting ones");
        }
                        public bool HasAnalyzerReference(AnalyzerReference analyzerReference)
                        {
                            Contract.ThrowIfNull(analyzerReference);

                            if (analyzerReference is AnalyzerFileReference)
                            {
                                // Filter out duplicate analyzer references with same assembly name/full path.
                                return(analyzerReference.Display != null && _diagnosticAnalyzerIdMap.ContainsKey(analyzerReference.Display));
                            }
                            else
                            {
                                // For non-file references, we will check individual DiagnosticAnalyzer instances for duplicates.
                                return(false);
                            }
                        }
Пример #7
0
        public AnalyzerReference GetReference()
        {
            if (_analyzerReference == null)
            {
                if (File.Exists(_fullPath))
                {
                    _analyzerReference = new AnalyzerFileReference(_fullPath, _loader);
                    ((AnalyzerFileReference)_analyzerReference).AnalyzerLoadFailed += OnAnalyzerLoadError;
                }
                else
                {
                    _analyzerReference = new UnresolvedAnalyzerReference(_fullPath);
                }
            }

            return _analyzerReference;
        }
Пример #8
0
        public AnalyzerReference GetReference()
        {
            if (_analyzerReference == null)
            {
                if (File.Exists(_fullPath))
                {
                    _analyzerReference = new AnalyzerFileReference(_fullPath, InMemoryAssemblyProvider.GetAssembly);
                    ((AnalyzerFileReference)_analyzerReference).AnalyzerLoadFailed += OnAnalyzerLoadError;
                }
                else
                {
                    _analyzerReference = new UnresolvedAnalyzerReference(_fullPath);
                }
            }

            return _analyzerReference;
        }
        public void SonarAnalyzerManager_HasCollidingAnalyzerReference()
        {
            var version = new Version("0.1.2.3");
            Assert.AreNotEqual(SonarAnalyzerManager.AnalyzerVersion, version,
                "Test input should be different from the expected analyzer version");

            IEnumerable<AnalyzerReference> references = new AnalyzerReference[]
            {
                new ConfigurableAnalyzerReference(
                    new AssemblyIdentity(SonarAnalyzerManager.AnalyzerName, version),
                    SonarAnalyzerManager.AnalyzerName)
            };

            Assert.IsTrue(
                SonarAnalyzerManager.HasConflictingAnalyzerReference(
                    SonarAnalyzerManager.GetProjectAnalyzerConflictStatus(references)),
                "Conflicting analyzer package not found");
        }
        public void SonarAnalyzerManager_GetIsBoundWithoutAnalyzer_Unbound_Conflicting()
        {
            this.activeSolutionBoundTracker.IsActiveSolutionBound = false;

            var version = new Version("0.1.2.3");
            Assert.AreNotEqual(SonarAnalyzerManager.AnalyzerVersion, version,
                "Test input should be different from the expected analyzer version");

            IEnumerable<AnalyzerReference> references = new AnalyzerReference[]
            {
                new ConfigurableAnalyzerReference(
                    new AssemblyIdentity(SonarAnalyzerManager.AnalyzerName, version),
                    SonarAnalyzerManager.AnalyzerName)
            };

            Assert.IsFalse(
                this.testSubject.GetIsBoundWithoutAnalyzer(
                    SonarAnalyzerManager.GetProjectAnalyzerConflictStatus(references)),
                "Unbound solution should never return true");
        }
Пример #11
0
        private void UpdateDiagnosticAnalyzerToPackageNameMap(
            ImmutableDictionary <string, string> nameMap,
            AnalyzerReference reference,
            ImmutableArray <DiagnosticAnalyzer> analyzers)
        {
            if (!(reference is AnalyzerFileReference fileReference))
            {
                return;
            }

            if (!nameMap.TryGetValue(fileReference.FullPath, out var name))
            {
                return;
            }

            foreach (var analyzer in analyzers)
            {
                ImmutableInterlocked.GetOrAdd(ref _hostDiagnosticAnalyzerPackageNameMap, analyzer, name);
            }
        }
Пример #12
0
 /// <summary>
 /// Creates a new instance of this project updated to no longer include the specified analyzer reference.
 /// </summary>
 public Project RemoveAnalyzerReference(AnalyzerReference analyzerReference)
 {
     return this.Solution.RemoveAnalyzerReference(this.Id, analyzerReference).GetProject(this.Id);
 }
        public void SonarAnalyzerManager_HasNoCollidingAnalyzerReference_SameVersionDifferentName()
        {
            var name = "Some test name";
            Assert.AreNotEqual(SonarAnalyzerManager.AnalyzerName, name,
                "Test input should be different from the expected analyzer name");

            IEnumerable<AnalyzerReference> references = new AnalyzerReference[]
            {
                new ConfigurableAnalyzerReference(
                    new AssemblyIdentity(name, SonarAnalyzerManager.AnalyzerVersion), name)
            };

            Assert.IsFalse(
                SonarAnalyzerManager.HasConflictingAnalyzerReference(
                    SonarAnalyzerManager.GetProjectAnalyzerConflictStatus(references)),
                "Name is not considered in the confliction checking");
        }
Пример #14
0
 public object GetAnalyzerReferenceIdentity(AnalyzerReference reference)
 => reference.Id;
 private static void WriteUnresolvedAnalyzerReferenceTo(AnalyzerReference reference, ObjectWriter writer)
 {
     writer.WriteString(nameof(UnresolvedAnalyzerReference));
     writer.WriteString(reference.FullPath);
 }
        public void SonarAnalyzerManager_MultipleReferencesWithSameName_NonCollidingVersion()
        {
            var version1 = new Version("0.1.2.3");
            Assert.AreNotEqual(SonarAnalyzerManager.AnalyzerVersion, version1,
                "Test input should be different from the expected analyzer version");
            var version2 = new Version("1.2.3.4");
            Assert.AreNotEqual(SonarAnalyzerManager.AnalyzerVersion, version2,
                "Test input should be different from the expected analyzer version");

            IEnumerable<AnalyzerReference> references = new AnalyzerReference[]
            {
                new ConfigurableAnalyzerReference(
                    new AssemblyIdentity(SonarAnalyzerManager.AnalyzerName, version1),
                    SonarAnalyzerManager.AnalyzerName),
                new ConfigurableAnalyzerReference(
                    new AssemblyIdentity(SonarAnalyzerManager.AnalyzerName, version2),
                    SonarAnalyzerManager.AnalyzerName),
            };

            Assert.IsTrue(
                SonarAnalyzerManager.HasConflictingAnalyzerReference(
                    SonarAnalyzerManager.GetProjectAnalyzerConflictStatus(references)),
                "Having only different reference versions should disable the embedded analyzer");
        }
        public void SonarAnalyzerManager_MultipleReferencesWithSameName_CollidingVersion()
        {
            var version = new Version("0.1.2.3");
            Assert.AreNotEqual(SonarAnalyzerManager.AnalyzerVersion, version,
                "Test input should be different from the expected analyzer version");

            IEnumerable<AnalyzerReference> references = new AnalyzerReference[]
            {
                new ConfigurableAnalyzerReference(
                    new AssemblyIdentity(SonarAnalyzerManager.AnalyzerName, version),
                    SonarAnalyzerManager.AnalyzerName),
                new ConfigurableAnalyzerReference(
                    new AssemblyIdentity(SonarAnalyzerManager.AnalyzerName, SonarAnalyzerManager.AnalyzerVersion),
                    SonarAnalyzerManager.AnalyzerName),
            };

            Assert.IsFalse(
                SonarAnalyzerManager.HasConflictingAnalyzerReference(
                    SonarAnalyzerManager.GetProjectAnalyzerConflictStatus(references)),
                "Having already colliding references should not disable the embedded analyzer if one is of the same version");
        }
 public ProjectCodeFixProvider(AnalyzerReference reference)
 {
     _reference = reference;
     _fixersPerLanguage = ImmutableDictionary<string, ImmutableArray<CodeFixProvider>>.Empty;
 }
Пример #19
0
 /// <summary>
 /// It returns a string that can be used as a way to de-duplicate <see cref="AnalyzerReference"/>s.
 /// </summary>
 public object GetAnalyzerReferenceIdentity(AnalyzerReference reference)
 {
     return reference.Id;
 }
Пример #20
0
        private bool CheckAnalyzerReferenceIdentity(AnalyzerReference reference)
        {
            if (reference == null)
            {
                return false;
            }

            return !_hostAnalyzerReferencesMap.ContainsKey(reference.Id);
        }
Пример #21
0
 public WorkspaceAnalyzerReferenceAsset(AnalyzerReference reference, Serializer serializer) :
     base(CreateChecksum(reference), WellKnownSynchronizationKinds.AnalyzerReference)
 {
     _reference = reference;
     _serializer = serializer;
 }
                    public bool HasAnalyzerReference(AnalyzerReference analyzerReference, string language)
                    {
                        var analyzersAndStates = this.GetOrCreatePerLanguageAnalyzersAndStates(language);

                        return(analyzersAndStates.HasAnalyzerReference(analyzerReference));
                    }
Пример #23
0
 protected override void ApplyAnalyzerReferenceRemoved(ProjectId projectId, AnalyzerReference analyzerReference)
 {
     Debug.Assert(_applyChangesProjectFile != null);
     _applyChangesProjectFile.RemoveAnalyzerReference(analyzerReference);
     this.OnAnalyzerReferenceRemoved(projectId, analyzerReference);
 }
Пример #24
0
 public CustomAsset Build(AnalyzerReference reference, CancellationToken cancellationToken)
 {
     return new WorkspaceAnalyzerReferenceAsset(reference, _serializer);
 }
Пример #25
0
        public ProjectState AddAnalyzerReference(AnalyzerReference analyzerReference)
        {
            Contract.Requires(!this.AnalyzerReferences.Contains(analyzerReference));

            return this.With(
                projectInfo: this.ProjectInfo.WithAnalyzerReferences(this.AnalyzerReferences.ToImmutableArray().Add(analyzerReference)).WithVersion(this.Version.GetNewerVersion()));
        }
Пример #26
0
        /// <summary>
        /// Create a new solution instance with the project specified updated to no longer include
        /// the specified analyzer reference.
        /// </summary>
        public SolutionState RemoveAnalyzerReference(ProjectId projectId, AnalyzerReference analyzerReference)
        {
            if (projectId == null)
            {
                throw new ArgumentNullException(nameof(projectId));
            }

            if (analyzerReference == null)
            {
                throw new ArgumentNullException(nameof(analyzerReference));
            }

            CheckContainsProject(projectId);
            return this.ForkProject(this.GetProjectState(projectId).RemoveAnalyzerReference(analyzerReference));
        }
        public void SonarAnalyzerManager_GetIsBoundWithoutAnalyzer_Unbound_NonConflicting()
        {
            this.activeSolutionBoundTracker.IsActiveSolutionBound = false;

            IEnumerable<AnalyzerReference> references = new AnalyzerReference[]
            {
                new ConfigurableAnalyzerReference(
                    new AssemblyIdentity(SonarAnalyzerManager.AnalyzerName, SonarAnalyzerManager.AnalyzerVersion),
                    SonarAnalyzerManager.AnalyzerName)
            };

            Assert.IsFalse(
                this.testSubject.GetIsBoundWithoutAnalyzer(
                    SonarAnalyzerManager.GetProjectAnalyzerConflictStatus(references)),
                "Unbound solution should never return true");
        }
Пример #28
0
        public void Reset()
        {
            var analyzerFileReference = _analyzerReference as AnalyzerFileReference;
            if (analyzerFileReference != null)
            {
                analyzerFileReference.AnalyzerLoadFailed -= OnAnalyzerLoadError;

                if (_analyzerLoadErrors != null && _analyzerLoadErrors.Count > 0)
                {
                    _hostDiagnosticUpdateSource.ClearDiagnosticsForProject(_projectId, this);
                }

                _hostDiagnosticUpdateSource.ClearAnalyzerReferenceDiagnostics(analyzerFileReference, _language, _projectId);
            }

            _analyzerLoadErrors = null;
            _analyzerReference = null;
        }
Пример #29
0
 /// <summary>
 /// It returns a string that can be used as a way to de-duplicate <see cref="AnalyzerReference"/>s.
 /// </summary>
 public string GetAnalyzerReferenceIdentity(AnalyzerReference reference)
 {
     return(GetAnalyzerReferenceId(reference));
 }
        public void WriteTo(AnalyzerReference reference, ObjectWriter writer, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var file = reference as AnalyzerFileReference;
            if (file != null)
            {
                writer.WriteString(nameof(AnalyzerFileReference));
                writer.WriteInt32((int)SerializationKinds.FilePath);

                writer.WriteString(file.FullPath);

                // TODO: remove this kind of host specific knowledge from common layer.
                //       but think moving it to host layer where this implementation detail actually exist.
                //
                // analyzer assembly path to load analyzer acts like
                // snapshot version for analyzer (since it is based on shadow copy)
                // we can't send over bits and load analyer from memory (image) due to CLR not being able
                // to find satellite dlls for analyzers.
                writer.WriteString(GetAnalyzerAssemblyPath(file));
                return;
            }

            var image = reference as AnalyzerImageReference;
            if (image != null)
            {
                // TODO: think a way to support this or a way to deal with this kind of situation.
                throw new NotSupportedException(nameof(AnalyzerImageReference));
            }

            var unresolved = reference as UnresolvedAnalyzerReference;
            if (unresolved != null)
            {
                writer.WriteString(nameof(UnresolvedAnalyzerReference));
                writer.WriteString(reference.FullPath);
                return;
            }

            throw ExceptionUtilities.UnexpectedValue(reference.GetType());
        }
Пример #31
0
 private static string GetDisplayName(AnalyzerReference reference)
 {
     return(reference.Display ?? FeaturesResources.Unknown);
 }
Пример #32
0
        private void UpdateDiagnosticAnalyzerToPackageNameMap(
            ImmutableDictionary<string, string> nameMap,
            AnalyzerReference reference,
            ImmutableArray<DiagnosticAnalyzer> analyzers)
        {
            var fileReference = reference as AnalyzerFileReference;
            if (fileReference == null)
            {
                return;
            }

            string name;
            if (!nameMap.TryGetValue(fileReference.FullPath, out name))
            {
                return;
            }

            foreach (var analyzer in analyzers)
            {
                ImmutableInterlocked.GetOrAdd(ref _hostDiagnosticAnalyzerPackageNameMap, analyzer, _ => name);
            }
        }
Пример #33
0
 public void SerializeAnalyzerReference(AnalyzerReference reference, ObjectWriter writer, CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     _hostSerializationService.WriteTo(reference, writer, cancellationToken);
 }
Пример #34
0
 /// <summary>
 /// It returns a string that can be used as a way to de-duplicate <see cref="AnalyzerReference"/>s.
 /// </summary>
 public object GetAnalyzerReferenceIdentity(AnalyzerReference reference)
 {
     return(reference.Id);
 }
        public void SonarAnalyzerManager_HasNoCollidingAnalyzerReference_NoDisplayName()
        {
            var version = new Version("0.1.2.3");
            Assert.AreNotEqual(SonarAnalyzerManager.AnalyzerVersion, version,
                "Test input should be different from the expected analyzer version");

            IEnumerable<AnalyzerReference> references = new AnalyzerReference[]
            {
                new ConfigurableAnalyzerReference(
                    new AssemblyIdentity(SonarAnalyzerManager.AnalyzerName, version),
                    null)
            };

            Assert.IsFalse(
                SonarAnalyzerManager.HasConflictingAnalyzerReference(
                    SonarAnalyzerManager.GetProjectAnalyzerConflictStatus(references)),
                "Null analyzer name should not report conflict");
        }
Пример #36
0
        public ProjectState RemoveAnalyzerReference(AnalyzerReference analyzerReference)
        {
            Contract.Requires(this.AnalyzerReferences.Contains(analyzerReference));

            return this.With(
                projectInfo: this.ProjectInfo.WithAnalyzerReferences(this.AnalyzerReferences.Remove(analyzerReference)).WithVersion(this.Version.GetNewerVersion()));
        }
        public void SonarAnalyzerManager_HasNoCollidingAnalyzerReference_NoAssemblyIdentity()
        {
            IEnumerable<AnalyzerReference> references = new AnalyzerReference[]
            {
                new ConfigurableAnalyzerReference(
                    new object(),
                    SonarAnalyzerManager.AnalyzerName)
            };

            Assert.IsTrue(
                SonarAnalyzerManager.HasConflictingAnalyzerReference(
                    SonarAnalyzerManager.GetProjectAnalyzerConflictStatus(references)),
                "If no AssemblyIdentity is present, but the name matches, we should report a conflict");
        }