Preprocessor: updates to 'normal' preprocessing
This commit is contained in:
parent
cdfe0d74e4
commit
9e50b7cb68
|
@ -1701,11 +1701,14 @@ static bool openHeader(std::string &filename, const std::list<std::string> &incl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string Preprocessor::handleIncludes(const std::string &code, const std::string &filePath, const std::list<std::string> &includePaths, std::map<std::string,int> &defs)
|
std::string Preprocessor::handleIncludes(const std::string &code, const std::string &filePath, const std::list<std::string> &includePaths, std::map<std::string,std::string> &defs)
|
||||||
{
|
{
|
||||||
const std::string path(filePath.substr(0, 1 + filePath.find_last_of("\\/")));
|
const std::string path(filePath.substr(0, 1 + filePath.find_last_of("\\/")));
|
||||||
|
|
||||||
|
// current #if indent level.
|
||||||
unsigned int indent = 0;
|
unsigned int indent = 0;
|
||||||
|
|
||||||
|
// how deep does the #if match? this can never be bigger than "indent".
|
||||||
unsigned int indentmatch = 0;
|
unsigned int indentmatch = 0;
|
||||||
|
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
|
@ -1739,6 +1742,10 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str
|
||||||
if (indent == indentmatch && defs.find(getdef(line,false)) == defs.end())
|
if (indent == indentmatch && defs.find(getdef(line,false)) == defs.end())
|
||||||
indentmatch++;
|
indentmatch++;
|
||||||
++indent;
|
++indent;
|
||||||
|
} else if (line.compare(0,4,"#if ") == 0) {
|
||||||
|
if (indent == indentmatch && match_cfg_def(defs, line.substr(4)))
|
||||||
|
indentmatch++;
|
||||||
|
++indent;
|
||||||
} else if (line.compare(0,5,"#else") == 0) {
|
} else if (line.compare(0,5,"#else") == 0) {
|
||||||
if (indentmatch == indent)
|
if (indentmatch == indent)
|
||||||
indentmatch = indent - 1;
|
indentmatch = indent - 1;
|
||||||
|
@ -1752,16 +1759,12 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str
|
||||||
if (line.compare(0,8,"#define ")==0) {
|
if (line.compare(0,8,"#define ")==0) {
|
||||||
// no value
|
// no value
|
||||||
if (line.find_first_of("( ", 8) == std::string::npos)
|
if (line.find_first_of("( ", 8) == std::string::npos)
|
||||||
defs[line.substr(8)] = 1;
|
defs[line.substr(8)] = "";
|
||||||
|
|
||||||
// define value
|
// define value
|
||||||
else if (line.find("(") == std::string::npos) {
|
else if (line.find("(") == std::string::npos) {
|
||||||
const std::string::size_type pos = line.find(" ", 8);
|
const std::string::size_type pos = line.find(" ", 8);
|
||||||
const std::string val(line.substr(pos + 1));
|
defs[line.substr(8,pos-8)] = line.substr(pos+1);
|
||||||
int i;
|
|
||||||
std::istringstream istr2(val);
|
|
||||||
istr2 >> i;
|
|
||||||
defs[line.substr(8,pos-8)] = i;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -217,7 +217,7 @@ public:
|
||||||
* @param defs defines (only values)
|
* @param defs defines (only values)
|
||||||
* \return resulting string
|
* \return resulting string
|
||||||
*/
|
*/
|
||||||
std::string handleIncludes(const std::string &code, const std::string &filePath, const std::list<std::string> &includePaths, std::map<std::string,int> &defs);
|
std::string handleIncludes(const std::string &code, const std::string &filePath, const std::list<std::string> &includePaths, std::map<std::string,std::string> &defs);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, bool userheader);
|
void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, bool userheader);
|
||||||
|
|
|
@ -2827,13 +2827,13 @@ private:
|
||||||
void handleIncludes_def() {
|
void handleIncludes_def() {
|
||||||
const std::string filePath("test.c");
|
const std::string filePath("test.c");
|
||||||
const std::list<std::string> includePaths;
|
const std::list<std::string> includePaths;
|
||||||
std::map<std::string,int> defs;
|
std::map<std::string,std::string> defs;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor;
|
||||||
|
|
||||||
// ifdef
|
// ifdef
|
||||||
{
|
{
|
||||||
defs.clear();
|
defs.clear();
|
||||||
defs["A"] = 1;
|
defs["A"] = "";
|
||||||
{
|
{
|
||||||
const std::string code("#ifdef A\n123\n#endif\n");
|
const std::string code("#ifdef A\n123\n#endif\n");
|
||||||
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs));
|
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs));
|
||||||
|
@ -2848,7 +2848,7 @@ private:
|
||||||
// ifndef
|
// ifndef
|
||||||
{
|
{
|
||||||
defs.clear();
|
defs.clear();
|
||||||
defs["A"] = 1;
|
defs["A"] = "";
|
||||||
{
|
{
|
||||||
const std::string code("#ifndef A\n123\n#endif\n");
|
const std::string code("#ifndef A\n123\n#endif\n");
|
||||||
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs));
|
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs));
|
||||||
|
@ -2868,15 +2868,17 @@ private:
|
||||||
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs));
|
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs));
|
||||||
ASSERT_EQUALS("\n\n123\n\n" "\n\n\n\n", actual);
|
ASSERT_EQUALS("\n\n123\n\n" "\n\n\n\n", actual);
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
// define X 123 - #if
|
// define X 123 - #if
|
||||||
{
|
{
|
||||||
defs.clear();
|
defs.clear();
|
||||||
const std::string code("#define X 123\n#if X==123\n456\n#endif\n");
|
const std::string code("#define X 123\n"
|
||||||
|
"#if X==123\n"
|
||||||
|
"456\n"
|
||||||
|
"#endif\n");
|
||||||
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs));
|
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs));
|
||||||
ASSERT_EQUALS("\n\n456\n\n", actual);
|
ASSERT_EQUALS("\n\n456\n\n", actual);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue