From 94d220e3707d77f2fe9a636f4a56f5ce4e5ac445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 6 Jan 2012 08:01:50 +0100 Subject: [PATCH] Refactoring: Made Preprocessor::getcode nonstatic --- lib/cppcheck.cpp | 4 +-- lib/preprocessor.cpp | 20 +++++------ lib/preprocessor.h | 2 +- test/testother.cpp | 6 ++-- test/testpreprocessor.cpp | 72 ++++++++++++++++++++++----------------- 5 files changed, 56 insertions(+), 48 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 7ea051637..624df0117 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -222,7 +222,7 @@ unsigned int CppCheck::processFile() cfg = *it; Timer t("Preprocessor::getcode", _settings._showtime, &S_timerResults); - const std::string codeWithoutCfg = Preprocessor::getcode(filedata, *it, _filename, &_settings, &_errorLogger); + const std::string codeWithoutCfg = preprocessor.getcode(filedata, *it, _filename); t.Stop(); // If only errors are printed, print filename after the check @@ -283,7 +283,7 @@ void CppCheck::analyseFile(std::istream &fin, const std::string &filename) std::list configurations; std::string filedata = ""; preprocessor.preprocess(fin, filedata, configurations, filename, _settings._includePaths); - const std::string code = Preprocessor::getcode(filedata, "", filename, &_settings, &_errorLogger); + const std::string code = preprocessor.getcode(filedata, "", filename); if (_settings.checkConfiguration) { return; diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 11f9d231d..c9621f5af 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -443,7 +443,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri // First check for a "fall through" comment match, but only // add a suppression if the next token is 'case' or 'default' - if (_settings->isEnabled("style") && _settings->experimental && fallThroughComment) { + if (_settings && _settings->isEnabled("style") && _settings->experimental && fallThroughComment) { std::string::size_type j = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz", i); std::string tok = str.substr(i, j - i); if (tok == "case" || tok == "default") @@ -668,7 +668,7 @@ void Preprocessor::preprocess(std::istream &istr, std::map::const_iterator it = configs.begin(); it != configs.end(); ++it) { if (_settings && (_settings->userUndefs.find(*it) == _settings->userUndefs.end())) - result[ *it ] = Preprocessor::getcode(data, *it, filename, _settings, _errorLogger); + result[ *it ] = getcode(data, *it, filename); } } @@ -1380,7 +1380,7 @@ bool Preprocessor::match_cfg_def(const std::map &cfg, } -std::string Preprocessor::getcode(const std::string &filedata, const std::string &cfg, const std::string &filename, const Settings *settings, ErrorLogger *errorLogger) +std::string Preprocessor::getcode(const std::string &filedata, const std::string &cfg, const std::string &filename) { // For the error report unsigned int lineno = 0; @@ -1440,7 +1440,7 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string break; if (line.find("=") != std::string::npos) { - Tokenizer tokenizer(settings, NULL); + Tokenizer tokenizer(_settings, NULL); line.erase(0, sizeof("#pragma endasm")); std::istringstream tempIstr(line); tokenizer.tokenize(tempIstr, ""); @@ -1462,9 +1462,9 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string if (line.compare(0, 8, "#define ") == 0) { match = true; - if (settings) { + if (_settings) { typedef std::set::const_iterator It; - for (It it = settings->userUndefs.begin(); it != settings->userUndefs.end(); ++it) { + for (It it = _settings->userUndefs.begin(); it != _settings->userUndefs.end(); ++it) { std::string::size_type pos = line.find_first_not_of(' ',8); if (pos != std::string::npos) { std::string::size_type pos2 = line.find(*it,pos); @@ -1548,9 +1548,9 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string // #error => return "" if (match && line.compare(0, 6, "#error") == 0) { - if (settings && !settings->userDefines.empty()) { - Settings settings2(*settings); - Preprocessor preprocessor(&settings2, errorLogger); + if (_settings && !_settings->userDefines.empty()) { + Settings settings2(*_settings); + Preprocessor preprocessor(&settings2, _errorLogger); preprocessor.error(filenames.top(), lineno, line); } return ""; @@ -1591,7 +1591,7 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string ret << line << "\n"; } - return expandMacros(ret.str(), filename, errorLogger); + return expandMacros(ret.str(), filename, _errorLogger); } void Preprocessor::error(const std::string &filename, unsigned int linenr, const std::string &msg) diff --git a/lib/preprocessor.h b/lib/preprocessor.h index f14d36515..7601c1f07 100644 --- a/lib/preprocessor.h +++ b/lib/preprocessor.h @@ -91,7 +91,7 @@ public: /** * Get preprocessed code for a given configuration */ - static std::string getcode(const std::string &filedata, const std::string &cfg, const std::string &filename, const Settings *settings, ErrorLogger *errorLogger); + std::string getcode(const std::string &filedata, const std::string &cfg, const std::string &filename); /** * simplify condition diff --git a/test/testother.cpp b/test/testother.cpp index 34904cbc3..fafc0578b 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -209,13 +209,13 @@ private: settings.experimental = true; // Preprocess file.. - Preprocessor preprocessor(&settings, this); + SimpleSuppressor logger(settings, this); + Preprocessor preprocessor(&settings, &logger); std::list configurations; std::string filedata = ""; std::istringstream fin(precode); preprocessor.preprocess(fin, filedata, configurations, filename, settings._includePaths); - SimpleSuppressor logger(settings, this); - const std::string code = Preprocessor::getcode(filedata, "", filename, &settings, &logger); + const std::string code = preprocessor.getcode(filedata, "", filename); // Tokenize.. Tokenizer tokenizer(&settings, &logger); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index b38ae8f44..d765c9192 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -656,8 +656,9 @@ private: errout.str(""); Settings settings; settings.userDefines = "__cplusplus"; + Preprocessor preprocessor(&settings, this); const std::string code("#error hello world!\n"); - Preprocessor::getcode(code, "X", "test.c", &settings, this); + preprocessor.getcode(code, "X", "test.c"); ASSERT_EQUALS("[test.c:1]: (error) #error hello world!\n", errout.str()); } @@ -668,8 +669,9 @@ private: errout.str(""); Settings settings; settings.userDefines = "TEST"; + Preprocessor preprocessor(&settings, this); const std::string code("#file \"ab.h\"\n#error hello world!\n#endfile"); - Preprocessor::getcode(code, "TEST", "test.c", &settings, this); + preprocessor.getcode(code, "TEST", "test.c"); ASSERT_EQUALS("[ab.h:1]: (error) #error hello world!\n", errout.str()); } @@ -678,8 +680,9 @@ private: errout.str(""); Settings settings; settings.userDefines = "TEST"; + Preprocessor preprocessor(&settings, this); const std::string code("#file \"ab.h\"\n\n#endfile\n#error aaa"); - Preprocessor::getcode(code, "TEST", "test.c", &settings, this); + preprocessor.getcode(code, "TEST", "test.c"); ASSERT_EQUALS("[test.c:2]: (error) #error aaa\n", errout.str()); } } @@ -1418,14 +1421,16 @@ private: "#if A == 1\n" ";\n" "#endif\n"; - ASSERT_EQUALS("\n\n;\n\n", Preprocessor::getcode(filedata,"","",NULL,NULL)); + Preprocessor preprocessor(NULL, this); + ASSERT_EQUALS("\n\n;\n\n", preprocessor.getcode(filedata,"","")); } void if_cond13() { const char filedata[] = "#if ('A' == 0x41)\n" "123\n" "#endif\n"; - ASSERT_EQUALS("\n123\n\n", Preprocessor::getcode(filedata,"","",NULL,NULL)); + Preprocessor preprocessor(NULL, this); + ASSERT_EQUALS("\n123\n\n", preprocessor.getcode(filedata,"","")); } @@ -1463,8 +1468,9 @@ private: const std::string code("#if X || Y\n" "a1;\n" "#endif\n"); - ASSERT_EQUALS("\na1;\n\n", Preprocessor::getcode(code, "X", "test.c", NULL, NULL)); - ASSERT_EQUALS("\na1;\n\n", Preprocessor::getcode(code, "Y", "test.c", NULL, NULL)); + Preprocessor preprocessor(NULL, this); + ASSERT_EQUALS("\na1;\n\n", preprocessor.getcode(code, "X", "test.c")); + ASSERT_EQUALS("\na1;\n\n", preprocessor.getcode(code, "Y", "test.c")); } @@ -1475,8 +1481,7 @@ private: // Preprocess => actual result.. std::istringstream istr(filedata); - Settings settings; - Preprocessor preprocessor(&settings, this); + Preprocessor preprocessor(NULL, this); ASSERT_EQUALS("#define str \"abc\" \"def\"\n\nabcdef = str;\n", preprocessor.read(istr, "test.c")); } @@ -1487,8 +1492,7 @@ private: // Preprocess => actual result.. std::istringstream istr(filedata); - Settings settings; - Preprocessor preprocessor(&settings, this); + Preprocessor preprocessor(NULL, this); ASSERT_EQUALS("#define sqr(aa) aa * aa\n\nsqr(5);\n", preprocessor.read(istr, "test.c")); } @@ -1499,8 +1503,7 @@ private: // Preprocess => actual result.. std::istringstream istr(filedata); - Settings settings; - Preprocessor preprocessor(&settings, this); + Preprocessor preprocessor(NULL, this); ASSERT_EQUALS("const char *str = \"abcdefghi\"\n\n\n", preprocessor.read(istr, "test.c")); } @@ -1511,9 +1514,9 @@ private: "A\n"; // Preprocess => actual result.. + Settings settings; std::istringstream istr(filedata); std::map actual; - Settings settings; Preprocessor preprocessor(&settings, this); preprocessor.preprocess(istr, actual, "file.c"); @@ -1913,7 +1916,8 @@ private: "int z;\n" "z = 0;\n"; - ASSERT_EQUALS("\n\nint z;\nz = 0;\n", OurPreprocessor::getcode(filedata, "", "", NULL, NULL)); + Preprocessor preprocessor(NULL, this); + ASSERT_EQUALS("\n\nint z;\nz = 0;\n", preprocessor.getcode(filedata, "", "")); } } @@ -2460,19 +2464,21 @@ private: } void define_if1() { + Preprocessor preprocessor(NULL, this); + { const char filedata[] = "#define A 0\n" "#if A\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\n\n\n", Preprocessor::getcode(filedata,"","",NULL,NULL)); + ASSERT_EQUALS("\n\n\n\n", preprocessor.getcode(filedata,"","")); } { const char filedata[] = "#define A 1\n" "#if A==1\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\nFOO\n\n", Preprocessor::getcode(filedata,"","",NULL,NULL)); + ASSERT_EQUALS("\n\nFOO\n\n", preprocessor.getcode(filedata,"","")); } } @@ -2482,7 +2488,8 @@ private: "#if (B==A) || (B==C)\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\n\nFOO\n\n", Preprocessor::getcode(filedata,"","",NULL,NULL)); + Preprocessor preprocessor(NULL, this); + ASSERT_EQUALS("\n\n\nFOO\n\n", preprocessor.getcode(filedata,"","")); } void define_if3() { @@ -2490,7 +2497,8 @@ private: "#if (A==0)\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\nFOO\n\n", Preprocessor::getcode(filedata,"","",NULL,NULL)); + Preprocessor preprocessor(NULL, this); + ASSERT_EQUALS("\n\nFOO\n\n", preprocessor.getcode(filedata,"","")); } void define_ifdef() { @@ -2617,8 +2625,9 @@ private: "B me;\n"; // Preprocess => actual result.. - ASSERT_EQUALS("\n\n\n\n\n\n$int me;\n", Preprocessor::getcode(filedata, "", "a.cpp", NULL, NULL)); - ASSERT_EQUALS("\n\n\n\n\n\n$char me;\n", Preprocessor::getcode(filedata, "A", "a.cpp", NULL, NULL)); + Preprocessor preprocessor(NULL, this); + ASSERT_EQUALS("\n\n\n\n\n\n$int me;\n", preprocessor.getcode(filedata, "", "a.cpp")); + ASSERT_EQUALS("\n\n\n\n\n\n$char me;\n", preprocessor.getcode(filedata, "A", "a.cpp")); } void redundant_config() { @@ -2803,30 +2812,29 @@ private: } void predefine1() { - Settings settings; - const std::string src("#ifdef X || Y\n" "Fred & Wilma\n" "#endif\n"); - std::string actual = Preprocessor::getcode(src, "X=1", "test.c", &settings, this); + Preprocessor preprocessor(NULL, this); + std::string actual = preprocessor.getcode(src, "X=1", "test.c"); ASSERT_EQUALS("\nFred & Wilma\n\n", actual); } void predefine2() { - Settings settings; - const std::string src("#ifdef X && Y\n" "Fred & Wilma\n" "#endif\n"); { - std::string actual = Preprocessor::getcode(src, "X=1", "test.c", &settings, this); + Preprocessor preprocessor(NULL, this); + std::string actual = preprocessor.getcode(src, "X=1", "test.c"); ASSERT_EQUALS("\n\n\n", actual); } { - std::string actual = Preprocessor::getcode(src, "X=1;Y=2", "test.c", &settings, this); + Preprocessor preprocessor(NULL, this); + std::string actual = preprocessor.getcode(src, "X=1;Y=2", "test.c"); ASSERT_EQUALS("\nFred & Wilma\n\n", actual); } } @@ -2838,8 +2846,8 @@ private: "#if (X == Y)\n" "Fred & Wilma\n" "#endif\n"; - const Settings settings; - const std::string actual = Preprocessor::getcode(code, "TEST", "test.c", &settings, this); + Preprocessor preprocessor(NULL,this); + const std::string actual = preprocessor.getcode(code, "TEST", "test.c"); ASSERT_EQUALS("\n\n\nFred & Wilma\n\n", actual); } @@ -2855,8 +2863,8 @@ private: void invalidElIf() { // #2942 - segfault const char code[] = "#elif (){\n"; - const Settings settings; - const std::string actual = Preprocessor::getcode(code, "TEST", "test.c", &settings, this); + Preprocessor preprocessor(NULL,this); + const std::string actual = preprocessor.getcode(code, "TEST", "test.c"); ASSERT_EQUALS("\n", actual); }