cppcheck icon indicating copy to clipboard operation
cppcheck copied to clipboard

Fix confusion between function call and variable declaration

Open shaneasd opened this issue 4 years ago • 2 comments

I'm at the point where I'd like some feedback on this (incomplete) solution to https://trac.cppcheck.net/ticket/9960

In a nutshell, the problem is that cppcheck sometimes does a poor job of distinguishing function calls from variable declarations. There are some cases where the two are indistinguishable because the declarations of the identifiers involved are hidden from cppcheck but my initial concern is the case where they are available. A simple example is that foo(bar); is a function call if foo is a function but a variable declaration if foo is a type (even if bar is a variable name and foo has a constructor that will accept bar). The example I encountered in real code was more like

void foo() {
  std::vector<int*> a(10); //In the real code this would have been initialized somehow
  for (int i = 0; i < 10; i++)
    bar(*a[4]); //This was misinterpreted as a variable declaration issuing a warning about shadowing 'a'
}

My solution is to, when determining whether a token is part of a variable declarations, check whether the "type" is actually already known to be a function. This is somewhat complicated by the fact that at the point where we process variables we have not fully completed processing functions so a lot of data I would like to be able to use is not yet available.

shaneasd avatar Jan 05 '21 17:01 shaneasd

@danmar could I get some input on this please?

shaneasd avatar Jan 16 '21 04:01 shaneasd

yes. Sorry for the delay.. it just seems complex.. thanks a lot for working on this!

danmar avatar Jan 16 '21 09:01 danmar