Skip to content

Commit 3b3df10

Browse files
committed
clang import: reject address-only function and enumerator declarations
1 parent 7d49e01 commit 3b3df10

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎lib/clangimport.cpp‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,10 @@ Token *clangimport::AstNode::createTokens(TokenList &tokenList)
10551055
return nullptr;
10561056
}
10571057
if (nodeType == EnumConstantDecl) {
1058-
Token *nameToken = addtoken(tokenList, getSpelling());
1058+
const std::string name = getSpelling();
1059+
if (name.empty())
1060+
throw InternalError(nullptr, "invalid AST node: " + nodeType, InternalError::AST);
1061+
Token *nameToken = addtoken(tokenList, name);
10591062
auto *scope = const_cast<Scope *>(nameToken->scope());
10601063
scope->enumeratorList.emplace_back(nameToken->scope());
10611064
Enumerator *e = &scope->enumeratorList.back();
@@ -1391,6 +1394,10 @@ Token * clangimport::AstNode::createTokensCall(TokenList &tokenList)
13911394

13921395
void clangimport::AstNode::createTokensFunctionDecl(TokenList &tokenList)
13931396
{
1397+
// an address-only node has no name: addtoken("") adds nothing and would return a null back()
1398+
const std::string name = getSpelling();
1399+
if (name.empty())
1400+
throw InternalError(nullptr, "invalid AST node: " + nodeType, InternalError::AST);
13941401
const bool prev = contains(mExtTokens, "prev");
13951402
const bool hasBody = !children.empty() && children.back()->nodeType == CompoundStmt;
13961403
const bool isStatic = contains(mExtTokens, "static");
@@ -1412,7 +1419,7 @@ void clangimport::AstNode::createTokensFunctionDecl(TokenList &tokenList)
14121419
if (mExtTokens.size() > 4 && mExtTokens[1] == "parent")
14131420
addFullScopeNameTokens(tokenList, mData->getScope(mExtTokens[2]));
14141421

1415-
Token *nameToken = addtoken(tokenList, getSpelling() + getTemplateParameters());
1422+
Token *nameToken = addtoken(tokenList, name + getTemplateParameters());
14161423
auto *nestedIn = const_cast<Scope *>(nameToken->scope());
14171424

14181425
if (prev) {

‎test/testclangimport.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,11 @@ class TestClangImport : public TestFixture {
14531453
ASSERT_THROW_INTERNAL_EQUALS(parse("`-EnumDecl "), AST, "invalid AST node: EnumDecl");
14541454
ASSERT_THROW_INTERNAL_EQUALS(parse("`-VarDecl 0x1"), AST, "invalid AST node: VarDecl");
14551455
ASSERT_THROW_INTERNAL_EQUALS(parse("`-FunctionDecl 0x1 <a.cpp:1:1, col:20> col:5 f 'int ()' prev"), AST, "invalid AST node: FunctionDecl");
1456+
// address-only declarations: getSpelling() is empty, addtoken() adds nothing and back() is null
1457+
ASSERT_THROW_INTERNAL_EQUALS(parse("`-FunctionDecl 0x1"), AST, "invalid AST node: FunctionDecl");
1458+
ASSERT_THROW_INTERNAL_EQUALS(parse("`-CXXConstructorDecl 0x1"), AST, "invalid AST node: CXXConstructorDecl");
1459+
ASSERT_THROW_INTERNAL_EQUALS(parse("`-CXXMethodDecl 0x1"), AST, "invalid AST node: CXXMethodDecl");
1460+
ASSERT_THROW_INTERNAL_EQUALS(parse("`-EnumConstantDecl 0x1"), AST, "invalid AST node: EnumConstantDecl");
14561461
}
14571462
};
14581463

0 commit comments

Comments
 (0)