From 7618e100b4dea5c40144922aabbac12325a6af2c Mon Sep 17 00:00:00 2001 From: Swasti Shrivastava <37058682+swasti16@users.noreply.github.com> Date: Tue, 31 Oct 2023 14:56:03 +0530 Subject: [PATCH] Do not add AST for pointer in variable declaration (#5593) --- lib/tokenlist.cpp | 8 ++++++++ test/testtokenize.cpp | 3 +++ 2 files changed, 11 insertions(+) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 0b892cc98e1..1d27dc810db 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1445,6 +1445,14 @@ static Token * findAstTop(Token *tok1, const Token *tok2) static Token * createAstAtToken(Token *tok, bool cpp) { // skip function pointer declaration + if (Token::Match(tok, "%type% %type%") && !Token::Match(tok, "return|throw|new|delete")) { + Token* tok2 = tok->tokAt(2); + // skip type tokens and qualifiers etc + while (Token::Match(tok2, "%type%|*|&")) + tok2 = tok2->next(); + if (Token::Match(tok2, "%var% [;,)]")) + return tok2; + } if (Token::Match(tok, "%type%") && !Token::Match(tok, "return|throw|if|while|new|delete")) { Token* type = tok; while (Token::Match(type, "%type%|*|&|<")) { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index ab4ab531dd6..0f1f54c930e 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -6474,6 +6474,9 @@ class TestTokenizer : public TestFixture { ASSERT_EQUALS("tmpa*=a*b*=,b*tmp=,", testAst("{ ((tmp) = (*a)), ((*a) = (*b)), ((*b) = (tmp)); }")); ASSERT_EQUALS("a(*v=", testAst("(*(volatile unsigned int *)(a) = (v));")); ASSERT_EQUALS("i(j=", testAst("(int&)(i) = j;")); + + ASSERT_EQUALS("", testAst("void f(enum E* var){}")); + ASSERT_EQUALS("", testAst("void f(enum E*& var){}")); } void astunaryop() const { // unary operators