Skip to content

Commit cef81a3

Browse files
committed
Handle delayed function like macros better
1 parent d035c61 commit cef81a3

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

UnityShaderParser/HLSL/PreProcessor/HLSLPreProcessor.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,33 @@ private List<HLSLToken> ApplyMacros()
423423
// First, check if we have a functionlike macro
424424
if (macro.FunctionLike)
425425
{
426-
// Try to parase parameters. If they aren't there, it's just an identifier.
426+
// Try to parse parameters.
427427
if (!TryParseFunctionLikeMacroInvocationParameters(expanded, ref i, out var parameters))
428-
next.Add(token);
428+
{
429+
// If they aren't present, it might be a deferred function-like macro.
430+
// Eat more tokens to get the parameters and retry.
431+
if (Match(TokenKind.OpenParenToken))
432+
{
433+
expanded.Add(Eat(TokenKind.OpenParenToken));
434+
int numParens = 1;
435+
while (numParens > 0) // Might have nested parens
436+
{
437+
var nextTok = Advance();
438+
if (nextTok.Kind == TokenKind.OpenParenToken)
439+
numParens++;
440+
else if (nextTok.Kind == TokenKind.CloseParenToken)
441+
numParens--;
442+
expanded.Add(nextTok);
443+
}
444+
if (!TryParseFunctionLikeMacroInvocationParameters(expanded, ref i, out parameters))
445+
next.Add(token); // Still no luck, must be a regular identifier.
446+
}
447+
// Otherwise, must be a regular identifier.
448+
else
449+
{
450+
next.Add(token);
451+
}
452+
}
429453

430454
if (parameters.Count != macro.Parameters.Count)
431455
Error(DiagnosticFlags.PreProcessorError, $"Incorrect number of arguments passed to macro '{macro.Name}', expected {macro.Parameters.Count}, got {parameters.Count}.");

0 commit comments

Comments
 (0)