Skip to content

[Clarification] Clang Emit AST with Errors #59700

@VenoVeno

Description

@VenoVeno

Hi,
I'm trying CodeChecker in Ubuntu 20.0. I need to generate AST for the available source information and perform static analysis on source. But Clang throws error on missing headers, methods, types that are defined in Windows headers.

Analyzers: Clang Static Analyzer

Sample File:

#include <stdio.h>
#include <stdlib.h>

#include <unknown-header.h>

int main()
{
    int data;
    scanf("%d", &data);

    char *array = (char *)malloc(sizeof(char) * data);
    free(array);

    Custom_Data_Type cData;

    return 0;
}

Command:

clang --analyze -Xclang -analyzer-opt-analyze-headers -Xclang -analyzer-output=plist-multi-file -o malloc.cpp_clangsa.plist -Xclang -analyzer-checker=alpha.security.taint.TaintPropagation -x c++ malloc.cpp -v

Output:

clang version 16.0.0 (https://github.com/llvm/llvm-project.git 4db687155bc12f31b5ed122ba1086c5f04838a24)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
 (in-process)
 "/usr/local/bin/clang-16" -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name malloc.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -v -fcoverage-compilation-dir=/home/local/ZOHOCORP/veno-pt4822/Documents/Zoho/C++/MakeTest/CTU-Test/Hello -resource-dir /usr/local/lib/clang/16.0.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/x86_64-linux-gnu/c++/11 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/backward -internal-isystem /usr/local/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/11/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir=/home/local/ZOHOCORP/veno-pt4822/Documents/Zoho/C++/MakeTest/CTU-Test/Hello -ferror-limit 20 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -analyzer-opt-analyze-headers -analyzer-output=plist-multi-file -analyzer-checker=alpha.security.taint.TaintPropagation -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o malloc.cpp_clangsa.plist -x c++ malloc.cpp
clang -cc1 version 16.0.0 based upon LLVM 16.0.0git default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/11/../../../../x86_64-linux-gnu/include"
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11
 /usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/x86_64-linux-gnu/c++/11
 /usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/backward
 /usr/local/lib/clang/16.0.0/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
malloc.cpp:4:10: fatal error: 'unknown-header.h' file not found
#include <unknown-header.h>
         ^~~~~~~~~~~~~~~~~~

malloc.cpp:14:5: error: unknown type name 'Custom_Data_Type'
    Custom_Data_Type cData;
    ^

Expected Result:
Can Clang emit AST and proceed with skipping errors?. Like a partial AST.
FYI: Tried -fsyntax-only flag too, that too doesn't worked. Also, tried clang-check to dump AST with ast-dump-filter flag.

CodeChecker Version : 6.19
Clang Version : 16 (Target: x86_64-unknown-linux-gnu)
Ubuntu Version : 20.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"questionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions