From 4bd59336918146332b43a45e5494b249aa01b51c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 29 Dec 2020 15:39:33 +0100 Subject: [PATCH] Clang import: Do not write unreferenced enum declaration --- lib/clangimport.cpp | 15 +++++++++++++-- test/testclangimport.cpp | 11 +++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/clangimport.cpp b/lib/clangimport.cpp index a180b5887..abb93ee41 100644 --- a/lib/clangimport.cpp +++ b/lib/clangimport.cpp @@ -893,11 +893,22 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList) return nameToken; } if (nodeType == EnumDecl) { + int colIndex = mExtTokens.size() - 1; + while (colIndex > 0 && mExtTokens[colIndex].compare(0,4,"col:") != 0) + --colIndex; + if (colIndex == 0 || colIndex + 2 >= mExtTokens.size() || mExtTokens[colIndex + 1] != "referenced") + return nullptr; + mData->enumValue = 0; Token *enumtok = addtoken(tokenList, "enum"); Token *nametok = nullptr; - if (mExtTokens[mExtTokens.size() - 3].compare(0,4,"col:") == 0) - nametok = addtoken(tokenList, mExtTokens.back()); + { + int nameIndex = mExtTokens.size() - 1; + while (nameIndex > 0 && mExtTokens[nameIndex][0] == '\'') + --nameIndex; + if (nameIndex > colIndex + 1) + nametok = addtoken(tokenList, mExtTokens[nameIndex]); + } Scope *enumscope = createScope(tokenList, Scope::ScopeType::eEnum, children, enumtok); if (nametok) enumscope->className = nametok->str(); diff --git a/test/testclangimport.cpp b/test/testclangimport.cpp index 9cee38267..b82e86dda 100644 --- a/test/testclangimport.cpp +++ b/test/testclangimport.cpp @@ -71,7 +71,8 @@ private: TEST_CASE(cxxThrowExpr); TEST_CASE(defaultStmt); TEST_CASE(doStmt); - TEST_CASE(enumDecl); + TEST_CASE(enumDecl1); + TEST_CASE(enumDecl2); TEST_CASE(forStmt); TEST_CASE(funcdecl1); TEST_CASE(funcdecl2); @@ -668,7 +669,7 @@ private: ASSERT_EQUALS("void foo ( ) { do { ++ x ; } while ( 1 ) ; }", parse(clang)); } - void enumDecl() { + void enumDecl1() { const char clang[] = "`-EnumDecl 0x2660660 col:6 referenced abc\n" " |-EnumConstantDecl 0x2660720 col:11 referenced a 'abc'\n" " |-EnumConstantDecl 0x2660768 col:13 b 'abc'\n" @@ -676,6 +677,12 @@ private: ASSERT_EQUALS("enum abc { a , b , c }", parse(clang)); } + void enumDecl2() { + // enum syntax_option_type : unsigned int { }; + const char clang[] = "`-EnumDecl 0xb55d50 <2.cpp:4:3, col:44> col:8 syntax_option_type 'unsigned int'"; + ASSERT_EQUALS("", parse(clang)); + } + void forStmt() { const char clang[] = "`-FunctionDecl 0x2f93ae0 <1.c:1:1, col:56> col:5 main 'int ()'\n" " `-CompoundStmt 0x2f93dc0 \n"