Clang import; Fixed function arguments in SymbolDatabase

This commit is contained in:
Daniel Marjamäki 2020-01-13 05:52:46 +01:00
parent 9f3df5d630
commit bd41b3d713
2 changed files with 55 additions and 4 deletions

View File

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

View File

@ -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:10, col:14> col:14 x 'int'\n"
"| `-ParmVarDecl 0x3aea6c8 <col:17, col:21> 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:10, col:14> col:14 'int'\n"
"| `-ParmVarDecl 0x3aea6c8 <col:17, col:21> 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)