Clang import; Better type

This commit is contained in:
Daniel Marjamäki 2020-01-13 17:56:30 +01:00
parent a17ded3862
commit dfbf347912
2 changed files with 49 additions and 24 deletions

View File

@ -232,7 +232,7 @@ namespace clangimport {
void createTokensForCXXRecord(TokenList *tokenList);
Token *createTokensVarDecl(TokenList *tokenList);
std::string getSpelling() const;
std::string getType() const;
std::string getType(int index = 0) const;
std::string getTemplateParameters() const;
const Scope *getNestedInScope(TokenList *tokenList);
void setValueType(Token *tok);
@ -271,7 +271,7 @@ std::string clangimport::AstNode::getSpelling() const
return str;
}
std::string clangimport::AstNode::getType() const
std::string clangimport::AstNode::getType(int index) const
{
int typeIndex = 1;
while (typeIndex < mExtTokens.size() && mExtTokens[typeIndex][0] != '\'')
@ -279,8 +279,12 @@ std::string clangimport::AstNode::getType() const
if (typeIndex >= mExtTokens.size())
return "";
std::string type = mExtTokens[typeIndex];
if (type.find("\':\'") != std::string::npos)
type.erase(type.find("\':\'") + 1);
if (type.find("\':\'") != std::string::npos) {
if (index == 0)
type.erase(type.find("\':\'") + 1);
else
type.erase(0, type.find("\':\'") + 2);
}
if (type.find(" (") != std::string::npos) {
std::string::size_type pos = type.find(" (");
type[pos] = '\'';
@ -398,22 +402,25 @@ const Scope *clangimport::AstNode::getNestedInScope(TokenList *tokenList)
void clangimport::AstNode::setValueType(Token *tok)
{
const std::string &type = getType();
for (int i = 0; i < 2; i++) {
const std::string &type = getType(i);
if (type.find("<") != std::string::npos) {
// TODO
tok->setValueType(new ValueType(ValueType::Sign::UNKNOWN_SIGN, ValueType::Type::NONSTD, 0));
return;
if (type.find("<") != std::string::npos)
// TODO
continue;
TokenList decl(nullptr);
addTypeTokens(&decl, type);
if (!decl.front())
break;
ValueType valueType = ValueType::parseDecl(decl.front(), mData->mSettings);
if (valueType.type != ValueType::Type::UNKNOWN_TYPE) {
tok->setValueType(new ValueType(valueType));
break;
}
}
TokenList decl(nullptr);
addTypeTokens(&decl, type);
if (!decl.front())
return;
ValueType valueType = ValueType::parseDecl(decl.front(), mData->mSettings);
if (valueType.type != ValueType::Type::UNKNOWN_TYPE)
tok->setValueType(new ValueType(valueType));
return;
}
Scope *clangimport::AstNode::createScope(TokenList *tokenList, Scope::ScopeType scopeType, AstNodePtr astNode)

View File

@ -74,8 +74,9 @@ private:
TEST_CASE(vardecl5);
TEST_CASE(whileStmt);
TEST_CASE(symbolDatabase1);
TEST_CASE(symbolDatabase2);
TEST_CASE(symbolDatabaseFunction1);
TEST_CASE(symbolDatabaseFunction2);
TEST_CASE(symbolDatabaseNodeType1);
}
std::string parse(const char clang[]) {
@ -581,7 +582,7 @@ private:
const SymbolDatabase *db = tokenizer.getSymbolDatabase(); \
ASSERT(db);
void symbolDatabase1() {
void symbolDatabaseFunction1() {
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";
@ -599,7 +600,7 @@ private:
ASSERT_EQUALS(ValueType::Type::INT, func->getArgumentVar(1)->valueType()->type);
}
void symbolDatabase2() {
void symbolDatabaseFunction2() {
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";
@ -613,8 +614,25 @@ private:
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);
}
void symbolDatabaseNodeType1() {
const char clang[] = "`-FunctionDecl 0x32438c0 <line:5:1, line:7:1> line:5:6 foo 'a::b (a::b)'\n"
" |-ParmVarDecl 0x32437b0 <col:10, col:15> col:15 used i 'a::b':'long'\n"
" `-CompoundStmt 0x3243a60 <col:18, line:7:1>\n"
" `-ReturnStmt 0x3243a48 <line:6:3, col:12>\n"
" `-BinaryOperator 0x3243a20 <col:10, col:12> 'long' '+'\n"
" |-ImplicitCastExpr 0x32439f0 <col:10> 'a::b':'long' <LValueToRValue>\n"
" | `-DeclRefExpr 0x32439a8 <col:10> 'a::b':'long' lvalue ParmVar 0x32437b0 'i' 'a::b':'long'\n"
" `-ImplicitCastExpr 0x3243a08 <col:12> 'long' <IntegralCast>\n"
" `-IntegerLiteral 0x32439d0 <col:12> 'int' 1\n";
GET_SYMBOL_DB(clang);
const Token *tok = Token::findsimplematch(tokenizer.tokens(), "i + 1");
ASSERT(!!tok);
ASSERT(!!tok->valueType());
ASSERT_EQUALS("signed long", tok->valueType()->str());
}
};