Skip to content

Commit e9042ae

Browse files
authored
fix #13775: internalError with operator new (danmar#7522)
Compiling an empty initializer list as a binary operator would previously take too many tokens from the operand stack.
1 parent beb3362 commit e9042ae

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/tokenlist.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,10 @@ static void compileTerm(Token *&tok, AST_state& state)
834834
if (precedes(tok,end)) // typically for something like `MACRO(x, { if (c) { ... } })`, where end is the last curly, and tok is the open curly for the if
835835
tok = end;
836836
}
837+
} else if (tok->next() == end) {
838+
tok->astOperand1(state.op.top());
839+
state.op.pop();
840+
tok = tok->next();
837841
} else
838842
compileBinOp(tok, state, compileExpression);
839843
if (tok != end)

test/testtokenize.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ class TestTokenizer : public TestFixture {
456456
TEST_CASE(simplifyIfSwitchForInit4);
457457
TEST_CASE(simplifyIfSwitchForInit5);
458458

459+
TEST_CASE(newPlacementArgsCppInit); // #13775
460+
459461
TEST_CASE(cpp20_default_bitfield_initializer);
460462

461463
TEST_CASE(cpp11init);
@@ -8070,6 +8072,18 @@ class TestTokenizer : public TestFixture {
80708072
ASSERT_EQUALS("void f ( ) { if ( [ ] { ; } ) { } }", tokenizeAndStringify(code, settings));
80718073
}
80728074

8075+
void newPlacementArgsCppInit() { // #13775
8076+
const char code[] = "::new(nullptr) int {};";
8077+
SimpleTokenizer tokenizer(settings1, *this);
8078+
tokenizer.tokenize(code);
8079+
const Token *inttok = Token::findsimplematch(tokenizer.tokens(), "int");
8080+
ASSERT(inttok);
8081+
const Token *brace = inttok->next();
8082+
ASSERT(brace);
8083+
ASSERT_EQUALS(brace->astOperand1(), inttok);
8084+
ASSERT_EQUALS(brace->astOperand2(), static_cast<const Token*>(nullptr));
8085+
}
8086+
80738087
void cpp20_default_bitfield_initializer() {
80748088
const Settings s1 = settingsBuilder().cpp(Standards::CPP20).build();
80758089
const char code[] = "struct S { int a:2 = 0; };";

0 commit comments

Comments
 (0)