Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4860,13 +4860,43 @@ public PreprocessingSymbolInfo GetPreprocessingSymbolInfo(IdentifierNameSyntax n
{
CheckSyntaxNode(node);

if (node.Ancestors().Any(n => SyntaxFacts.IsPreprocessorDirective(n.Kind())))
if (isPossiblePreprocessingSymbolReference(node))
{
bool isDefined = this.SyntaxTree.IsPreprocessorSymbolDefined(node.Identifier.ValueText, node.Identifier.SpanStart);
return new PreprocessingSymbolInfo(new Symbols.PublicModel.PreprocessingSymbol(node.Identifier.ValueText), isDefined);
}

return PreprocessingSymbolInfo.None;

bool isPossiblePreprocessingSymbolReference(IdentifierNameSyntax node)
{
var parentNode = node.Parent;
while (parentNode is not null)
{
var kind = parentNode.Kind();
switch (kind)
{
case SyntaxKind.IfDirectiveTrivia:
{
var parentIf = (IfDirectiveTriviaSyntax)parentNode;
return parentIf.Condition.FullSpan.Contains(node.FullSpan);
}
case SyntaxKind.ElifDirectiveTrivia:
{
var parentElif = (ElifDirectiveTriviaSyntax)parentNode;
return parentElif.Condition.FullSpan.Contains(node.FullSpan);
}
}

if (SyntaxFacts.IsPreprocessorDirective(kind))
{
return false;
}

parentNode = parentNode.Parent;
}
return false;
Comment on lines +4873 to +4898
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var parentNode = node.Parent;
while (parentNode is not null)
{
var kind = parentNode.Kind();
switch (kind)
{
case SyntaxKind.IfDirectiveTrivia:
{
var parentIf = (IfDirectiveTriviaSyntax)parentNode;
return parentIf.Condition.FullSpan.Contains(node.FullSpan);
}
case SyntaxKind.ElifDirectiveTrivia:
{
var parentElif = (ElifDirectiveTriviaSyntax)parentNode;
return parentElif.Condition.FullSpan.Contains(node.FullSpan);
}
}
if (SyntaxFacts.IsPreprocessorDirective(kind))
{
return false;
}
parentNode = parentNode.Parent;
}
return false;
for (var parentNode = node.Parent; parentNode != null; parentNode = parentNode.Parent)
{
var kind = parentNode.Kind();
switch (kind)
{
case SyntaxKind.IfDirectiveTrivia:
{
var parentIf = (IfDirectiveTriviaSyntax)parentNode;
return parentIf.Condition.FullSpan.Contains(node.FullSpan);
}
case SyntaxKind.ElifDirectiveTrivia:
{
var parentElif = (ElifDirectiveTriviaSyntax)parentNode;
return parentElif.Condition.FullSpan.Contains(node.FullSpan);
}
}
if (SyntaxFacts.IsPreprocessorDirective(kind))
{
return false;
}
}
return false;

}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5027,6 +5027,17 @@ public void GetPreprocessingSymbolInfoForSymbolDefinedLaterInSource()
Assert.False(symbolInfo.IsDefined, "must not be defined");
}

[Fact, WorkItem(72907, "https://github.com/dotnet/roslyn/issues/72907")]
public void GetPreprocessingSymbolInfoOnUnrelatedDirective()
Copy link
Contributor

@AlekseyTs AlekseyTs Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetPreprocessingSymbolInfoOnUnrelatedDirective

It looks like no tests were added for positive scenarios #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other tests already cover the positive scenarios, that's why this test didn't focus on them. I will add the assertion for #define which we expect to return a non-null symbol

{
const string sourceCode = """
#pragma warning disable Z //bind pragma warning disable
""";

var symbolInfo = GetPreprocessingSymbolInfoForTest(sourceCode, "Z //bind pragma warning disable");
Assert.Null(symbolInfo.Symbol);
}

[Fact, WorkItem(720566, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/720566")]
public void Bug720566_01()
{
Expand Down
Loading