From da881fdd0a2138176024c0fd63cce154cbba8935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 11 Jan 2009 15:51:46 +0000 Subject: [PATCH] preprocessor: Fixed bug when expanding macros without parameters --- src/preprocessor.cpp | 46 ++++++++++++++++++++++++--------------- test/testpreprocessor.cpp | 8 +++++++ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index 1b8f01f8a..a74f6e1d4 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -453,7 +453,7 @@ std::string Preprocessor::expandMacros(std::string code) } // Extract the whole macro into a separate variable "macro" and then erase it from "code" - std::string macro(code.substr(defpos + 8, endpos - defpos - 7)); + const std::string macro(code.substr(defpos + 8, endpos - defpos - 7)); code.erase(defpos, endpos - defpos); // Tokenize the macro to make it easier to handle @@ -484,7 +484,7 @@ std::string Preprocessor::expandMacros(std::string code) continue; // The char after the macroname must not be alphanumeric or '_' - if ( pos1 + macroname.length() < code.length() ) + if (pos1 + macroname.length() < code.length()) { std::string::size_type pos2 = pos1 + macroname.length(); if (isalnum(code[pos2]) || code[pos2] == '_') @@ -538,33 +538,45 @@ std::string Preprocessor::expandMacros(std::string code) // Create macro code.. std::string macrocode; - const Token *tok = tokenizer.tokens(); - if (! macroparams.empty()) + if (macroparams.empty()) { + std::string::size_type pos = macro.find(" "); + if (pos == std::string::npos) + macrocode = ""; + else + { + macrocode = macro.substr(pos + 1); + if ((pos = macrocode.find_first_of("\r\n")) != std::string::npos) + macrocode.erase(pos); + } + } + else + { + const Token *tok = tokenizer.tokens(); while (tok && tok->str() != ")") tok = tok->next(); - } - while ((tok = tok->next()) != NULL) - { - std::string str = tok->str(); - if (tok->isName()) + while ((tok = tok->next()) != NULL) { - for (unsigned int i = 0; i < macroparams.size(); ++i) + std::string str = tok->str(); + if (tok->isName()) { - if (str == macroparams[i]) + for (unsigned int i = 0; i < macroparams.size(); ++i) { - str = params[i]; - break; + if (str == macroparams[i]) + { + str = params[i]; + break; + } } } + macrocode += str; + if (Token::Match(tok, "%type% %var%")) + macrocode += " "; } - macrocode += str; - if (Token::Match(tok, "%type% %var%")) - macrocode += " "; } // Insert macro code.. - if ( !macroparams.empty() ) + if (!macroparams.empty()) ++pos2; code.erase(pos1, pos2 - pos1); code.insert(pos1, macrocode); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index cba5912ec..19dcd38f9 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -71,6 +71,7 @@ private: TEST_CASE(macro_simple1); TEST_CASE(macro_simple2); TEST_CASE(macro_simple3); + TEST_CASE(macro_simple4); TEST_CASE(macro_mismatch); TEST_CASE(preprocessor_inside_string); } @@ -450,6 +451,13 @@ private: ASSERT_EQUALS("\n4 AA\n", Preprocessor::expandMacros(filedata)); } + void macro_simple4() + { + const char filedata[] = "#define TEMP_1 if( temp > 0 ) return 1;\n" + "TEMP_1\n"; + ASSERT_EQUALS("\nif( temp > 0 ) return 1;\n", Preprocessor::expandMacros(filedata)); + } + void macro_mismatch() { const char filedata[] = "#define AAA(aa,bb) f(aa)\n"