Source code

Clang trip - to implement a custom plug-in Clang plug-in specification

Clang travel series:
Clang trip -- using Xcode to develop Clang plug-in
Clang trip - [translation] add custom attribute
Clang trip -- a Clang plug-in that implements a custom check specification

Preface

A series of articles on Clang trip Opening chapter At the time, I mentioned that the direct reason for my contact with Clang is to achieve a custom checking requirement: is there any way to check that the parameters of a method are the same as the type of the return value at compile time, and if the type is not consistent, we can throw out the hints of compile errors. Now I have completed the plug-in according to my own needs. This article will explain the implementation idea of this plug-in, and the corresponding code is here: Https://github.com/VernonVan/SameTypeClangPlugin

Customized demand

First of all, I need to make the demand more extensive.

Imagine that we have such a function modelOfClass:

ModelOfClass accepts a Class type parameter, and then performs different operations according to the corresponding class of Class, and finally returns the processed instance instance of the Class corresponding class. We use __kindof NSObject * to return the value type to ensure that it must be NSObject or its subclass. However, there is such a wrong way of calling, but it can be compiled through:

You can see that there are two rows of modelOfClass function calls in someMethod. The first line call is correct. The property of the NSString * type is called [NSString class] when the myString is called; the second row call is wrong, and the property of the NSArray * type is called [NSString class] when the myArray is invoked. That is to say, in the Objective-C language, there is no way to check that the parameter type and the return value type are exactly the same when calling the function.

This requirement is abstracted from the project of my company. We can not see what this function is for. We may feel that this demand is not common, and there is no universality. However, this article wants readers to see the big ones and draw inferences from them, and more importantly, learn how to use the universal way to implement custom plug-in Clang plug-ins according to their needs.

Final effect

Let's take a look at the final effect.

Fabulous ( Zero )

This article is composed of Contributor Creation, article address: Https://blog.isoyu.com/archives/clang-zhilv-shixianyigezidingyijianchaguifande-clang-chajian.html
Use Knowledge sharing signature 4 The international license agreement is licensed. In addition to the reprint / provenance, all originals or translations of this site must be signed before retransmission. The final editing time is April, 20, 2018 at 04:08 afternoon.

Hot articles