preprocessor: Refactoring

This commit is contained in:
Daniel Marjamäki 2008-10-27 07:47:48 +00:00
parent ccab3e3bb4
commit 5e25e706ea
3 changed files with 160 additions and 119 deletions

View File

@ -1,4 +1,4 @@
/* /*
* c++check - c/c++ syntax checking * c++check - c/c++ syntax checking
* Copyright (C) 2007 Daniel Marjamäki * Copyright (C) 2007 Daniel Marjamäki
* *
@ -125,6 +125,42 @@ 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)
{
// Replace tabs with spaces..
while ( line.find("\t") != std::string::npos )
line[ line.find("\t") ] = 0;
// Trim off any leading spaces..
while (line[0] == ' ')
line.erase(0, 1);
// If def is true, the line must start with "#ifdef"
if ( def && line.find("#ifdef ") != 0 )
{
return "";
}
// If def is false, the line must start with "#ifndef"
if ( !def && line.find("#ifndef ") != 0 )
{
return "";
}
// Remove the "#ifdef" or "#ifndef"
line.erase( 0, line.find(" ") );
// Remove all spaces.
while ( line.find(" ") != std::string::npos )
line.erase( line.find(" "), 1 );
// The remaining string is our result.
return line;
}
static std::list<std::string> getcfgs( const std::string &filedata ) static std::list<std::string> getcfgs( const std::string &filedata )
{ {
std::list<std::string> ret; std::list<std::string> ret;
@ -134,9 +170,9 @@ static std::list<std::string> getcfgs( const std::string &filedata )
std::string line; std::string line;
while ( getline(istr, line) ) while ( getline(istr, line) )
{ {
if ( line.find("#ifdef ")==0 || line.find("#ifndef ")==0 ) std::string def = getdef(line, true) + getdef(line, false);
if (!def.empty())
{ {
std::string def( line.substr(line.find(" ") + 1) );
if (std::find(ret.begin(), ret.end(), def) == ret.end()) if (std::find(ret.begin(), ret.end(), def) == ret.end())
ret.push_back( def ); ret.push_back( def );
} }
@ -158,11 +194,14 @@ static std::string getcode(const std::string &filedata, std::string cfg)
std::string line; std::string line;
while ( getline(istr, line) ) while ( getline(istr, line) )
{ {
if ( line.find("#ifdef ") == 0 ) std::string def = getdef( line, true );
matching_ifdef.push_back( !cfg.empty() && line.find(cfg) != std::string::npos ); std::string ndef = getdef( line, false );
else if ( line.find("#ifndef ") == 0 ) if ( ! def.empty() )
matching_ifdef.push_back( cfg.empty() || line.find(cfg) == std::string::npos ); matching_ifdef.push_back( cfg == def );
else if ( ! ndef.empty() )
matching_ifdef.push_back( cfg != ndef );
else if ( line.find("#else") == 0) else if ( line.find("#else") == 0)
matching_ifdef.back() = ! matching_ifdef.back(); matching_ifdef.back() = ! matching_ifdef.back();

View File

@ -1,4 +1,4 @@
/* /*
* c++check - c/c++ syntax checking * c++check - c/c++ syntax checking
* Copyright (C) 2007 Daniel Marjamäki * Copyright (C) 2007 Daniel Marjamäki
* *

View File

@ -1,4 +1,4 @@
/* /*
* c++check - c/c++ syntax checking * c++check - c/c++ syntax checking
* Copyright (C) 2007 Daniel Marjamäki * Copyright (C) 2007 Daniel Marjamäki
* *
@ -66,7 +66,7 @@ private:
void test1() void test1()
{ {
const char filedata[] = "#ifdef WIN32\n" const char filedata[] = "#ifdef WIN32 \n"
" abcdef\n" " abcdef\n"
"#else\n" "#else\n"
" qwerty\n" " qwerty\n"
@ -94,6 +94,8 @@ private:
check( filedata, expected ); check( filedata, expected );
} }
void comments1() void comments1()
{ {
const char filedata[] = "/*\n" const char filedata[] = "/*\n"