Skip to content

Commit 377bd8c

Browse files
autoantwortclaude
andcommitted
Don't take placement new array size as lambda, keep findTypeEnd() unchanged
`new (p) T*[n]{...}` and `new decltype(x)*[n]{...}` in an initializer list were taken as lambdas. Revert the findTypeEnd() change to keep the PR small, lambdas with templates in the trailing return type keep the old behaviour. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c0ebd17 commit 377bd8c

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

‎lib/token.cpp‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,10 +2683,8 @@ bool Token::Impl::getCppcheckAttribute(CppcheckAttributesType attrType, MathLib:
26832683
Token* findTypeEnd(Token* tok)
26842684
{
26852685
while (Token::Match(tok, "%name%|.|::|*|&|&&|<|(|template|decltype|sizeof")) {
2686-
if (tok->str() == "(")
2686+
if (Token::Match(tok, "(|<"))
26872687
tok = tok->link();
2688-
else if (tok->str() == "<") // template brackets are not linked before Tokenizer::createLinks2()
2689-
tok = tok->link() ? tok->link() : tok->findClosingBracket();
26902688
if (!tok)
26912689
return nullptr;
26922690
tok = tok->next();

‎lib/tokenize.cpp‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4768,11 +4768,13 @@ static const Token* findInitListLambdaEnd(const Token* tok)
47684768
{
47694769
if (!Token::simpleMatch(tok, "[") || Token::Match(tok->previous(), "%name%|)|]|>"))
47704770
return nullptr; // array subscript or array size of a new expression
4771-
// array size of a new expression with pointer or reference type: new T*[n]{...}
4772-
for (const Token* prev = tok->previous(); Token::Match(prev, "*|&|&&|::|%name%|>"); prev = prev->previous()) {
4771+
// array size of a new expression with pointer or reference type: new T*[n]{...}, new (p) T*[n]{...}
4772+
for (const Token* prev = tok->previous(); Token::Match(prev, "*|&|&&|::|%name%|>|)"); prev = prev->previous()) {
47734773
if (prev->str() == "new")
47744774
return nullptr;
4775-
if (prev->str() == ">") {
4775+
if (prev->str() == ")")
4776+
prev = prev->link();
4777+
else if (prev->str() == ">") {
47764778
prev = prev->findOpeningBracket();
47774779
if (!prev)
47784780
break;
@@ -4933,7 +4935,8 @@ void Tokenizer::setVarIdPass1()
49334935
if (!initlistLambdaEnds.empty() && initlistLambdaEnds.top() == tok) {
49344936
// end of lambda in initializer list
49354937
initlistLambdaEnds.pop();
4936-
scopeStack.pop();
4938+
if (scopeStack.size() > 1)
4939+
scopeStack.pop();
49374940
variableMap.leaveScope();
49384941
initlist = true;
49394942
}

‎test/testvarid.cpp‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,12 +2820,12 @@ class TestVarID : public TestFixture {
28202820
const char code2[] = "struct S {\n"
28212821
" int x;\n"
28222822
" int* p;\n"
2823-
" S(int* p) : x([p]() -> std::map<int, int> { return {{*p, 1}}; }().size()), p(p) {}\n"
2823+
" S(int* p) : x(*[p]() -> int* { return p; }()), p(p) {}\n"
28242824
"};\n";
28252825
ASSERT_EQUALS("1: struct S {\n"
28262826
"2: int x@1 ;\n"
28272827
"3: int * p@2 ;\n"
2828-
"4: S ( int * p@3 ) : x@1 ( [ p@3 ] ( ) . std :: map < int , int > { return { { * p@3 , 1 } } ; } ( ) . size ( ) ) , p@2 ( p@3 ) { }\n"
2828+
"4: S ( int * p@3 ) : x@1 ( * [ p@3 ] ( ) . int * { return p@3 ; } ( ) ) , p@2 ( p@3 ) { }\n"
28292829
"5: } ;\n",
28302830
tokenize(code2));
28312831

@@ -2837,6 +2837,14 @@ class TestVarID : public TestFixture {
28372837
"struct T {\n"
28382838
" std::vector<int>** r;\n"
28392839
" T(std::vector<int>* c) : r(new std::vector<int>*[2]{ N * c, c }) {}\n"
2840+
"};\n"
2841+
"struct U {\n"
2842+
" int** r;\n"
2843+
" U(int* c) : r(new (std::nothrow) int*[2]{ N * c, c }) {}\n"
2844+
"};\n"
2845+
"struct V {\n"
2846+
" int** r;\n"
2847+
" V(int* c) : r(new decltype(c)*[2]{ N * c, c }) {}\n"
28402848
"};\n";
28412849
ASSERT_EQUALS("1: enum Anonymous0 { N = 2 } ;\n"
28422850
"2: struct S {\n"
@@ -2846,7 +2854,15 @@ class TestVarID : public TestFixture {
28462854
"6: struct T {\n"
28472855
"7: std :: vector < int > * * r@3 ;\n"
28482856
"8: T ( std :: vector < int > * c@4 ) : r@3 ( new std :: vector < int > * [ 2 ] { N * c@4 , c@4 } ) { }\n"
2849-
"9: } ;\n",
2857+
"9: } ;\n"
2858+
"10: struct U {\n"
2859+
"11: int * * r@5 ;\n"
2860+
"12: U ( int * c@6 ) : r@5 ( new ( std :: nothrow ) int * [ 2 ] { N * c@6 , c@6 } ) { }\n"
2861+
"13: } ;\n"
2862+
"14: struct V {\n"
2863+
"15: int * * r@7 ;\n"
2864+
"16: V ( int * c@8 ) : r@7 ( new decltype ( c@8 ) * [ 2 ] { N * c@8 , c@8 } ) { }\n"
2865+
"17: } ;\n",
28502866
tokenize(code3));
28512867
}
28522868

0 commit comments

Comments
 (0)