从 clang AST for ObjectiveC 中省略代码块

Omitted code blocks from clang AST for ObjectiveC

我正在为 .m 文件中的 Objective C 代码生成 AST 该命令大致类似于 clang-check -ast-dump /source/file.m &> /output/file.txt

它有一个错误说

Error while trying to load a compilation database: Could not auto-detect compilation database for file '/source/file.m' 

No compilation database found in /source or any parent directory 

json-compilation-database: Error while opening JSON database: No such file or directory

Running without flags.

In file included from /source .. fatal error:'UIKit/UIKit.h' file not found

我不确定这是否与上面抛出的错误有关,但我的许多 CompoundStmt 块都是空的。如果它们包含 C 或 C++ 代码,那么它们会反映在 CompoundStmt 中,但当它包含 NSString *query = [NSString stringWithFormat:@"select * from peopleInfo where peopleInfoID=%d, self.recordIDToEdit] 甚至 NSString *abc = "ABC"

之类的代码时则不会

您需要生成一个叫做编译数据库的东西。对于 Clang,它将是一个 json 文件(默认名称:compile_commands.json)

你可以阅读它 here

Tools based on the C++ Abstract Syntax Tree need full information how to parse a translation unit. Usually this information is implicitly available in the build system, but running tools as part of the build system is not necessarily the best solution.

A compilation database is a JSON file, which consist of an array of “command objects”, where each command object specifies one way a translation unit is compiled in the project.

示例 json 文件如下所示:

[
  {
    "directory": "/source",
    "command": "clang++ <file>.m",
    "file": "/source/div0.c"
  }
]

您可以阅读here and here了解如何设置编译数据库。