Menu

Bug: Quotes and spaces in compile_commands.json arguments[] values handled incorrectly

2024-08-12
2024-08-16
  • Chris Hollis-Locke

    Using cppcheck 2.13.0

    Two issue related to parsing -D flag values from compile_commands.json:
    * Quotes are dropped from quoted values
    * Flag values containing spaces are completely dropped

    This can be shown by looking at the -D values printed by cppcheck in its config summary...
    (Note that in the config summary, the value passed on the CLI remains quoted, whereas the one from the project file does not.)

    (using bear to generate the json file)

    % touch null.c
    % bear -- gcc '-DPROJ_LITERAL="HelloWord"' -c null.c
    % cppcheck --project=compile_commands.json '-DCLI_LITERAL="HellowWorld"'
    Checking null.c ...
    Checking null.c: CLI_LITERAL="HellowWorld";PROJ_LITERAL=HelloWord...
    

    Inspecting compile_commands.json we can see the JSON is well formed and properly quoted...

    % cat compile_commands.json 
    [
      {
        "arguments": [
          "/usr/bin/gcc",
          "-DPROJ_LITERAL=\"HelloWord\"",
          "-c",
          "null.c"
        ],
    ...
    

    If a quoted value contains spaces then the value is dropped completely.
    (Note that PROJ_LITERAL is not reported in the config summary.)

    % touch null.c
    % bear -- gcc '-DPROJ_LITERAL="Hello Word"' -c null.c
    % cppcheck --project=compile_commands.json '-DCLI_LITERAL="Hello World"'
    Checking null.c ...
    Checking null.c: CLI_LITERAL="Hello World"...
    
     

    Last edit: Chris Hollis-Locke 2024-08-16
  • Chris Hollis-Locke

    @danielmarjamaki I've attached a patch file for test/testimportproject.cpp
    that adds tests for quoted -D symbol values.

    We have code that requires certain string literals to be supplied via -D defines.
    Dropping the quotes results in cppcheck reporting syntax errors or false-positive errors (e.g if contents of the string literal matches a valid symbol name)

     
  • Daniel Marjamäki

    Thanks! I have created ticket: https://trac.cppcheck.net/ticket/13019

    if you have time to make a fix also that would be excellent. The handling of quotes is not trivial as far as I remember.

     

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.