Skip to content

Commit 906558c

Browse files
committed
small simplecpp::Macro::Error cleanup
1 parent 8a30993 commit 906558c

2 files changed

Lines changed: 11 additions & 20 deletions

File tree

simplecpp.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,10 +1692,9 @@ namespace simplecpp {
16921692
}
16931693

16941694
/** base class for errors */
1695-
struct Error {
1696-
Error(const Location &loc, const std::string &s) : location(loc), what(s) {}
1695+
struct Error : public std::runtime_error {
1696+
Error(const Location &loc, const std::string &s) : std::runtime_error(s), location(loc) {}
16971697
const Location location;
1698-
const std::string what;
16991698
};
17001699

17011700
/** Struct that is thrown when macro is expanded with wrong number of parameters */
@@ -1739,6 +1738,9 @@ namespace simplecpp {
17391738
return tok;
17401739
}
17411740

1741+
/**
1742+
* @throws Error thrown in case of __VA_OPT__ issues
1743+
*/
17421744
bool parseDefine(const Token *nametoken) {
17431745
nameTokDef = nametoken;
17441746
variadic = false;
@@ -3278,7 +3280,7 @@ static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token
32783280
simplecpp::Output out{
32793281
simplecpp::Output::SYNTAX_ERROR,
32803282
err.location,
3281-
"failed to expand \'" + tok->str() + "\', " + err.what
3283+
"failed to expand \'" + tok->str() + "\', " + err.what()
32823284
};
32833285
outputList->emplace_back(std::move(out));
32843286
}
@@ -3507,23 +3509,12 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
35073509
else
35083510
it->second = macro;
35093511
}
3510-
} catch (const std::runtime_error &) {
3512+
} catch (const std::runtime_error &err) {
35113513
if (outputList) {
3512-
simplecpp::Output err{
3514+
simplecpp::Output out{
35133515
Output::SYNTAX_ERROR,
35143516
rawtok->location,
3515-
"Failed to parse #define"
3516-
};
3517-
outputList->emplace_back(std::move(err));
3518-
}
3519-
output.clear();
3520-
return;
3521-
} catch (const simplecpp::Macro::Error &err) {
3522-
if (outputList) {
3523-
simplecpp::Output out{
3524-
simplecpp::Output::SYNTAX_ERROR,
3525-
err.location,
3526-
"Failed to parse #define, " + err.what
3517+
std::string("Failed to parse #define, ") + err.what()
35273518
};
35283519
outputList->emplace_back(std::move(out));
35293520
}

test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,15 +687,15 @@ static void define_invalid_1()
687687
const char code[] = "#define A(\nB\n";
688688
simplecpp::OutputList outputList;
689689
ASSERT_EQUALS("", preprocess(code, &outputList));
690-
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define\n", toString(outputList));
690+
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define, bad macro syntax\n", toString(outputList));
691691
}
692692

693693
static void define_invalid_2()
694694
{
695695
const char code[] = "#define\nhas#";
696696
simplecpp::OutputList outputList;
697697
ASSERT_EQUALS("", preprocess(code, &outputList));
698-
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define\n", toString(outputList));
698+
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define, bad macro syntax\n", toString(outputList));
699699
}
700700

701701
static void define_define_1()

0 commit comments

Comments
 (0)