bump simplecpp

This commit is contained in:
Daniel Marjamäki 2016-07-20 15:04:03 +02:00
parent 30e3897247
commit b5ca8ff70f
2 changed files with 42 additions and 15 deletions

View File

@ -103,6 +103,7 @@ void simplecpp::TokenList::operator=(const TokenList &other) {
clear(); clear();
for (const Token *tok = other.cbegin(); tok; tok = tok->next) for (const Token *tok = other.cbegin(); tok; tok = tok->next)
push_back(new Token(*tok)); push_back(new Token(*tok));
sizeOfType = other.sizeOfType;
} }
void simplecpp::TokenList::clear() { void simplecpp::TokenList::clear() {
@ -112,6 +113,7 @@ void simplecpp::TokenList::clear() {
first = next; first = next;
} }
last = nullptr; last = nullptr;
sizeOfType.clear();
} }
void simplecpp::TokenList::push_back(Token *tok) { void simplecpp::TokenList::push_back(Token *tok) {
@ -1053,35 +1055,57 @@ private:
namespace { namespace {
void simplifySizeof(simplecpp::TokenList &expr) { void simplifySizeof(simplecpp::TokenList &expr) {
std::map<std::string, std::size_t> sizeOfType(expr.sizeOfType);
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("char"), sizeof(char)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("short"), sizeof(short)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("short int"), sizeof(short int)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("int"), sizeof(int)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("long int"), sizeof(long int)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("long"), sizeof(long)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("long long"), sizeof(long long)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("float"), sizeof(float)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("double"), sizeof(double)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("long double"), sizeof(long double)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("char *"), sizeof(char *)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("short *"), sizeof(short *)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("short int *"), sizeof(short int *)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("int *"), sizeof(int *)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("long int *"), sizeof(long int *)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("long *"), sizeof(long *)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("long long *"), sizeof(long long *)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("float *"), sizeof(float *)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("double *"), sizeof(double *)));
sizeOfType.insert(std::pair<std::string, std::size_t>(std::string("long double *"), sizeof(long double *)));
for (simplecpp::Token *tok = expr.begin(); tok; tok = tok->next) { for (simplecpp::Token *tok = expr.begin(); tok; tok = tok->next) {
if (tok->str != "sizeof") if (tok->str != "sizeof")
continue; continue;
simplecpp::Token *tok1 = tok->next; simplecpp::Token *tok1 = tok->next;
simplecpp::Token *tok2 = tok1->next; simplecpp::Token *tok2 = tok1->next;
if (tok1->op == '(') { if (tok1->op == '(') {
tok1 = tok1->next;
while (tok2->op != ')') while (tok2->op != ')')
tok2 = tok2->next; tok2 = tok2->next;
tok2 = tok2->next;
} }
unsigned int sz = 0; std::string type;
for (simplecpp::Token *typeToken = tok1; typeToken != tok2; typeToken = typeToken->next) { for (simplecpp::Token *typeToken = tok1; typeToken != tok2; typeToken = typeToken->next) {
if (typeToken->str == "char") if ((typeToken->str == "unsigned" || typeToken->str == "signed") && typeToken->next->name)
sz = sizeof(char); continue;
else if (typeToken->str == "short") if (typeToken->str == "*" && type.find("*") != std::string::npos)
sz = sizeof(short); continue;
else if (typeToken->str == "int") if (!type.empty())
sz = sizeof(int); type += ' ';
else if (typeToken->str == "long") type += typeToken->str;
sz = sizeof(long);
else if (typeToken->str == "float")
sz = sizeof(float);
else if (typeToken->str == "double")
sz = sizeof(double);
} }
tok->setstr(std::to_string(sz)); const std::map<std::string, std::size_t>::const_iterator it = sizeOfType.find(type);
if (it != sizeOfType.end())
tok->setstr(std::to_string(it->second));
else
continue;
tok2 = tok2->next;
while (tok->next != tok2) while (tok->next != tok2)
expr.deleteToken(tok->next); expr.deleteToken(tok->next);
} }

View File

@ -187,6 +187,9 @@ public:
delete tok; delete tok;
} }
/** sizeof(T) */
std::map<std::string, std::size_t> sizeOfType;
private: private:
void combineOperators(); void combineOperators();