Skip to content

Commit 4bd5933

Browse files
committed
Clang import: Do not write unreferenced enum declaration
1 parent 1e9f679 commit 4bd5933

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

lib/clangimport.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -893,11 +893,22 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
893893
return nameToken;
894894
}
895895
if (nodeType == EnumDecl) {
896+
int colIndex = mExtTokens.size() - 1;
897+
while (colIndex > 0 && mExtTokens[colIndex].compare(0,4,"col:") != 0)
898+
--colIndex;
899+
if (colIndex == 0 || colIndex + 2 >= mExtTokens.size() || mExtTokens[colIndex + 1] != "referenced")
900+
return nullptr;
901+
896902
mData->enumValue = 0;
897903
Token *enumtok = addtoken(tokenList, "enum");
898904
Token *nametok = nullptr;
899-
if (mExtTokens[mExtTokens.size() - 3].compare(0,4,"col:") == 0)
900-
nametok = addtoken(tokenList, mExtTokens.back());
905+
{
906+
int nameIndex = mExtTokens.size() - 1;
907+
while (nameIndex > 0 && mExtTokens[nameIndex][0] == '\'')
908+
--nameIndex;
909+
if (nameIndex > colIndex + 1)
910+
nametok = addtoken(tokenList, mExtTokens[nameIndex]);
911+
}
901912
Scope *enumscope = createScope(tokenList, Scope::ScopeType::eEnum, children, enumtok);
902913
if (nametok)
903914
enumscope->className = nametok->str();

test/testclangimport.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ class TestClangImport: public TestFixture {
7171
TEST_CASE(cxxThrowExpr);
7272
TEST_CASE(defaultStmt);
7373
TEST_CASE(doStmt);
74-
TEST_CASE(enumDecl);
74+
TEST_CASE(enumDecl1);
75+
TEST_CASE(enumDecl2);
7576
TEST_CASE(forStmt);
7677
TEST_CASE(funcdecl1);
7778
TEST_CASE(funcdecl2);
@@ -668,14 +669,20 @@ class TestClangImport: public TestFixture {
668669
ASSERT_EQUALS("void foo ( ) { do { ++ x ; } while ( 1 ) ; }", parse(clang));
669670
}
670671

671-
void enumDecl() {
672+
void enumDecl1() {
672673
const char clang[] = "`-EnumDecl 0x2660660 <line:3:1, col:16> col:6 referenced abc\n"
673674
" |-EnumConstantDecl 0x2660720 <col:11> col:11 referenced a 'abc'\n"
674675
" |-EnumConstantDecl 0x2660768 <col:13> col:13 b 'abc'\n"
675676
" `-EnumConstantDecl 0x26607b0 <col:15> col:15 c 'abc'";
676677
ASSERT_EQUALS("enum abc { a , b , c }", parse(clang));
677678
}
678679

680+
void enumDecl2() {
681+
// enum syntax_option_type : unsigned int { };
682+
const char clang[] = "`-EnumDecl 0xb55d50 <2.cpp:4:3, col:44> col:8 syntax_option_type 'unsigned int'";
683+
ASSERT_EQUALS("", parse(clang));
684+
}
685+
679686
void forStmt() {
680687
const char clang[] = "`-FunctionDecl 0x2f93ae0 <1.c:1:1, col:56> col:5 main 'int ()'\n"
681688
" `-CompoundStmt 0x2f93dc0 <col:12, col:56>\n"

0 commit comments

Comments
 (0)