Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line directive has optional filename argument. #266

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 14 additions & 32 deletions src/ShaderTools.CodeAnalysis.Hlsl.Tests/Parser/PreprocessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -874,13 +874,19 @@ public void TestIfTrueSingleLineCommentAndDefine()
public void TestLine()
{
const string text = @"
#line 190
#line 3 ""a\path\to.hlsl""
";
#line 8

#line 30";
var node = Parse(text);

TestRoundTripping(node, text);
VerifyDirectivesSpecial(node,
new DirectiveInfo { Kind = SyntaxKind.LineDirectiveTrivia, Status = NodeStatus.IsActive, Number = 3, Text = @"a\path\to.hlsl" });
new DirectiveInfo { Kind = SyntaxKind.LineDirectiveTrivia, Status = NodeStatus.IsActive, Number = 190 },
new DirectiveInfo { Kind = SyntaxKind.LineDirectiveTrivia, Status = NodeStatus.IsActive, Number = 3, Text = @"a\path\to.hlsl" },
new DirectiveInfo { Kind = SyntaxKind.LineDirectiveTrivia, Status = NodeStatus.IsActive, Number = 8 },
new DirectiveInfo { Kind = SyntaxKind.LineDirectiveTrivia, Status = NodeStatus.IsActive, Number = 30 });
}

