Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 16 additions & 1 deletion simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 &macroName) : Error(loc, "Wrong number of parameters for macro \'" + macroName + "\'.") {}
Expand Down Expand Up @@ -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{
Expand Down
1 change: 1 addition & 0 deletions simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));

}

Expand Down
Loading