当在python bindings / clang中的get-includes中解析cpp文件时,过滤目录 [英] Filtering directories when parsing cpp files in get-includes in python bindings/clang

查看:242
本文介绍了当在python bindings / clang中的get-includes中解析cpp文件时,过滤目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该写一个python-clang解析器,返回cpp文件中的所有包含。
所以我使用sth像下面的代码:

I should write a python-clang parser which returns all the inclusions in cpp files. So I use sth like 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 just some of inclusions like specific directories:

filter=['/usr/lib','usr/include']

问题1:我想知道这种过滤是如何实现的我的代码应该改变?

Question 1: I would like to know how this filtering is possible and how my code should change?

问题2:如何使一个配置文件包括所有这些过滤器,而不是只写它们硬编码?

Question 2: How to make a config file to include all these filter direstories in that instead of just writting them hardcoded?

运行测试:您需要一个cpp文件,如:

to run the test: you need to have a cpp file like:

  #include<iostream>
  #include"ex1.h"

  int main(){
      return 0;
  }

和* .h文件:

 #include<QMap>

运行:

./python-clang.py ex1.cpp

结果样本: p>

sample of 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


推荐答案

您可以在中执行循环:

...
for i in t.get_includes():
    if not i.include in filter:
        print i.include
...

对于包含排除项的配置文件。你可以这样做:

As for the config file containing the exclusions. You could do something like this:

def _main():
    ...
    with open('/path/to/file/ignore.txt') as f:
        filter = f.readlines()
    ...

然后在 ignore.txt

/usr/lib
/usr/include
...


$ b b

UPDATE



根据您对您的问题的意见和编辑。

UPDATE

Based on your comments and edits to your question.

def _main():
    ...
    with open('/path/to/file/ignore.txt') as f:
        ignore = map(lambda l: l.strip(), f.readlines())

    for i in t.get_includes():
        if not i.include.startswith(ignore):
            print i.include

这里要注意几件事。


  1. 我已将过滤器更改为忽略 $ c> filter
    是一个内置类型。

  2. ignore.txt中的行 \\\
    剥离并映射到
    a tuple ,而不是 list ,以便在读取时可以与
    startswith 方法一起使用。

  3. 您还可以使用列表推导将过滤的结果
    添加到以后使用的列表中。

  1. I've changed the variable name filter to ignore since filter is a built-in type.
  2. The lines in ignore.txt are having the \n stripped and mapped to a tuple instead of a list so they can be used with the startswith method when being read.
  3. You could also use list comprehension to put the filtered results into a list to be used later.

results = [i.include for i in t.get_includes()if not i.startswith(ignore)]

这篇关于当在python bindings / clang中的get-includes中解析cpp文件时,过滤目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