Menu

Unable to load misra rules file from the command line

2019-03-13
2019-03-14
  • Grahame White

    Grahame White - 2019-03-13

    I built the tagged 1.87 release of cppcheck on a linux machine using the

    make -j 8 SRCDIR=build CFGDIR=/usr/share/cppcheck/cfg HAVE_RULES=yes CXXFLAGS="-O2 -DNDEBUG -Wall -Wno-sign-compare -Wno-unused-function -lpcre" install
    

    Aside from a few warnings, the build was succesful and I'm able to run cppcheck against a test C project

    However, when I wanted to add in the MISRA-C 2012 checks things don't go quite as smoothly.
    I'm calling cppcheck using the following commandline options from the root of my test C project, as per this topic.

    cppcheck -j 4 --enable=all --inconclusive --xml --xml-version=2 --rule-file=misra_rules.txt .
    

    The only output I get from this is

    cppcheck: error: unable to load rule-file: misra_rules.txt
    

    My misra_rules.txt file looks something like:

    Appendix A Summary of guidelines
    Dir 1.1
    Directive 1.1 Required - blah blah blah
    Dir 2.1
    Directive 2.1 Required - waffle waffle waffle
    

    From a Windows machine, using the GUI version of cppcheck, this same exact same file works as expected

    I had a look at cmdlineparser.cpp, specifically:

                // Rule file
                else if (std::strncmp(argv[i], "--rule-file=", 12) == 0) {
                    tinyxml2::XMLDocument doc;
                    if (doc.LoadFile(12+argv[i]) == tinyxml2::XML_SUCCESS) {
                        tinyxml2::XMLElement *node = doc.FirstChildElement();
                        for (; node && strcmp(node->Value(), "rule") == 0; node = node->NextSiblingElement()) {
                            Settings::Rule rule;
    
                            tinyxml2::XMLElement *tokenlist = node->FirstChildElement("tokenlist");
                            if (tokenlist)
                                rule.tokenlist = tokenlist->GetText();
    
                            tinyxml2::XMLElement *pattern = node->FirstChildElement("pattern");
                            if (pattern) {
                                rule.pattern = pattern->GetText();
                            }
    
                            tinyxml2::XMLElement *message = node->FirstChildElement("message");
                            if (message) {
                                tinyxml2::XMLElement *severity = message->FirstChildElement("severity");
                                if (severity)
                                    rule.severity = Severity::fromString(severity->GetText());
    
                                tinyxml2::XMLElement *id = message->FirstChildElement("id");
                                if (id)
                                    rule.id = id->GetText();
    
                                tinyxml2::XMLElement *summary = message->FirstChildElement("summary");
                                if (summary)
                                    rule.summary = summary->GetText() ? summary->GetText() : "";
                            }
    
                            if (!rule.pattern.empty())
                                mSettings->rules.push_back(rule);
                        }
                    } else {
                        printMessage("cppcheck: error: unable to load rule-file: " + std::string(12+argv[i]));
                        return false;
    }
    

    It's not clear, from a cursorary look, what the magic number 12 is all about but it does appear that it's trying to run my misra-rules.txt file through an XML parser, which doesn't make much sense to me. I'm guessing I must be missing something obvious.

     
  • versat

    versat - 2019-03-13

    The Cppcheck parameter --rule-file has nothing to do with the Misra rule file.
    The Misra rule texts file can not be specified as a Cppcheck parameter but as a Misra addon parameter.
    For using the Addons via command line please see the chapter "Using Cppcheck addons" in the manual (http://cppcheck.sourceforge.net/manual.pdf). The Misra addon script is misra.py
    The rule texts file can be specified as a parameter (IIRC it is --rule-texts) to this addon script.
    See "Chapter 8. Misra" for more details.
    Sadly there is no simpler way to run the addons at the moment.

    I hope this helps. If the manual is not clear enough or you have further questions don't hesitate to ask.

     
  • Grahame White

    Grahame White - 2019-03-14

    Thanks for your reply, that was very helpful.

    Unfortunately, I did not find Chapter 8 of the manual to be as helpful as it could be.
    It does explain nicely how to process the MISRA C pdf to generate a usable text file.
    It doesn't explain clearly how to run the misra plugin (or any other plug in for that matter)
    * I didn't find it all that clear that the main cppcheck code was unable to trigger the addons.

    Once I've got things working from my end I'll take a look at writing something for inclusion in the manual.

     
    • versat

      versat - 2019-03-14

      Yes Chapter 8 is not really complete. Maybe a reference to the chapter explaining how addons work should be added there so this important information would be easier to find.
      I am also not sure if this "pdftotext" process is really necessary. I just marked the relevant text, copied it and pasted it into a text file. Maybe pdftotext avoids some encoding / decoding problems, not sure about this.

      I am happy that i was able to help a bit and it would be great if you could improve the manual.
      The "source" for the manual is now a MarkDown file so it should be easy to enhance it:
      https://github.com/danmar/cppcheck/blob/master/man/manual.md

       

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.