Menu

Improve support for catch2

federico
2024-06-28
2024-07-11
  • federico

    federico - 2024-06-28

    Hello, I want to report a bug.

    cppcheck currently has issue parsing the macros of the catch2 project, a test suite for c++

    A minimal example for reproducing the issue looks like following

    main.cpp

    #define CATCH_CONFIG_MAIN
    #include "catch_amalgamated.hpp"
    int factorial( int number ) {
        return number <= 1 ? number : factorial(number-1) * number;     
    }
    TEST_CASE( "Factorials are computed" ) {
            REQUIRE_THROWS_AS(factorial(1), int);
    }
    

    then download catch_amalgamated.cpp and catch_amalgamated.hpp from the catch project (https://github.com/catchorg/Catch2) in the same directory (issue is also reproducible with the non-amalgamated library

    the project can be compiled and executed with "g++ main.cpp catch_amalgamated.cpp && ./a.out"

    compile_command.json

    [
    {
      "directory": "/home/debianuser/cppcheck-test",
      "command": "g++ main.cpp catch_amalgamated.cpp",
      "file": "main.cpp",
      "output": "a.out"
    }
    ]
    

    "cppcheck main.cpp" reports as error

    main.cpp:11:3: error: There is an unknown macro here somewhere. Configuration is required. If _catch_sr is a macro then please configure it. [unknownMacro]
      REQUIRE_THROWS_AS(factorial(1), int);
    

    and the issue persists when using a compile_command.json project file

    [
    {
      "directory": "<path to current directory>",
      "command": "g++ main.cpp catch_amalgamated.cpp",
      "file": "main.cpp",
      "output": "a.out"
    }
    ]
    

    Note that _catch_sr is not a macro, but is concatenated to a macro, for example

    #define CATCH_REQUIRE_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "CATCH_REQUIRE_THROWS_AS", exceptionType, Catch::ResultDisposition::Normal, expr )
    
    
    #define INTERNAL_CATCH_THROWS_AS( macroName, exceptionType, resultDisposition, expr ) \
        do { \
            Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(expr) ", " CATCH_INTERNAL_STRINGIFY(exceptionType), resultDisposition ); \
            if( catchAssertionHandler.allowThrows() ) \
    
     

    Last edit: federico 2024-06-28
  • CHR

    CHR - 2024-07-03

    It's probably best to create a library configuration for Catch2 (similar to e.g. googletest.cfg that we already have) and not make headers like catch_amalgamated.hppvisible to cppcheck.

     
  • federico

    federico - 2024-07-11

    It would be better if cppcheck is able to handle such construct too, but having a short-term solution is fine too, as it makes possible to use cppcheck on the test suite too

    catch headers should all match the pattern "catch_.*\.hpp"

     

    Last edit: federico 2024-07-11

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.