diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 7e4717564..a2fa46d3d 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1701,11 +1701,14 @@ static bool openHeader(std::string &filename, const std::list &incl } -std::string Preprocessor::handleIncludes(const std::string &code, const std::string &filePath, const std::list &includePaths, std::map &defs) +std::string Preprocessor::handleIncludes(const std::string &code, const std::string &filePath, const std::list &includePaths, std::map &defs) { const std::string path(filePath.substr(0, 1 + filePath.find_last_of("\\/"))); + // current #if indent level. unsigned int indent = 0; + + // how deep does the #if match? this can never be bigger than "indent". unsigned int indentmatch = 0; std::ostringstream ostr; @@ -1739,6 +1742,10 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str if (indent == indentmatch && defs.find(getdef(line,false)) == defs.end()) indentmatch++; ++indent; + } else if (line.compare(0,4,"#if ") == 0) { + if (indent == indentmatch && match_cfg_def(defs, line.substr(4))) + indentmatch++; + ++indent; } else if (line.compare(0,5,"#else") == 0) { if (indentmatch == indent) indentmatch = indent - 1; @@ -1752,16 +1759,12 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str if (line.compare(0,8,"#define ")==0) { // no value if (line.find_first_of("( ", 8) == std::string::npos) - defs[line.substr(8)] = 1; + defs[line.substr(8)] = ""; // define value else if (line.find("(") == std::string::npos) { const std::string::size_type pos = line.find(" ", 8); - const std::string val(line.substr(pos + 1)); - int i; - std::istringstream istr2(val); - istr2 >> i; - defs[line.substr(8,pos-8)] = i; + defs[line.substr(8,pos-8)] = line.substr(pos+1); } } diff --git a/lib/preprocessor.h b/lib/preprocessor.h index 34b16a24b..a89d21552 100644 --- a/lib/preprocessor.h +++ b/lib/preprocessor.h @@ -217,7 +217,7 @@ public: * @param defs defines (only values) * \return resulting string */ - std::string handleIncludes(const std::string &code, const std::string &filePath, const std::list &includePaths, std::map &defs); + std::string handleIncludes(const std::string &code, const std::string &filePath, const std::list &includePaths, std::map &defs); private: void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, bool userheader); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 3aa8323db..c05f7c0fc 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -2827,13 +2827,13 @@ private: void handleIncludes_def() { const std::string filePath("test.c"); const std::list includePaths; - std::map defs; + std::map defs; Preprocessor preprocessor; // ifdef { defs.clear(); - defs["A"] = 1; + defs["A"] = ""; { const std::string code("#ifdef A\n123\n#endif\n"); const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); @@ -2848,7 +2848,7 @@ private: // ifndef { defs.clear(); - defs["A"] = 1; + defs["A"] = ""; { const std::string code("#ifndef A\n123\n#endif\n"); const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); @@ -2868,15 +2868,17 @@ private: const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); ASSERT_EQUALS("\n\n123\n\n" "\n\n\n\n", actual); } - /* - // define X 123 - #if - { - defs.clear(); - const std::string code("#define X 123\n#if X==123\n456\n#endif\n"); - const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); - ASSERT_EQUALS("\n\n456\n\n", actual); - } - */ + + // define X 123 - #if + { + defs.clear(); + const std::string code("#define X 123\n" + "#if X==123\n" + "456\n" + "#endif\n"); + const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); + ASSERT_EQUALS("\n\n456\n\n", actual); + } } };