Clang import; Better handling of enums

This commit is contained in:
Daniel Marjamäki 2020-01-26 07:32:14 +01:00
parent 625da9af5c
commit 66dd985c9d
1 changed files with 39 additions and 13 deletions

View File

@ -148,16 +148,21 @@ static std::vector<std::string> splitString(const std::string &line)
namespace clangimport {
struct Data {
struct Decl {
Decl(Token *def, Variable *var) : def(def), function(nullptr), var(var) {}
Decl(Token *def, Function *function) : def(def), function(function), var(nullptr) {}
Decl(Token *def, Variable *var) : def(def), enumerator(nullptr), function(nullptr), var(var) {}
Decl(Token *def, Function *function) : def(def), enumerator(nullptr), function(function), var(nullptr) {}
Decl(Token *def, Enumerator *enumerator) : def(def), enumerator(enumerator), function(nullptr), var(nullptr) {}
void ref(Token *tok) {
if (enumerator)
tok->enumerator(enumerator);
if (function)
tok->function(function);
tok->varId(var ? var->declarationId() : 0);
if (var)
if (var) {
tok->variable(var);
tok->varId(var->declarationId());
}
}
Token *def;
Enumerator *enumerator;
Function *function;
Variable *var;
};
@ -165,6 +170,22 @@ namespace clangimport {
const Settings *mSettings = nullptr;
SymbolDatabase *mSymbolDatabase = nullptr;
int enumValue = 0;
void enumDecl(const std::string &addr, Token *nameToken, Enumerator *enumerator) {
Decl decl(nameToken, enumerator);
mDeclMap.insert(std::pair<std::string, Decl>(addr, decl));
nameToken->enumerator(enumerator);
notFound(addr);
}
void funcDecl(const std::string &addr, Token *nameToken, Function *function) {
Decl decl(nameToken, function);
mDeclMap.insert(std::pair<std::string, Decl>(addr, decl));
nameToken->function(function);
notFound(addr);
}
void varDecl(const std::string &addr, Token *def, Variable *var) {
Decl decl(def, var);
mDeclMap.insert(std::pair<std::string, Decl>(addr, decl));
@ -175,13 +196,6 @@ namespace clangimport {
notFound(addr);
}
void funcDecl(const std::string &addr, Token *nameToken, Function *function) {
Decl decl(nameToken, function);
mDeclMap.insert(std::pair<std::string, Decl>(addr, decl));
nameToken->function(function);
notFound(addr);
}
void ref(const std::string &addr, Token *tok) {
auto it = mDeclMap.find(addr);
if (it != mDeclMap.end())
@ -469,6 +483,8 @@ Scope *clangimport::AstNode::createScope(TokenList *tokenList, Scope::ScopeType
symbolDatabase->scopeList.push_back(Scope(nullptr, nullptr, nestedIn));
Scope *scope = &symbolDatabase->scopeList.back();
if (scopeType == Scope::ScopeType::eEnum)
scope->enumeratorList.reserve(children.size());
nestedIn->nestedList.push_back(scope);
scope->type = scopeType;
scope->classDef = def;
@ -733,9 +749,19 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
par1->astOperand2(expr);
return nullptr;
}
if (nodeType == EnumConstantDecl)
return addtoken(tokenList, getSpelling());
if (nodeType == EnumConstantDecl) {
Token *nameToken = addtoken(tokenList, getSpelling());
Scope *scope = const_cast<Scope *>(nameToken->scope());
scope->enumeratorList.push_back(Enumerator(nameToken->scope()));
Enumerator *e = &scope->enumeratorList.back();
e->name = nameToken;
e->value = mData->enumValue++;
e->value_known = true;
mData->enumDecl(mExtTokens.front(), nameToken, e);
return nameToken;
}
if (nodeType == EnumDecl) {
mData->enumValue = 0;
Token *enumtok = addtoken(tokenList, "enum");
Token *nametok = nullptr;
if (mExtTokens[mExtTokens.size() - 3].compare(0,4,"col:") == 0)