Partial fix for #322 (get configurations from headers that don't start at the beginning of the file)

This commit is contained in:
Daniel Marjamäki 2010-02-06 09:30:48 +01:00
parent fb81100bec
commit cbc853fdba
2 changed files with 35 additions and 4 deletions

View File

@ -637,6 +637,8 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
// 0=>Source file, 1=>Included by source file, 2=>included by header that was included by source file, etc
int filelevel = 0;
bool includeguard = false;
unsigned int linenr = 0;
std::istringstream istr(filedata);
std::string line;
@ -646,18 +648,20 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
if (line.compare(0, 6, "#file ") == 0)
{
includeguard = true;
++filelevel;
continue;
}
else if (line == "#endfile")
{
includeguard = false;
if (filelevel > 0)
--filelevel;
continue;
}
else if (line.compare(0, 8, "#define ") == 0 && line.find("(", 8) == std::string::npos)
if (line.compare(0, 8, "#define ") == 0 && line.find("(", 8) == std::string::npos)
{
if (line.find(" ", 8) == std::string::npos)
defines.insert(line.substr(8));
@ -669,7 +673,10 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
}
}
if (filelevel > 0)
if (!line.empty() && line.compare(0, 3, "#if") != 0)
includeguard = false;
if (includeguard)
continue;
std::string def = getdef(line, true) + getdef(line, false);

View File

@ -82,7 +82,8 @@ private:
TEST_CASE(error2);
// Handling include guards (don't create extra configuration for it)
TEST_CASE(includeguard);
TEST_CASE(includeguard1);
TEST_CASE(includeguard2);
TEST_CASE(newlines);
@ -436,7 +437,7 @@ private:
}
void includeguard()
void includeguard1()
{
// Handling include guards..
const char filedata[] = "#file \"abc.h\"\n"
@ -457,6 +458,29 @@ private:
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
}
void includeguard2()
{
// Handling include guards..
const char filedata[] = "#file \"abc.h\"\n"
"foo\n"
"#ifdef ABC\n"
"\n"
"#endif\n"
"#endfile\n";
// 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, static_cast<unsigned int>(actual.size()));
ASSERT_EQUALS(true, actual.find("") != actual.end());
ASSERT_EQUALS(true, actual.find("ABC") != actual.end());
}
void ifdefwithfile()
{
// Handling include guards..