Skip to content

Commit

Permalink
[#190][Preprocessor] Undefined identifiers are set to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
dmed256 committed Aug 9, 2018
1 parent 8646ac0 commit 5bf34fa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/lang/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,16 @@ namespace occa {
getExpandedLineTokens(lineTokens);
removeNewline(lineTokens);

const int tokenCount = (int) lineTokens.size();
for (int i = 0; i < tokenCount; ++i) {
token_t *token = lineTokens[i];
if (!(token->type() & tokenType::identifier)) {
continue;
}
lineTokens[i] = new primitiveToken(token->origin, 0, "0");
delete token;
}

exprNode *expr = getExpression(lineTokens);

// Errors when expr is NULL are handled
Expand Down
14 changes: 14 additions & 0 deletions tests/src/lang/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void testMacroDefines();
void testCppStandardTests();
void testIfElse();
void testIfElseDefines();
void testIfWithUndefines();
void testErrorDefines();
void testSpecialMacros();
void testInclude();
Expand Down Expand Up @@ -96,6 +97,7 @@ int main(const int argc, const char **argv) {
testCppStandardTests();
testIfElse();
testIfElseDefines();
testIfWithUndefines();
testErrorDefines();
testSpecialMacros();
testInclude();
Expand Down Expand Up @@ -485,6 +487,18 @@ void testIfElseDefines () {
}
}

void testIfWithUndefines() {
setStream(
"#if foo == 0\n"
" 1\n"
"#else\n"
" 0\n"
"#endif"
);
ASSERT_EQ(1,
(int) nextTokenPrimitiveValue());
}

void testErrorDefines() {
std::cerr << "Testing error and warning directives\n";
setStream(
Expand Down

0 comments on commit 5bf34fa

Please sign in to comment.