Clang import: Do not write unreferenced enum declaration

This commit is contained in:
Daniel Marjamäki 2020-12-29 15:39:33 +01:00
parent 1e9f67936e
commit 4bd5933691
2 changed files with 22 additions and 4 deletions

View File

@ -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();

View File

@ -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 <line:3:1, col:16> col:6 referenced abc\n"
" |-EnumConstantDecl 0x2660720 <col:11> col:11 referenced a 'abc'\n"
" |-EnumConstantDecl 0x2660768 <col:13> 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 <col:12, col:56>\n"