Clang import; Refactoring

This commit is contained in:
Daniel Marjamäki 2020-01-07 19:47:06 +01:00
parent a3abc75ca1
commit e5b99d3299
2 changed files with 31 additions and 31 deletions

View File

@ -118,13 +118,16 @@ namespace clangastdump {
int mVarId = 0; int mVarId = 0;
}; };
class AstNode;
typedef std::shared_ptr<AstNode> AstNodePtr;
class AstNode { class AstNode {
public: public:
AstNode(const std::string &nodeType, const std::string &ext, Data *data) AstNode(const std::string &nodeType, const std::string &ext, Data *data)
: nodeType(nodeType), mExtTokens(splitString(ext)), mData(data) : nodeType(nodeType), mExtTokens(splitString(ext)), mData(data)
{} {}
std::string nodeType; std::string nodeType;
std::vector<std::shared_ptr<AstNode>> children; std::vector<AstNodePtr> children;
void setLocations(TokenList *tokenList, int file, int line, int col); void setLocations(TokenList *tokenList, int file, int line, int col);
@ -133,7 +136,8 @@ namespace clangastdump {
private: private:
Token *addtoken(TokenList *tokenList, const std::string &str); Token *addtoken(TokenList *tokenList, const std::string &str);
Token *addTypeTokens(TokenList *tokenList, const std::string &str); Token *addTypeTokens(TokenList *tokenList, const std::string &str);
Scope *createScope(TokenList *tokenList, Scope::ScopeType scopeType, AstNode *astNode); Scope *createScope(TokenList *tokenList, Scope::ScopeType scopeType, AstNodePtr astNode);
Scope *createScope(TokenList *tokenList, Scope::ScopeType scopeType, const std::vector<AstNodePtr> &children);
std::string getSpelling() const; std::string getSpelling() const;
std::string getType() const; std::string getType() const;
const Scope *getNestedInScope(TokenList *tokenList); const Scope *getNestedInScope(TokenList *tokenList);
@ -145,8 +149,6 @@ namespace clangastdump {
std::vector<std::string> mExtTokens; std::vector<std::string> mExtTokens;
Data *mData; Data *mData;
}; };
typedef std::shared_ptr<AstNode> AstNodePtr;
} }
std::string clangastdump::AstNode::getSpelling() const std::string clangastdump::AstNode::getSpelling() const
@ -224,7 +226,13 @@ const Scope *clangastdump::AstNode::getNestedInScope(TokenList *tokenList)
return tokenList->back()->scope(); return tokenList->back()->scope();
} }
Scope *clangastdump::AstNode::createScope(TokenList *tokenList, Scope::ScopeType scopeType, AstNode *astNode) Scope *clangastdump::AstNode::createScope(TokenList *tokenList, Scope::ScopeType scopeType, AstNodePtr astNode)
{
std::vector<AstNodePtr> children{astNode};
return createScope(tokenList, scopeType, children);
}
Scope *clangastdump::AstNode::createScope(TokenList *tokenList, Scope::ScopeType scopeType, const std::vector<AstNodePtr> &children)
{ {
SymbolDatabase *symbolDatabase = mData->mSymbolDatabase; SymbolDatabase *symbolDatabase = mData->mSymbolDatabase;
@ -233,12 +241,14 @@ Scope *clangastdump::AstNode::createScope(TokenList *tokenList, Scope::ScopeType
symbolDatabase->scopeList.push_back(Scope(nullptr, nullptr, nestedIn)); symbolDatabase->scopeList.push_back(Scope(nullptr, nullptr, nestedIn));
Scope *scope = &symbolDatabase->scopeList.back(); Scope *scope = &symbolDatabase->scopeList.back();
scope->type = scopeType; scope->type = scopeType;
Token *bodyStart = astNode->addtoken(tokenList, "{"); Token *bodyStart = children[0]->addtoken(tokenList, "{");
tokenList->back()->scope(scope); tokenList->back()->scope(scope);
astNode->createTokens(tokenList); for (AstNodePtr astNode: children) {
if (!Token::Match(tokenList->back(), "[;{}]")) astNode->createTokens(tokenList);
astNode->addtoken(tokenList, ";"); if (!Token::Match(tokenList->back(), "[;{}]"))
Token *bodyEnd = astNode->addtoken(tokenList, "}"); astNode->addtoken(tokenList, ";");
}
Token *bodyEnd = children.back()->addtoken(tokenList, "}");
bodyStart->link(bodyEnd); bodyStart->link(bodyEnd);
scope->bodyStart = bodyStart; scope->bodyStart = bodyStart;
scope->bodyEnd = bodyEnd; scope->bodyEnd = bodyEnd;
@ -365,9 +375,9 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList)
return nullptr; return nullptr;
} }
if (nodeType == IfStmt) { if (nodeType == IfStmt) {
AstNode *cond = children[2].get(); AstNodePtr cond = children[2];
AstNode *then = children[3].get(); AstNodePtr then = children[3];
AstNode *else_ = children[4].get(); AstNodePtr else_ = children[4];
Token *iftok = addtoken(tokenList, "if"); Token *iftok = addtoken(tokenList, "if");
Token *par1 = addtoken(tokenList, "("); Token *par1 = addtoken(tokenList, "(");
par1->astOperand1(iftok); par1->astOperand1(iftok);
@ -387,22 +397,12 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList)
return addtoken(tokenList, mExtTokens.back()); return addtoken(tokenList, mExtTokens.back());
if (nodeType == RecordDecl) { if (nodeType == RecordDecl) {
const Token *classDef = addtoken(tokenList, "struct"); const Token *classDef = addtoken(tokenList, "struct");
/*const Token *nameToken =*/ addtoken(tokenList, getSpelling()); const std::string &recordName = getSpelling();
const Scope *nestedIn = getNestedInScope(tokenList); if (!recordName.empty())
mData->mSymbolDatabase->scopeList.push_back(Scope(nullptr, nullptr, nestedIn)); addtoken(tokenList, getSpelling());
Scope *scope = &mData->mSymbolDatabase->scopeList.back(); Scope *recordScope = createScope(tokenList, Scope::ScopeType::eStruct, children);
scope->type = Scope::ScopeType::eStruct; mData->mSymbolDatabase->typeList.push_back(Type(classDef, recordScope, classDef->scope()));
mData->mSymbolDatabase->typeList.push_back(Type(classDef, scope, nestedIn)); recordScope->definedType = &mData->mSymbolDatabase->typeList.back();
scope->definedType = &mData->mSymbolDatabase->typeList.back();
Token *bodyStart = addtoken(tokenList, "{");
bodyStart->scope(scope);
for (AstNodePtr child: children) {
child->createTokens(tokenList);
}
Token *bodyEnd = addtoken(tokenList, "}");
bodyStart->link(bodyEnd);
scope->bodyStart = bodyStart;
scope->bodyEnd = bodyEnd;
return nullptr; return nullptr;
} }
if (nodeType == ReturnStmt) { if (nodeType == ReturnStmt) {

View File

@ -90,8 +90,8 @@ private:
const char clang[] = "`-RecordDecl 0x354eac8 <1.c:1:1, line:4:1> line:1:8 struct S definition\n" const char clang[] = "`-RecordDecl 0x354eac8 <1.c:1:1, line:4:1> line:1:8 struct S definition\n"
" |-FieldDecl 0x354eb88 <line:2:3, col:7> col:7 x 'int'\n" " |-FieldDecl 0x354eb88 <line:2:3, col:7> col:7 x 'int'\n"
" `-FieldDecl 0x354ebe8 <line:3:3, col:7> col:7 y 'int'"; " `-FieldDecl 0x354ebe8 <line:3:3, col:7> col:7 y 'int'";
ASSERT_EQUALS("struct S {\n" ASSERT_EQUALS("struct S\n"
"int x@1 ;\n" "{ int x@1 ;\n"
"int y@2 ; }", "int y@2 ; }",
parse(clang)); parse(clang));
} }