Skip to content

Commit 4ee9409

Browse files
Implement find all references to preprocessing symbols (#66425)
2 parents f8d72fd + d12a2e1 commit 4ee9409

File tree

47 files changed

+2898
-1907
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2898
-1907
lines changed

src/Compilers/CSharp/Portable/Compilation/CSharpCompilation.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,11 @@ protected override INamespaceSymbol CommonCreateErrorNamespaceSymbol(INamespaceS
334334
name).GetPublicSymbol();
335335
}
336336

337+
protected override IPreprocessingSymbol CommonCreatePreprocessingSymbol(string name)
338+
{
339+
return new Symbols.PublicModel.PreprocessingSymbol(name);
340+
}
341+
337342
#region Constructors and Factories
338343

339344
private static readonly CSharpCompilationOptions s_defaultOptions = new CSharpCompilationOptions(OutputKind.ConsoleApplication);

src/Compilers/CSharp/Portable/SymbolDisplay/SymbolDisplayVisitor.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,13 @@ public override void VisitAlias(IAliasSymbol symbol)
341341
}
342342
}
343343

344+
internal override void VisitPreprocessing(IPreprocessingSymbol symbol)
345+
{
346+
// Currently using 'Text' part kind as there is no kind specific to preprocessing symbols.
347+
var part = new SymbolDisplayPart(SymbolDisplayPartKind.Text, symbol, symbol.Name);
348+
Builder.Add(part);
349+
}
350+
344351
protected override void AddSpace()
345352
{
346353
Builder.Add(CreatePart(SymbolDisplayPartKind.Space, null, " "));

src/Compilers/CSharp/Portable/Symbols/PublicModel/PreprocessingSymbol.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ bool ISymbol.Equals(ISymbol? other, CodeAnalysis.SymbolEqualityComparer equality
6565

6666
Accessibility ISymbol.DeclaredAccessibility => Accessibility.NotApplicable;
6767

68-
void ISymbol.Accept(SymbolVisitor visitor) => throw new System.NotSupportedException();
68+
void ISymbol.Accept(SymbolVisitor visitor) => visitor.VisitPreprocessing(this);
6969

70-
TResult ISymbol.Accept<TResult>(SymbolVisitor<TResult> visitor) => throw new System.NotSupportedException();
70+
TResult ISymbol.Accept<TResult>(SymbolVisitor<TResult> visitor) => visitor.VisitPreprocessing(this)!;
7171

72-
TResult ISymbol.Accept<TArgument, TResult>(SymbolVisitor<TArgument, TResult> visitor, TArgument argument) => throw new System.NotSupportedException();
72+
TResult ISymbol.Accept<TArgument, TResult>(SymbolVisitor<TArgument, TResult> visitor, TArgument argument) => visitor.VisitPreprocessing(this, argument);
7373

7474
string? ISymbol.GetDocumentationCommentId() => null;
7575

src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4745,6 +4745,24 @@ class C(out Type[M2(out object y)])
47454745
Assert.Null(model.GetAliasInfo(identifier));
47464746
}
47474747

4748+
[Fact]
4749+
public void CommonPreprocessingSymbolProperties()
4750+
{
4751+
var text = """
4752+
#if NET5_0_OR_GREATER
4753+
#endif
4754+
""";
4755+
var compilation = CreateCompilation(text);
4756+
4757+
var tree = compilation.SyntaxTrees.Single();
4758+
var identifier = tree.GetRoot().DescendantNodes(descendIntoTrivia: true).OfType<IdentifierNameSyntax>().First();
4759+
var model = compilation.GetSemanticModel(tree);
4760+
var preprocessingSymbol = model.GetPreprocessingSymbolInfo(identifier).Symbol;
4761+
Assert.NotNull(preprocessingSymbol);
4762+
Assert.Equal("NET5_0_OR_GREATER", preprocessingSymbol.Name);
4763+
Assert.True(preprocessingSymbol.CanBeReferencedByName);
4764+
}
4765+
47484766
#region "regression helper"
47494767
private void Regression(string text)
47504768
{

src/Compilers/CSharp/Test/Symbol/SymbolDisplay/SymbolDisplayTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8837,5 +8837,37 @@ void M()
88378837
SymbolDisplayPartKind.ParameterName,
88388838
SymbolDisplayPartKind.Punctuation);
88398839
}
8840+
8841+
[Fact, WorkItem(66009, "https://github.com/dotnet/roslyn/issues/66009")]
8842+
public void PreprocessingSymbol()
8843+
{
8844+
var source = """
8845+
#if NET5_0_OR_GREATER
8846+
#endif
8847+
""";
8848+
8849+
var comp = CreateCompilation(source);
8850+
var tree = comp.SyntaxTrees.First();
8851+
var model = comp.GetSemanticModel(tree);
8852+
var preprocessingNameSyntax = tree.GetRoot().DescendantNodes(descendIntoTrivia: true)
8853+
.OfType<IdentifierNameSyntax>().First();
8854+
var preprocessingSymbolInfo = model.GetPreprocessingSymbolInfo(preprocessingNameSyntax);
8855+
var preprocessingSymbol = preprocessingSymbolInfo.Symbol;
8856+
8857+
var format = new SymbolDisplayFormat(
8858+
memberOptions: SymbolDisplayMemberOptions.IncludeParameters,
8859+
parameterOptions: SymbolDisplayParameterOptions.IncludeType |
8860+
SymbolDisplayParameterOptions.IncludeName |
8861+
SymbolDisplayParameterOptions.IncludeDefaultValue);
8862+
8863+
Assert.Equal("NET5_0_OR_GREATER", preprocessingSymbol.ToDisplayString(format));
8864+
8865+
var displayParts = preprocessingSymbol.ToDisplayParts(format);
8866+
AssertEx.Equal(
8867+
expected: [
8868+
new SymbolDisplayPart(SymbolDisplayPartKind.Text, preprocessingSymbol, "NET5_0_OR_GREATER")
8869+
],
8870+
actual: displayParts);
8871+
}
88408872
}
88418873
}

