preprocessor: Added tests test3, test4, test5 and if1

This commit is contained in:
Daniel Marjamäki 2008-10-29 17:20:56 +00:00
parent 0ffa0b13d8
commit 18efbaf433
2 changed files with 150 additions and 40 deletions

View File

@ -139,41 +139,6 @@ void preprocess(std::istream &istr, std::map<std::string, std::string> &result)
while ( codestr.find(" \n") != std::string::npos )
codestr.erase( codestr.find(" \n"), 1 );
// Remove "#if 0" blocks..
if ( codestr.find("#if 0") != std::string::npos )
{
code.str("");
std::string line;
std::istringstream istr(codestr.c_str());
while (getline(istr,line))
{
if ( line == "#if 0" )
{
code << "\n";
unsigned int iflevel = 1;
while ( getline(istr, line) )
{
code << "\n";
if ( line.find("#if") != std::string::npos )
++iflevel;
else if ( line.find("#endif") != std::string::npos )
{
--iflevel;
if ( iflevel == 0 )
break;
}
}
}
else
{
code << line << "\n";
}
}
codestr = code.str();
}
// Get all possible configurations..
std::list<std::string> cfgs = getcfgs( codestr );
@ -190,6 +155,11 @@ void preprocess(std::istream &istr, std::map<std::string, std::string> &result)
// Get the DEF in this line: "#ifdef DEF"
static std::string getdef(std::string line, bool def)
{
if ( def && line.find("#if 0") == 0 )
return "0";
if ( def && line.find("#if 1") == 0 )
return "1";
// If def is true, the line must start with "#ifdef"
if ( def && line.find("#ifdef ") != 0 )
{
@ -220,6 +190,8 @@ static std::list<std::string> getcfgs( const std::string &filedata )
std::list<std::string> ret;
ret.push_back("");
std::list<std::string> deflist;
std::istringstream istr(filedata);
std::string line;
while ( getline(istr, line) )
@ -227,9 +199,25 @@ static std::list<std::string> getcfgs( const std::string &filedata )
std::string def = getdef(line, true) + getdef(line, false);
if (!def.empty())
{
deflist.push_back(def);
def = "";
for ( std::list<std::string>::const_iterator it = deflist.begin(); it != deflist.end(); ++it)
{
if ( *it == "0" )
break;
if ( *it == "1" )
continue;
if ( ! def.empty() )
def += ";";
def += *it;
}
if (std::find(ret.begin(), ret.end(), def) == ret.end())
ret.push_back( def );
}
if ( line.find("#endif") == 0 || line.find("#else") == 0 )
deflist.pop_back();
}
return ret;
@ -237,6 +225,30 @@ static std::list<std::string> getcfgs( const std::string &filedata )
static bool match_cfg_def( std::string cfg, const std::string &def )
{
if ( def == "0" )
return false;
if ( def == "1" )
return true;
if ( cfg.empty() )
return false;
while ( ! cfg.empty() )
{
if ( cfg.find(";") == std::string::npos )
return bool(cfg == def);
std::string _cfg = cfg.substr( 0, cfg.find(";") );
if ( _cfg == def )
return true;
cfg.erase( 0, cfg.find(";") + 1 );
}
return false;
}
static std::string getcode(const std::string &filedata, std::string cfg)
{
@ -252,10 +264,10 @@ static std::string getcode(const std::string &filedata, std::string cfg)
std::string ndef = getdef( line, false );
if ( ! def.empty() )
matching_ifdef.push_back( cfg == def );
matching_ifdef.push_back( match_cfg_def(cfg, def) );
else if ( ! ndef.empty() )
matching_ifdef.push_back( cfg != ndef );
matching_ifdef.push_back( ! match_cfg_def(cfg, ndef) );
else if ( line == "#else" )
matching_ifdef.back() = ! matching_ifdef.back();

View File

@ -1,4 +1,4 @@
/*
/*
* c++check - c/c++ syntax checking
* Copyright (C) 2007 Daniel Marjamäki
*
@ -39,10 +39,14 @@ private:
{
TEST_CASE( test1 );
TEST_CASE( test2 );
TEST_CASE( test3 );
TEST_CASE( test4 );
TEST_CASE( test5 );
TEST_CASE( comments1 );
TEST_CASE( if0 );
TEST_CASE( if1 );
TEST_CASE( include1 );
}
@ -56,7 +60,8 @@ private:
// Check each item in the maps..
for ( std::map<std::string,std::string>::const_iterator it1 = m1.begin(); it1 != m1.end(); ++it1 )
{
std::map<std::string,std::string>::const_iterator it2 = m2.find(it1->first);
std::string s1 = it1->first;
std::map<std::string,std::string>::const_iterator it2 = m2.find(s1);
if ( it2 == m2.end() )
return false;
else
@ -105,8 +110,82 @@ private:
// Expected result..
std::map<std::string, std::string> expected;
expected[""] = "\n\" # ifdef WIN32\"\n\n\n\n";
expected["WIN32"] = "\n\n\nqwerty\n\n";
expected[""] = "\n\" # ifdef WIN32\"\n\n\n\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
preprocess( istr, actual );
// Compare results..
ASSERT_EQUALS( true, cmpmaps(actual, expected));
}
void test3()
{
const char filedata[] = "#ifdef ABC\n"
"a\n"
"#ifdef DEF\n"
"b\n"
"#endif\n"
"c\n"
"#endif\n";
// Expected result..
std::map<std::string, std::string> 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<std::string, std::string> actual;
preprocess( istr, actual );
// Compare results..
ASSERT_EQUALS( true, cmpmaps(actual, expected));
}
void test4()
{
const char filedata[] = "#ifdef ABC\n"
"A\n"
"#endif\n"
"#ifdef ABC\n"
"A\n"
"#endif\n";
// Expected result..
std::map<std::string, std::string> 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<std::string, std::string> actual;
preprocess( istr, actual );
// Compare results..
ASSERT_EQUALS( true, cmpmaps(actual, expected));
}
void test5()
{
const char filedata[] = "#ifdef ABC\n"
"A\n"
"#else\n"
"B\n"
"#ifdef DEF\n"
"C\n"
"#endif\n"
"#endif\n";
// Expected result..
std::map<std::string, std::string> 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);
@ -161,6 +240,25 @@ private:
ASSERT_EQUALS( true, cmpmaps(actual, expected));
}
void if1()
{
const char filedata[] = " # if /* comment */ 1 // comment\n"
"ABC\n"
" # endif \n";
// Expected result..
std::map<std::string, std::string> expected;
expected[""] = "\nABC\n\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
preprocess( istr, actual );
// Compare results..
ASSERT_EQUALS( true, cmpmaps(actual, expected));
}
void include1()