0

We've started using Cppcheck recently and everything is working great except scopes.

I might have misunderstood something but aren't there supposed to be separate scopes for enums, structs, unions etc. in the resulted .dump file after parsing ?

I was expecting there would be based in the cppcheck documentation ("ScopeType" enumerate) and the misra_8_12 rule.

Header file parsed as an example :

enum week {
  LOW = 1,
  MEDIUM,
  HIGH
};

void testfct(void)
{
  enum week day;
}

The only scopes I get as a result are a "global" scope, and a "function" scope for the testfct function. Shouldn't I get a scope for the "week" enum as well ?

My python script for context (with cfg being each file configuration obtained after parsing) :

for scope in cfg.scopes:
  print('    Id:' + scope.Id)
    if scope.className:
      print(' name:' + scope.className)
    if scope.type:
      print(' type:' + scope.type)

The python script doesn't really matter here since the scopes are not even in the .dump xml result file.

And our call to cppcheck (in a .bat file):

cppcheck.exe --enable=all -j 4 --xml --dump <target_file>
Nikeau
  • 1
  • 2
  • Despite using `{ }` the enum declaration is not a separate scope. If it was, the values wouldn't be visible after the `};`. – BoP Jun 29 '23 at 08:46
  • @BoP If we're strictly talking about C and not Cppcheck, I agree. But if that's also the case for Cppcheck, what's the "ScopeType" enumerate used for then ? Also how is the Cppcheck misra_8_12 rule supposed to work ? Because looking at its code, it's clearling searching for "enum scopes" Cppcheck doc : https://cppcheck.sourceforge.io/devinfo/doxyoutput/classScope.html – Nikeau Jun 29 '23 at 08:58
  • 1
    I don't know the tool, but know that C++ *has* scoped enums (`enum class`) where that values are not visible without a scope prefix. – BoP Jun 29 '23 at 09:49
  • @BoP Well I was not aware scoped enum were a thing, I learned something today. Anyway, found the solution to my problem. Thanks for your help. – Nikeau Jun 29 '23 at 12:05

1 Answers1

0

Found the solution.

Turns out you need a C file to include the header where the "enum" is defined. The scopes of enums, struct... are all available in the result .dumb file of the ".c" file but not in the ".h" dumb file, contrary to function scopes that are available in both. I don't really understand the logic behind this, but, here it is.

Nikeau
  • 1
  • 2