Skip to content

Commit

Permalink
PP: Don't report certain error about '#' when #if'd out
Browse files Browse the repository at this point in the history
Don't report the following error when scanning inactive code (e.g. code inside #if 0):
"error: '#' : (#) can be preceded in its line only by spaces or horizontal tab"
Adds a variable to PpContext to say whether we're currently skipping over an inactive #if/#ifdef/#elif/#else, and don't report the error inside scanToken if true.

fixes 3704
  • Loading branch information
rj123-nv authored and arcady-lunarg committed Sep 2, 2024
1 parent 45f2b11 commit 9cd7ca2
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 4 deletions.
Empty file.
29 changes: 29 additions & 0 deletions Test/baseResults/preprocess.inactive_stringify.vert.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#version 460























void main()
{
gl_Position = vec4(1.0);
}

28 changes: 28 additions & 0 deletions Test/preprocess.inactive_stringify.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#version 460

// This tests that the preprocessor error
// "error: '#' : (#) can be preceded in its line only by spaces or horizontal tab"
// isn't enforced when inactive (e.g. inside #if 0)

#if 0
#define STRINGIFY(X) #X
#endif

#define C 0

#if 1
#ifdef A
#elif defined B
#elif C
// OK, since preprocessor evaluates to inactive
#define STRINGIFY(X) #X
#endif
#endif

// OK in comments
// #define STRINGIFY(X) #X

void main()
{
gl_Position = vec4(1.0);
}
6 changes: 4 additions & 2 deletions glslang/MachineIndependent/preprocessor/Pp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ int TPpContext::CPPundef(TPpToken* ppToken)
*/
int TPpContext::CPPelse(int matchelse, TPpToken* ppToken)
{
inElseSkip = true;
int depth = 0;
int token = scanToken(ppToken);

Expand Down Expand Up @@ -297,7 +298,7 @@ int TPpContext::CPPelse(int matchelse, TPpToken* ppToken)
elseSeen[elsetracker] = false;
--elsetracker;
}

inElseSkip = false;
return CPPif(ppToken);
}
} else if (nextAtom == PpAtomElse) {
Expand All @@ -311,7 +312,8 @@ int TPpContext::CPPelse(int matchelse, TPpToken* ppToken)
parseContext.ppError(ppToken->loc, "#elif after #else", "#elif", "");
}
}


inElseSkip = false;
return token;
}

Expand Down
3 changes: 2 additions & 1 deletion glslang/MachineIndependent/preprocessor/PpContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ TPpContext::TPpContext(TParseContextBase& pc, const std::string& rootFileName, T
preamble(nullptr), strings(nullptr), previous_token('\n'), parseContext(pc), includer(inclr), inComment(false),
rootFileName(rootFileName),
currentSourceFile(rootFileName),
disableEscapeSequences(false)
disableEscapeSequences(false),
inElseSkip(false)
{
ifdepth = 0;
for (elsetracker = 0; elsetracker < maxIfNesting; elsetracker++)
Expand Down
5 changes: 4 additions & 1 deletion glslang/MachineIndependent/preprocessor/PpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ class TPpContext {
break;
popInput();
}
if (!inputStack.empty() && inputStack.back()->isStringInput()) {
if (!inputStack.empty() && inputStack.back()->isStringInput() && !inElseSkip) {
if (token == '\n') {
bool seenNumSign = false;
for (int i = 0; i < (int)lastLineTokens.size() - 1;) {
Expand Down Expand Up @@ -732,6 +732,9 @@ class TPpContext {

std::istringstream strtodStream;
bool disableEscapeSequences;
// True if we're skipping a section enclosed by #if/#ifdef/#elif/#else which was evaluated to
// be inactive, e.g. #if 0
bool inElseSkip;
};

} // end namespace glslang
Expand Down
1 change: 1 addition & 0 deletions gtests/Pp.FromFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ INSTANTIATE_TEST_SUITE_P(
"preprocessor.eof_missing.vert",
"preprocess.arb_shading_language_include.vert",
"preprocess.include_directive_missing_extension.vert",
"preprocess.inactive_stringify.vert"
})),
FileNameAsCustomTestSuffix
);
Expand Down

0 comments on commit 9cd7ca2

Please sign in to comment.