clang import; fix symbol database for 'struct Fred { int a; }; int b; void f(int c, int d) { int e; }'

This commit is contained in:
Daniel Marjamäki 2020-10-06 19:06:10 +02:00
parent 372161c89b
commit 64608f4e95
3 changed files with 16 additions and 7 deletions

View File

@ -22,6 +22,7 @@
#include "tokenize.h"
#include "utils.h"
#include <algorithm>
#include <memory>
#include <vector>
#include <iostream>
@ -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<AstNodePtr> 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<Scope *>(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, "=");

View File

@ -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))

View File

@ -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; }')