From e5b99d3299f8e4ba0f17734daf23a932b4fab727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 7 Jan 2020 19:47:06 +0100 Subject: [PATCH] Clang import; Refactoring --- lib/clangastdump.cpp | 58 +++++++++++++++++++-------------------- test/testclangastdump.cpp | 4 +-- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/lib/clangastdump.cpp b/lib/clangastdump.cpp index 65e31a0e5..dbba7eda4 100644 --- a/lib/clangastdump.cpp +++ b/lib/clangastdump.cpp @@ -118,13 +118,16 @@ namespace clangastdump { int mVarId = 0; }; + class AstNode; + typedef std::shared_ptr AstNodePtr; + class AstNode { public: AstNode(const std::string &nodeType, const std::string &ext, Data *data) : nodeType(nodeType), mExtTokens(splitString(ext)), mData(data) {} std::string nodeType; - std::vector> children; + std::vector children; void setLocations(TokenList *tokenList, int file, int line, int col); @@ -133,7 +136,8 @@ namespace clangastdump { private: Token *addtoken(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 &children); std::string getSpelling() const; std::string getType() const; const Scope *getNestedInScope(TokenList *tokenList); @@ -145,8 +149,6 @@ namespace clangastdump { std::vector mExtTokens; Data *mData; }; - - typedef std::shared_ptr AstNodePtr; } std::string clangastdump::AstNode::getSpelling() const @@ -224,7 +226,13 @@ const Scope *clangastdump::AstNode::getNestedInScope(TokenList *tokenList) 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 children{astNode}; + return createScope(tokenList, scopeType, children); +} + +Scope *clangastdump::AstNode::createScope(TokenList *tokenList, Scope::ScopeType scopeType, const std::vector &children) { SymbolDatabase *symbolDatabase = mData->mSymbolDatabase; @@ -233,12 +241,14 @@ Scope *clangastdump::AstNode::createScope(TokenList *tokenList, Scope::ScopeType symbolDatabase->scopeList.push_back(Scope(nullptr, nullptr, nestedIn)); Scope *scope = &symbolDatabase->scopeList.back(); scope->type = scopeType; - Token *bodyStart = astNode->addtoken(tokenList, "{"); + Token *bodyStart = children[0]->addtoken(tokenList, "{"); tokenList->back()->scope(scope); - astNode->createTokens(tokenList); - if (!Token::Match(tokenList->back(), "[;{}]")) - astNode->addtoken(tokenList, ";"); - Token *bodyEnd = astNode->addtoken(tokenList, "}"); + for (AstNodePtr astNode: children) { + astNode->createTokens(tokenList); + if (!Token::Match(tokenList->back(), "[;{}]")) + astNode->addtoken(tokenList, ";"); + } + Token *bodyEnd = children.back()->addtoken(tokenList, "}"); bodyStart->link(bodyEnd); scope->bodyStart = bodyStart; scope->bodyEnd = bodyEnd; @@ -365,9 +375,9 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList) return nullptr; } if (nodeType == IfStmt) { - AstNode *cond = children[2].get(); - AstNode *then = children[3].get(); - AstNode *else_ = children[4].get(); + AstNodePtr cond = children[2]; + AstNodePtr then = children[3]; + AstNodePtr else_ = children[4]; Token *iftok = addtoken(tokenList, "if"); Token *par1 = addtoken(tokenList, "("); par1->astOperand1(iftok); @@ -387,22 +397,12 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList) return addtoken(tokenList, mExtTokens.back()); if (nodeType == RecordDecl) { const Token *classDef = addtoken(tokenList, "struct"); - /*const Token *nameToken =*/ addtoken(tokenList, getSpelling()); - const Scope *nestedIn = getNestedInScope(tokenList); - mData->mSymbolDatabase->scopeList.push_back(Scope(nullptr, nullptr, nestedIn)); - Scope *scope = &mData->mSymbolDatabase->scopeList.back(); - scope->type = Scope::ScopeType::eStruct; - mData->mSymbolDatabase->typeList.push_back(Type(classDef, scope, nestedIn)); - 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; + const std::string &recordName = getSpelling(); + if (!recordName.empty()) + addtoken(tokenList, getSpelling()); + Scope *recordScope = createScope(tokenList, Scope::ScopeType::eStruct, children); + mData->mSymbolDatabase->typeList.push_back(Type(classDef, recordScope, classDef->scope())); + recordScope->definedType = &mData->mSymbolDatabase->typeList.back(); return nullptr; } if (nodeType == ReturnStmt) { diff --git a/test/testclangastdump.cpp b/test/testclangastdump.cpp index 06b1af61d..29792b559 100644 --- a/test/testclangastdump.cpp +++ b/test/testclangastdump.cpp @@ -90,8 +90,8 @@ private: const char clang[] = "`-RecordDecl 0x354eac8 <1.c:1:1, line:4:1> line:1:8 struct S definition\n" " |-FieldDecl 0x354eb88 col:7 x 'int'\n" " `-FieldDecl 0x354ebe8 col:7 y 'int'"; - ASSERT_EQUALS("struct S {\n" - "int x@1 ;\n" + ASSERT_EQUALS("struct S\n" + "{ int x@1 ;\n" "int y@2 ; }", parse(clang)); }