src/Compilers/Core/Portable/Compilation/Compilation.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,14 @@ public INamespaceSymbol CreateErrorNamespaceSymbol(INamespaceSymbol container, s
379379

380380
protected abstract INamespaceSymbol CommonCreateErrorNamespaceSymbol(INamespaceSymbol container, string name);
381381

382+
/// <summary>
383+
/// Returns a new IPreprocessingSymbol representing a preprocessing symbol with the given name.
384+
/// </summary>
385+
public IPreprocessingSymbol CreatePreprocessingSymbol(string name)
386+
=> CommonCreatePreprocessingSymbol(name ?? throw new ArgumentNullException(nameof(name)));
387+
388+
protected abstract IPreprocessingSymbol CommonCreatePreprocessingSymbol(string name);
389+
382390
#region Name
383391

384392
internal const string UnspecifiedModuleAssemblyName = "?";

src/Compilers/Core/Portable/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Microsoft.CodeAnalysis.ITypeParameterSymbol.AllowsRefLikeType.get -> bool
1919
Microsoft.CodeAnalysis.RuntimeCapability.ByRefLikeGenerics = 8 -> Microsoft.CodeAnalysis.RuntimeCapability
2020
static Microsoft.CodeAnalysis.GeneratorExtensions.AsIncrementalGenerator(this Microsoft.CodeAnalysis.ISourceGenerator! sourceGenerator) -> Microsoft.CodeAnalysis.IIncrementalGenerator!
2121
static Microsoft.CodeAnalysis.GeneratorExtensions.GetGeneratorType(this Microsoft.CodeAnalysis.IIncrementalGenerator! generator) -> System.Type!
22+
Microsoft.CodeAnalysis.Compilation.CreatePreprocessingSymbol(string! name) -> Microsoft.CodeAnalysis.IPreprocessingSymbol!
2223
[RSEXPERIMENTAL004]Microsoft.CodeAnalysis.HostOutputProductionContext
2324
[RSEXPERIMENTAL004]Microsoft.CodeAnalysis.HostOutputProductionContext.AddOutput(string! name, object! value) -> void
2425
[RSEXPERIMENTAL004]Microsoft.CodeAnalysis.HostOutputProductionContext.CancellationToken.get -> System.Threading.CancellationToken

src/Compilers/Core/Portable/Symbols/SymbolVisitor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,10 @@ public virtual void VisitTypeParameter(ITypeParameterSymbol symbol)
109109
{
110110
DefaultVisit(symbol);
111111
}
112+
113+
internal virtual void VisitPreprocessing(IPreprocessingSymbol symbol)
114+
{
115+
DefaultVisit(symbol);
116+
}
112117
}
113118
}

src/Compilers/Core/Portable/Symbols/SymbolVisitor`1.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,10 @@ public abstract class SymbolVisitor<TResult>
112112
{
113113
return DefaultVisit(symbol);
114114
}
115+
116+
internal virtual TResult? VisitPreprocessing(IPreprocessingSymbol symbol)
117+
{
118+
return DefaultVisit(symbol);
119+
}
115120
}
116121
}

src/Compilers/Core/Portable/Symbols/SymbolVisitor`2.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,10 @@ public virtual TResult VisitTypeParameter(ITypeParameterSymbol symbol, TArgument
117117
{
118118
return DefaultVisit(symbol, argument);
119119
}
120+
121+
internal virtual TResult VisitPreprocessing(IPreprocessingSymbol symbol, TArgument argument)
122+
{
123+
return DefaultVisit(symbol, argument);
124+
}
120125
}
121126
}

0 commit comments

Comments
 (0)