Clang Import; Better handling of derived classes

This commit is contained in:
Daniel Marjamäki 2020-11-01 20:32:42 +01:00
parent 70fc2a78e5
commit a2a948a311
3 changed files with 30 additions and 3 deletions

View File

@ -1178,7 +1178,7 @@ void clangimport::AstNode::createTokensFunctionDecl(TokenList *tokenList)
void clangimport::AstNode::createTokensForCXXRecord(TokenList *tokenList)
{
bool isStruct = (std::find(mExtTokens.begin(), mExtTokens.end(), "struct") != mExtTokens.end());
Token *classToken = addtoken(tokenList, isStruct ? "struct" : "class");
Token * const classToken = addtoken(tokenList, isStruct ? "struct" : "class");
std::string className;
if (mExtTokens[mExtTokens.size() - 2] == (isStruct?"struct":"class"))
className = mExtTokens.back();
@ -1186,13 +1186,25 @@ void clangimport::AstNode::createTokensForCXXRecord(TokenList *tokenList)
className = mExtTokens[mExtTokens.size() - 2];
className += getTemplateParameters();
/*Token *nameToken =*/ addtoken(tokenList, className);
// base classes
bool firstBase = true;
for (AstNodePtr child: children) {
if (child->nodeType == "public" || child->nodeType == "protected" || child->nodeType == "private") {
addtoken(tokenList, firstBase ? ":" : ",");
addtoken(tokenList, child->nodeType);
addtoken(tokenList, unquote(child->mExtTokens.back()));
firstBase = false;
}
}
// definition
if (isDefinition()) {
std::vector<AstNodePtr> children2;
for (AstNodePtr child: children) {
if (child->nodeType == CXXConstructorDecl ||
child->nodeType == CXXDestructorDecl ||
child->nodeType == CXXMethodDecl ||
child->nodeType == FieldDecl)
child->nodeType == FieldDecl ||
child->nodeType == VarDecl)
children2.push_back(child);
}
Scope *scope = createScope(tokenList, isStruct ? Scope::ScopeType::eStruct : Scope::ScopeType::eClass, children2, classToken);
@ -1201,6 +1213,7 @@ void clangimport::AstNode::createTokensForCXXRecord(TokenList *tokenList)
scope->definedType = &mData->mSymbolDatabase->typeList.back();
}
addtoken(tokenList, ";");
const_cast<Token *>(tokenList->back())->scope(classToken->scope());
}
Token * clangimport::AstNode::createTokensVarDecl(TokenList *tokenList)

View File

@ -2045,7 +2045,8 @@ std::string Variable::getTypeName() const
return ret;
}
static bool isOperator(const Token *tokenDef) {
static bool isOperator(const Token *tokenDef)
{
if (!tokenDef)
return false;
if (tokenDef->isOperatorKeyword())

View File

@ -61,6 +61,7 @@ private:
TEST_CASE(cxxOperatorCallExpr);
TEST_CASE(cxxRecordDecl1);
TEST_CASE(cxxRecordDecl2);
TEST_CASE(cxxRecordDeclDerived);
TEST_CASE(cxxStaticCastExpr1);
TEST_CASE(cxxStaticCastExpr2);
TEST_CASE(cxxStdInitializerListExpr);
@ -545,6 +546,18 @@ private:
ASSERT_EQUALS("struct Foo { } ;", parse(clang));
}
void cxxRecordDeclDerived() {
const char clang[] = "|-CXXRecordDecl 0x19ccd38 <e.cpp:4:1, line:6:1> line:4:8 referenced struct base definition\n"
"| `-VarDecl 0x19ccf00 <line:5:5, col:35> col:27 value 'const bool' static constexpr cinit\n"
"| |-value: Int 0\n"
"| `-CXXBoolLiteralExpr 0x19ccf68 <col:35> 'bool' false\n"
"`-CXXRecordDecl 0x19ccfe8 <line:8:1, col:32> col:8 struct derived definition\n"
" |-public 'base'\n"
" `-CXXRecordDecl 0x19cd150 <col:1, col:8> col:8 implicit struct derived";
ASSERT_EQUALS("struct base { static const bool value@1 = false ; } ; struct derived : public base { } ;", parse(clang));
}
void cxxStaticCastExpr1() {
const char clang[] = "`-VarDecl 0x2e0e650 <a.cpp:2:1, col:27> col:5 a 'int' cinit\n"
" `-CXXStaticCastExpr 0x2e0e728 <col:9, col:27> 'int' static_cast<int> <NoOp>\n"