Skip to content

Commit fe89cd1

Browse files
committed
Test the simplecpp portability reports and the macro usage arg in the dump
The simplecpp update reports PORTABILITY_BACKSLASH and PORTABILITY_NO_EOF_NEWLINE as portability messages and writes the argument of an integer constant macro call to <macro-usage>, with no test for either. portabilityReports checks both ids are reported with portability enabled and nothing without it. dumpMacroUsageArg checks arg= is written for INT64_C(5) and left out for other macros. getcode() now calls reportSimplecppPortability() after removeComments(), as CppCheck::checkInternal does, so the main file's portability messages reach the test logger.
1 parent d2987e8 commit fe89cd1

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

‎test/testpreprocessor.cpp‎

Lines changed: 36 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,39 @@ 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+
ASSERT_EQUALS(
3320+
" <macro-usage>\n"
3321+
" <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"
3322+
" <macro name=\"F\" file=\"test.cpp\" line=\"2\" column=\"9\" usefile=\"test.cpp\" useline=\"3\" usecolumn=\"14\" is-known-value=\"true\"/>\n"
3323+
" </macro-usage>\n",
3324+
ostr.str());
3325+
}
3326+
3327+
void portabilityReports() {
3328+
const char code[] = "#define A 1 \\ \n"
3329+
"+ 2\n"
3330+
"int x = A;";
3331+
const Settings settings = settingsBuilder().severity(Severity::portability).build();
3332+
(void)getcode(settings, *this, code);
3333+
ASSERT_EQUALS("[file.c:1:13]: (portability) Combination 'backslash space newline' is not portable. [simplecppPortabilityBackslash]\n"
3334+
"[file.c:3:11]: (portability) No newline at end of file is undefined behavior in C. [simplecppPortabilityNoEofNewline]\n", errout_str());
3335+
3336+
(void)getcode(settingsDefault, *this, code);
3337+
ASSERT_EQUALS("", errout_str());
3338+
}
33033339
};
33043340

33053341
REGISTER_TEST(TestPreprocessor)

0 commit comments

Comments
 (0)