diff --git a/lib/clangastdump.cpp b/lib/clangastdump.cpp index bf875daf5..7a1e5b271 100644 --- a/lib/clangastdump.cpp +++ b/lib/clangastdump.cpp @@ -24,6 +24,7 @@ #include #include +static const std::string ArraySubscriptExpr = "ArraySubscriptExpr"; static const std::string BinaryOperator = "BinaryOperator"; static const std::string CallExpr = "CallExpr"; static const std::string CompoundStmt = "CompoundStmt"; @@ -246,6 +247,16 @@ Scope *clangastdump::AstNode::createScope(TokenList *tokenList, Scope::ScopeType Token *clangastdump::AstNode::createTokens(TokenList *tokenList) { + if (nodeType == ArraySubscriptExpr) { + Token *array = children[0]->createTokens(tokenList); + Token *bracket1 = addtoken(tokenList, "["); + Token *index = children[1]->createTokens(tokenList); + Token *bracket2 = addtoken(tokenList, "]"); + bracket1->astOperand1(array); + bracket1->astOperand2(index); + bracket1->link(bracket2); + return bracket1; + } if (nodeType == BinaryOperator) { Token *tok1 = children[0]->createTokens(tokenList); Token *binop = addtoken(tokenList, unquote(mExtTokens.back())); @@ -367,7 +378,7 @@ Token *clangastdump::AstNode::createTokens(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, nullptr, nullptr, 0, accessControl, nullptr, scope, nullptr)); + scope->varlist.push_back(Variable(vartok1, type, 0, accessControl, nullptr, scope)); mData->varDecl(addr, vartok1, &scope->varlist.back()); addtoken(tokenList, ";"); if (isInit) { diff --git a/lib/clangastdump.h b/lib/clangastdump.h index 0d712d824..43c5870a0 100644 --- a/lib/clangastdump.h +++ b/lib/clangastdump.h @@ -25,7 +25,6 @@ #include #include -class SymbolDatabase; class Tokenizer; namespace clangastdump { diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 0a88f179f..2907afdbd 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1723,6 +1723,36 @@ void SymbolDatabase::clangSetVariables(const std::vector &vari mVariableList = variableList; } +Variable::Variable(const Token *name_, const std::string &clangType, + nonneg int index_, AccessControl access_, const Type *type_, + const Scope *scope_) + : mNameToken(name_), + mTypeStartToken(nullptr), + mTypeEndToken(nullptr), + mIndex(index_), + mAccess(access_), + mFlags(0), + mType(type_), + mScope(scope_), + mValueType(nullptr) +{ + + std::string::size_type pos = clangType.find("["); + if (pos != std::string::npos) { + setFlag(fIsArray, true); + do { + const std::string::size_type pos1 = pos+1; + pos = clangType.find("]", pos1); + Dimension dim; + dim.tok = nullptr; + dim.known = true; + dim.num = MathLib::toLongNumber(clangType.substr(pos1, pos-pos1)); + mDimensions.push_back(dim); + ++pos; + } while (pos < clangType.size() && clangType[pos] == '['); + } +} + Variable::~Variable() { delete mValueType; diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index c530e7fef..189b3fa4a 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -235,6 +235,10 @@ public: evaluate(settings); } + Variable(const Token *name_, const std::string &clangType, + nonneg int index_, AccessControl access_, const Type *type_, + const Scope *scope_); + ~Variable(); /** diff --git a/test/testclangastdump.cpp b/test/testclangastdump.cpp index fde62d0bc..04b358153 100644 --- a/test/testclangastdump.cpp +++ b/test/testclangastdump.cpp @@ -32,6 +32,7 @@ private: TEST_CASE(funcdecl1); TEST_CASE(funcdecl2); TEST_CASE(vardecl1); + TEST_CASE(vardecl2); } std::string parse(const char clang[]) { @@ -76,6 +77,25 @@ private: "int b@2 ; b@2 = a@1 ;", parse(clang)); } + + void vardecl2() { + const char clang[] = "|-VarDecl 0x3873b50 <1.c:1:1, col:9> col:5 used a 'int [10]'\n" + "`-FunctionDecl 0x3873c38 line:3:6 foo 'void ()'\n" + " `-CompoundStmt 0x3873dd0 \n" + " `-BinaryOperator 0x3873da8 'int' '='\n" + " |-ArraySubscriptExpr 0x3873d60 'int' lvalue\n" + " | |-ImplicitCastExpr 0x3873d48 'int *' \n" + " | | `-DeclRefExpr 0x3873cd8 'int [10]' lvalue Var 0x3873b50 'a' 'int [10]'\n" + " | `-IntegerLiteral 0x3873d00 'int' 0\n" + " `-IntegerLiteral 0x3873d88 'int' 0\n"; + + ASSERT_EQUALS("int[10] a@1 ;\n" + "\n" + "void foo ( ) {\n" + "\n" + "a@1 [ 0 ] = 0 ; }", + parse(clang)); + } }; REGISTER_TEST(TestClangAstDump)