diff --git a/simplecpp.cpp b/simplecpp.cpp index 1539c380..f73d5e89 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1069,7 +1069,8 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename, if (cback() && cback()->location.line == location.line && cback()->previous && cback()->previous->op == '#') { const Token* const ppTok = cback()->previous; - if (ppTok->next && (ppTok->next->str() == "error" || ppTok->next->str() == "warning")) { + // #error/#warning directive => '#' is the first token on the line (not the stringizing operator in a #define) + if (ppTok->next && (ppTok->next->str() == "error" || ppTok->next->str() == "warning") && isLastLinePreprocessor(2) == ppTok) { readErrorText(stream, ch, location); continue; } diff --git a/test.cpp b/test.cpp index 2cd8ed1f..4f8e16cd 100644 --- a/test.cpp +++ b/test.cpp @@ -1967,6 +1967,17 @@ static void error8() ASSERT_EQUALS("file0,1,#error,#error blabla\n", toString(outputList)); } +static void error9() +{ + // #error and #warning in a #define is the stringizing operator, not a directive + const char code[] = "#define A(error) f(#error); break\n" + "#define B(warning) g(#warning); x\n" + "A(e1) B(w1)\n"; + simplecpp::OutputList outputList; + ASSERT_EQUALS("\n\nf ( \"e1\" ) ; break g ( \"w1\" ) ; x", preprocess(code, &outputList)); + ASSERT_EQUALS("", toString(outputList)); +} + static void garbage() { simplecpp::OutputList outputList; @@ -6094,6 +6105,7 @@ static void runTests(int argc, char **argv, Input input) TEST_CASE(error6); TEST_CASE(error7); TEST_CASE(error8); + TEST_CASE(error9); TEST_CASE(garbage); TEST_CASE(garbage_endif);