clang omits a part of the code

Hello all,

I’m trying to get to the code inside a function body,
but clang doesn’t print a part of the body for some reason.

here is the function I’m trying to get to:

{
FILE f;
int* b1 = malloc(sizeof(int));
return 0;
}

and here is what clang prints out (the “malloc” in the second line is missing):

{
FILE f;
int *b1;
return 0;
}

I’m currently using the ASTConsumer and DeclVisitor to print out the fucntion body.
I’ve attached 3 files to the mail:

activation.cpp - this file which contains all the initilizations.
GeneralConsumer.h - the class which inherits from ASTConsumer and DeclVisitor.
test5.c - the test file containing the function.

I think the problem is in my configuration/initilizations,
because when I change line 48 in the activation.cpp file
from true to false: “headerSearchOpts->UseStandardIncludes = false;”,

I get the malloc, but now I don’t get the “FILE f;” line in the fucntion body (This is the new output):

{
int *b1 = malloc(sizeof(int));
return 0;
}

Does anyone have any idea what I’m doing wrong?

activation.cpp (2.34 KB)

GeneralConsumer.h (1.2 KB)

test5.c (109 Bytes)

   Does anyone have any idea what I'm doing wrong?

Most likely one of your header files is causing an error diagnostic
to be emitted. If a malformed input file is provided then Clang will
try to build as complete an AST as possible.

  diags->setSuppressAllDiagnostics(true);

If you remove this line then you should be able to see the
diagnostic(s).

Thanks,

Thank you for your quick response Peter,

I did as you said, and this is the output I got:

Stack dump:
0. /usr/include/stdio.h:34:3: current parser token ‘include’

It’s a standard library header which comes builtin in ubuntu.

Also,
If I switch the order of the includes in the file from:
#include <stdio.h>
#include <stdlib.h>

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

I get the output:

Stack dump:
0. /usr/include/stdlib.h:33:2: current parser token ‘include’

Again, It’s a standard library header.

Is there anything I can do about it?

note:
In both cases the problematic line in these standard library headers is:
#include <stddef.h>

Thank you for your quick response Peter,

I did as you said, and this is the output I got:

Stack dump:
0\.    /usr/include/stdio\.h:34:3: current parser token &#39;include&#39;

This is not a standard diagnostic but part of Clang's output if it
segfaults/asserts. Since you received the segfault/assert at the point
where a diagnostic should be emitted, I suspect it is because you did
not create the Diagnostic object correctly. This is how I normally
build a Diagnostic object and point it to a TextDiagnosticPrinter
(which should just print the diagnostics to standard error):

  clang::TextDiagnosticPrinter *DiagClient =
    new clang::TextDiagnosticPrinter(errs(), clang::DiagnosticOptions());
  IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID(new clang::DiagnosticIDs());
  clang::Diagnostic *Diag = new clang::Diagnostic(DiagID, DiagClient);

note:
In both cases the problematic line in these standard library headers is:
#include <stddef.h>

This means that Clang could not find its stddef.h header, because
HeaderSearchOptions::ResourceDir (i.e. headerSearchOpts->ResourceDir)
was not set correctly. You should set it to something like

LLVM_PREFIX "/lib/clang/" CLANG_VERSION_STRING

where LLVM_PREFIX is your LLVM build directory and CLANG_VERSION_STRING
is a macro defined in clang/Basic/Version.h.

Thanks,

Peter, your remark regarding the HeaderSearchOptions::ResourceDir solved the problem.

My ResourceDir was an empty string, I didn’t know it had to be set,
after doing so, clang found all the required files and the program ran fine,
without omitting anything.

I also changed the diagnostics according to what you wrote,
and it’s working correctly now.

Thank you for all your help

Nir.