Menu

Duplicate defines in library config

2024-12-04
2024-12-06
  • Frank Winklmeier

    Using cppcheck with multiple library configs that define the same macro results in some surprising results, in terms of which one takes precedence.

    Consider the two following libraries:

    > cat lib1.cfg 
    <?xml version="1.0"?>
    <def format="2">
      <define name="slots" value=""/>
    </def>
    
    > cat lib2.cfg
    <?xml version="1.0"?>
    <def format="2">
      <define name="slots" value="foo"/>
    </def>
    

    and execute:

    > cat test.cxx 
    a = slots;
    > cppcheck -E --library=lib1.cfg --library=lib2.cfg test.cxx
    a = ;
    

    No matter the order of --library the result is always the same. That's because all library definitions are stored as <name> <value> string in a std::set. So by alphabetical ordering the empty define in the above example always takes precedence.

    From a user-perspective it would make more sense to either:
    * throw an error if the same macro is defined with different values (currently that's only the case for identical name/value pairs), or
    * define a clear precedence depending on the order of the libraries specified on the command line.

    Personally, I would probably favor the second option. That would also allow the user to re-define some of the built-in defines, which is how I came across this.

    -Frank

    P.S.: If there is a consensus I might be able to provide a PR...

     
  • Oliver Stöneberg

    I thought we bail out on duplicated defines nowadays...

     
    • CHR

      CHR - 2024-12-05

      That's only for duplicates within the same .cfg IIRC.

       
  • Frank Winklmeier

    Maybe that was the intention, but it's not what the code does. You only get an error if the name and value of the define are identical.

     
  • André Coquim

    André Coquim - 2024-12-06

    Hi, not about this topic, but somehow related with the problem mentioned here. 5 days ago I opened a similar situation related with the loading of the macros in std.cfg and their colosion, but I had got no answer, could you please provide an answer there?
    https://sourceforge.net/p/cppcheck/discussion/general/thread/89612cfdd4/
    Thanks

     

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.