From 226e996e4675e73d19adbb6b51ba6a444cb7467c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 1 Nov 2020 08:44:38 +0100 Subject: [PATCH] Clang import: Improved handling of empty structs/classes --- lib/clangimport.cpp | 24 ++++++++++++++---------- test/testclangimport.cpp | 2 +- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/clangimport.cpp b/lib/clangimport.cpp index 612ca10a5..2337fcd4a 100644 --- a/lib/clangimport.cpp +++ b/lib/clangimport.cpp @@ -717,8 +717,7 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList) if (nodeType == CXXOperatorCallExpr) return createTokensCall(tokenList); if (nodeType == CXXRecordDecl) { - if (!children.empty()) - createTokensForCXXRecord(tokenList); + createTokensForCXXRecord(tokenList); return nullptr; } if (nodeType == CXXStaticCastExpr || nodeType == CXXFunctionalCastExpr) { @@ -957,13 +956,13 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList) addtoken(tokenList, getSpelling()); if (children.empty()) addtoken(tokenList, ";"); - else { - Scope *recordScope = createScope(tokenList, Scope::ScopeType::eStruct, children, classDef); - mData->mSymbolDatabase->typeList.push_back(Type(classDef, recordScope, classDef->scope())); - recordScope->definedType = &mData->mSymbolDatabase->typeList.back(); - if (!recordName.empty()) - const_cast(classDef->scope())->definedTypesMap[recordName] = recordScope->definedType; - } + + Scope *recordScope = createScope(tokenList, Scope::ScopeType::eStruct, children, classDef); + mData->mSymbolDatabase->typeList.push_back(Type(classDef, recordScope, classDef->scope())); + recordScope->definedType = &mData->mSymbolDatabase->typeList.back(); + if (!recordName.empty()) + const_cast(classDef->scope())->definedTypesMap[recordName] = recordScope->definedType; + return nullptr; } if (nodeType == ReturnStmt) { @@ -1175,7 +1174,12 @@ void clangimport::AstNode::createTokensForCXXRecord(TokenList *tokenList) { bool isStruct = (std::find(mExtTokens.begin(), mExtTokens.end(), "struct") != mExtTokens.end()); Token *classToken = addtoken(tokenList, isStruct ? "struct" : "class"); - const std::string className = mExtTokens[mExtTokens.size() - 2] + getTemplateParameters(); + std::string className; + if (mExtTokens[mExtTokens.size() - 2] == (isStruct?"struct":"class")) + className = mExtTokens.back(); + else + className = mExtTokens[mExtTokens.size() - 2]; + className += getTemplateParameters(); /*Token *nameToken =*/ addtoken(tokenList, className); std::vector children2; for (AstNodePtr child: children) { diff --git a/test/testclangimport.cpp b/test/testclangimport.cpp index 541ea8a81..af0f48264 100644 --- a/test/testclangimport.cpp +++ b/test/testclangimport.cpp @@ -551,7 +551,7 @@ private: void cxxRecordDecl1() { const char clang[] = "`-CXXRecordDecl 0x34cc5f8 <1.cpp:2:1, col:7> col:7 class Foo"; - ASSERT_EQUALS("", parse(clang)); + ASSERT_EQUALS("class Foo ;", parse(clang)); } void cxxStaticCastExpr1() {