From 10788550294328314035cae4a109f19d8bdb2783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 8 Nov 2020 10:31:48 +0100 Subject: [PATCH] Clang import: Full name for methods --- lib/clangimport.cpp | 42 +++++++++++++++++++++++++++++++++++++--- test/testclangimport.cpp | 2 +- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/clangimport.cpp b/lib/clangimport.cpp index f4ef07c33..03c75b73e 100644 --- a/lib/clangimport.cpp +++ b/lib/clangimport.cpp @@ -161,9 +161,10 @@ static std::vector splitString(const std::string &line) namespace clangimport { struct Data { struct Decl { - 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) {} + Decl(Scope *scope) : def(nullptr), enumerator(nullptr), function(nullptr), scope(scope), var(nullptr) {} + Decl(Token *def, Variable *var) : def(def), enumerator(nullptr), function(nullptr), scope(nullptr), var(var) {} + Decl(Token *def, Function *function) : def(def), enumerator(nullptr), function(function), scope(nullptr), var(nullptr) {} + Decl(Token *def, Enumerator *enumerator) : def(def), enumerator(enumerator), function(nullptr), scope(nullptr), var(nullptr) {} void ref(Token *tok) { if (enumerator) tok->enumerator(enumerator); @@ -177,6 +178,7 @@ namespace clangimport { Token *def; Enumerator *enumerator; Function *function; + Scope *scope; Variable *var; }; @@ -199,6 +201,11 @@ namespace clangimport { notFound(addr); } + void scopeDecl(const std::string &addr, Scope *scope) { + Decl decl(scope); + mDeclMap.insert(std::pair(addr, decl)); + } + void varDecl(const std::string &addr, Token *def, Variable *var) { Decl decl(def, var); mDeclMap.insert(std::pair(addr, decl)); @@ -231,6 +238,11 @@ namespace clangimport { return mDeclMap.find(addr) != mDeclMap.end(); } + const Scope *getScope(const std::string &addr) { + auto it = mDeclMap.find(addr); + return (it == mDeclMap.end() ? nullptr : it->second.scope); + } + // "}" tokens that are not end-of-scope std::set mNotScope; private: @@ -277,6 +289,7 @@ namespace clangimport { Token *createTokens(TokenList *tokenList); Token *addtoken(TokenList *tokenList, const std::string &str, bool valueType=true); const ::Type *addTypeTokens(TokenList *tokenList, const std::string &str, const Scope *scope = nullptr); + void addFullScopeNameTokens(TokenList *tokenList, const Scope *recordScope); Scope *createScope(TokenList *tokenList, Scope::ScopeType scopeType, AstNodePtr astNode, const Token *def); Scope *createScope(TokenList *tokenList, Scope::ScopeType scopeType, const std::vector &children, const Token *def); Token *createTokensCall(TokenList *tokenList); @@ -475,6 +488,23 @@ const ::Type * clangimport::AstNode::addTypeTokens(TokenList *tokenList, const s return nullptr; } +void clangimport::AstNode::addFullScopeNameTokens(TokenList *tokenList, const Scope *recordScope) +{ + if (!recordScope) + return; + std::list scopes; + while (recordScope && recordScope != tokenList->back()->scope() && !recordScope->isExecutable()) { + scopes.push_front(recordScope); + recordScope = recordScope->nestedIn; + } + for (const Scope *s: scopes) { + if (!s->className.empty()) { + addtoken(tokenList, s->className); + addtoken(tokenList, "::"); + } + } +} + const Scope *clangimport::AstNode::getNestedInScope(TokenList *tokenList) { if (!tokenList->back()) @@ -1127,6 +1157,10 @@ void clangimport::AstNode::createTokensFunctionDecl(TokenList *tokenList) addTypeTokens(tokenList, '\'' + getType() + '\''); startToken = before ? before->next() : tokenList->front(); } + + if (mExtTokens.size() > 4 && mExtTokens[1] == "parent") + addFullScopeNameTokens(tokenList, mData->getScope(mExtTokens[2])); + Token *nameToken = addtoken(tokenList, getSpelling() + getTemplateParameters()); Scope *nestedIn = const_cast(nameToken->scope()); @@ -1249,6 +1283,8 @@ void clangimport::AstNode::createTokensForCXXRecord(TokenList *tokenList) children2.push_back(child); } Scope *scope = createScope(tokenList, isStruct ? Scope::ScopeType::eStruct : Scope::ScopeType::eClass, children2, classToken); + const std::string addr = mExtTokens[0]; + mData->scopeDecl(addr, scope); scope->className = className; mData->mSymbolDatabase->typeList.push_back(Type(classToken, scope, classToken->scope())); scope->definedType = &mData->mSymbolDatabase->typeList.back(); diff --git a/test/testclangimport.cpp b/test/testclangimport.cpp index 49ed2010a..b052d617b 100644 --- a/test/testclangimport.cpp +++ b/test/testclangimport.cpp @@ -500,7 +500,7 @@ private: "| `-CXXMethodDecl 0x21ccc68 col:6 foo 'void ()'\n" "`-CXXMethodDecl 0x21ccd60 parent 0x21cca40 prev 0x21ccc68 col:12 foo 'void ()'\n" " `-CompoundStmt 0x21cce50 "; - ASSERT_EQUALS("class Fred { void foo ( ) ; } ; void foo ( ) { }", parse(clang)); + ASSERT_EQUALS("class Fred { void foo ( ) ; } ; void Fred :: foo ( ) { }", parse(clang)); } void cxxNewExpr1() {