Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 17 additions & 5 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2818,7 +2818,7 @@ static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI

static const char * const altopData[] = {"and","or","bitand","bitor","compl","not","not_eq","xor"};
static const std::set<std::string> altop(&altopData[0], &altopData[8]);
static void simplifyName(simplecpp::TokenList &expr)
static bool simplifyName(simplecpp::TokenList &expr, simplecpp::OutputList *outputList)
{
for (simplecpp::Token *tok = expr.front(); tok; tok = tok->next) {
if (tok->name) {
Expand All @@ -2832,9 +2832,20 @@ static void simplifyName(simplecpp::TokenList &expr)
if (alt)
continue;
}
if (tok->next && tok->next->str() == "(") {
if (outputList) {
Comment thread
ludviggunne marked this conversation as resolved.
Outdated
simplecpp::Output err(tok->location.files);
err.type = simplecpp::Output::SYNTAX_ERROR;
err.location = tok->location;
err.msg = "Undefined function-like macro in directive";
Comment thread
ludviggunne marked this conversation as resolved.
Outdated
outputList->push_back(err);
}
return false;
}
tok->setstr("0");
}
}
return true;
}

/*
Expand Down Expand Up @@ -3106,12 +3117,13 @@ static void simplifyComments(simplecpp::TokenList &expr)
}
}

static long long evaluate(simplecpp::TokenList &expr, const simplecpp::DUI &dui, const std::map<std::string, std::size_t> &sizeOfType)
static long long evaluate(simplecpp::TokenList &expr, const simplecpp::DUI &dui, const std::map<std::string, std::size_t> &sizeOfType, simplecpp::OutputList *outputList)
{
simplifyComments(expr);
simplifySizeof(expr, sizeOfType);
simplifyHasInclude(expr, dui);
simplifyName(expr);
if (!simplifyName(expr, outputList))
return 0;
simplifyNumbers(expr);
expr.constFold();
// TODO: handle invalid expressions
Expand Down Expand Up @@ -3820,11 +3832,11 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
std::string E;
for (const simplecpp::Token *tok = expr.cfront(); tok; tok = tok->next)
E += (E.empty() ? "" : " ") + tok->str();
const long long result = evaluate(expr, dui, sizeOfType);
const long long result = evaluate(expr, dui, sizeOfType, outputList);
Comment thread
ludviggunne marked this conversation as resolved.
Outdated
conditionIsTrue = (result != 0);
ifCond->push_back(IfCond(rawtok->location, E, result));
} else {
const long long result = evaluate(expr, dui, sizeOfType);
const long long result = evaluate(expr, dui, sizeOfType, outputList);
conditionIsTrue = (result != 0);
}
} catch (const std::exception &e) {
Expand Down
10 changes: 10 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,15 @@ static void ifexpr()
ASSERT_EQUALS("\n\n1", preprocess(code));
}

static void ifUndefFuncStyleMacro()
{
const char code[] = "#if A(<dir/file.h>)\n"
"#endif\n";
simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code, &outputList));
ASSERT_EQUALS("file0,1,syntax_error,Undefined function-like macro in directive\n", toString(outputList));
}

static void location1()
{
const char *code;
Expand Down Expand Up @@ -3145,6 +3154,7 @@ int main(int argc, char **argv)
TEST_CASE(ifdiv0);
TEST_CASE(ifalt); // using "and", "or", etc
TEST_CASE(ifexpr);
TEST_CASE(ifUndefFuncStyleMacro);

TEST_CASE(location1);
TEST_CASE(location2);
Expand Down
Loading