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.
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
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
.
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
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
...
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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.