Skip to content

Commit

Permalink
expandHashHash(): Allow combining with = also in cases which yield a …
Browse files Browse the repository at this point in the history
…comparison rather than an assignment operator
  • Loading branch information
datadiode committed Sep 28, 2024
1 parent cec0202 commit 38f26e2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2178,7 +2178,7 @@ namespace simplecpp {
if (!sameline(tok, tok->next) || !sameline(tok, tok->next->next))
throw invalidHashHash::unexpectedNewline(tok->location, name());

const bool canBeConcatenatedWithEqual = A->isOneOf("+-*/%&|^") || A->str() == "<<" || A->str() == ">>";
const bool canBeConcatenatedWithEqual = A->isOneOf("+-*/%&|^=!<>") || A->str() == "<<" || A->str() == ">>";
const bool canBeConcatenatedStringOrChar = isStringLiteral_(A->str()) || isCharLiteral_(A->str());
if (!A->name && !A->number && A->op != ',' && !A->str().empty() && !canBeConcatenatedWithEqual && !canBeConcatenatedStringOrChar)
throw invalidHashHash::unexpectedToken(tok->location, name(), A);
Expand Down
16 changes: 12 additions & 4 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,10 @@ static void hashhash9()
"ADD_OPERATOR(&);\n"
"ADD_OPERATOR(|);\n"
"ADD_OPERATOR(^);\n"
"ADD_OPERATOR(=);\n"
"ADD_OPERATOR(!);\n"
"ADD_OPERATOR(<);\n"
"ADD_OPERATOR(>);\n"
"ADD_OPERATOR(<<);\n"
"ADD_OPERATOR(>>);\n";
const char expected[] = "\n"
Expand All @@ -1166,6 +1170,10 @@ static void hashhash9()
"void operator &= ( void ) { x = x & 1 ; } ;\n"
"void operator |= ( void ) { x = x | 1 ; } ;\n"
"void operator ^= ( void ) { x = x ^ 1 ; } ;\n"
"void operator == ( void ) { x = x = 1 ; } ;\n"
"void operator != ( void ) { x = x ! 1 ; } ;\n"
"void operator <= ( void ) { x = x < 1 ; } ;\n"
"void operator >= ( void ) { x = x > 1 ; } ;\n"
"void operator <<= ( void ) { x = x << 1 ; } ;\n"
"void operator >>= ( void ) { x = x >> 1 ; } ;";
ASSERT_EQUALS(expected, preprocess(code));
Expand Down Expand Up @@ -1374,7 +1382,7 @@ static void hashhash_invalid_string_number()
"#define BAD(x) x##12345\nBAD(\"ABC\")";

simplecpp::OutputList outputList;
preprocess(code, simplecpp::DUI(), &outputList);
preprocess(code, &outputList);
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'BAD', Invalid ## usage when expanding 'BAD': Combining '\"ABC\"' and '12345' yields an invalid token.\n", toString(outputList));
}

Expand All @@ -1384,7 +1392,7 @@ static void hashhash_invalid_missing_args()
"#define BAD(x) ##x\nBAD()";

simplecpp::OutputList outputList;
preprocess(code, simplecpp::DUI(), &outputList);
preprocess(code, &outputList);
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'BAD', Invalid ## usage when expanding 'BAD': Missing first argument\n", toString(outputList));
}

Expand All @@ -1405,7 +1413,7 @@ static void hashhash_universal_character()
const char code[] =
"#define A(x,y) x##y\nint A(\\u01,04);";
simplecpp::OutputList outputList;
preprocess(code, simplecpp::DUI(), &outputList);
preprocess(code, &outputList);
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'A', Invalid ## usage when expanding 'A': Combining '\\u01' and '04' yields universal character '\\u0104'. This is undefined behavior according to C standard chapter 5.1.1.2, paragraph 4.\n", toString(outputList));
}

Expand Down Expand Up @@ -2915,7 +2923,7 @@ static void fuzz_crash()
{
const char code[] = "#define n __VA_OPT__(u\n"
"n\n";
(void)preprocess(code, simplecpp::DUI()); // do not crash
(void)preprocess(code); // do not crash
}
}

Expand Down

0 comments on commit 38f26e2

Please sign in to comment.