diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2018-09-19 08:32:28 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-09-19 08:32:28 -0700 |
| commit | a37b3539d94c434c5d74ab524eae2988e48e0756 (patch) | |
| tree | 248759eca216d5a47c2b8a5c7f54ec93fe46b3b1 | |
| parent | 091f89aaf379d93a40a718a92a27e6c5ef2cbb23 (diff) | |
Warn when undefined identifier used in preprocessor conditional (#642)
This can mask an error when the user either typos a macro name when writing a conditional, or (as was the case for the user who pointed out this issue) they mistakenly assume that a `#define` in an `import`ed file has been made visible to them.
This change just adds the warning in the obvious place, with a test code to ensure it triggers.
| -rw-r--r-- | source/slang/diagnostic-defs.h | 1 | ||||
| -rw-r--r-- | source/slang/preprocessor.cpp | 1 | ||||
| -rw-r--r-- | tests/diagnostics/undefined-in-preprocessor-conditional.slang | 10 | ||||
| -rw-r--r-- | tests/diagnostics/undefined-in-preprocessor-conditional.slang.expected | 6 |
4 files changed, 18 insertions, 0 deletions
diff --git a/source/slang/diagnostic-defs.h b/source/slang/diagnostic-defs.h index 83e82fc81..02edb6521 100644 --- a/source/slang/diagnostic-defs.h +++ b/source/slang/diagnostic-defs.h @@ -114,6 +114,7 @@ DIAGNOSTIC(15201, Error, syntaxErrorInPreprocessorExpression, "syntax error in p DIAGNOSTIC(15202, Error, divideByZeroInPreprocessorExpression, "division by zero in preprocessor expression"); DIAGNOSTIC(15203, Error, expectedTokenInDefinedExpression, "expected '$0' in 'defined' expression"); DIAGNOSTIC(15204, Warning, directiveExpectsExpression, "'$0' directive requires an expression"); +DIAGNOSTIC(15205, Warning, undefinedIdentifierInPreprocessorExpression, "undefined idenfier '$0' in preprocessor expression will evaluate to zero") DIAGNOSTIC(-1, Note, seeOpeningToken, "see opening '$0'") diff --git a/source/slang/preprocessor.cpp b/source/slang/preprocessor.cpp index 71c6fd4bb..d652d07bb 100644 --- a/source/slang/preprocessor.cpp +++ b/source/slang/preprocessor.cpp @@ -1213,6 +1213,7 @@ static PreprocessorExpressionValue ParseAndEvaluateUnaryExpression(PreprocessorD // An identifier here means it was not defined as a macro (or // it is defined, but as a function-like macro. These should // just evaluate to zero (possibly with a warning) + GetSink(context)->diagnose(token.loc, Diagnostics::undefinedIdentifierInPreprocessorExpression, token.getName()); return 0; } diff --git a/tests/diagnostics/undefined-in-preprocessor-conditional.slang b/tests/diagnostics/undefined-in-preprocessor-conditional.slang new file mode 100644 index 000000000..d46c68d33 --- /dev/null +++ b/tests/diagnostics/undefined-in-preprocessor-conditional.slang @@ -0,0 +1,10 @@ +//TEST(smoke):SIMPLE: + +// Use an undefined identifier in a preprocessor conditional + +#define FOO 1 +#define BORT 1 + +#if FOO && BART +#error Should not get here +#endif diff --git a/tests/diagnostics/undefined-in-preprocessor-conditional.slang.expected b/tests/diagnostics/undefined-in-preprocessor-conditional.slang.expected new file mode 100644 index 000000000..ac8be68a2 --- /dev/null +++ b/tests/diagnostics/undefined-in-preprocessor-conditional.slang.expected @@ -0,0 +1,6 @@ +result code = 0 +standard error = { +tests/diagnostics/undefined-in-preprocessor-conditional.slang(8): warning 15205: undefined idenfier 'BART' in preprocessor expression will evaluate to zero +} +standard output = { +} |
