c++ - 检索有关预处理器指令的信息

标签 c++ serialization clang libclang

我最近开始使用 libclang 来解析 C 文件。我遇到的问题显然是,libclang 在生成 AST 之前启动了预处理器。我想禁止预处理器运行,而是提供预处理器指令在文件中的信息...

我使用以下 python 脚本(cindex.py 和 libclang)

import codecs
from clang.cindex import *

class SourceFile(object):
    def __init__(self, path):
        with codecs.open(path, 'r', 'utf-8') as file:
            self.file_content = file.read()

        index = Index.create()
        root_node = index.parse(path)

        for included in root_node.get_includes():
            print included.include

        self.print_declerations(root_node.cursor)

    def print_declerations(self, root, recurse=True):
        print root.kind.name, root.spelling
        if root.kind.is_declaration():
            node_def = root.get_definition()
            if node_def is not None:
                start_offset = node_def.extent.start.offset
                end_offset = node_def.extent.end.offset + 1
                print self.file_content[start_offset:end_offset], '\n'

        if recurse:
            for child in root.get_children():
                self.print_declerations(child, False)

if __name__ == '__main__':
    path = 'Sample.cpp'
    print 'Translation unit:', path
    source = SourceFile(path)

哪些输出

Translation unit: Sample.cpp
/mingw/include\stdio.h
/mingw/include\_mingw.h
/mingw/include\sys/types.h
TRANSLATION_UNIT None
TYPEDEF_DECL __builtin_va_list

STRUCT_DECL _iobuf

TYPEDEF_DECL FILE

VAR_DECL _iob
UNEXPOSED_DECL 

FUNCTION_DECL main
int main()
{
    printf(HELLO_WORLD);
    return 0;
}

对于以下 C 代码:

#include <stdio.h>
#define HELLO_WORLD "HELLO!"

int main()
{
    printf(HELLO_WORLD);
    return 0;
}

我想要的是在代码中为我的#define 获取 DEFINE_DECL HELLO_WORLD(目前我什么也得不到)。当然,我的#include 也会得到类似的语句。这可能吗?

编辑:基本上,我想在不扩展预处理器指令的情况下解析文件。

最佳答案

如果您将 PARSE_DETAILED_PROCESSING_RECORD 添加为调用 index.parse() 的选项,您将可以访问预处理器节点。

index = clang.cindex.Index.create()                                                                         
tu = index.parse(filename, options=clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD)

此选项映射到以下 libclang C API 选项值。那里有一条评论,其中包含更多上下文。

/**                                                                         
 * \brief Used to indicate that the parser should construct a "detailed"    
 * preprocessing record, including all macro definitions and instantiations.
 *                                                                          
 * Constructing a detailed preprocessing record requires more memory        
 * and time to parse, since the information contained in the record         
 * is usually not retained. However, it can be useful for                   
 * applications that require more detailed information about the            
 * behavior of the preprocessor.                                            
 */                                                                         
CXTranslationUnit_DetailedPreprocessingRecord = 0x01,   

关于c++ - 检索有关预处理器指令的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13881506/

相关文章:

python - 我编写的二进制 PLY 文件导致 meshlab 崩溃

java - 序列化包含不可序列化类的类

c++ - Clang vs GCC vs MSVC 模板转换运算符——哪个编译器是对的?

objective-c - 覆盖类别中的类实例方法时如何抑制链接器警告

linux - 在 Windows 10 上运行 LLVM passes 在终端中没有输出?

c++ - C++ 11 触发异步任务并忘记它的方法是什么?

c++ - OpenSSL:当输出指针为空时,BIGNUM 的 bin2bn 不会分配新的 BIGNUM

c# - 如何防止使用 Newtonsoft JSON 序列化 DependencyObjects 上的 DependencyObjectType 信息?

c++ - 将 argv[1] 转换为 int 并将其数字转换为数组

c++ - 模板的成员 typedef 在 VS 而非 GCC 中用于参数未声明的标识符