Fixed #7102 (Preprocessor: skip __cplusplus sections in headers when .c file is checked)

This commit is contained in:
Daniel Marjamäki 2016-02-29 07:34:06 +01:00
parent 542d610d4e
commit c5c376513b
2 changed files with 34 additions and 52 deletions

View File

@ -1565,6 +1565,17 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
ret.sort(); ret.sort();
ret.unique(); ret.unique();
// C code => remove __cplusplus configurations..
if (!cplusplus(&_settings, filename) && Path::isC(filename)) {
for (std::list<std::string>::iterator it = ret.begin(); it != ret.end();) {
if (it->find("__cplusplus") != std::string::npos) {
ret.erase(it++);
} else {
++it;
}
}
}
// cleanup unhandled configurations.. // cleanup unhandled configurations..
for (std::list<std::string>::iterator it = ret.begin(); it != ret.end();) { for (std::list<std::string>::iterator it = ret.begin(); it != ret.end();) {
const std::string s(*it + ";"); const std::string s(*it + ";");

View File

@ -316,10 +316,10 @@ private:
return preprocessor0.read(istr, "test.c"); return preprocessor0.read(istr, "test.c");
} }
void preprocess(const char* code, std::map<std::string, std::string>& actual) { void preprocess(const char* code, std::map<std::string, std::string>& actual, const char filename[] = "file.c") {
errout.str(""); errout.str("");
std::istringstream istr(code); std::istringstream istr(code);
preprocessor0.preprocess(istr, actual, "file.c"); preprocessor0.preprocess(istr, actual, filename);
} }
@ -429,61 +429,32 @@ private:
void Bug2190219() { void Bug2190219() {
const char filedata[] = "int main()\n" const char filedata[] = "#ifdef __cplusplus\n"
"{\n" "cpp\n"
"#ifdef __cplusplus\n"
" int* flags = new int[10];\n"
"#else\n" "#else\n"
" int* flags = (int*)malloc((10)*sizeof(int));\n" "c\n"
"#endif\n" "#endif";
"\n"
"#ifdef __cplusplus\n"
" delete [] flags;\n"
"#else\n"
" free(flags);\n"
"#endif\n"
"}\n";
// Expected result.. {
std::map<std::string, std::string> expected; // Preprocess => actual result..
expected[""] = "int main()\n" std::map<std::string, std::string> actual;
"{\n" preprocess(filedata, actual, "file.cpp");
"\n"
"\n"
"\n"
"int* flags = (int*)malloc((10)*sizeof(int));\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"free(flags);\n"
"\n"
"}\n";
expected["__cplusplus"] = "int main()\n" // Compare results..
"{\n" ASSERT_EQUALS(1U, actual.size());
"\n" ASSERT_EQUALS("\ncpp\n\n\n\n", actual[""]);
"int* flags = new int[10];\n" }
"\n"
"\n"
"\n"
"\n"
"\n"
"delete [] flags;\n"
"\n"
"\n"
"\n"
"}\n";
// Preprocess => actual result.. {
std::map<std::string, std::string> actual; // Ticket #7102 - skip __cplusplus in C code
preprocess(filedata, actual); // Preprocess => actual result..
std::map<std::string, std::string> actual;
preprocess(filedata, actual, "file.c");
// Compare results.. // Compare results..
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size())); ASSERT_EQUALS(1U, actual.size());
ASSERT_EQUALS(expected[""], actual[""]); ASSERT_EQUALS("\n\n\nc\n\n", actual[""]);
ASSERT_EQUALS(expected["__cplusplus"], actual["__cplusplus"]); }
} }