cppcheck icon indicating copy to clipboard operation
cppcheck copied to clipboard

fix #12207: FP variableScope with brace init

Open ludviggunne opened this issue 1 year ago • 7 comments

ludviggunne avatar Sep 14 '24 08:09 ludviggunne

As noted in the ticket, this is caused by a bad simplification for any struct UPPERCASENAME varname declaration. If anything, we should fix that and not add another hack elsewhere.

1: struct S { int * p@var1 ; } ;
2:
3: void f ( ) {
4: int i@var2 [@exprUNIQUE 2 ] ;
5: struct s { i@var2 } ;
6: }

chrchr-github avatar Sep 14 '24 10:09 chrchr-github

So the simplification happens before the macros are expanded?

ludviggunne avatar Sep 14 '24 10:09 ludviggunne

I think we try to proceed even when there are unknown macros (by removing tokens that look like a macro name), which we shouldn't do in the first place.

chrchr-github avatar Sep 14 '24 11:09 chrchr-github

removeMacroInClassDef was added 10ish years ago. Maybe it has become unnecessary since then? If the preprocessing/project import has improved.

ludviggunne avatar Sep 14 '24 11:09 ludviggunne

Here's a related ticket: https://trac.cppcheck.net/ticket/11048 I would say we should just bail out with an error message on ambiguous code. But we also generally try to handle incomplete code, so there has to be some kind of trade-off.

chrchr-github avatar Sep 14 '24 11:09 chrchr-github

... so there has to be some kind of trade-off.

Prehaps remove it only if it's longer than one character? class S isn't unheard of, but a longer class name in uppercase only is probably not very common.

ludviggunne avatar Sep 14 '24 13:09 ludviggunne

In the case of #12207, we should track that S has already been defined, so struct S s; unambigously declares a variable. But this is probably difficult to implement. A different heuristic for type names would just be moving the goal posts IMHO.

chrchr-github avatar Sep 14 '24 14:09 chrchr-github