Skip to content

Commit c5b26d1

Browse files
author
Daniel Marjamäki
committed
preprocessor: simple optimizations
1 parent f25de18 commit c5b26d1

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

preprocessor.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* c++check - c/c++ syntax checking
33
* Copyright (C) 2007 Daniel Marjamäki
44
*
@@ -143,24 +143,25 @@ void preprocess(std::istream &istr, std::map<std::string, std::string> &result,
143143
std::string codestr( code.str() );
144144

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

151152
// Remove all trailing spaces..
152-
while ( codestr.find(" \n") != std::string::npos )
153-
codestr.erase( codestr.find(" \n"), 1 );
153+
loc = 0;
154+
while ( (loc = codestr.find(" \n", loc)) != std::string::npos )
155+
codestr.erase( loc, 1 );
154156

155157
// Using the backslash at the end of a line..
156-
while ( codestr.find("\\\n") != std::string::npos )
158+
while ( (loc = codestr.rfind("\\\n")) != std::string::npos )
157159
{
158-
std::string::size_type pos = codestr.rfind("\\\n");
159-
codestr.erase(pos,2);
160-
if (pos > 0 && codestr[pos-1] != ' ')
161-
codestr.insert(pos, " ");
162-
if ( codestr.find("\n", pos) != std::string::npos)
163-
codestr.insert( codestr.find("\n", pos), "\n" );
160+
codestr.erase(loc, 2);
161+
if (loc > 0 && codestr[loc-1] != ' ')
162+
codestr.insert(loc, " ");
163+
if ( (loc = codestr.find("\n", loc)) != std::string::npos)
164+
codestr.insert( loc, "\n" );
164165
}
165166

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

286+
bool match = true;
285287
std::list<bool> matching_ifdef;
286288
std::list<bool> matched_ifdef;
287289

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

337-
bool match = true;
338-
for ( std::list<bool>::const_iterator it = matching_ifdef.begin(); it != matching_ifdef.end(); ++it )
339-
match &= bool(*it);
339+
if ( !line.empty() && line[0] == '#' )
340+
{
341+
match = true;
342+
for ( std::list<bool>::const_iterator it = matching_ifdef.begin(); it != matching_ifdef.end(); ++it )
343+
match &= bool(*it);
344+
}
340345
if ( ! match )
341346
line = "";
342347

0 commit comments

Comments
 (0)