diff --git a/preprocessor.cpp b/preprocessor.cpp index fccd362a3..560a06a5f 100644 --- a/preprocessor.cpp +++ b/preprocessor.cpp @@ -173,6 +173,27 @@ std::string Preprocessor::removeSpaceNearNL( const std::string &str ) return tmp; } +std::string Preprocessor::replaceIfDefined( const std::string &str ) +{ + std::string ret(str); + std::string::size_type pos = 0; + while ((pos = ret.find("#if defined(", pos)) != std::string::npos) + { + std::string::size_type pos2 = ret.find(")", pos+9); + if (pos2 > ret.length() - 1) + break; + if (ret[pos2+1] == '\n') + { + ret.erase( pos2, 1 ); + ret.erase( pos + 3, 9 ); + ret.insert( pos + 3, "def " ); + } + ++pos; + } + + return ret; +} + void Preprocessor::preprocess(std::istream &istr, std::string &processedFile, std::list &resultConfigurations) { processedFile = read(istr); @@ -198,6 +219,8 @@ void Preprocessor::preprocess(std::istream &istr, std::string &processedFile, st processedFile.insert( loc, "\n" ); } + processedFile = replaceIfDefined(processedFile); + // Get all possible configurations.. resultConfigurations = getcfgs( processedFile ); } diff --git a/preprocessor.h b/preprocessor.h index 6cf381414..be2cde3de 100644 --- a/preprocessor.h +++ b/preprocessor.h @@ -60,7 +60,9 @@ public: */ static std::string getcode(const std::string &filedata, std::string cfg); +#ifndef UNIT_TESTING private: +#endif /** * Remove space that has new line character on left or right side of it. @@ -70,6 +72,14 @@ private: */ static std::string removeSpaceNearNL( const std::string &str ); + /** + * Replace "#if defined" with "#ifdef" where possible + * + * @param str The string to be converted + * @return The replaced string + */ + static std::string replaceIfDefined( const std::string &str ); + /** * Get all possible configurations. By looking at the ifdefs and ifndefs in filedata */ diff --git a/testpreprocessor.cpp b/testpreprocessor.cpp index 2932f99bc..c512bbfa8 100644 --- a/testpreprocessor.cpp +++ b/testpreprocessor.cpp @@ -22,6 +22,7 @@ #include "testsuite.h" +#define UNIT_TESTING #include "preprocessor.h" #include @@ -61,6 +62,8 @@ private: TEST_CASE( if_cond1 ); TEST_CASE( multiline ); + + TEST_CASE( if_defined ); // "#if defined(AAA)" => "#ifdef AAA" } @@ -441,6 +444,20 @@ private: ASSERT_EQUALS( true, cmpmaps(actual, expected)); } + + void if_defined() + { + const char filedata[] = "#if defined(AAA)\n" + "#endif\n"; + + // Expected result.. + std::string expected( "#ifdef AAA\n#endif\n" ); + + // Compare result.. + ASSERT_EQUALS( expected, Preprocessor::replaceIfDefined(filedata) ); + } + + }; REGISTER_TEST( TestPreprocessor )