Preprocessor : Replace "#if defined(.." with "ifdef .." where possible

This commit is contained in:
Daniel Marjamäki 2009-01-02 18:27:50 +00:00
parent 0e219ec24b
commit 80e35e7332
3 changed files with 50 additions and 0 deletions

View File

@ -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<std::string> &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 );
}

View File

@ -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
*/

View File

@ -22,6 +22,7 @@
#include "testsuite.h"
#define UNIT_TESTING
#include "preprocessor.h"
#include <map>
@ -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 )