Skip to content

Commit

Permalink
preprocessor: simple optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Marjamäki committed Nov 3, 2008
1 parent f25de18 commit c5b26d1
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions preprocessor.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* c++check - c/c++ syntax checking
* Copyright (C) 2007 Daniel Marjamäki
*
Expand Down Expand Up @@ -143,24 +143,25 @@ void preprocess(std::istream &istr, std::map<std::string, std::string> &result,
std::string codestr( code.str() );

// Remove all indentation..
while ( ! codestr.empty() && codestr[0] == ' ' )
codestr.erase(0, 1);
while ( codestr.find("\n ") != std::string::npos )
codestr.erase( 1 + codestr.find("\n "), 1 );
if ( !codestr.empty() && codestr[0] == ' ' )
codestr.erase( 0, codestr.find_first_not_of(" ") );
std::string::size_type loc = 0;
while ( (loc = codestr.find("\n ", loc)) != std::string::npos )
codestr.erase( 1 + loc, 1 );

// Remove all trailing spaces..
while ( codestr.find(" \n") != std::string::npos )
codestr.erase( codestr.find(" \n"), 1 );
loc = 0;
while ( (loc = codestr.find(" \n", loc)) != std::string::npos )
codestr.erase( loc, 1 );

// Using the backslash at the end of a line..
while ( codestr.find("\\\n") != std::string::npos )
while ( (loc = codestr.rfind("\\\n")) != std::string::npos )
{
std::string::size_type pos = codestr.rfind("\\\n");
codestr.erase(pos,2);
if (pos > 0 && codestr[pos-1] != ' ')
codestr.insert(pos, " ");
if ( codestr.find("\n", pos) != std::string::npos)
codestr.insert( codestr.find("\n", pos), "\n" );
codestr.erase(loc, 2);
if (loc > 0 && codestr[loc-1] != ' ')
codestr.insert(loc, " ");
if ( (loc = codestr.find("\n", loc)) != std::string::npos)
codestr.insert( loc, "\n" );
}

// Get all possible configurations..
Expand Down Expand Up @@ -282,6 +283,7 @@ static std::string getcode(const std::string &filedata, std::string cfg)
{
std::ostringstream ret;

bool match = true;
std::list<bool> matching_ifdef;
std::list<bool> matched_ifdef;

Expand Down Expand Up @@ -334,9 +336,12 @@ static std::string getcode(const std::string &filedata, std::string cfg)
matching_ifdef.pop_back();
}

bool match = true;
for ( std::list<bool>::const_iterator it = matching_ifdef.begin(); it != matching_ifdef.end(); ++it )
match &= bool(*it);
if ( !line.empty() && line[0] == '#' )
{
match = true;
for ( std::list<bool>::const_iterator it = matching_ifdef.begin(); it != matching_ifdef.end(); ++it )
match &= bool(*it);
}
if ( ! match )
line = "";

Expand Down

0 comments on commit c5b26d1

Please sign in to comment.