preprocessor: handle include guards by not checking for configurations in header files

This commit is contained in:
Daniel Marjamäki 2009-01-22 18:19:17 +00:00
parent f9191539a8
commit a9e84c5cd5
2 changed files with 44 additions and 0 deletions

View File

@ -287,10 +287,30 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata)
std::list<std::string> deflist;
// How deep into included files are we currently parsing?
// 0=>Source file, 1=>Included by source file, 2=>included by header that was included by source file, etc
int filelevel = 0;
std::istringstream istr(filedata);
std::string line;
while (getline(istr, line))
{
if (line.substr(0, 6) == "#file ")
{
++filelevel;
continue;
}
else if (line == "#endfile")
{
if (filelevel > 0)
--filelevel;
continue;
}
if (filelevel > 0)
continue;
std::string def = getdef(line, true) + getdef(line, false);
if (!def.empty())
{

View File

@ -51,6 +51,9 @@ private:
TEST_CASE(test4);
TEST_CASE(test5);
// Handling include guards (don't create extra configuration for it)
TEST_CASE(includeguard);
TEST_CASE(newlines);
TEST_CASE(comments1);
@ -269,6 +272,27 @@ private:
}
void includeguard()
{
// Handling include guards..
const char filedata[] = "#file \"abc.h\"\n"
"#ifndef abcH\n"
"#define abcH\n"
"#endif\n"
"#endfile\n"
"#ifdef ABC\n"
"#endif";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Preprocessor preprocessor;
preprocessor.preprocess(istr, actual, "file.c");
// Expected configurations: "" and "ABC"
ASSERT_EQUALS(2, actual.size());
}
void newlines()
{