public async Task <ISymbol> GetCurrentSymbolAsync(ISymbol symbol, CancellationToken cancellationToken = default) { var symbolId = DocumentationCommentId.CreateDeclarationId(symbol); // check to see if symbol is from current solution var project = _currentSolution.GetProject(symbol.ContainingAssembly, cancellationToken); if (project != null) { return(await GetSymbolAsync(_currentSolution, project.Id, symbolId, cancellationToken).ConfigureAwait(false)); } // check to see if it is from original solution project = _originalSolution.GetProject(symbol.ContainingAssembly, cancellationToken); if (project != null) { return(await GetSymbolAsync(_currentSolution, project.Id, symbolId, cancellationToken).ConfigureAwait(false)); } // try to find symbol from any project (from current solution) with matching assembly name foreach (var projectId in this.GetProjectsForAssembly(symbol.ContainingAssembly)) { var currentSymbol = await GetSymbolAsync(_currentSolution, projectId, symbolId, cancellationToken).ConfigureAwait(false); if (currentSymbol != null) { return(currentSymbol); } } return(null); }
public void DynamicParameterMethod() { string code = @" class C { int DoStuff(dynamic dynamic) => 0; }"; var comp = CreateCSharpCompilation(code); var symbol = comp.GetSymbolsWithName("DoStuff").Single(); var actualDocId = DocumentationCommentId.CreateDeclarationId(symbol); var expectedDocId = "M:C.DoStuff(System.Object)~System.Int32"; Assert.Equal(expectedDocId, actualDocId); var foundSymbols = DocumentationCommentId.GetSymbolsForDeclarationId( expectedDocId, comp ); Assert.Equal(new[] { symbol }, foundSymbols); }
public void TupleReturnMethod() { string code = @" class C { (int i, int) DoStuff() => default; }"; var comp = CreateCompilation(code); comp.VerifyDiagnostics(); var symbol = comp.GetSymbolsWithName("DoStuff").Single(); var actualDocId = DocumentationCommentId.CreateDeclarationId(symbol); string expectedDocId = "M:C.DoStuff~System.ValueTuple{System.Int32,System.Int32}"; Assert.Equal(expectedDocId, actualDocId); var foundSymbols = DocumentationCommentId.GetSymbolsForDeclarationId( expectedDocId, comp ); Assert.Equal(new[] { symbol }, foundSymbols); }
private static void CheckDeclarationIdExact <TSymbol>(string expectedId, Compilation compilation, Func <TSymbol, bool> test) where TSymbol : ISymbol { var symbol = CheckDeclarationId(expectedId, compilation, test); var id = DocumentationCommentId.CreateDeclarationId(symbol); Assert.Equal(expectedId, id); }
private static void CheckDeclarationId(string expectedId, INamespaceOrTypeSymbol symbol, Compilation compilation) { var id = DocumentationCommentId.CreateDeclarationId(symbol); Assert.Equal(expectedId, id); var sym = DocumentationCommentId.GetFirstSymbolForDeclarationId(id, compilation); Assert.Equal(symbol, sym); }
protected static bool ShouldFindReferencesInGlobalSuppressions(ISymbol symbol, [NotNullWhen(returnValue: true)] out string?documentationCommentId) { if (!SupportsGlobalSuppression(symbol)) { documentationCommentId = null; return(false); } documentationCommentId = DocumentationCommentId.CreateDeclarationId(symbol); return(documentationCommentId != null);
private static string CreateSuppressionTarget(ISymbol symbol) { var actualSymbol = symbol; if (actualSymbol is IMethodSymbol methodSymbol && methodSymbol.ReducedFrom != null) { actualSymbol = methodSymbol.ReducedFrom; } return(DocumentationCommentId.CreateDeclarationId(actualSymbol)); }
public void AnalyzeAssemblyOrModuleAttribute(SyntaxNode attributeSyntax, SemanticModel model, Action <Diagnostic> reportDiagnostic, CancellationToken cancellationToken) { if (!_state.IsSuppressMessageAttributeWithNamedArguments(attributeSyntax, model, cancellationToken, out var namedAttributeArguments)) { return; } if (!SuppressMessageAttributeState.HasValidScope(namedAttributeArguments, out var targetScope)) { reportDiagnostic(Diagnostic.Create(s_invalidScopeDescriptor, attributeSyntax.GetLocation())); return; } if (!_state.HasValidTarget(namedAttributeArguments, targetScope, out var targetHasDocCommentIdFormat, out var targetSymbolString, out var targetValueOperation, out var resolvedSymbols)) { reportDiagnostic(Diagnostic.Create(s_invalidOrMissingTargetDescriptor, attributeSyntax.GetLocation())); return; } // We want to flag valid target which uses legacy format to update to Roslyn based DocCommentId format. if (resolvedSymbols.Length > 0 && !targetHasDocCommentIdFormat) { RoslynDebug.Assert(!string.IsNullOrEmpty(targetSymbolString)); RoslynDebug.Assert(targetValueOperation != null); var properties = ImmutableDictionary <string, string?> .Empty; if (resolvedSymbols.Length == 1) { // We provide a code fix for the case where the target resolved to a single symbol. var docCommentId = DocumentationCommentId.CreateDeclarationId(resolvedSymbols[0]); if (!string.IsNullOrEmpty(docCommentId)) { // Suppression target has an optional "~" prefix to distinguish it from legacy FxCop suppressions. // IDE suppression code fixes emit this prefix, so we we also add this prefix to new suppression target string. properties = properties.Add(DocCommentIdKey, "~" + docCommentId); } } reportDiagnostic(Diagnostic.Create(LegacyFormatTargetDescriptor, targetValueOperation.Syntax.GetLocation(), properties !, targetSymbolString)); return; } }
public void TupleParameterMethod(string methodCode, string expectedDocId) { string code = $@" class C {{ {methodCode} }}"; var comp = CreateCompilation(code); comp.VerifyDiagnostics(); var symbol = comp.GetSymbolsWithName("DoStuff").Single(); var actualDocId = DocumentationCommentId.CreateDeclarationId(symbol); Assert.Equal(expectedDocId, actualDocId); var foundSymbols = DocumentationCommentId.GetSymbolsForDeclarationId(expectedDocId, comp); Assert.Equal(new[] { symbol }, foundSymbols); }
protected static string GetTargetString(ISymbol targetSymbol) => "~" + DocumentationCommentId.CreateDeclarationId(targetSymbol);
protected string GetTargetString(ISymbol targetSymbol) { return("~" + DocumentationCommentId.CreateDeclarationId(targetSymbol)); }