Skip to content

Commit 4ccfcc6

Browse files
authored
Fix #15159 Wrong varid: function declaration with pointer-to-array parameter of unknown type (#8981)
1 parent 58ee583 commit 4ccfcc6

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

‎lib/tokenize.cpp‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4779,6 +4779,21 @@ static const Token* findInitListLambdaEnd(const Token* tok)
47794779
return findLambdaEndScope(tok);
47804780
}
47814781

4782+
static bool isPointerToArrayDeclarator(const Token* tok, const VariableMap& variableMap)
4783+
{
4784+
if (!Token::Match(tok, "( *|&|&&"))
4785+
return false;
4786+
tok = tok->next();
4787+
while (Token::Match(tok, "*|&|&&|const|volatile"))
4788+
tok = tok->next();
4789+
if (tok && tok->isName()) {
4790+
if (variableMap.hasVariable(tok->str()))
4791+
return false;
4792+
tok = tok->next();
4793+
}
4794+
return Token::simpleMatch(tok, ") [");
4795+
}
4796+
47824797
void Tokenizer::setVarIdPass1()
47834798
{
47844799
const bool cpp = isCPP();
@@ -5058,7 +5073,8 @@ void Tokenizer::setVarIdPass1()
50585073

50595074
if (tok3->isLiteral() ||
50605075
(tok3->isName() && (variableMap.hasVariable(tok3->str()) ||
5061-
(tok3->strAt(-1) == "(" && Token::simpleMatch(tok3->next(), "(") && !Token::simpleMatch(tok3->linkAt(1)->next(), "(")))) ||
5076+
(tok3->strAt(-1) == "(" && Token::simpleMatch(tok3->next(), "(") && !Token::simpleMatch(tok3->linkAt(1)->next(), "(") &&
5077+
!isPointerToArrayDeclarator(tok3->next(), variableMap)))) ||
50625078
tok3->isOp() ||
50635079
tok3->str() == "(" ||
50645080
notstart.find(tok3->str()) != notstart.end()) {

‎test/testvarid.cpp‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,12 +1632,22 @@ class TestVarID : public TestFixture {
16321632
"A f(3);\n"
16331633
"A f2(true);\n"
16341634
"A g();\n"
1635-
"A e(int c);\n", dinit(TokenizeOptions, $.cpp = false));
1635+
"A e(int c);\n"
1636+
"A h(T (*p)[16]);\n"
1637+
"int x(g(1)[0]);\n"
1638+
"A u(T (*)[16]);\n"
1639+
"A h3(T (*const p)[16]);\n"
1640+
"A h4(T (**p)[16]);\n", dinit(TokenizeOptions, $.cpp = false));
16361641

16371642
const char expected[] = "1: A f@1 ( 3 ) ;\n"
16381643
"2: A f2@2 ( true ) ;\n"
16391644
"3: A g ( ) ;\n"
1640-
"4: A e ( int c@3 ) ;\n";
1645+
"4: A e ( int c@3 ) ;\n"
1646+
"5: A h ( T ( * p@4 ) [ 16 ] ) ;\n"
1647+
"6: int x@5 ( g ( 1 ) [ 0 ] ) ;\n"
1648+
"7: A u ( T ( * ) [ 16 ] ) ;\n"
1649+
"8: A h3 ( T ( * const p@6 ) [ 16 ] ) ;\n"
1650+
"9: A h4 ( T ( * * p@7 ) [ 16 ] ) ;\n";
16411651

16421652
ASSERT_EQUALS(expected, actual);
16431653
}

0 commit comments

Comments
 (0)