Menu

Syntax Error Misdirection

2018-04-27
2018-05-04
  • Clint Chelak

    Clint Chelak - 2018-04-27

    Like a typical syntax error in most code, if a semi-colon is forgotton, the syntax error will point you to the following line. For example:

    int noSemicolon = 0
    int oopsError = 1;

    For a compiler, the above example would probably throw an error on line 2 saying something like "expecting a semicolon." The same goes for cppcheck. However, if the semicolon is missing at the end of a line, we have a new problem. The error carries over to other files. Let's say we have the following code (which I had in mine):

    //include/included.hpp
    #ifndef SOME_FILE_HPP_
    #error someFile.hpp needs to be included before inclusion of this file.
    #endif

    SOME_MACRO_FROM_SOME_FILE

    We can have a lecture on how this type of backwards-looking include guard is bad practice, but unfortunately, it's littered throughout my company's code. Anyways, this macro SOME_MACRO_FROM_SOME_FILE contains no semi-colon, which causes cppcheck to freak out when we run into the configuration -DSOME_FILE_HPP_ yet no actual inclusion of SOME_FILE_HPP. Now, I have one other include file and a source file as follows:

    //include/secondInclude.hpp
    void someFuction(){
    int nothing = 0;
    }

    //mySrc.cpp
    #include<included.hpp>
    #include<secondInclude.hpp>

    I run the command cppcheck mySrc.cpp -Iinclude --enable=all -f. It will not point out the style error found in someFunction() but will instead give [include/secondInclude.hpp:2]: (error) syntax error.

    I feel that there should be some guard added to the end of a file to make sure the syntax error will land in the appropriate file, giving the developer an easier time to find where the error exactly took place.

     
  • versat

    versat - 2018-05-04

    Hmm, IMHO this is really tricky and maybe there could be several issues with Cppcheck that prevent correct analysis here.
    Nonetheless i tried to reproduce it and did not get a syntax error:

    $ cat include/include1.hpp
    #ifndef SOME_FILE_HPP_
    #error someFile.hpp needs to be included before inclusion of this file.
    #endif
    
    SOME_MACRO_FROM_SOME_FILE
    
    $ cat include/include2.hpp
    void someFuction(){
    int nothing = 0;
    }
    
    $ cat include_issue.cpp
    #include <include1.hpp>
    #include <include2.hpp>
    
    $ ./cppcheck -Iinclude --enable=all -f include_issue.cpp
    Checking include_issue.cpp ...
    Checking include_issue.cpp: SOME_FILE_HPP_...
    [include/include2.hpp:2]: (style) Variable 'nothing' is assigned a value that is never used.
    

    I have tried it with Version 1.83 and the current development version. If i did not make any mistake there is nothing wrong with Cppcheck here. It seems to be able to analyze the code despite the syntax error.
    Which version did you use?

    One more or less related ticket could be #error macro makes unrelated errors undetectable

    What you can try additionally (not necessarily related to this issue):
    Use the -E option of Cppcheck to verify that Cppcheck (or more precisely the program "simplecpp" which is part of it) preprocesses everything correctly.
    Use the --check-config option to see if the includes you want to use are found correctly.

    At the moment i am not sure how to further help on this issue or how a ticket in trac should be written to handle this better. Have you any idea or like to write a ticket?

     

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.