2

I've written a script to parse C/C++ files using the libclang library.

The file I was trying to parse: osf_sys.c. The program prints each function/variable etc. in the file. Sample output:

...
CURSORKIND: 6; __user at line: 469
CURSORKIND: 43; struct proplistname_args at line: 469
CURSORKIND: 6; nbytes at line: 470
...

So I run my script ./script osf_sys.c and parsing mostly works. However, I notice that certain parts of osf_sys.c were not being printed (missing variables and whatnot) and I suspect it had to do with the header files (which were located in a separate directory). I added a code snippet to help with debugging / printing of the error messages shown below.

Attempt 1

After building the script with CMake, I proceed to run it. This is the error that is printed:

/Linux_Kernel/linux/arch/alpha/kernel/osf_sys.c:13:10:
fatal error: 'linux/errno.h' file not found

It seems like it is having some issues locating the header files of this osf_sys.c. So I added this to CMakeLists.txt (which is probably wrong since the problem doesn't seem to be with the compilation of my program but with how libclang locates stuff):

-I/Linux_Kernel/linux/include

Note that the full path of errno.h is /Linux_Kernel/linux/include/linux/errno.h

Attempt 2

I decided to drag the entire include/ folder into the same directory that osf_sys.c resides in.

Now, running it gives me a new error printout:

/Linux_Kernel/linux/arch/alpha/kernel/osf_sys.c:13:10: error:
'linux/errno.h' file not found with <angled> include; use "quotes" instead

followed by:

/Linux_Kernel/linux/arch/alpha/kernel/linux/errno.h:4:10:
fatal error: 'asm/errno.h' file not found

Okay, now it seems to be reading something. Sure enough, the line in question is #include <linux/errno.h> from the osf_sys.c file. The second error message occurs presumably because I didn't drag the asm/ folder into the same working directory as osf_sys.c.

Attempt 3

So, as requested, I manually edit osf_sys.c to change #include <linux/errno.h> to #include "linux.errno.h"

This time, I'm left with the following error message:

/Linux_Kernel/linux/arch/alpha/kernel/linux/errno.h:4:10: 
fatal error: 'asm/errno.h' file not found

Attempt 4

Passing -I/Linux_Kernel/linux/include as an argument into the script. The script uses the following parameters for the CXTranslationUnit:

CXTranslationUnit translationUnit = clang_parseTranslationUnit(index, nullptr, argv, argc, 0, 0, CXTranslationUnit_KeepGoing);

This yields other kinds of error messages:

warning: 'linker' input unused [-Wunused-command-line-argument]
/Linux_Kernel/linux/arch/alpha/kernel/linux/errno.h:4:10: error: 'asm/errno.h' file not found
/Linux_Kernel/linux/include/linux/types.h:4:10: error: 'asm/types.h' file not found
...

What's left to do?

I have little intention of editing all the #include statements (in every single the linux kernel file) to use double quotes AND dragging the include/ folder into every single directory with a .c file.

Would appreciate some advice on how to make libclang search the include/ file directory I specify.

Any suggestions / alternatives as to how I can go about solving this?

JTJM
  • 255
  • 1
  • 6
  • 18

1 Answers1

0

To parse the source file correctly you have to pass to clang_parseTranslationUnit (via argv) all the compilation flags that were used for compilation of the kernel. The easiest way to get all of them is to construct compilation database. You could try using bear or look for other tools. Also, look at similar question: AST of a project by Clang

AlexDenisov
  • 4,022
  • 25
  • 31