Skip to content

Commit 7bfa458

Browse files
authored
Fix #14474: Bad varid for global variable matching function pointer argument (#8467)
1 parent ae2816b commit 7bfa458

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

addons/misra.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,7 @@ def get_function_pointer_type(tok):
803803
ret += '('
804804
tok = tok.next.next
805805
while tok and (tok.str not in '()'):
806-
if tok.varId is None:
807-
ret += ' ' + tok.str
806+
ret += ' ' + tok.str
808807
tok = tok.next
809808
if (tok is None) or tok.str != ')':
810809
return None

lib/tokenize.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,12 @@ namespace {
10251025
}
10261026

10271027
bool Tokenizer::isFunctionPointer(const Token* tok) {
1028-
return Token::Match(tok, "%name% ) (");
1028+
if (!Token::Match(tok, "%name%"))
1029+
return false;
1030+
tok = tok->next();
1031+
while (Token::Match(tok, "["))
1032+
tok = tok->link()->next();
1033+
return Token::simpleMatch(tok, ") (");
10291034
}
10301035

10311036
static bool matchCurrentType(const Token* tok, std::map<int, std::string>& types)

test/testvarid.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ class TestVarID : public TestFixture {
13281328
ASSERT_EQUALS(expected2, tokenize(code2));
13291329

13301330
const char code3[] = "extern void (*arr[10])(uint32_t some);\n";
1331-
const char expected3[] = "1: extern void ( * arr@1 [ 10 ] ) ( uint32_t some@2 ) ;\n";
1331+
const char expected3[] = "1: extern void ( * arr@1 [ 10 ] ) ( uint32_t ) ;\n";
13321332
ASSERT_EQUALS(expected3, tokenize(code3));
13331333

13341334
const char code4[] = "_Static_assert(sizeof((struct S){0}.i) == 4);\n"; // #12729
@@ -3545,9 +3545,15 @@ class TestVarID : public TestFixture {
35453545
"}\n";
35463546
ASSERT_EQUALS("1: void f ( ) {\n"
35473547
"2: int * p@1 ;\n"
3548-
"3: void ( * a@2 [ 1 ] ) ( int * p ) = { 0 } ;\n"
3548+
"3: void ( * a@2 [ 1 ] ) ( int * ) = { 0 } ;\n"
35493549
"4: }\n",
35503550
tokenize(code4));
3551+
3552+
const char code5[] = "int *p;\n"
3553+
"void (*a[1])(int* p) = { 0 } ;\n";
3554+
ASSERT_EQUALS("1: int * p@1 ;\n"
3555+
"2: void ( * a@2 [ 1 ] ) ( int * ) = { 0 } ;\n"
3556+
, tokenize(code5));
35513557
}
35523558

35533559
void varid_alignas() {

0 commit comments

Comments
 (0)