Preprocessor: Improved 'normal' preprocessing. better handling of multiple #elif and #else blocks
This commit is contained in:
parent
3de70a7244
commit
08ba378730
|
@ -1711,6 +1711,10 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str
|
||||||
// how deep does the #if match? this can never be bigger than "indent".
|
// how deep does the #if match? this can never be bigger than "indent".
|
||||||
unsigned int indentmatch = 0;
|
unsigned int indentmatch = 0;
|
||||||
|
|
||||||
|
// has there been a true #if condition at the current indentmatch level?
|
||||||
|
// then no more #elif or #else can be true before the #endif is seen.
|
||||||
|
bool elseIsTrue = true;
|
||||||
|
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
std::istringstream istr(code);
|
std::istringstream istr(code);
|
||||||
std::string line;
|
std::string line;
|
||||||
|
@ -1735,31 +1739,52 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str
|
||||||
<< handleIncludes(read(fin, filename, NULL), filename, includePaths, defs) << std::endl
|
<< handleIncludes(read(fin, filename, NULL), filename, includePaths, defs) << std::endl
|
||||||
<< "#endfile";
|
<< "#endfile";
|
||||||
} else if (line.compare(0,7,"#ifdef ") == 0) {
|
} else if (line.compare(0,7,"#ifdef ") == 0) {
|
||||||
if (indent == indentmatch && defs.find(getdef(line,true)) != defs.end())
|
if (indent == indentmatch && defs.find(getdef(line,true)) != defs.end()) {
|
||||||
|
elseIsTrue = false;
|
||||||
indentmatch++;
|
indentmatch++;
|
||||||
|
}
|
||||||
++indent;
|
++indent;
|
||||||
|
|
||||||
|
if (indent == indentmatch + 1)
|
||||||
|
elseIsTrue = true;
|
||||||
} else if (line.compare(0,8,"#ifndef ") == 0) {
|
} else if (line.compare(0,8,"#ifndef ") == 0) {
|
||||||
if (indent == indentmatch && defs.find(getdef(line,false)) == defs.end())
|
if (indent == indentmatch && defs.find(getdef(line,false)) == defs.end()) {
|
||||||
|
elseIsTrue = false;
|
||||||
indentmatch++;
|
indentmatch++;
|
||||||
|
}
|
||||||
++indent;
|
++indent;
|
||||||
|
|
||||||
|
if (indent == indentmatch + 1)
|
||||||
|
elseIsTrue = true;
|
||||||
} else if (line.compare(0,4,"#if ") == 0) {
|
} else if (line.compare(0,4,"#if ") == 0) {
|
||||||
if (indent == indentmatch && match_cfg_def(defs, line.substr(4)))
|
if (indent == indentmatch && match_cfg_def(defs, line.substr(4))) {
|
||||||
|
elseIsTrue = false;
|
||||||
indentmatch++;
|
indentmatch++;
|
||||||
|
}
|
||||||
++indent;
|
++indent;
|
||||||
} else if (line.compare(0,6,"#elif ") == 0) {
|
|
||||||
if (indentmatch == indent)
|
if (indent == indentmatch + 1)
|
||||||
indentmatch = indent - 1; // TODO: Make sure all remaining #elif and #else are skipped
|
elseIsTrue = true;
|
||||||
else if (indentmatch == indent - 1 && match_cfg_def(defs, line.substr(6)))
|
} else if (line.compare(0,6,"#elif ") == 0 || line.compare(0,5,"#else") == 0) {
|
||||||
indentmatch = indent;
|
if (!elseIsTrue) {
|
||||||
} else if (line.compare(0,5,"#else") == 0) {
|
|
||||||
if (indentmatch == indent)
|
if (indentmatch == indent)
|
||||||
indentmatch = indent - 1;
|
indentmatch = indent - 1;
|
||||||
else if (indentmatch == indent - 1)
|
} else {
|
||||||
|
if (indentmatch == indent)
|
||||||
|
indentmatch = indent - 1;
|
||||||
|
else if (indentmatch == indent - 1) {
|
||||||
|
if (line.compare(0,5,"#else")==0 || match_cfg_def(defs,line.substr(6))) {
|
||||||
indentmatch = indent;
|
indentmatch = indent;
|
||||||
|
elseIsTrue = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if (line == "#endif") {
|
} else if (line == "#endif") {
|
||||||
--indent;
|
--indent;
|
||||||
if (indentmatch > indent)
|
if (indentmatch > indent) {
|
||||||
indentmatch = indent;
|
indentmatch = indent;
|
||||||
|
elseIsTrue = false;
|
||||||
|
}
|
||||||
} else if (indentmatch == indent) {
|
} else if (indentmatch == indent) {
|
||||||
if (line.compare(0,8,"#define ")==0) {
|
if (line.compare(0,8,"#define ")==0) {
|
||||||
// no value
|
// no value
|
||||||
|
|
|
@ -2881,16 +2881,36 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
// #elif
|
// #elif
|
||||||
|
{
|
||||||
|
const std::string code("#if defined(A)\n"
|
||||||
|
"1\n"
|
||||||
|
"#elif defined(B)\n"
|
||||||
|
"2\n"
|
||||||
|
"#elif defined(C)\n"
|
||||||
|
"3\n"
|
||||||
|
"#else\n"
|
||||||
|
"4\n"
|
||||||
|
"#endif");
|
||||||
|
{
|
||||||
|
defs.clear();
|
||||||
|
defs["A"] = "";
|
||||||
|
defs["C"] = "";
|
||||||
|
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs));
|
||||||
|
ASSERT_EQUALS("\n1\n\n\n\n\n\n\n\n", actual);
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
defs.clear();
|
defs.clear();
|
||||||
defs["B"] = "";
|
defs["B"] = "";
|
||||||
const std::string code("#if defined(A)\n"
|
|
||||||
"123\n"
|
|
||||||
"#elif defined(B)\n"
|
|
||||||
"456\n"
|
|
||||||
"#endif");
|
|
||||||
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs));
|
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs));
|
||||||
ASSERT_EQUALS("\n\n\n456\n\n", actual);
|
ASSERT_EQUALS("\n\n\n2\n\n\n\n\n\n", actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
defs.clear();
|
||||||
|
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs));
|
||||||
|
ASSERT_EQUALS("\n\n\n\n\n\n\n4\n\n", actual);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue