Skip to content

Commit 21459e9

Browse files
authored
Fix #15239 (Add tests for the simplecpp portability reports and the macro-usage arg) (#9057)
1 parent 64cee51 commit 21459e9

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

‎test/testpreprocessor.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class TestPreprocessor : public TestFixture {
136136
if (inlineSuppression)
137137
preprocessor.inlineSuppressions(*inlineSuppression);
138138
preprocessor.removeComments();
139+
preprocessor.reportSimplecppPortability(outputList);
139140
preprocessor.simplifyPragmaAsm();
140141

141142
std::map<std::string, std::string> cfgcode;
@@ -391,6 +392,8 @@ class TestPreprocessor : public TestFixture {
391392
TEST_CASE(pragmaAsm);
392393

393394
TEST_CASE(dumpPreprocessor);
395+
TEST_CASE(dumpMacroUsageArg);
396+
TEST_CASE(portabilityReports);
394397
}
395398

396399
template<size_t size>
@@ -3300,6 +3303,41 @@ class TestPreprocessor : public TestFixture {
33003303
" </simplecpp-if-cond>\n",
33013304
ostr.str());
33023305
}
3306+
void dumpMacroUsageArg() {
3307+
const char code[] = "#define INT64_C(x) x ## LL\n"
3308+
"#define F(x) x\n"
3309+
"INT64_C(5) + F(1);\n";
3310+
std::vector<std::string> files;
3311+
simplecpp::OutputList outputList;
3312+
simplecpp::TokenList tokens1(code, files, "test.cpp", {}, &outputList);
3313+
Preprocessor preprocessor(tokens1, settingsDefault, *this, Standards::Language::CPP);
3314+
(void)preprocessor.preprocess("", files, outputList);
3315+
(void)preprocessor.reportOutput(outputList, true);
3316+
3317+
std::ostringstream ostr;
3318+
preprocessor.dump(ostr);
3319+
// simplecpp emits these from an unordered_map, so their order differs between standard libraries
3320+
const std::string dump = ostr.str();
3321+
std::size_t entries = 0;
3322+
for (std::string::size_type pos = dump.find("<macro "); pos != std::string::npos; pos = dump.find("<macro ", pos + 1))
3323+
++entries;
3324+
ASSERT_EQUALS(2U, entries);
3325+
ASSERT(dump.find(" <macro name=\"INT64_C\" file=\"test.cpp\" line=\"1\" column=\"9\" usefile=\"test.cpp\" useline=\"3\" usecolumn=\"1\" is-known-value=\"true\" arg=\"5\"/>\n") != std::string::npos);
3326+
ASSERT(dump.find(" <macro name=\"F\" file=\"test.cpp\" line=\"2\" column=\"9\" usefile=\"test.cpp\" useline=\"3\" usecolumn=\"14\" is-known-value=\"true\"/>\n") != std::string::npos);
3327+
}
3328+
3329+
void portabilityReports() {
3330+
const char code[] = "#define A 1 \\ \n"
3331+
"+ 2\n"
3332+
"int x = A;";
3333+
const Settings settings = settingsBuilder().severity(Severity::portability).build();
3334+
(void)getcode(settings, *this, code);
3335+
ASSERT_EQUALS("[file.c:1:13]: (portability) Combination 'backslash space newline' is not portable. [simplecppPortabilityBackslash]\n"
3336+
"[file.c:3:11]: (portability) No newline at end of file is undefined behavior in C. [simplecppPortabilityNoEofNewline]\n", errout_str());
3337+
3338+
(void)getcode(settingsDefault, *this, code);
3339+
ASSERT_EQUALS("", errout_str());
3340+
}
33033341
};
33043342

33053343
REGISTER_TEST(TestPreprocessor)

0 commit comments

Comments
 (0)