解析 Objective-C 代码以进行静态分析

发布于 2024-11-26 08:24:41 字数 784 浏览 3 评论 0原文

我喜欢静态分析和编译时检查,几乎到了过分的地步,但我日常工作的大部分时间都是在 Objective-C 中进行的。为了解决这种紧张关系,我希望能够编写自己的分析工具,并可以在我的 Objective-C 项目上运行。

但在互联网上谷歌搜索表明,人们很难组合出完整的 Objective-C 语法。 一个站点基本上建议放弃。

我确实在 ANTLR 网站上找到了语法,但是当我启动它时,我找不到让它解析任何东西。例如,它响应了以下行:

void x();

with src/main/resources/somecode.m line 1:0 no可行的替代方案 at input 'void'

:(

我仔细查看了语法并发现以下令人沮丧的免责声明:

it's a work in progress, most of the .h file can be parsed

但我需要可以解析接口和实现的东西。

有没有完整的 Objective-C 2.0 语法?我更喜欢可以与 Scala 一起使用的东西(所以任何与 Java 兼容的东西,比如 ANTLR,都可以)是完美的),但此时我愿意改编为另一个解析器工具包设计的东西。

I love static analysis and compile-time checks, almost to a fault, but most of my day job is in Objective-C. To resolve this tension, I'd like to be able to write my own analysis tools that I can run on my Objective-C projects.

But googling around the Internet suggests that people are having a hard time putting together a complete Objective-C grammar. One site basically recommends giving up.

I did find a grammar on the ANTLR website, but when I fired it up, I couldn't get it to parse anything at all. For example, it responded to the line:

void x();

with src/main/resources/somecode.m line 1:0 no viable alternative at input 'void'

:(

I took a closer look at the grammar and found the following disheartening disclaimer:

it's a work in progress, most of the .h file can be parsed

But I need something that can parse both interface and implementation.

Is there a complete Objective-C 2.0 grammar out there somewhere? I'd prefer something that can work with Scala (so anything Java compatible, like ANTLR, would be perfect), but at this point I'd be willing to adapt something designed for another parser toolkit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嘦怹 2024-12-03 08:24:41

正如其他人提到的,Clang 将是正确的解决方案。您可以提供自己的 AST 使用者,即在检查 AST 时将调用的类,这样您就不必担心解析或弄乱语法。

Clang 完全支持 Objective-C,并且静态分析器中已有很多类,您可以根据这些类对自己的检查进行建模。 (在clang/lib/StaticAnalyzer/Checkers中)。

该目录包含许多静态分析器检查器,但您也可以只创建一个普通的 AST 使用者。请参阅http://code.google.com/p/chromium/wiki/WritingClangPlugins 了解更多信息。

As others mentioned, Clang would be the right solution. You can provide your own AST consumers, i.e. classes that will be invoked when going over the AST, leaving you not having to worry about parsing or messing with grammar.

Clang supports Objective-C in its entirety, and there's a lot of classes already in the static analyzer that you can model your own checks after. (in clang/lib/StaticAnalyzer/Checkers).

That directory contains a lot of static analyzer checkers, but you can also just create a normal AST consumer. Refer to http://code.google.com/p/chromium/wiki/WritingClangPlugins for more information.

马蹄踏│碎落叶 2024-12-03 08:24:41

Clang 是一个支持 Objective-C 的静态分析工具。我过去发现它非常有用。

http://clang-analyzer.llvm.org/

Clang is a static analysis tool that has support for Objective-C. I've found it very useful in the past.

http://clang-analyzer.llvm.org/

烟酒忠诚 2024-12-03 08:24:41

clang 是可扩展的;您可以扩展他们现有的静态分析或创建您自己的静态分析。 llvm / clang 被构建为一系列可以链接到(动态或静态)的库。一个很好的起点是 ARC(自动引用计数)迁移器库,负责静态分析和重写 Objective-C 代码。

< code>arcmt-test 是一个使用 ARC 迁移器库的小示例程序。

clang is extensible; you can extend their existing static analysis or create your own. llvm / clang is architected as a series of libraries you can link to (dynamically or statically). A great starting point is the ARC (automatic reference counting) migrator library, which is responsible for statically analysing and rewriting objective-c code.

arcmt-test is a small example program that consumes the ARC migrator library.

节枝 2024-12-03 08:24:41

您可以使用OCDepend,它是一个基于Clang的静态分析工具,可以简化管理Objective-C代码质量并提供高度灵活的代码查询框架。

You can use OCDepend, it's a static analysis tool based on Clang that simplifies managing Objective-C code quality and provides a highly flexible code query framework.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文