Skip to content

Commit f437d61

Browse files
authored
fixed #632 - avoid unhandled simplecpp::Macro::Error in simplecpp::preprocess with -D (#631)
1 parent ae1f0fb commit f437d61

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

simplecpp.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,9 @@ namespace simplecpp {
17391739
return tok;
17401740
}
17411741

1742+
/**
1743+
* @throws Error thrown in case of __VA_OPT__ issues
1744+
*/
17421745
bool parseDefine(const Token *nametoken) {
17431746
nameTokDef = nametoken;
17441747
variadic = false;
@@ -3379,6 +3382,17 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
33793382
}
33803383
output.clear();
33813384
return;
3385+
} catch (const simplecpp::Macro::Error& e) {
3386+
if (outputList) {
3387+
simplecpp::Output err{
3388+
Output::DUI_ERROR,
3389+
{},
3390+
e.what
3391+
};
3392+
outputList->emplace_back(std::move(err));
3393+
}
3394+
output.clear();
3395+
return;
33823396
}
33833397
}
33843398

@@ -3507,14 +3521,14 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
35073521
else
35083522
it->second = macro;
35093523
}
3510-
} catch (const std::runtime_error &) {
3524+
} catch (const std::runtime_error &err) {
35113525
if (outputList) {
3512-
simplecpp::Output err{
3526+
simplecpp::Output out{
35133527
Output::SYNTAX_ERROR,
35143528
rawtok->location,
3515-
"Failed to parse #define"
3529+
std::string("Failed to parse #define, ") + err.what()
35163530
};
3517-
outputList->emplace_back(std::move(err));
3531+
outputList->emplace_back(std::move(out));
35183532
}
35193533
output.clear();
35203534
return;

test.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,15 +698,15 @@ static void define_invalid_1()
698698
const char code[] = "#define A(\nB\n";
699699
simplecpp::OutputList outputList;
700700
ASSERT_EQUALS("", preprocess(code, &outputList));
701-
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define\n", toString(outputList));
701+
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define, bad macro syntax\n", toString(outputList));
702702
}
703703

704704
static void define_invalid_2()
705705
{
706706
const char code[] = "#define\nhas#";
707707
simplecpp::OutputList outputList;
708708
ASSERT_EQUALS("", preprocess(code, &outputList));
709-
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define\n", toString(outputList));
709+
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define, bad macro syntax\n", toString(outputList));
710710
}
711711

712712
static void define_define_1()
@@ -1105,6 +1105,15 @@ static void define_va_opt_8()
11051105
ASSERT_EQUALS("", toString(outputList));
11061106
}
11071107

1108+
static void define_va_opt_9()
1109+
{
1110+
simplecpp::DUI dui;
1111+
dui.defines.emplace_back("f(...)=__VA_OPT__");
1112+
simplecpp::OutputList outputList;
1113+
ASSERT_EQUALS("", preprocess("", dui, &outputList));
1114+
ASSERT_EQUALS("file0,0,dui_error,In definition of 'f': Missing opening parenthesis for __VA_OPT__\n", toString(outputList));
1115+
}
1116+
11081117
static void define_ifdef()
11091118
{
11101119
const char code[] = "#define A(X) X\n"
@@ -3674,6 +3683,7 @@ int main(int argc, char **argv)
36743683
TEST_CASE(define_va_opt_6);
36753684
TEST_CASE(define_va_opt_7);
36763685
TEST_CASE(define_va_opt_8);
3686+
TEST_CASE(define_va_opt_9); // #632
36773687

36783688
TEST_CASE(pragma_backslash); // multiline pragma directive
36793689

0 commit comments

Comments
 (0)