From bd41b3d713aed723a3185543f6b6934f02395527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 13 Jan 2020 05:52:46 +0100 Subject: [PATCH] Clang import; Fixed function arguments in SymbolDatabase --- lib/clangimport.cpp | 11 +++++---- test/testclangimport.cpp | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/lib/clangimport.cpp b/lib/clangimport.cpp index 213c59f81..85d307b0e 100644 --- a/lib/clangimport.cpp +++ b/lib/clangimport.cpp @@ -811,17 +811,20 @@ void clangimport::AstNode::createTokensFunctionDecl(TokenList *tokenList) mData->funcDecl(mExtTokens.front(), nameToken, scope.function); Token *par1 = addtoken(tokenList, "("); // Function arguments - for (AstNodePtr child: children) { + for (int i = 0; i < children.size(); ++i) { + AstNodePtr child = children[i]; if (child->nodeType != ParmVarDecl) continue; if (tokenList->back() != par1) addtoken(tokenList, ","); addTypeTokens(tokenList, child->mExtTokens.back()); const std::string spelling = child->getSpelling(); - if (!spelling.empty()) { + Token *vartok = nullptr; + if (!spelling.empty()) + vartok = child->addtoken(tokenList, spelling); + scope.function->argumentList.push_back(Variable(vartok, child->getType(), i, AccessControl::Argument, nullptr, &scope)); + if (vartok) { const std::string addr = child->mExtTokens[0]; - Token *vartok = addtoken(tokenList, spelling); - scope.function->argumentList.push_back(Variable(vartok, nullptr, nullptr, 0, AccessControl::Argument, nullptr, &scope, nullptr)); mData->varDecl(addr, vartok, &scope.function->argumentList.back()); } } diff --git a/test/testclangimport.cpp b/test/testclangimport.cpp index 99c29ef17..dfcb65e16 100644 --- a/test/testclangimport.cpp +++ b/test/testclangimport.cpp @@ -16,6 +16,7 @@ #include "clangimport.h" #include "settings.h" +#include "symboldatabase.h" #include "tokenize.h" #include "testsuite.h" @@ -71,6 +72,9 @@ private: TEST_CASE(vardecl4); TEST_CASE(vardecl5); TEST_CASE(whileStmt); + + TEST_CASE(symbolDatabase1); + TEST_CASE(symbolDatabase2); } std::string parse(const char clang[]) { @@ -558,6 +562,50 @@ private: "while ( 0 ) { ; } }", parse(clang)); } + +#define GET_SYMBOL_DB(clang) \ + Settings settings; \ + Tokenizer tokenizer(&settings, this); \ + std::istringstream istr(clang); \ + clangimport::parseClangAstDump(&tokenizer, istr); \ + const SymbolDatabase *db = tokenizer.getSymbolDatabase(); \ + ASSERT(db); + + void symbolDatabase1() { + const char clang[] = "|-FunctionDecl 0x3aea7a0 <1.cpp:2:1, col:22> col:6 used foo 'void (int, int)'\n" + "| |-ParmVarDecl 0x3aea650 col:14 x 'int'\n" + "| `-ParmVarDecl 0x3aea6c8 col:21 y 'int'\n"; + + GET_SYMBOL_DB(clang); + + // There is a function foo that has 2 arguments + ASSERT_EQUALS(1, db->functionScopes.size()); + const Scope *scope = db->functionScopes[0]; + const Function *func = scope->function; + ASSERT_EQUALS(2, func->argCount()); + ASSERT_EQUALS("x", func->getArgumentVar(0)->name()); + ASSERT_EQUALS("y", func->getArgumentVar(1)->name()); + ASSERT_EQUALS(ValueType::Type::INT, func->getArgumentVar(0)->valueType()->type); + ASSERT_EQUALS(ValueType::Type::INT, func->getArgumentVar(1)->valueType()->type); + } + + void symbolDatabase2() { + const char clang[] = "|-FunctionDecl 0x3aea7a0 <1.cpp:2:1, col:22> col:6 used foo 'void (int, int)'\n" + "| |-ParmVarDecl 0x3aea650 col:14 'int'\n" + "| `-ParmVarDecl 0x3aea6c8 col:21 'int'\n"; + + GET_SYMBOL_DB(clang); + + // There is a function foo that has 2 arguments + ASSERT_EQUALS(1, db->functionScopes.size()); + const Scope *scope = db->functionScopes[0]; + const Function *func = scope->function; + ASSERT_EQUALS(2, func->argCount()); + ASSERT_EQUALS(0, (long long)func->getArgumentVar(0)->nameToken()); + ASSERT_EQUALS(0, (long long)func->getArgumentVar(1)->nameToken()); + //ASSERT_EQUALS(ValueType::Type::INT, func->getArgumentVar(0)->valueType()->type); + //ASSERT_EQUALS(ValueType::Type::INT, func->getArgumentVar(1)->valueType()->type); + } }; REGISTER_TEST(TestClangImport)