Menu

AST broken by compound literals

2025-05-06
2025-05-06
  • Oliver Parczyk

    Oliver Parczyk - 2025-05-06

    I've failed to execute cppcheck on a larger codebase and condensed the problem to the following example:

    #define ADD_INT(x, y) add(x, &(int){y})
    
    int add(int a, int *b) {
        return a+*b;
    }
    
    struct {
        int a;
        int b;
    } tuple = {
        .a = 10,
        .b = 10
    };
    
    int main() {
        ADD_INT(tuple.a, 5);
        ADD_INT(5, tuple.b);
    }
    

    Running cppcheck on this file yields this error:

    ast_problem.c:17:2: error: Syntax Error: AST broken, 'tuple' doesn't have a parent. [internalAstError]
     ADD_INT(5, tuple.b);
     ^
    

    This happens for both a fresh build from master and the latest release.

    Note that if I add redundant parentheses to the macro like this: #define ADD_INT(x, y) add(x, &((int){y})) it works fine, but these expressions are equivalent as compound literals have higher precedence than the & operator. They build fine in both gcc and clang.

    When using the experimental clang parser this error doesn't occur.

     
  • CHR

    CHR - 2025-05-06

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/13833

     

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.