Creating and using Clang plugin with Xcode

57 篇文章 0 订阅

链接:https://railsware.com/blog/2014/02/28/creation-and-using-clang-plugin-with-xcode/

如何写一个clang plugin,很不错的文章,记录一下。

This tutorial describes how to create Clang plugin and covers the next things:
  • environment setup
  • basic plugin setup
  • setup Xcode project for plugin development
  • warnings reporting
  • errors reporting
  • Xcode integration
  • interactive hints for errors/warnings riddance

tl;dr

Clang Rocks!!!

You can find the plugin here.

Intro

While working on BloodMagic, I realised that it’d be nice to have a tool for checking semantic errors related to BM usage. For example: in the interface property marked as lazy, but not defined as @dynamic in the implementation, or property marked as lazy, but class container doesn’t support injections.

I concluded that I need to work with and I need a full-featured parser.

I’ve tried different approaches: flex+bisonlibclang, but ultimately I decided to write a Clang plugin.

Just for testing purposes I’ve started a simple plugin with the following goals:

  • use Xcode for development
  • integrate ready plugin into Xcode and use it workaday
  • plugin should report warnings, errors and propose interactive hints for fixes (via Xcode UI)

Features of the test plugin:

  • report warning in case of class’ name starts with lowercase letter
  • report error in case of class’ name contains underscore _
  • propose hints for fixes
Environment setup

For plugin development we need llvm/clang, built from source

Current clang version on my system – 3.3.1, so let’s build respective version:

Basic plugin setup

Create directory for plugin

Our plugin based on example from Clang repo and here is it’s structure:

We’ll use one source file for simplification

Now we’re able to generate Xcode-project, based on CMakeLists.txt

Run ALL_BUILD target, and you’ll see dynamic library at lib/Debug/ToyCLangPlugin.dylib.

RecursiveASTVisitor

Clang AST module provides RecursiveASTVisitor, which allows us to traverse via AST.
We just need to create a subclass and implement interesting methods.
For test we’ll print all the found class names.

Let’s create test source file and check how plugin works.

Rebuild plugin and run

We’ll see a huge list of classes.

Report warnings

Let’s report warning in case if class’ name starts with lowercase letter

Add ASTContext to ToyClassVisitor

Add check

Add some class with “bad” name

rebuild and run

Report error

Let’s generate error in case of class’ name contains _

Output after running

Uncomment first check checkForLowercasedName and you’ll see both error and warning in the output

Xcode integration

Unfortunately, system (under ‘system’ I mean Xcode’s clang) clang doesn’t support plugins, so we need to hack Xcode a bit, to allow using of custom compiler.

Unzip this archive and run following commands:

This will enable custom compiler for Xcode.

Reopen Xcode and you’ll see new compiler:

custom_compiler

Create new project and select newly added custom clang in Build settings

To enable plugin add following parameters to the OTHER_CFLAGS section

OTHER_CFLAGS

Note, that we use -add-plugin here, because we want to add our ASTAction, not to replace the existing

Also, you should disable modules for this target/build

disable_modules

Add test.m to this project, or create new one, with class names that match plugin criteria

After build you’ll see error and warnings in a more familiar form

error_warning

Interactive hints

It’s time to add FixItHints for both warning and error

Rebuild plugin and try to build test project

warning_fixit_hint
error_fixit_hint

Conclusion

As you can see, creating a clang plugin is relatively simple, but it needs some dirty hacks with Xcode, and you should use custom built clang, so I’d not recommend you to use this clang for building apps for production usage. Apple provides patched version, and we can’t know the difference between them. Also, it needs a lot of efforts to make it work, which doesn’t make it widely usable.
Another issue you might face is unstable API, cause it uses internal API which changes continuously.

You still can use it on your system for different diagnostic purposes, but please do not force other people to depend on such heavyweight things.

If you have any comments, questions or suggestions feel free to ask me on twitter, open issue on GitHub, or just leave a comment here.

Happy hacking!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值