[Fact]
Expand Down Expand Up @@ -1305,36 +1311,12 @@ private void VerifyDirectivesSpecial(SyntaxNode node, params DirectiveInfo[] exp
case SyntaxKind.LineDirectiveTrivia:
var ld = dt as LineDirectiveTriviaSyntax;

// default number = 0 - no number
if (exp.Number == -1)
{
Assert.Equal(SyntaxKind.LineKeyword, ld.LineKeyword.Kind);
Assert.Equal(SyntaxKind.DefaultKeyword, ld.Line.Kind);
}
else if (exp.Number == -2)
{
Assert.Equal(SyntaxKind.LineKeyword, ld.LineKeyword.Kind);
//Assert.Equal(SyntaxKind.HiddenKeyword, ld.Line.Kind);
}
else if (exp.Number == 0)
{
Assert.Equal(String.Empty, ld.Line.Text);
}
else if (exp.Number > 0)
{
Assert.Equal(exp.Number, ld.Line.Value); // Number
Assert.Equal(exp.Number, Int32.Parse(ld.Line.Text));
}

if (null == exp.Text)
{
Assert.Equal(SyntaxKind.None, ld.File.Kind);
}
else
{
Assert.NotEqual(SyntaxKind.None, ld.File.Kind);
Assert.Equal(exp.Text, ld.File.Value);
}
Assert.True(exp.Number >= 0);
Assert.Equal(exp.Number, ld.Line.Value); // Number
Assert.Equal(exp.Number, Int32.Parse(ld.Line.Text));

Assert.NotEqual(SyntaxKind.None, ld.File.Kind);
Assert.Equal(exp.Text, ld.File.Value);

break;
} // switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public enum DiagnosticId
AlreadyDefined,
BadDirectivePlacement,
EndOfPreprocessorLineExpected,
MissingPreprocessorFile,
IncludeNotFound,
IncludeUnexpectedError,
NotEnoughMacroParameters,
Expand Down
54 changes: 25 additions & 29 deletions src/ShaderTools.CodeAnalysis.Hlsl/Parser/DirectiveParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ private IncludeDirectiveTriviaSyntax ParseIncludeDirective(SyntaxToken hash, Syn
private LineDirectiveTriviaSyntax ParseLineDirective(SyntaxToken hash, SyntaxToken keyword, bool isActive)
{
var line = Match(SyntaxKind.IntegerLiteralToken);
var filename = Match(SyntaxKind.StringLiteralToken);
var filename = Current.Kind != SyntaxKind.EndOfDirectiveToken ? Match(SyntaxKind.StringLiteralToken) : new SyntaxToken(SyntaxKind.EndOfDirectiveToken, true,
GetDiagnosticSourceRangeForMissingToken(),
GetDiagnosticTextSpanForMissingToken());
var eod = ParseEndOfDirective(line.IsMissing || !isActive);

return new LineDirectiveTriviaSyntax(hash, keyword, line, filename, eod, isActive);
Expand Down Expand Up @@ -437,7 +439,7 @@ private ExpressionSyntax ParseDirectiveParenthesizedExpression()
return new ParenthesizedExpressionSyntax(openParen, expression, closeParen);
}

private SyntaxToken ParseEndOfDirective(bool ignoreErrors, bool afterLineNumber = false)
private SyntaxToken ParseEndOfDirective(bool ignoreErrors)
{
var skippedTokens = new List<SyntaxToken>();

Expand All @@ -448,13 +450,7 @@ private SyntaxToken ParseEndOfDirective(bool ignoreErrors, bool afterLineNumber
skippedTokens = new List<SyntaxToken>(10);

if (!ignoreErrors)
{
var errorCode = DiagnosticId.EndOfPreprocessorLineExpected;
if (afterLineNumber)
errorCode = DiagnosticId.MissingPreprocessorFile;

skippedTokens.Add(WithDiagnostic(NextToken().WithoutDiagnostics(), errorCode));
}
skippedTokens.Add(WithDiagnostic(NextToken().WithoutDiagnostics(), DiagnosticId.EndOfPreprocessorLineExpected));

while (Current.Kind != SyntaxKind.EndOfDirectiveToken &&
Current.Kind != SyntaxKind.EndOfFileToken)
Expand Down Expand Up @@ -492,9 +488,9 @@ private bool EvaluateBool(ExpressionSyntax expr)
{
var result = Evaluate(expr);
if (result is bool)
return (bool)result;
return (bool) result;
if (result is int)
return (int)result != 0;
return (int) result != 0;

return false;
}
Expand All @@ -503,7 +499,7 @@ private int EvaluateInt(ExpressionSyntax expr)
{
var result = Evaluate(expr);
if (result is int)
return (int)result;
return (int) result;

return 0;
}
Expand All @@ -516,49 +512,49 @@ private object Evaluate(ExpressionSyntax expr)
switch (expr.Kind)
{
case SyntaxKind.ParenthesizedExpression:
return Evaluate(((ParenthesizedExpressionSyntax)expr).Expression);
return Evaluate(((ParenthesizedExpressionSyntax) expr).Expression);
case SyntaxKind.TrueLiteralExpression:
case SyntaxKind.FalseLiteralExpression:
case SyntaxKind.NumericLiteralExpression:
return ((LiteralExpressionSyntax)expr).Token.Value;
return ((LiteralExpressionSyntax) expr).Token.Value;
case SyntaxKind.LogicalAndExpression:
case SyntaxKind.BitwiseAndExpression:
return EvaluateBool(((BinaryExpressionSyntax)expr).Left) && EvaluateBool(((BinaryExpressionSyntax)expr).Right);
return EvaluateBool(((BinaryExpressionSyntax) expr).Left) && EvaluateBool(((BinaryExpressionSyntax) expr).Right);
case SyntaxKind.LogicalOrExpression:
case SyntaxKind.BitwiseOrExpression:
return EvaluateBool(((BinaryExpressionSyntax)expr).Left) || EvaluateBool(((BinaryExpressionSyntax)expr).Right);
return EvaluateBool(((BinaryExpressionSyntax) expr).Left) || EvaluateBool(((BinaryExpressionSyntax) expr).Right);
case SyntaxKind.EqualsExpression:
return Equals(Evaluate(((BinaryExpressionSyntax)expr).Left), Evaluate(((BinaryExpressionSyntax)expr).Right));
return Equals(Evaluate(((BinaryExpressionSyntax) expr).Left), Evaluate(((BinaryExpressionSyntax) expr).Right));
case SyntaxKind.NotEqualsExpression:
return !Equals(Evaluate(((BinaryExpressionSyntax)expr).Left), Evaluate(((BinaryExpressionSyntax)expr).Right));
return !Equals(Evaluate(((BinaryExpressionSyntax) expr).Left), Evaluate(((BinaryExpressionSyntax) expr).Right));
case SyntaxKind.LogicalNotExpression:
return !EvaluateBool(((PrefixUnaryExpressionSyntax)expr).Operand);
return !EvaluateBool(((PrefixUnaryExpressionSyntax) expr).Operand);
case SyntaxKind.AddExpression:
return EvaluateInt(((BinaryExpressionSyntax)expr).Left) + EvaluateInt(((BinaryExpressionSyntax)expr).Right);
return EvaluateInt(((BinaryExpressionSyntax) expr).Left) + EvaluateInt(((BinaryExpressionSyntax) expr).Right);
case SyntaxKind.SubtractExpression:
return EvaluateInt(((BinaryExpressionSyntax)expr).Left) - EvaluateInt(((BinaryExpressionSyntax)expr).Right);
return EvaluateInt(((BinaryExpressionSyntax) expr).Left) - EvaluateInt(((BinaryExpressionSyntax) expr).Right);
case SyntaxKind.MultiplyExpression:
return EvaluateInt(((BinaryExpressionSyntax)expr).Left) * EvaluateInt(((BinaryExpressionSyntax)expr).Right);
return EvaluateInt(((BinaryExpressionSyntax) expr).Left) * EvaluateInt(((BinaryExpressionSyntax) expr).Right);
case SyntaxKind.DivideExpression:
var divisor = EvaluateInt(((BinaryExpressionSyntax) expr).Right);
return (divisor != 0)
? EvaluateInt(((BinaryExpressionSyntax) expr).Left) / divisor
: int.MaxValue;
case SyntaxKind.GreaterThanExpression:
return EvaluateInt(((BinaryExpressionSyntax)expr).Left) > EvaluateInt(((BinaryExpressionSyntax)expr).Right);
return EvaluateInt(((BinaryExpressionSyntax) expr).Left) > EvaluateInt(((BinaryExpressionSyntax) expr).Right);
case SyntaxKind.GreaterThanOrEqualExpression:
return EvaluateInt(((BinaryExpressionSyntax)expr).Left) >= EvaluateInt(((BinaryExpressionSyntax)expr).Right);
return EvaluateInt(((BinaryExpressionSyntax) expr).Left) >= EvaluateInt(((BinaryExpressionSyntax) expr).Right);
case SyntaxKind.LessThanExpression:
return EvaluateInt(((BinaryExpressionSyntax)expr).Left) < EvaluateInt(((BinaryExpressionSyntax)expr).Right);
return EvaluateInt(((BinaryExpressionSyntax) expr).Left) < EvaluateInt(((BinaryExpressionSyntax) expr).Right);
case SyntaxKind.LessThanOrEqualExpression:
return EvaluateInt(((BinaryExpressionSyntax)expr).Left) <= EvaluateInt(((BinaryExpressionSyntax)expr).Right);
return EvaluateInt(((BinaryExpressionSyntax) expr).Left) <= EvaluateInt(((BinaryExpressionSyntax) expr).Right);
case SyntaxKind.IdentifierName:
var id = ((IdentifierNameSyntax)expr).Name.Text;
var id = ((IdentifierNameSyntax) expr).Name.Text;
return IsDirectiveDefined(id);
case SyntaxKind.FunctionInvocationExpression:
// It must be a call to "defined" - that's the only one allowed by the parser.
var functionCall = (FunctionInvocationExpressionSyntax)expr;
var identifierName = ((IdentifierNameSyntax)functionCall.ArgumentList.Arguments[0]).Name;
var functionCall = (FunctionInvocationExpressionSyntax) expr;
var identifierName = ((IdentifierNameSyntax) functionCall.ArgumentList.Arguments[0]).Name;
return IsDirectiveDefined(identifierName.Text);
default:
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private SyntaxToken LexDirectiveToken()
{
_kind = SyntaxKind.BadToken;
_contextualKind = SyntaxKind.BadToken;

_value = null;
_diagnostics.Clear();
_start = _charReader.Position;

Expand Down Expand Up @@ -82,7 +82,7 @@ private SyntaxToken LexDirectiveToken()

LexDirectiveTrailingTrivia(trailingTrivia, kind, isEndOfLine);

var token = new SyntaxToken(kind, _contextualKind, false, MakeAbsolute(span), fileSpan, text, _value,
var token = new SyntaxToken(kind, _contextualKind, false, MakeAbsolute(span), fileSpan, text, _value,
ImmutableArray<SyntaxNode>.Empty, trailingTrivia.ToImmutableArray(),
diagnostics, null, false);

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions src/ShaderTools.CodeAnalysis.Hlsl/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,6 @@
<data name="MethodOverloadResolutionFailure" xml:space="preserve">
<value>No overload for method '{0}' takes {1} arguments.</value>
</data>
<data name="MissingPreprocessorFile" xml:space="preserve">
<value>Quoted file name, single-line comment or end-of-line expected.</value>
</data>
<data name="NotEnoughMacroParameters" xml:space="preserve">
<value>Not enough actual parameters for macro '{0}'.</value>
</data>
Expand Down