Skip to content

Commit 9ed4dc4

Browse files
committed
Clang Import: Fix AST for 'new S;'
1 parent de19dc9 commit 9ed4dc4

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/clangimport.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,10 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
694694
return createTokensCall(tokenList);
695695
if (nodeType == CXXNewExpr) {
696696
Token *newtok = addtoken(tokenList, "new");
697+
if (children.size() == 1 && children[0]->nodeType == CXXConstructExpr) {
698+
newtok->astOperand1(children[0]->createTokens(tokenList));
699+
return newtok;
700+
}
697701
std::string type = getType();
698702
if (type.find("*") != std::string::npos)
699703
type = type.erase(type.rfind("*"));

test/testclangimport.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class TestClangImport: public TestFixture {
5555
TEST_CASE(cxxMethodDecl1);
5656
TEST_CASE(cxxMethodDecl2);
5757
TEST_CASE(cxxMethodDecl3);
58-
TEST_CASE(cxxNewExpr);
58+
TEST_CASE(cxxNewExpr1);
59+
TEST_CASE(cxxNewExpr2);
5960
TEST_CASE(cxxNullPtrLiteralExpr);
6061
TEST_CASE(cxxOperatorCallExpr);
6162
TEST_CASE(cxxRecordDecl1);
@@ -504,7 +505,7 @@ class TestClangImport: public TestFixture {
504505
"void foo ( ) { }", parse(clang));
505506
}
506507

507-
void cxxNewExpr() {
508+
void cxxNewExpr1() {
508509
const char clang[] = "|-VarDecl 0x3a97680 <1.cpp:2:1, col:14> col:6 i 'int *' cinit\n"
509510
"| `-CXXNewExpr 0x3a97d18 <col:10, col:14> 'int *' Function 0x3a97778 'operator new' 'void *(unsigned long)'\n"
510511
"`-VarDecl 0x3a97d80 <line:3:1, col:21> col:6 j 'int *' cinit\n"
@@ -516,6 +517,17 @@ class TestClangImport: public TestFixture {
516517
parse(clang));
517518
}
518519

520+
void cxxNewExpr2() {
521+
const char clang[] = "|-FunctionDecl 0x59a188 <line:7:1, line:9:1> line:7:11 f 'struct S *()'\n"
522+
"| `-CompoundStmt 0x5c4318 <col:15, line:9:1>\n"
523+
"| `-ReturnStmt 0x5c4308 <line:8:3, col:14>\n"
524+
"| `-CXXNewExpr 0x5c42c8 <col:10, col:14> 'S *' Function 0x59a378 'operator new' 'void *(unsigned long)'\n"
525+
"| `-CXXConstructExpr 0x5c42a0 <col:14> 'S' 'void () noexcept'";
526+
ASSERT_EQUALS("struct S * f ( ) {\n"
527+
"return new S ( ) ; }",
528+
parse(clang));
529+
}
530+
519531
void cxxNullPtrLiteralExpr() {
520532
const char clang[] = "`-VarDecl 0x2a7d650 <1.cpp:1:1, col:17> col:13 p 'const char *' cinit\n"
521533
" `-ImplicitCastExpr 0x2a7d708 <col:17> 'const char *' <NullToPointer>\n"

0 commit comments

Comments
 (0)