Menu

Scope of MISRA conformance checker

Rob Arthan
2022-05-04
2022-05-05
  • Rob Arthan

    Rob Arthan - 2022-05-04

    According to the MISRA feature matrix, the MISRA add-on for cppcheck should check rule 5.3, which states that an identifier declared in an inner scope shall not hide an identifier declared in an outer scope. However, I don't get any issues reported when I check the following code:

    extern int a;
    int a;
    static int f(void)
    {
        int a;
        return 0;
    }
    

    I am running this using:

    cppcheck --addon=misra.json t.c
    

    where my misra.json file looks like this:

    {
        "script": "misra.py",
        "args": [
            "--rule-texts=/Users/rda/git/pp-pro/src/qcz/misra.txt"
        ]
    }
    

    Am I not running the add-on correctly?

     
  • Daniel Marjamäki

    Can you add the --enable=style please this particular warning is provided by core cppcheck and I think the message id is shadowVariable.

     
  • Natalia Tejedor

    Natalia Tejedor - 2022-05-04

    I have checked also your file, t.c, with option --enable=style:

     ./cppcheck --language=c --enable=style --addon=misra.json  t.c 
    

    and it returns just:

    ../t.c:5:9: style: Unused variable: a [unusedVariable]
        int a;
            ^
    

    Being "misra.jon":

    {
      "script": "misra.py",
      "args": [
        "--suppress-rules 1.2,15.2,15.3,21.6,21.8,21.9,21.10"
      ]
    }
    

    Regarding 5.3 rule, this is not checked by Misra addon, it is checked by cppcheck itself, as it states here: http://cppcheck.net/misra.php

    BTW, I take a pure example from MISRA pdf:

    void fn1 (void)
    {
      int16_t i;
      {
        int16_t i;
        i = 3;
      }
    }
    

    And, running the same command, it returns:

    t-misra.c:5:13: style: Local variable 'i' shadows outer variable [shadowVariable]
        int16_t i;
                ^
    t-misra.c:3:11: note: Shadowed declaration
      int16_t i;
              ^
    t-misra.c:5:13: note: Shadow variable
        int16_t i;
                ^
    t-misra.c:6:7: style: Variable 'i' is assigned a value that is never used. [unreadVariable]
        i = 3;
          ^
    t-misra.c:3:11: style: Unused variable: i [unusedVariable]
      int16_t i;
              ^
    t-misra.c:1:6: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-8.4]
    void fn1 (void)
         ^
    

    It seems the first 3 comments refers to this rule, 5.3, and last comment refers to other rule, and it seems to be ok.

     
  • CHR

    CHR - 2022-05-05

    The problem in cppcheck has been fixed with https://github.com/danmar/cppcheck/pull/4079

     
    • Daniel Marjamäki

      Thanks CHR!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.