private bool IsSuppressedInSuppressionFile(SemanticModel semanticModel, Diagnostic diagnostic, CancellationToken cancellation)
        {
            cancellation.ThrowIfCancellationRequested();

            var(assembly, message) = SuppressMessage.GetSuppressionInfo(semanticModel, diagnostic, cancellation);

            if (assembly == null)
            {
                return(false);
            }

            SuppressionFile file = GetSuppressionFile(assembly);

            if (file == null)
            {
                return(false);
            }

            if (file.GenerateSuppressionBase)
            {
                if (IsSuppressableSeverity(diagnostic?.Descriptor.DefaultSeverity))
                {
                    file.AddMessage(message);
                }

                return(true);
            }

            return(file.ContainsMessage(message));
        }
        internal SuppressionFile LoadSuppressionFileFrom(string filePath)
        {
            SuppressionFile suppressionFile = LoadFileAndTrackItsChanges(filePath, generateSuppressionBase: false);

            _fileByAssembly[suppressionFile.AssemblyName] = suppressionFile;
            return(suppressionFile);
        }
 private SuppressionFile LoadFileAndTrackItsChanges(string suppressionFilePath, bool generateSuppressionBase)
 {
     lock (_fileSystemService)
     {
         SuppressionFile suppressionFile = SuppressionFile.Load(_fileSystemService, suppressionFilePath, generateSuppressionBase);
         suppressionFile.Changed += ReloadFile;
         return(suppressionFile);
     }
 }
        private static SuppressionDiffResult CompareFiles(SuppressionFile oldFile, SuppressionFile newFile)
        {
            var oldMessages = oldFile.CopyMessages();
            var newMessages = newFile.CopyMessages();

            var addedMessages = new HashSet <SuppressMessage>(newMessages);

            addedMessages.ExceptWith(oldMessages);

            var deletedMessages = new HashSet <SuppressMessage>(oldMessages);

            deletedMessages.ExceptWith(newMessages);

            return(new SuppressionDiffResult(oldFile.AssemblyName, oldFile.Path, addedMessages, deletedMessages));
        }
        private void LoadSuppressionFiles(IEnumerable <SuppressionManagerInitInfo> suppressionFiles)
        {
            foreach (SuppressionManagerInitInfo fileInfo in suppressionFiles)
            {
                if (!SuppressionFile.IsSuppressionFile(fileInfo.Path))
                {
                    throw new ArgumentException($"File {fileInfo.Path} is not a suppression file");
                }

                var file = LoadFileAndTrackItsChanges(fileInfo.Path, fileInfo.GenerateSuppressionBase);

                if (!_fileByAssembly.TryAdd(file.AssemblyName, file))
                {
                    throw new InvalidOperationException($"Suppression information for assembly {file.AssemblyName} has been already loaded");
                }
            }
        }
            public TextDocument AddAdditionalSuppressionDocumentToProject(Project project)
            {
                project.ThrowOnNull(nameof(project));

                string suppressionFileName = project.AssemblyName + SuppressionFile.SuppressionFileExtension;
                string projectDir          = Instance._fileSystemService.GetFileDirectory(project.FilePath);
                string suppressionFilePath = Path.Combine(projectDir, suppressionFileName);

                //Create new xml document and get its text
                var newXDocument = SuppressionFile.NewDocumentFromMessages(Enumerable.Empty <SuppressMessage>());

                if (newXDocument == null)
                {
                    return(null);
                }

                string docText = newXDocument.GetXDocumentStringWithDeclaration();

                //Add file to project without applying changes to workspace
                return(project.AddAdditionalDocument(suppressionFileName, docText, filePath: suppressionFilePath));
            }
        public static IEnumerable <SuppressionDiffResult> ValidateSuppressionBaseDiff()
        {
            if (Instance == null)
            {
                return(Enumerable.Empty <SuppressionDiffResult>());
            }

            var diffList = new List <SuppressionDiffResult>();

            lock (Instance._fileSystemService)
            {
                foreach (SuppressionFile currentFile in Instance._fileByAssembly.Files)
                {
                    var oldFile = SuppressionFile.Load(Instance._fileSystemService, suppressionFilePath: currentFile.Path,
                                                       generateSuppressionBase: false);

                    diffList.Add(CompareFiles(oldFile, currentFile));
                }
            }

            return(diffList);
        }
 public bool TryGetValue(string assemblyName, out SuppressionFile file) => _fileByAssembly.TryGetValue(assemblyName, out file);
 public bool TryAdd(string assemblyName, SuppressionFile file) => _fileByAssembly.TryAdd(assemblyName, file);