Menu

Using clang parser: invalid AST location and crashes

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

    Oliver Parczyk - 2025-05-06

    Greetings!

    I've had an issue with the internal parser, for which I'll need to build a minimal example I can share. I've tried using the experimental clang parser but as soon as a standard header is included, I get the following error (both latest release and fresh build from master):

    $ cppcheck --clang helloworld.c 
    Checking helloworld.c ...
    helloworld.c:0:0: error: Bailing out from analysis: Processing Clang AST dump failed: invalid AST location: <<built-in> [internalAstError]
    

    With helloworld.c being

    #include<stdio.h>
    
    int main() {
        printf("Hello World\n");
        return 0;
    }
    

    Another invalid AST location I've observed is invalid AST location: <<scratch space> [internalAstError]
    The corresponding clang output seems to be mostly for system libraries:

    |-FunctionDecl 0x150e0ee0 </usr/include/math.h:297:3, line:295:79> <scratch space>:138:1 __ceil 'double (double)' extern
    ...
    |-FunctionDecl 0x150e0978 </usr/include/math.h:297:3, /usr/include/x86_64-linux-gnu/sys/cdefs.h:79:54> <scratch space>:137:1 __cbrt 'double (double)' extern
    

    I fail to build a minimal example, however, as this code:

    #include <math.h>
    
    int add(int a, int b) {
        return a + b;
    }
    

    makes cppcheck crash:

    $ cppcheck --clang -v add.c 
    Checking add.c ...
    clang -fsyntax-only -Xclang -ast-dump -fno-color-diagnostics -x c add.c
    cppcheck: /home/parczyk/from_source/cppcheck/lib/token.cpp:195: void Token::update_property_info(): Assertion `!mImpl->mVarId || mTokType == eVariable' failed.
    
    Aborted
    

    Note that it works fine if I remove the #include <math.h> line.

    I'm on debian 12, clang version is Debian clang version 14.0.6. I've built cppcheck from source, both latest master and latest release. Debian 12's cppcheck version is quite old and crashes regularly when used with --clang.

    I've attached the output of both clang commands. Compressed, as sourceforge's spam protection triggers if I dont...

     
  • Oliver Stöneberg

     

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.