Skip to content

Commit a2a948a

Browse files
committed
Clang Import; Better handling of derived classes
1 parent 70fc2a7 commit a2a948a

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

lib/clangimport.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,21 +1178,33 @@ void clangimport::AstNode::createTokensFunctionDecl(TokenList *tokenList)
11781178
void clangimport::AstNode::createTokensForCXXRecord(TokenList *tokenList)
11791179
{
11801180
bool isStruct = (std::find(mExtTokens.begin(), mExtTokens.end(), "struct") != mExtTokens.end());
1181-
Token *classToken = addtoken(tokenList, isStruct ? "struct" : "class");
1181+
Token * const classToken = addtoken(tokenList, isStruct ? "struct" : "class");
11821182
std::string className;
11831183
if (mExtTokens[mExtTokens.size() - 2] == (isStruct?"struct":"class"))
11841184
className = mExtTokens.back();
11851185
else
11861186
className = mExtTokens[mExtTokens.size() - 2];
11871187
className += getTemplateParameters();
11881188
/*Token *nameToken =*/ addtoken(tokenList, className);
1189+
// base classes
1190+
bool firstBase = true;
1191+
for (AstNodePtr child: children) {
1192+
if (child->nodeType == "public" || child->nodeType == "protected" || child->nodeType == "private") {
1193+
addtoken(tokenList, firstBase ? ":" : ",");
1194+
addtoken(tokenList, child->nodeType);
1195+
addtoken(tokenList, unquote(child->mExtTokens.back()));
1196+
firstBase = false;
1197+
}
1198+
}
1199+
// definition
11891200
if (isDefinition()) {
11901201
std::vector<AstNodePtr> children2;
11911202
for (AstNodePtr child: children) {
11921203
if (child->nodeType == CXXConstructorDecl ||
11931204
child->nodeType == CXXDestructorDecl ||
11941205
child->nodeType == CXXMethodDecl ||
1195-
child->nodeType == FieldDecl)
1206+
child->nodeType == FieldDecl ||
1207+
child->nodeType == VarDecl)
11961208
children2.push_back(child);
11971209
}
11981210
Scope *scope = createScope(tokenList, isStruct ? Scope::ScopeType::eStruct : Scope::ScopeType::eClass, children2, classToken);
@@ -1201,6 +1213,7 @@ void clangimport::AstNode::createTokensForCXXRecord(TokenList *tokenList)
12011213
scope->definedType = &mData->mSymbolDatabase->typeList.back();
12021214
}
12031215
addtoken(tokenList, ";");
1216+
const_cast<Token *>(tokenList->back())->scope(classToken->scope());
12041217
}
12051218

12061219
Token * clangimport::AstNode::createTokensVarDecl(TokenList *tokenList)

lib/symboldatabase.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2045,7 +2045,8 @@ std::string Variable::getTypeName() const
20452045
return ret;
20462046
}
20472047

2048-
static bool isOperator(const Token *tokenDef) {
2048+
static bool isOperator(const Token *tokenDef)
2049+
{
20492050
if (!tokenDef)
20502051
return false;
20512052
if (tokenDef->isOperatorKeyword())

test/testclangimport.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class TestClangImport: public TestFixture {
6161
TEST_CASE(cxxOperatorCallExpr);
6262
TEST_CASE(cxxRecordDecl1);
6363
TEST_CASE(cxxRecordDecl2);
64+
TEST_CASE(cxxRecordDeclDerived);
6465
TEST_CASE(cxxStaticCastExpr1);
6566
TEST_CASE(cxxStaticCastExpr2);
6667
TEST_CASE(cxxStdInitializerListExpr);
@@ -545,6 +546,18 @@ class TestClangImport: public TestFixture {
545546
ASSERT_EQUALS("struct Foo { } ;", parse(clang));
546547
}
547548

549+
void cxxRecordDeclDerived() {
550+
const char clang[] = "|-CXXRecordDecl 0x19ccd38 <e.cpp:4:1, line:6:1> line:4:8 referenced struct base definition\n"
551+
"| `-VarDecl 0x19ccf00 <line:5:5, col:35> col:27 value 'const bool' static constexpr cinit\n"
552+
"| |-value: Int 0\n"
553+
"| `-CXXBoolLiteralExpr 0x19ccf68 <col:35> 'bool' false\n"
554+
"`-CXXRecordDecl 0x19ccfe8 <line:8:1, col:32> col:8 struct derived definition\n"
555+
" |-public 'base'\n"
556+
" `-CXXRecordDecl 0x19cd150 <col:1, col:8> col:8 implicit struct derived";
557+
558+
ASSERT_EQUALS("struct base { static const bool value@1 = false ; } ; struct derived : public base { } ;", parse(clang));
559+
}
560+
548561
void cxxStaticCastExpr1() {
549562
const char clang[] = "`-VarDecl 0x2e0e650 <a.cpp:2:1, col:27> col:5 a 'int' cinit\n"
550563
" `-CXXStaticCastExpr 0x2e0e728 <col:9, col:27> 'int' static_cast<int> <NoOp>\n"

0 commit comments

Comments
 (0)