From 6302b5307597a29effa71e7e8551353742683eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 6 Jan 2009 09:43:26 +0000 Subject: [PATCH] Preprocessor: Refactoring the unit testing. And enabled the macro expansion --- preprocessor.cpp | 86 ++++++++++++++++----------- testpreprocessor.cpp | 137 ++++++++++++------------------------------- 2 files changed, 91 insertions(+), 132 deletions(-) diff --git a/preprocessor.cpp b/preprocessor.cpp index 44db33eab..78cd3597f 100644 --- a/preprocessor.cpp +++ b/preprocessor.cpp @@ -224,6 +224,8 @@ void Preprocessor::preprocess(std::istream &istr, std::string &processedFile, st processedFile = replaceIfDefined(processedFile); + processedFile = expandMacros(processedFile); + // Get all possible configurations.. resultConfigurations = getcfgs(processedFile); } @@ -445,53 +447,66 @@ std::string Preprocessor::expandMacros(std::string code) std::istringstream istr(macro.c_str()); tokenizer.tokenize(istr, ""); - if (! Token::Match(tokenizer.tokens(), "%var% ( %var%")) - continue; + // Extract macro parameters std::vector macroparams; - for (const Token *tok = tokenizer.tokens()->tokAt(2); tok; tok = tok->next()) + if (Token::Match(tokenizer.tokens(), "%var% ( %var%")) { - if (tok->str() == ")") - break; - if (tok->isName()) - macroparams.push_back(tok->str()); + for (const Token *tok = tokenizer.tokens()->tokAt(2); tok; tok = tok->next()) + { + if (tok->str() == ")") + break; + if (tok->isName()) + macroparams.push_back(tok->str()); + } } + + // Expand all macros in the code.. const std::string macroname(tokenizer.tokens()->str()); std::string::size_type pos1 = defpos; - while ((pos1 = code.find(macroname + "(", pos1 + 1)) != std::string::npos) + while ((pos1 = code.find(macroname, pos1 + 1)) != std::string::npos) { // Previous char must not be alphanumeric or '_' if (pos1 != 0 && (std::isalnum(code[pos1-1]) || code[pos1-1] == '_')) continue; + std::vector params; - int parlevel = 0; - std::string par; - std::string::size_type pos2; - for (pos2 = pos1; pos2 < code.length(); ++pos2) + std::string::size_type pos2 = pos1 + macroname.length(); + if (pos2 >= macro.length()) + continue; + if (macroparams.size()) { - if (code[pos2] == '(') + if (code[pos2] != '(') + continue; + + int parlevel = 0; + std::string par; + for (; pos2 < code.length(); ++pos2) { - ++parlevel; - if (parlevel == 1) - continue; - } - else if (code[pos2] == ')') - { - --parlevel; - if (parlevel <= 0) + if (code[pos2] == '(') + { + ++parlevel; + if (parlevel == 1) + continue; + } + else if (code[pos2] == ')') + { + --parlevel; + if (parlevel <= 0) + { + params.push_back(par); + break; + } + } + + if (parlevel == 1 && code[pos2] == ',') { params.push_back(par); - break; + par = ""; + } + else if (parlevel >= 1) + { + par += std::string(1, code[pos2]); } - } - - if (parlevel == 1 && code[pos2] == ',') - { - params.push_back(par); - par = ""; - } - else if (parlevel >= 1) - { - par += std::string(1, code[pos2]); } } @@ -502,8 +517,11 @@ std::string Preprocessor::expandMacros(std::string code) // Create macro code.. std::string macrocode; const Token *tok = tokenizer.tokens(); - while (tok && tok->str() != ")") - tok = tok->next(); + if (! macroparams.empty()) + { + while (tok && tok->str() != ")") + tok = tok->next(); + } while ((tok = tok->next()) != NULL) { std::string str = tok->str(); diff --git a/testpreprocessor.cpp b/testpreprocessor.cpp index 9d656c0bf..93c125192 100644 --- a/testpreprocessor.cpp +++ b/testpreprocessor.cpp @@ -84,33 +84,6 @@ private: } - bool cmpmaps(const std::map &m1, const std::map &m2) - { - // Begin by checking the sizes - if (m1.size() != m2.size()) - return false; - - // Check each item in the maps.. - for (std::map::const_iterator it1 = m1.begin(); it1 != m1.end(); ++it1) - { - std::string s1 = it1->first; - std::map::const_iterator it2 = m2.find(s1); - if (it2 == m2.end()) - return false; - else - { - std::string s1 = it1->second; - std::string s2 = it2->second; - if (s1 != s2) - return false; - } - } - - // No diffs were found - return true; - } - - void Bug2190219() { const char filedata[] = "int main()\n" @@ -167,7 +140,9 @@ private: preprocessor.preprocess(istr, actual); // Compare results.. - ASSERT_EQUALS(true, cmpmaps(actual, expected)); + ASSERT_EQUALS(expected[""], actual[""]); + ASSERT_EQUALS(expected["__cplusplus"], actual["__cplusplus"]); + ASSERT_EQUALS(2, actual.size()); } @@ -179,11 +154,6 @@ private: " qwerty\n" "#endif \n"; - // Expected result.. - std::map expected; - expected[""] = "\n\n\nqwerty\n\n"; - expected["WIN32"] = "\nabcdef\n\n\n\n"; - // Preprocess => actual result.. std::istringstream istr(filedata); std::map actual; @@ -191,7 +161,9 @@ private: preprocessor.preprocess(istr, actual); // Compare results.. - ASSERT_EQUALS(true, cmpmaps(actual, expected)); + ASSERT_EQUALS("\n\n\nqwerty\n\n", actual[""]); + ASSERT_EQUALS("\nabcdef\n\n\n\n", actual["WIN32"]); + ASSERT_EQUALS(2, actual.size()); } void test2() @@ -202,11 +174,6 @@ private: " qwerty\n" " # endif \n"; - // Expected result.. - std::map expected; - expected["WIN32"] = "\n\n\nqwerty\n\n"; - expected[""] = "\n\" # ifdef WIN32\"\n\n\n\n"; - // Preprocess => actual result.. std::istringstream istr(filedata); std::map actual; @@ -214,7 +181,9 @@ private: preprocessor.preprocess(istr, actual); // Compare results.. - ASSERT_EQUALS(true, cmpmaps(actual, expected)); + ASSERT_EQUALS("\n\" # ifdef WIN32\"\n\n\n\n", actual[""]); + ASSERT_EQUALS("\n\n\nqwerty\n\n", actual["WIN32"]); + ASSERT_EQUALS(2, actual.size()); } void test3() @@ -227,12 +196,6 @@ private: "c\n" "#endif\n"; - // Expected result.. - std::map expected; - expected[""] = "\n\n\n\n\n\n\n"; - expected["ABC"] = "\na\n\n\n\nc\n\n"; - expected["ABC;DEF"] = "\na\n\nb\n\nc\n\n"; - // Preprocess => actual result.. std::istringstream istr(filedata); std::map actual; @@ -240,7 +203,10 @@ private: preprocessor.preprocess(istr, actual); // Compare results.. - ASSERT_EQUALS(true, cmpmaps(actual, expected)); + ASSERT_EQUALS("\n\n\n\n\n\n\n", actual[""]); + ASSERT_EQUALS("\na\n\n\n\nc\n\n", actual["ABC"]); + ASSERT_EQUALS("\na\n\nb\n\nc\n\n", actual["ABC;DEF"]); + ASSERT_EQUALS(3, actual.size()); } void test4() @@ -252,11 +218,6 @@ private: "A\n" "#endif\n"; - // Expected result.. - std::map expected; - expected[""] = "\n\n\n\n\n\n"; - expected["ABC"] = "\nA\n\n\nA\n\n"; - // Preprocess => actual result.. std::istringstream istr(filedata); std::map actual; @@ -264,7 +225,9 @@ private: preprocessor.preprocess(istr, actual); // Compare results.. - ASSERT_EQUALS(true, cmpmaps(actual, expected)); + ASSERT_EQUALS("\n\n\n\n\n\n", actual[""]); + ASSERT_EQUALS("\nA\n\n\nA\n\n", actual["ABC"]); + ASSERT_EQUALS(2, actual.size()); } void test5() @@ -278,12 +241,6 @@ private: "#endif\n" "#endif\n"; - // Expected result.. - std::map expected; - expected[""] = "\n\n\nB\n\n\n\n\n"; - expected["ABC"] = "\nA\n\n\n\n\n\n\n"; - expected["DEF"] = "\n\n\nB\n\nC\n\n\n"; - // Preprocess => actual result.. std::istringstream istr(filedata); std::map actual; @@ -291,7 +248,10 @@ private: preprocessor.preprocess(istr, actual); // Compare results.. - ASSERT_EQUALS(true, cmpmaps(actual, expected)); + ASSERT_EQUALS("\n\n\nB\n\n\n\n\n", actual[""]); + ASSERT_EQUALS("\nA\n\n\n\n\n\n\n", actual["ABC"]); + ASSERT_EQUALS("\n\n\nB\n\nC\n\n\n", actual["DEF"]); + ASSERT_EQUALS(3, actual.size()); } @@ -303,10 +263,6 @@ private: "#endif\n" "*/\n"; - // Expected result.. - std::map expected; - expected[""] = "\n\n\n\n"; - // Preprocess => actual result.. std::istringstream istr(filedata); std::map actual; @@ -314,7 +270,8 @@ private: preprocessor.preprocess(istr, actual); // Compare results.. - ASSERT_EQUALS(true, cmpmaps(actual, expected)); + ASSERT_EQUALS("\n\n\n\n", actual[""]); + ASSERT_EQUALS(1, actual.size()); } @@ -326,10 +283,6 @@ private: "#endif\n" "#endif\n"; - // Expected result.. - std::map expected; - expected[""] = "\n\n\n\n"; - // Preprocess => actual result.. std::istringstream istr(filedata); std::map actual; @@ -337,7 +290,8 @@ private: preprocessor.preprocess(istr, actual); // Compare results.. - ASSERT_EQUALS(true, cmpmaps(actual, expected)); + ASSERT_EQUALS("\n\n\n\n", actual[""]); + ASSERT_EQUALS(1, actual.size()); } void if1() @@ -346,10 +300,6 @@ private: "ABC\n" " # endif \n"; - // Expected result.. - std::map expected; - expected[""] = "\nABC\n\n"; - // Preprocess => actual result.. std::istringstream istr(filedata); std::map actual; @@ -357,7 +307,8 @@ private: preprocessor.preprocess(istr, actual); // Compare results.. - ASSERT_EQUALS(true, cmpmaps(actual, expected)); + ASSERT_EQUALS("\nABC\n\n", actual[""]); + ASSERT_EQUALS(1, actual.size()); } @@ -369,12 +320,6 @@ private: "DEF\n" "#endif\n"; - // Expected result.. - std::map expected; - expected[""] = "\n\n\n\n\n"; - expected["DEF1"] = "\nABC\n\n\n\n"; - expected["DEF2"] = "\n\n\nDEF\n\n"; - // Preprocess => actual result.. std::istringstream istr(filedata); std::map actual; @@ -382,7 +327,10 @@ private: preprocessor.preprocess(istr, actual); // Compare results.. - ASSERT_EQUALS(true, cmpmaps(actual, expected)); + ASSERT_EQUALS("\n\n\n\n\n", actual[""]); + ASSERT_EQUALS("\nABC\n\n\n\n", actual["DEF1"]); + ASSERT_EQUALS("\n\n\nDEF\n\n", actual["DEF2"]); + ASSERT_EQUALS(3, actual.size()); } @@ -391,10 +339,6 @@ private: { const char filedata[] = " # include \"abcd.h\" // abcd\n"; - // Expected result.. - std::map expected; - expected[""] = "#include \"abcd.h\"\n"; - // Preprocess => actual result.. std::istringstream istr(filedata); std::map actual; @@ -402,7 +346,8 @@ private: preprocessor.preprocess(istr, actual); // Compare results.. - ASSERT_EQUALS(true, cmpmaps(actual, expected)); + ASSERT_EQUALS("#include \"abcd.h\"\n", actual[""]); + ASSERT_EQUALS(1, actual.size()); } @@ -416,10 +361,6 @@ private: " B\n" "#endif\n"; - std::map expected; - expected[""] = "\n\n\nB\n\n"; - expected["LIBVER>100"] = "\nA\n\n\n\n"; - // Preprocess => actual result.. std::istringstream istr(filedata); std::map actual; @@ -427,18 +368,17 @@ private: preprocessor.preprocess(istr, actual); // Compare results.. - ASSERT_EQUALS(true, cmpmaps(actual, expected)); + ASSERT_EQUALS("\n\n\nB\n\n", actual[""]); + ASSERT_EQUALS("\nA\n\n\n\n", actual["LIBVER>100"]); + ASSERT_EQUALS(2, actual.size()); } void multiline() { const char filedata[] = "#define str \"abc\" \\ \n" - " \"def\" \\ \n" - " \"ghi\" \n"; - - std::map expected; - expected[""] = "#define str \"abc\" \"def\" \"ghi\"\n\n\n"; + " \"def\" \n" + "abcdef = str;\n"; // Preprocess => actual result.. std::istringstream istr(filedata); @@ -447,7 +387,8 @@ private: preprocessor.preprocess(istr, actual); // Compare results.. - ASSERT_EQUALS(true, cmpmaps(actual, expected)); + ASSERT_EQUALS("\n\nabcdef = \"abc\"\"def\"\n", actual[""]); + ASSERT_EQUALS(1, actual.size()); }