Refactorizations in preprocessor.cpp
This commit is contained in:
parent
621d43e9e4
commit
9c423b82c1
|
@ -71,30 +71,11 @@ static unsigned char readChar(std::istream &istr)
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Splits a string that contains the specified separator into substrings
|
|
||||||
static std::list<std::string> split(const std::string &s, char separator)
|
|
||||||
{
|
|
||||||
std::list<std::string> parts;
|
|
||||||
|
|
||||||
std::string::size_type prevPos = 0;
|
|
||||||
for (std::string::size_type pos = 0; pos < s.length(); ++pos) {
|
|
||||||
if (s[pos] == separator) {
|
|
||||||
if (pos > prevPos)
|
|
||||||
parts.push_back(s.substr(prevPos, pos - prevPos));
|
|
||||||
prevPos = pos + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (prevPos < s.length())
|
|
||||||
parts.push_back(s.substr(prevPos));
|
|
||||||
|
|
||||||
return parts;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Concatenates a list of strings, inserting a separator between parts
|
// Concatenates a list of strings, inserting a separator between parts
|
||||||
static std::string join(const std::list<std::string> &list, char separator)
|
static std::string join(const std::set<std::string>& list, char separator)
|
||||||
{
|
{
|
||||||
std::string s;
|
std::string s;
|
||||||
for (std::list<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) {
|
for (std::set<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) {
|
||||||
if (!s.empty())
|
if (!s.empty())
|
||||||
s += separator;
|
s += separator;
|
||||||
|
|
||||||
|
@ -103,6 +84,25 @@ static std::string join(const std::list<std::string> &list, char separator)
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Removes duplicate string portions separated by the specified separator
|
||||||
|
static std::string unify(const std::string &s, char separator)
|
||||||
|
{
|
||||||
|
std::set<std::string> parts;
|
||||||
|
|
||||||
|
std::string::size_type prevPos = 0;
|
||||||
|
for (std::string::size_type pos = 0; pos < s.length(); ++pos) {
|
||||||
|
if (s[pos] == separator) {
|
||||||
|
if (pos > prevPos)
|
||||||
|
parts.insert(s.substr(prevPos, pos - prevPos));
|
||||||
|
prevPos = pos + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (prevPos < s.length())
|
||||||
|
parts.insert(s.substr(prevPos));
|
||||||
|
|
||||||
|
return join(parts, separator);
|
||||||
|
}
|
||||||
|
|
||||||
/** Just read the code into a string. Perform simple cleanup of the code */
|
/** Just read the code into a string. Perform simple cleanup of the code */
|
||||||
std::string Preprocessor::read(std::istream &istr, const std::string &filename, Settings *settings)
|
std::string Preprocessor::read(std::istream &istr, const std::string &filename, Settings *settings)
|
||||||
{
|
{
|
||||||
|
@ -549,12 +549,8 @@ std::string Preprocessor::removeIf0(const std::string &code)
|
||||||
std::istringstream istr(code);
|
std::istringstream istr(code);
|
||||||
std::string line;
|
std::string line;
|
||||||
while (std::getline(istr,line)) {
|
while (std::getline(istr,line)) {
|
||||||
if (line != "#if 0")
|
ret << line << "\n";
|
||||||
ret << line << "\n";
|
if (line == "#if 0") {
|
||||||
else {
|
|
||||||
// replace '#if 0' with empty line
|
|
||||||
ret << line << "\n";
|
|
||||||
|
|
||||||
// goto the end of the '#if 0' block
|
// goto the end of the '#if 0' block
|
||||||
unsigned int level = 1;
|
unsigned int level = 1;
|
||||||
bool in = false;
|
bool in = false;
|
||||||
|
@ -1136,23 +1132,22 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
|
||||||
|
|
||||||
|
|
||||||
const Token *tok = tokenizer.tokens();
|
const Token *tok = tokenizer.tokens();
|
||||||
std::list<std::string> varList;
|
std::set<std::string> varList;
|
||||||
while (tok) {
|
while (tok) {
|
||||||
if (Token::Match(tok, "defined ( %var% )")) {
|
if (Token::Match(tok, "defined ( %var% )")) {
|
||||||
varList.push_back(tok->strAt(2));
|
varList.insert(tok->strAt(2));
|
||||||
tok = tok->tokAt(4);
|
tok = tok->tokAt(4);
|
||||||
if (tok && tok->str() == "&&") {
|
if (tok && tok->str() == "&&") {
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
}
|
}
|
||||||
} else if (Token::Match(tok, "%var% ;")) {
|
} else if (Token::Match(tok, "%var% ;")) {
|
||||||
varList.push_back(tok->str());
|
varList.insert(tok->str());
|
||||||
tok = tok->tokAt(2);
|
tok = tok->tokAt(2);
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
varList.sort();
|
|
||||||
s = join(varList, ';');
|
s = join(varList, ';');
|
||||||
|
|
||||||
if (!s.empty())
|
if (!s.empty())
|
||||||
|
@ -1161,15 +1156,8 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert configurations into a canonical form: B;C;A or C;A;B => A;B;C
|
// Convert configurations into a canonical form: B;C;A or C;A;B => A;B;C
|
||||||
for (std::list<std::string>::iterator it = ret.begin(); it != ret.end(); ++it) {
|
for (std::list<std::string>::iterator it = ret.begin(); it != ret.end(); ++it)
|
||||||
// Split the configuration into a list of defines
|
*it = unify(*it, ';');
|
||||||
std::list<std::string> defs = split(*it, ';');
|
|
||||||
|
|
||||||
// Re-constitute the configuration after sorting the defines
|
|
||||||
defs.sort();
|
|
||||||
defs.unique();
|
|
||||||
*it = join(defs, ';');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove duplicates from the ret list..
|
// Remove duplicates from the ret list..
|
||||||
ret.sort();
|
ret.sort();
|
||||||
|
|
Loading…
Reference in New Issue