Fixed #4256 (Preprocessor: '#pragma once' doesn't work properly when using -D)

This commit is contained in:
Daniel Marjamäki 2013-08-18 18:04:06 +02:00
parent 6a369f0841
commit 342142b783
3 changed files with 85 additions and 31 deletions

View File

@ -942,7 +942,9 @@ void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processe
std::map<std::string, std::string> defs(getcfgmap(_settings ? _settings->userDefines : std::string(""))); std::map<std::string, std::string> defs(getcfgmap(_settings ? _settings->userDefines : std::string("")));
if (_settings && _settings->_maxConfigs == 1U) { if (_settings && _settings->_maxConfigs == 1U) {
processedFile = handleIncludes(processedFile, filename, includePaths, defs); std::list<std::string> pragmaOnce;
std::list<std::string> includes;
processedFile = handleIncludes(processedFile, filename, includePaths, defs, pragmaOnce, includes);
resultConfigurations = getcfgs(processedFile, filename, defs); resultConfigurations = getcfgs(processedFile, filename, defs);
} else { } else {
handleIncludes(processedFile, filename, includePaths); handleIncludes(processedFile, filename, includePaths);
@ -1932,7 +1934,7 @@ static bool openHeader(std::string &filename, const std::list<std::string> &incl
} }
std::string Preprocessor::handleIncludes(const std::string &code, const std::string &filePath, const std::list<std::string> &includePaths, std::map<std::string,std::string> &defs, std::list<std::string> includes) std::string Preprocessor::handleIncludes(const std::string &code, const std::string &filePath, const std::list<std::string> &includePaths, std::map<std::string,std::string> &defs, std::list<std::string> &pragmaOnce, std::list<std::string> includes)
{ {
const std::string path(filePath.substr(0, 1 + filePath.find_last_of("\\/"))); const std::string path(filePath.substr(0, 1 + filePath.find_last_of("\\/")));
@ -1975,7 +1977,9 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str
std::stack<bool>::reference elseIsTrue = elseIsTrueStack.top(); std::stack<bool>::reference elseIsTrue = elseIsTrueStack.top();
if (line.compare(0,7,"#ifdef ") == 0) { if (line == "#pragma once") {
pragmaOnce.push_back(filePath);
} else if (line.compare(0,7,"#ifdef ") == 0) {
if (indent == indentmatch) { if (indent == indentmatch) {
const std::string tag = getdef(line,true); const std::string tag = getdef(line,true);
if (defs.find(tag) != defs.end()) { if (defs.find(tag) != defs.end()) {
@ -2115,8 +2119,14 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str
includes.push_back(filename); includes.push_back(filename);
// Don't include header if it's already included and contains #pragma once
if (std::find(pragmaOnce.begin(), pragmaOnce.end(), filename) != pragmaOnce.end()) {
ostr << std::endl;
continue;
}
ostr << "#file \"" << filename << "\"\n" ostr << "#file \"" << filename << "\"\n"
<< handleIncludes(read(fin, filename), filename, includePaths, defs, includes) << std::endl << handleIncludes(read(fin, filename), filename, includePaths, defs, pragmaOnce, includes) << std::endl
<< "#endfile\n"; << "#endfile\n";
continue; continue;
} }

View File

@ -233,10 +233,11 @@ public:
* @param filePath filename of code * @param filePath filename of code
* @param includePaths Paths where headers might be * @param includePaths Paths where headers might be
* @param defs defines (only values) * @param defs defines (only values)
* @param pragmaOnce includes that has already been included and contains a #pragma once statement
* @param includes provide a empty list. this is just used to prevent recursive inclusions. * @param includes provide a empty list. this is just used to prevent recursive inclusions.
* \return resulting string * \return resulting string
*/ */
std::string handleIncludes(const std::string &code, const std::string &filePath, const std::list<std::string> &includePaths, std::map<std::string,std::string> &defs, std::list<std::string> includes = std::list<std::string>()); std::string handleIncludes(const std::string &code, const std::string &filePath, const std::list<std::string> &includePaths, std::map<std::string,std::string> &defs, std::list<std::string> &pragmaOnce, std::list<std::string> includes);
void setFile0(const std::string &f) { void setFile0(const std::string &f) {
file0 = f; file0 = f;

View File

@ -208,6 +208,7 @@ private:
TEST_CASE(pragma); TEST_CASE(pragma);
TEST_CASE(pragma_asm_1); TEST_CASE(pragma_asm_1);
TEST_CASE(pragma_asm_2); TEST_CASE(pragma_asm_2);
TEST_CASE(pragma_once);
TEST_CASE(endifsemicolon); TEST_CASE(endifsemicolon);
TEST_CASE(missing_doublequote); TEST_CASE(missing_doublequote);
TEST_CASE(handle_error); TEST_CASE(handle_error);
@ -2410,6 +2411,19 @@ private:
ASSERT_EQUALS("\n\nasm(temp);\nbbb\n", actual[""]); ASSERT_EQUALS("\n\nasm(temp);\nbbb\n", actual[""]);
} }
void pragma_once() {
const char code[] = "#pragma once\n"
"int x";
Preprocessor preprocessor(NULL, this);
const std::list<std::string> includePaths;
std::map<std::string,std::string> defs;
std::list<std::string> pragmaOnce;
preprocessor.handleIncludes(code, "123.h", includePaths, defs, pragmaOnce, std::list<std::string>());
ASSERT_EQUALS(1U, pragmaOnce.size());
ASSERT_EQUALS("123.h", *(pragmaOnce.begin()));
}
void endifsemicolon() { void endifsemicolon() {
const char filedata[] = "void f() {\n" const char filedata[] = "void f() {\n"
"#ifdef A\n" "#ifdef A\n"
@ -3278,12 +3292,14 @@ private:
defs.clear(); defs.clear();
defs["A"] = ""; defs["A"] = "";
{ {
std::list<std::string> pragmaOnce;
const std::string code("#ifdef A\n123\n#endif\n"); const std::string code("#ifdef A\n123\n#endif\n");
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list<std::string>()));
ASSERT_EQUALS("\n123\n\n", actual); ASSERT_EQUALS("\n123\n\n", actual);
}{ }{
std::list<std::string> pragmaOnce;
const std::string code("#ifdef B\n123\n#endif\n"); const std::string code("#ifdef B\n123\n#endif\n");
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list<std::string>()));
ASSERT_EQUALS("\n\n\n", actual); ASSERT_EQUALS("\n\n\n", actual);
} }
} }
@ -3293,33 +3309,37 @@ private:
defs.clear(); defs.clear();
defs["A"] = ""; defs["A"] = "";
{ {
std::list<std::string> pragmaOnce;
const std::string code("#ifndef A\n123\n#endif\n"); const std::string code("#ifndef A\n123\n#endif\n");
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list<std::string>()));
ASSERT_EQUALS("\n\n\n", actual); ASSERT_EQUALS("\n\n\n", actual);
}{ }{
std::list<std::string> pragmaOnce;
const std::string code("#ifndef B\n123\n#endif\n"); const std::string code("#ifndef B\n123\n#endif\n");
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list<std::string>()));
ASSERT_EQUALS("\n123\n\n", actual); ASSERT_EQUALS("\n123\n\n", actual);
} }
} }
// define - ifndef // define - ifndef
{ {
std::list<std::string> pragmaOnce;
defs.clear(); defs.clear();
const std::string code("#ifndef X\n#define X\n123\n#endif\n" const std::string code("#ifndef X\n#define X\n123\n#endif\n"
"#ifndef X\n#define X\n123\n#endif\n"); "#ifndef X\n#define X\n123\n#endif\n");
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list<std::string>()));
ASSERT_EQUALS("\n#define X\n123\n\n" "\n\n\n\n", actual); ASSERT_EQUALS("\n#define X\n123\n\n" "\n\n\n\n", actual);
} }
// #define => #if // #define => #if
{ {
std::list<std::string> pragmaOnce;
defs.clear(); defs.clear();
const std::string code("#define X 123\n" const std::string code("#define X 123\n"
"#if X==123\n" "#if X==123\n"
"456\n" "456\n"
"#endif\n"); "#endif\n");
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list<std::string>()));
ASSERT_EQUALS("#define X 123\n\n456\n\n", actual); ASSERT_EQUALS("#define X 123\n\n456\n\n", actual);
} }
@ -3335,23 +3355,26 @@ private:
"4\n" "4\n"
"#endif"); "#endif");
{ {
std::list<std::string> pragmaOnce;
defs.clear(); defs.clear();
defs["A"] = ""; defs["A"] = "";
defs["C"] = ""; defs["C"] = "";
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list<std::string>()));
ASSERT_EQUALS("\n1\n\n\n\n\n\n\n\n", actual); ASSERT_EQUALS("\n1\n\n\n\n\n\n\n\n", actual);
} }
{ {
std::list<std::string> pragmaOnce;
defs.clear(); defs.clear();
defs["B"] = ""; defs["B"] = "";
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list<std::string>()));
ASSERT_EQUALS("\n\n\n2\n\n\n\n\n\n", actual); ASSERT_EQUALS("\n\n\n2\n\n\n\n\n\n", actual);
} }
{ {
std::list<std::string> pragmaOnce;
defs.clear(); defs.clear();
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list<std::string>()));
ASSERT_EQUALS("\n\n\n\n\n\n\n4\n\n", actual); ASSERT_EQUALS("\n\n\n\n\n\n\n4\n\n", actual);
} }
} }
@ -3360,9 +3383,10 @@ private:
{ {
// see also endifsemicolon // see also endifsemicolon
const std::string code("{\n#ifdef X\n#endif;\n}"); const std::string code("{\n#ifdef X\n#endif;\n}");
std::list<std::string> pragmaOnce;
defs.clear(); defs.clear();
defs["Z"] = ""; defs["Z"] = "";
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list<std::string>()));
ASSERT_EQUALS("{\n\n\n}\n", actual); ASSERT_EQUALS("{\n\n\n}\n", actual);
} }
@ -3373,11 +3397,15 @@ private:
"123\n" "123\n"
"#endif\n"); "#endif\n");
defs.clear(); std::list<std::string> pragmaOnce;
const std::string actual1(preprocessor.handleIncludes(code,filePath,includePaths,defs));
pragmaOnce.clear();
defs.clear(); defs.clear();
const std::string actual(preprocessor.handleIncludes(code + "#undef X\n" + code, filePath, includePaths, defs)); const std::string actual1(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list<std::string>()));
pragmaOnce.clear();
defs.clear();
const std::string actual(preprocessor.handleIncludes(code + "#undef X\n" + code, filePath, includePaths, defs,pragmaOnce,std::list<std::string>()));
ASSERT_EQUALS(actual1 + "#undef X\n" + actual1, actual); ASSERT_EQUALS(actual1 + "#undef X\n" + actual1, actual);
} }
@ -3385,9 +3413,10 @@ private:
// #error // #error
{ {
errout.str(""); errout.str("");
std::list<std::string> pragmaOnce;
defs.clear(); defs.clear();
const std::string code("#ifndef X\n#error abc\n#endif"); const std::string code("#ifndef X\n#error abc\n#endif");
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list<std::string>()));
ASSERT_EQUALS("\n#error abc\n\n", actual); ASSERT_EQUALS("\n#error abc\n\n", actual);
ASSERT_EQUALS("[test.c:2]: (error) abc\n", errout.str()); ASSERT_EQUALS("[test.c:2]: (error) abc\n", errout.str());
} }
@ -3403,40 +3432,48 @@ private:
// missing local include // missing local include
{ {
const std::string code("#include \"missing-include!!.h\"\n"); const std::string code("#include \"missing-include!!.h\"\n");
std::list<std::string> pragmaOnce;
pragmaOnce.clear();
errout.str(""); errout.str("");
settings = Settings(); settings = Settings();
preprocessor.handleIncludes(code,"test.c",includePaths,defs); preprocessor.handleIncludes(code,"test.c",includePaths,defs,pragmaOnce,std::list<std::string>());
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
pragmaOnce.clear();
errout.str(""); errout.str("");
settings.checkConfiguration = true; settings.checkConfiguration = true;
preprocessor.handleIncludes(code,"test.c",includePaths,defs); preprocessor.handleIncludes(code,"test.c",includePaths,defs,pragmaOnce,std::list<std::string>());
ASSERT_EQUALS("[test.c:1]: (information) Include file: \"missing-include!!.h\" not found.\n", errout.str()); ASSERT_EQUALS("[test.c:1]: (information) Include file: \"missing-include!!.h\" not found.\n", errout.str());
pragmaOnce.clear();
errout.str(""); errout.str("");
settings.nomsg.addSuppression("missingInclude"); settings.nomsg.addSuppression("missingInclude");
preprocessor.handleIncludes(code,"test.c",includePaths,defs); preprocessor.handleIncludes(code,"test.c",includePaths,defs,pragmaOnce,std::list<std::string>());
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
// missing system header // missing system header
{ {
const std::string code("#include <missing-include!!.h>\n"); const std::string code("#include <missing-include!!.h>\n");
std::list<std::string> pragmaOnce;
pragmaOnce.clear();
errout.str(""); errout.str("");
settings = Settings(); settings = Settings();
preprocessor.handleIncludes(code,"test.c",includePaths,defs); preprocessor.handleIncludes(code,"test.c",includePaths,defs,pragmaOnce,std::list<std::string>());
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
pragmaOnce.clear();
errout.str(""); errout.str("");
settings.checkConfiguration = true; settings.checkConfiguration = true;
preprocessor.handleIncludes(code,"test.c",includePaths,defs); preprocessor.handleIncludes(code,"test.c",includePaths,defs,pragmaOnce,std::list<std::string>());
ASSERT_EQUALS("[test.c:1]: (information) Include file: <missing-include!!.h> not found.\n", errout.str()); ASSERT_EQUALS("[test.c:1]: (information) Include file: <missing-include!!.h> not found.\n", errout.str());
pragmaOnce.clear();
errout.str(""); errout.str("");
settings.nomsg.addSuppression("missingIncludeSystem"); settings.nomsg.addSuppression("missingIncludeSystem");
preprocessor.handleIncludes(code,"test.c",includePaths,defs); preprocessor.handleIncludes(code,"test.c",includePaths,defs,pragmaOnce,std::list<std::string>());
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -3449,9 +3486,10 @@ private:
defs.clear(); defs.clear();
defs["GNU"] = ""; defs["GNU"] = "";
std::list<std::string> pragmaOnce;
errout.str(""); errout.str("");
settings = Settings(); settings = Settings();
preprocessor.handleIncludes(code,"test.c",includePaths,defs); preprocessor.handleIncludes(code,"test.c",includePaths,defs,pragmaOnce,std::list<std::string>());
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
} }
@ -3464,6 +3502,7 @@ private:
// #3405 // #3405
{ {
std::list<std::string> pragmaOnce;
defs.clear(); defs.clear();
defs["A"] = ""; defs["A"] = "";
const std::string code("\n#ifndef PAL_UTIL_UTILS_H_\n" const std::string code("\n#ifndef PAL_UTIL_UTILS_H_\n"
@ -3488,7 +3527,7 @@ private:
"8\n" "8\n"
"#endif\n" "#endif\n"
"\n"); "\n");
std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list<std::string>()));
// the 1,2,4 should be in the result // the 1,2,4 should be in the result
actual.erase(0, actual.find("1")); actual.erase(0, actual.find("1"));
@ -3499,6 +3538,7 @@ private:
// #3418 // #3418
{ {
std::list<std::string> pragmaOnce;
defs.clear(); defs.clear();
const char code[] = "#define A 1\n" const char code[] = "#define A 1\n"
"#define B A\n" "#define B A\n"
@ -3506,7 +3546,7 @@ private:
"123\n" "123\n"
"#endif\n"; "#endif\n";
std::string actual(preprocessor.handleIncludes(code, filePath, includePaths, defs)); std::string actual(preprocessor.handleIncludes(code, filePath, includePaths, defs,pragmaOnce,std::list<std::string>()));
ASSERT_EQUALS("#define A 1\n#define B A\n\n123\n\n", actual); ASSERT_EQUALS("#define A 1\n#define B A\n\n123\n\n", actual);
} }
} }
@ -3530,8 +3570,9 @@ private:
const std::list<std::string> includePaths; const std::list<std::string> includePaths;
std::map<std::string,std::string> defs; std::map<std::string,std::string> defs;
defs["A"] = "1"; defs["A"] = "1";
std::list<std::string> pragmaOnce;
ASSERT_EQUALS(std::string::npos, // No "123" in the output ASSERT_EQUALS(std::string::npos, // No "123" in the output
preprocessor.handleIncludes(code, "test.c", includePaths, defs).find("123")); preprocessor.handleIncludes(code, "test.c", includePaths, defs, pragmaOnce,std::list<std::string>()).find("123"));
} }
void def_handleIncludes_ifelse3() { // #4865 void def_handleIncludes_ifelse3() { // #4865
@ -3550,7 +3591,8 @@ private:
std::map<std::string,std::string> defs; std::map<std::string,std::string> defs;
defs["B"] = "1"; defs["B"] = "1";
defs["C"] = "1"; defs["C"] = "1";
preprocessor.handleIncludes(code, "test.c", includePaths, defs); // don't crash std::list<std::string> pragmaOnce;
preprocessor.handleIncludes(code, "test.c", includePaths, defs, pragmaOnce, std::list<std::string>()); // don't crash
} }
void def_valueWithParentheses() { void def_valueWithParentheses() {
@ -3564,11 +3606,12 @@ private:
const std::string filePath("test.c"); const std::string filePath("test.c");
const std::list<std::string> includePaths; const std::list<std::string> includePaths;
std::map<std::string,std::string> defs; std::map<std::string,std::string> defs;
std::list<std::string> pragmaOnce;
Preprocessor preprocessor(NULL, this); Preprocessor preprocessor(NULL, this);
std::istringstream istr(code); std::istringstream istr(code);
const std::string s(preprocessor.read(istr, "")); const std::string s(preprocessor.read(istr, ""));
preprocessor.handleIncludes(s, filePath, includePaths, defs); preprocessor.handleIncludes(s, filePath, includePaths, defs, pragmaOnce, std::list<std::string>());
ASSERT(defs.find("A") != defs.end()); ASSERT(defs.find("A") != defs.end());
ASSERT_EQUALS("(Fred)", defs["A"]); ASSERT_EQUALS("(Fred)", defs["A"]);