I have to write a python-clang parser that returns all inclusions in cpp files. Therefore, I use sth as the following code:
def _main():
from clang.cindex import Index
from optparse import OptionParser
filter=['/usr/lib','usr/include']
p=OptionParser()
(o,a)=p.parse_args()
i=Index.create()
t=i.parse(None,a)
for i in t.get_includes():
print i.include
if __name__=='__main__':
_main()
Now I need to filter out only some inclusions, such as specific directories:
filter=['/usr/lib','usr/include']
Question 1: I would like to know how this filtering is possible and how should my code change?
Question 2: How to make a configuration file to include all these filters in it, and not just impose them hard-coded?
to run the test: you need to have a cpp file, for example:
#include<iostream>
#include"ex1.h"
int main(){
return 0;
}
and * .h file:
#include<QMap>
mileage:
./python-clang.py ex1.cpp
sample results:
/usr/include/pthread.h
/usr/include/sched.h
/usr/include/time.h
/usr/include/bits/sched.h
/usr/include/time.h
/usr/include/bits/time.h
/usr/include/signal.h
/usr/include/bits/sigset.h
/usr/include/bits/pthreadtypes.h
/usr/include/bits/wordsize.h
/usr/include/bits/setjmp.h
/usr/include/bits/wordsize.h
/usr/include/bits/wordsize.h
/usr/include/unistd.h
/usr/include/bits/posix_opt.h
/usr/include/bits/environments.h
/usr/include/bits/wordsize.h
/usr/include/bits/confname.h
/usr/include/getopt.h
/usr/lib/gcc/i486-linux-gnu/4.4/../../../../include/c++/4.4/i486-linux-gnu/bits /atomic_word.h
/usr/lib/gcc/i486-linux-gnu/4.4/../../../../include/c++/4.4/bits/locale_classes.h
/usr/lib/gcc/i486-linux-gnu/4.4/../../../../include/c++/4.4/string
/usr/lib/gcc/i486-linux-gnu/4.4/../../../../include/c++/4.4/bits/allocator.h
/usr/lib/gcc/i486-linux-gnu/4.4/../../../../include/c++/4.4/i486-linux-gnu/bits/c++allocator.h
/usr/lib/gcc/i486-linux-gnu/4.4/../../../../include/c++/4.4/ext/new_allocator.h
source
share