From 64608f4e95ce3541233eed862391049d07b47597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 6 Oct 2020 19:06:10 +0200 Subject: [PATCH] clang import; fix symbol database for 'struct Fred { int a; }; int b; void f(int c, int d) { int e; }' --- lib/clangimport.cpp | 9 +++++---- lib/symboldatabase.cpp | 7 ++++++- test/cli/test-clang-import.py | 7 +++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/clangimport.cpp b/lib/clangimport.cpp index 5428ec692..d2cd2ff6b 100644 --- a/lib/clangimport.cpp +++ b/lib/clangimport.cpp @@ -22,6 +22,7 @@ #include "tokenize.h" #include "utils.h" +#include #include #include #include @@ -1158,7 +1159,8 @@ void clangimport::AstNode::createTokensFunctionDecl(TokenList *tokenList) void clangimport::AstNode::createTokensForCXXRecord(TokenList *tokenList) { - Token *classToken = addtoken(tokenList, "class"); + bool isStruct = (std::find(mExtTokens.begin(), mExtTokens.end(), "struct") != mExtTokens.end()); + Token *classToken = addtoken(tokenList, isStruct ? "struct" : "class"); const std::string className = mExtTokens[mExtTokens.size() - 2] + getTemplateParameters(); /*Token *nameToken =*/ addtoken(tokenList, className); std::vector children2; @@ -1173,7 +1175,7 @@ void clangimport::AstNode::createTokensForCXXRecord(TokenList *tokenList) addtoken(tokenList, ";"); return; } - Scope *scope = createScope(tokenList, Scope::ScopeType::eClass, children2, classToken); + Scope *scope = createScope(tokenList, isStruct ? Scope::ScopeType::eStruct : Scope::ScopeType::eClass, children2, classToken); scope->className = className; mData->mSymbolDatabase->typeList.push_back(Type(classToken, scope, classToken->scope())); scope->definedType = &mData->mSymbolDatabase->typeList.back(); @@ -1198,8 +1200,7 @@ Token * clangimport::AstNode::createTokensVarDecl(TokenList *tokenList) } Token *vartok1 = addtoken(tokenList, name); Scope *scope = const_cast(tokenList->back()->scope()); - const AccessControl accessControl = (scope->type == Scope::ScopeType::eGlobal) ? (AccessControl::Global) : (AccessControl::Local); - scope->varlist.push_back(Variable(vartok1, type, startToken, 0, accessControl, nullptr, scope)); + scope->varlist.push_back(Variable(vartok1, type, startToken, 0, scope->defaultAccess(), nullptr, scope)); mData->varDecl(addr, vartok1, &scope->varlist.back()); if (mExtTokens.back() == "cinit" && !children.empty()) { Token *eq = addtoken(tokenList, "="); diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index c2c95d309..30ccc04e6 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1856,7 +1856,12 @@ Variable::Variable(const Token *name_, const std::string &clangType, const Token mScope(scope_), mValueType(nullptr) { - if (start && start->str() == "static") + if (!mTypeStartToken && mTypeEndToken) { + mTypeStartToken = mTypeEndToken; + while (Token::Match(mTypeStartToken->previous(), "%type%|*|&")) + mTypeStartToken = mTypeStartToken->previous(); + } + if (Token::simpleMatch(mTypeStartToken, "static")) setFlag(fIsStatic, true); if (endsWith(clangType, " &", 2)) diff --git a/test/cli/test-clang-import.py b/test/cli/test-clang-import.py index 2811e23f6..055b6b04a 100644 --- a/test/cli/test-clang-import.py +++ b/test/cli/test-clang-import.py @@ -75,13 +75,16 @@ def todo_check_ast(code): -def test1(): +def test_symbol_database_1(): check_symbol_database('int main(){return 0;}') -def test2(): +def test_symbol_database_2(): code = 'struct Foo { void f(); }; void Foo::f() {}' check_symbol_database(code) +def test_symbol_database_3(): + check_symbol_database('struct Fred { int a; }; int b; void f(int c, int d) { int e; }') + def test_ast_calculations(): check_ast('int x = 5; int y = (x + 4) * 2;') check_ast('long long dostuff(int x) { return x ? 3 : 5; }')