diff --git a/main.cpp b/main.cpp index 685ada12..06afcfbb 100644 --- a/main.cpp +++ b/main.cpp @@ -272,6 +272,9 @@ int main(int argc, char **argv) case simplecpp::Output::SYNTAX_ERROR: std::cerr << "syntax error: "; break; + case simplecpp::Output::DIRECTIVE_AS_MACRO_PARAMETER: + std::cerr << "directive as macro parameter: "; + break; case simplecpp::Output::PORTABILITY_BACKSLASH: std::cerr << "portability: "; break; diff --git a/simplecpp.cpp b/simplecpp.cpp index d2c398c4..748ea6a9 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1602,7 +1602,7 @@ namespace simplecpp { else if (rawtok->op == ')') --par; else if (rawtok->op == '#' && !sameline(rawtok->previous, rawtok)) - throw Error(rawtok->location, "it is invalid to use a preprocessor directive as macro parameter"); + throw invalidDirectiveAsMacroParameter(rawtok->location); rawtokens2.push_back(new Token(rawtok->str(), rawtok1->location, rawtok->whitespaceahead)); rawtok = rawtok->next; } @@ -1698,6 +1698,11 @@ namespace simplecpp { const std::string what; }; + struct invalidDirectiveAsMacroParameter : public Error { + invalidDirectiveAsMacroParameter(const Location &loc) + : Error(loc, "it is invalid to use a preprocessor directive as macro parameter") {} + }; + /** Struct that is thrown when macro is expanded with wrong number of parameters */ struct wrongNumberOfParameters : public Error { wrongNumberOfParameters(const Location &loc, const std::string ¯oName) : Error(loc, "Wrong number of parameters for macro \'" + macroName + "\'.") {} @@ -3344,6 +3349,16 @@ static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token simplecpp::TokenList value(files); try { tok1 = it->second.expand(value, tok, macros, files); + } catch (const simplecpp::Macro::invalidDirectiveAsMacroParameter &err) { + if (outputList) { + simplecpp::Output out{ + simplecpp::Output::DIRECTIVE_AS_MACRO_PARAMETER, + err.location, + "failed to expand \'" + tok->str() + "\', " + err.what + }; + outputList->emplace_back(std::move(out)); + } + return false; } catch (const simplecpp::Macro::Error &err) { if (outputList) { simplecpp::Output out{ diff --git a/simplecpp.h b/simplecpp.h index 3d6fc526..ee2eb100 100644 --- a/simplecpp.h +++ b/simplecpp.h @@ -238,6 +238,7 @@ namespace simplecpp { MISSING_HEADER, INCLUDE_NESTED_TOO_DEEPLY, SYNTAX_ERROR, + DIRECTIVE_AS_MACRO_PARAMETER, PORTABILITY_BACKSLASH, UNHANDLED_CHAR_ERROR, EXPLICIT_INCLUDE_NOT_FOUND, diff --git a/test.cpp b/test.cpp index b13140aa..3a858929 100644 --- a/test.cpp +++ b/test.cpp @@ -197,6 +197,9 @@ static std::string toString(const simplecpp::OutputList &outputList) case simplecpp::Output::Type::SYNTAX_ERROR: ostr << "syntax_error,"; break; + case simplecpp::Output::DIRECTIVE_AS_MACRO_PARAMETER: + ostr << "directive_as_macro_parameter,"; + break; case simplecpp::Output::Type::PORTABILITY_BACKSLASH: ostr << "portability_backslash,"; break; @@ -1338,7 +1341,7 @@ static void define_ifdef() simplecpp::OutputList outputList; ASSERT_EQUALS("", preprocess(code, &outputList)); - ASSERT_EQUALS("file0,3,syntax_error,failed to expand 'A', it is invalid to use a preprocessor directive as macro parameter\n", toString(outputList)); + ASSERT_EQUALS("file0,3,directive_as_macro_parameter,failed to expand 'A', it is invalid to use a preprocessor directive as macro parameter\n", toString(outputList)); }