├── .gitignore ├── LICENSE ├── README.md ├── SameTypeClangPlugin.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── Vernon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── Vernon.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist └── SameTypeClangPlugin ├── ClangPlugin ├── CMakeLists.txt └── SameTypePlugin.cpp ├── DemoClass.h ├── DemoClass.m ├── Info.plist └── other ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets ├── AppIcon.appiconset │ └── Contents.json └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard └── main.m /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 36 | # 37 | # Pods/ 38 | 39 | # Carthage 40 | # 41 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 42 | # Carthage/Checkouts 43 | 44 | Carthage/Build 45 | 46 | # fastlane 47 | # 48 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 49 | # screenshots whenever they are needed. 50 | # For more information about the recommended setup visit: 51 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 52 | 53 | fastlane/report.xml 54 | fastlane/Preview.html 55 | fastlane/screenshots 56 | fastlane/test_output 57 | 58 | # Code Injection 59 | # 60 | # After new code Injection tools there's a generated folder /iOSInjectionProject 61 | # https://github.com/johnno1962/injectionforxcode 62 | 63 | iOSInjectionProject/ 64 | 65 | */.DS_Store 66 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Vernon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SameTypeClangPlugin — 自定义检查规范的 Clang 插件 2 | 3 | 这是一个实现了自定义检查规范的 Clang 插件,插件的实现方法请查看:[Clang 之旅--实现一个自定义检查规范的 Clang 插件](https://www.jianshu.com/p/c27b77f70616) 4 | 5 | 6 | 7 | ### 效果演示 8 | 9 | ![演示效果](https://upload-images.jianshu.io/upload_images/698554-c7aa746724799734.GIF?imageMogr2/auto-orient/strip) 10 | 11 | 最终实现了:在编译阶段检查某个方法的参数与返回值的类型相同,如果类型不一致的话能抛出编译错误的提示,同时还给出了对应的修改方法(FixIt),点击修改就能改成正确的参数类型 🎉🎉🎉 12 | 13 | 14 | 15 | ### 插件使用方法 16 | 17 | 1. 从[这里](https://github.com/VernonVan/SameTypeClangPlugin/releases/download/1.0/SameTypePlugin.dylib)下载我已经编译好的 Clang 插件 — SameTypeClangPlugin.dylib 18 | 2. 按照[这个教程](https://www.jianshu.com/p/e3f46d42643b)编译一份 Clang 工程,同时按照教程所示方法将 SameTypeClangPlugin.dylib 插件挂载到 Xcode 中,本 Demo 工程可以用来编译查看该插件的使用效果 19 | 20 | > 生成插件所用到的文件在工程目录 SameTypeClangPlugin -> ClangPlugin 中:CMakeLists.txt 和 SameTypePlugin.cpp。 21 | 22 | -------------------------------------------------------------------------------- /SameTypeClangPlugin.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | AD7FC97F2085F53F00830686 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = AD7FC97E2085F53F00830686 /* AppDelegate.m */; }; 11 | AD7FC9852085F53F00830686 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AD7FC9832085F53F00830686 /* Main.storyboard */; }; 12 | AD7FC9872085F54100830686 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AD7FC9862085F54100830686 /* Assets.xcassets */; }; 13 | AD7FC98A2085F54100830686 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AD7FC9882085F54100830686 /* LaunchScreen.storyboard */; }; 14 | AD7FC98D2085F54100830686 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = AD7FC98C2085F54100830686 /* main.m */; }; 15 | AD7FC9952085F55900830686 /* DemoClass.m in Sources */ = {isa = PBXBuildFile; fileRef = AD7FC9942085F55900830686 /* DemoClass.m */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | AD7FC97A2085F53F00830686 /* SameTypeClangPlugin.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SameTypeClangPlugin.app; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | AD7FC97D2085F53F00830686 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 21 | AD7FC97E2085F53F00830686 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 22 | AD7FC9842085F53F00830686 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 23 | AD7FC9862085F54100830686 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 24 | AD7FC9892085F54100830686 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 25 | AD7FC98B2085F54100830686 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 26 | AD7FC98C2085F54100830686 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 27 | AD7FC9932085F55900830686 /* DemoClass.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DemoClass.h; sourceTree = ""; }; 28 | AD7FC9942085F55900830686 /* DemoClass.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DemoClass.m; sourceTree = ""; }; 29 | /* End PBXFileReference section */ 30 | 31 | /* Begin PBXFrameworksBuildPhase section */ 32 | AD7FC9772085F53F00830686 /* Frameworks */ = { 33 | isa = PBXFrameworksBuildPhase; 34 | buildActionMask = 2147483647; 35 | files = ( 36 | ); 37 | runOnlyForDeploymentPostprocessing = 0; 38 | }; 39 | /* End PBXFrameworksBuildPhase section */ 40 | 41 | /* Begin PBXGroup section */ 42 | AD7FC9712085F53E00830686 = { 43 | isa = PBXGroup; 44 | children = ( 45 | AD7FC97C2085F53F00830686 /* SameTypeClangPlugin */, 46 | AD7FC97B2085F53F00830686 /* Products */, 47 | ); 48 | sourceTree = ""; 49 | }; 50 | AD7FC97B2085F53F00830686 /* Products */ = { 51 | isa = PBXGroup; 52 | children = ( 53 | AD7FC97A2085F53F00830686 /* SameTypeClangPlugin.app */, 54 | ); 55 | name = Products; 56 | sourceTree = ""; 57 | }; 58 | AD7FC97C2085F53F00830686 /* SameTypeClangPlugin */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | AD7FC9932085F55900830686 /* DemoClass.h */, 62 | AD7FC9942085F55900830686 /* DemoClass.m */, 63 | AD7FC98B2085F54100830686 /* Info.plist */, 64 | AD7FC9962085F56700830686 /* other */, 65 | ); 66 | path = SameTypeClangPlugin; 67 | sourceTree = ""; 68 | }; 69 | AD7FC9962085F56700830686 /* other */ = { 70 | isa = PBXGroup; 71 | children = ( 72 | AD7FC97D2085F53F00830686 /* AppDelegate.h */, 73 | AD7FC97E2085F53F00830686 /* AppDelegate.m */, 74 | AD7FC9832085F53F00830686 /* Main.storyboard */, 75 | AD7FC9862085F54100830686 /* Assets.xcassets */, 76 | AD7FC9882085F54100830686 /* LaunchScreen.storyboard */, 77 | AD7FC98C2085F54100830686 /* main.m */, 78 | ); 79 | path = other; 80 | sourceTree = ""; 81 | }; 82 | /* End PBXGroup section */ 83 | 84 | /* Begin PBXNativeTarget section */ 85 | AD7FC9792085F53F00830686 /* SameTypeClangPlugin */ = { 86 | isa = PBXNativeTarget; 87 | buildConfigurationList = AD7FC9902085F54100830686 /* Build configuration list for PBXNativeTarget "SameTypeClangPlugin" */; 88 | buildPhases = ( 89 | AD7FC9762085F53F00830686 /* Sources */, 90 | AD7FC9772085F53F00830686 /* Frameworks */, 91 | AD7FC9782085F53F00830686 /* Resources */, 92 | ); 93 | buildRules = ( 94 | ); 95 | dependencies = ( 96 | ); 97 | name = SameTypeClangPlugin; 98 | productName = SameTypeClangPlugin; 99 | productReference = AD7FC97A2085F53F00830686 /* SameTypeClangPlugin.app */; 100 | productType = "com.apple.product-type.application"; 101 | }; 102 | /* End PBXNativeTarget section */ 103 | 104 | /* Begin PBXProject section */ 105 | AD7FC9722085F53E00830686 /* Project object */ = { 106 | isa = PBXProject; 107 | attributes = { 108 | LastUpgradeCheck = 0930; 109 | ORGANIZATIONNAME = Vernon; 110 | TargetAttributes = { 111 | AD7FC9792085F53F00830686 = { 112 | CreatedOnToolsVersion = 9.3; 113 | }; 114 | }; 115 | }; 116 | buildConfigurationList = AD7FC9752085F53E00830686 /* Build configuration list for PBXProject "SameTypeClangPlugin" */; 117 | compatibilityVersion = "Xcode 9.3"; 118 | developmentRegion = en; 119 | hasScannedForEncodings = 0; 120 | knownRegions = ( 121 | en, 122 | Base, 123 | ); 124 | mainGroup = AD7FC9712085F53E00830686; 125 | productRefGroup = AD7FC97B2085F53F00830686 /* Products */; 126 | projectDirPath = ""; 127 | projectRoot = ""; 128 | targets = ( 129 | AD7FC9792085F53F00830686 /* SameTypeClangPlugin */, 130 | ); 131 | }; 132 | /* End PBXProject section */ 133 | 134 | /* Begin PBXResourcesBuildPhase section */ 135 | AD7FC9782085F53F00830686 /* Resources */ = { 136 | isa = PBXResourcesBuildPhase; 137 | buildActionMask = 2147483647; 138 | files = ( 139 | AD7FC98A2085F54100830686 /* LaunchScreen.storyboard in Resources */, 140 | AD7FC9872085F54100830686 /* Assets.xcassets in Resources */, 141 | AD7FC9852085F53F00830686 /* Main.storyboard in Resources */, 142 | ); 143 | runOnlyForDeploymentPostprocessing = 0; 144 | }; 145 | /* End PBXResourcesBuildPhase section */ 146 | 147 | /* Begin PBXSourcesBuildPhase section */ 148 | AD7FC9762085F53F00830686 /* Sources */ = { 149 | isa = PBXSourcesBuildPhase; 150 | buildActionMask = 2147483647; 151 | files = ( 152 | AD7FC9952085F55900830686 /* DemoClass.m in Sources */, 153 | AD7FC98D2085F54100830686 /* main.m in Sources */, 154 | AD7FC97F2085F53F00830686 /* AppDelegate.m in Sources */, 155 | ); 156 | runOnlyForDeploymentPostprocessing = 0; 157 | }; 158 | /* End PBXSourcesBuildPhase section */ 159 | 160 | /* Begin PBXVariantGroup section */ 161 | AD7FC9832085F53F00830686 /* Main.storyboard */ = { 162 | isa = PBXVariantGroup; 163 | children = ( 164 | AD7FC9842085F53F00830686 /* Base */, 165 | ); 166 | name = Main.storyboard; 167 | sourceTree = ""; 168 | }; 169 | AD7FC9882085F54100830686 /* LaunchScreen.storyboard */ = { 170 | isa = PBXVariantGroup; 171 | children = ( 172 | AD7FC9892085F54100830686 /* Base */, 173 | ); 174 | name = LaunchScreen.storyboard; 175 | sourceTree = ""; 176 | }; 177 | /* End PBXVariantGroup section */ 178 | 179 | /* Begin XCBuildConfiguration section */ 180 | AD7FC98E2085F54100830686 /* Debug */ = { 181 | isa = XCBuildConfiguration; 182 | buildSettings = { 183 | ALWAYS_SEARCH_USER_PATHS = NO; 184 | CLANG_ANALYZER_NONNULL = YES; 185 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 186 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 187 | CLANG_CXX_LIBRARY = "libc++"; 188 | CLANG_ENABLE_MODULES = YES; 189 | CLANG_ENABLE_OBJC_ARC = YES; 190 | CLANG_ENABLE_OBJC_WEAK = YES; 191 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 192 | CLANG_WARN_BOOL_CONVERSION = YES; 193 | CLANG_WARN_COMMA = YES; 194 | CLANG_WARN_CONSTANT_CONVERSION = YES; 195 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 196 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 197 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 198 | CLANG_WARN_EMPTY_BODY = YES; 199 | CLANG_WARN_ENUM_CONVERSION = YES; 200 | CLANG_WARN_INFINITE_RECURSION = YES; 201 | CLANG_WARN_INT_CONVERSION = YES; 202 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 203 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 204 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 205 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 206 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 207 | CLANG_WARN_STRICT_PROTOTYPES = YES; 208 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 209 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 210 | CLANG_WARN_UNREACHABLE_CODE = YES; 211 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 212 | CODE_SIGN_IDENTITY = "iPhone Developer"; 213 | COPY_PHASE_STRIP = NO; 214 | DEBUG_INFORMATION_FORMAT = dwarf; 215 | ENABLE_STRICT_OBJC_MSGSEND = YES; 216 | ENABLE_TESTABILITY = YES; 217 | GCC_C_LANGUAGE_STANDARD = gnu11; 218 | GCC_DYNAMIC_NO_PIC = NO; 219 | GCC_NO_COMMON_BLOCKS = YES; 220 | GCC_OPTIMIZATION_LEVEL = 0; 221 | GCC_PREPROCESSOR_DEFINITIONS = ( 222 | "DEBUG=1", 223 | "$(inherited)", 224 | ); 225 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 226 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 227 | GCC_WARN_UNDECLARED_SELECTOR = YES; 228 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 229 | GCC_WARN_UNUSED_FUNCTION = YES; 230 | GCC_WARN_UNUSED_VARIABLE = YES; 231 | IPHONEOS_DEPLOYMENT_TARGET = 11.3; 232 | MTL_ENABLE_DEBUG_INFO = YES; 233 | ONLY_ACTIVE_ARCH = YES; 234 | SDKROOT = iphoneos; 235 | }; 236 | name = Debug; 237 | }; 238 | AD7FC98F2085F54100830686 /* Release */ = { 239 | isa = XCBuildConfiguration; 240 | buildSettings = { 241 | ALWAYS_SEARCH_USER_PATHS = NO; 242 | CLANG_ANALYZER_NONNULL = YES; 243 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 244 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 245 | CLANG_CXX_LIBRARY = "libc++"; 246 | CLANG_ENABLE_MODULES = YES; 247 | CLANG_ENABLE_OBJC_ARC = YES; 248 | CLANG_ENABLE_OBJC_WEAK = YES; 249 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 250 | CLANG_WARN_BOOL_CONVERSION = YES; 251 | CLANG_WARN_COMMA = YES; 252 | CLANG_WARN_CONSTANT_CONVERSION = YES; 253 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 254 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 255 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 256 | CLANG_WARN_EMPTY_BODY = YES; 257 | CLANG_WARN_ENUM_CONVERSION = YES; 258 | CLANG_WARN_INFINITE_RECURSION = YES; 259 | CLANG_WARN_INT_CONVERSION = YES; 260 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 261 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 262 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 263 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 264 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 265 | CLANG_WARN_STRICT_PROTOTYPES = YES; 266 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 267 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 268 | CLANG_WARN_UNREACHABLE_CODE = YES; 269 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 270 | CODE_SIGN_IDENTITY = "iPhone Developer"; 271 | COPY_PHASE_STRIP = NO; 272 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 273 | ENABLE_NS_ASSERTIONS = NO; 274 | ENABLE_STRICT_OBJC_MSGSEND = YES; 275 | GCC_C_LANGUAGE_STANDARD = gnu11; 276 | GCC_NO_COMMON_BLOCKS = YES; 277 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 278 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 279 | GCC_WARN_UNDECLARED_SELECTOR = YES; 280 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 281 | GCC_WARN_UNUSED_FUNCTION = YES; 282 | GCC_WARN_UNUSED_VARIABLE = YES; 283 | IPHONEOS_DEPLOYMENT_TARGET = 11.3; 284 | MTL_ENABLE_DEBUG_INFO = NO; 285 | SDKROOT = iphoneos; 286 | VALIDATE_PRODUCT = YES; 287 | }; 288 | name = Release; 289 | }; 290 | AD7FC9912085F54100830686 /* Debug */ = { 291 | isa = XCBuildConfiguration; 292 | buildSettings = { 293 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 294 | CODE_SIGN_STYLE = Automatic; 295 | DEVELOPMENT_TEAM = R6964LXMCM; 296 | GCC_VERSION = com.apple.compilers.llvm.clang.hacked.compiler; 297 | INFOPLIST_FILE = SameTypeClangPlugin/Info.plist; 298 | LD_RUNPATH_SEARCH_PATHS = ( 299 | "$(inherited)", 300 | "@executable_path/Frameworks", 301 | ); 302 | OTHER_CFLAGS = ( 303 | "-Xclang", 304 | "-load", 305 | "-Xclang", 306 | /opt/llvm/llvm_build/Debug/lib/SameTypePlugin.dylib, 307 | "-Xclang", 308 | "-add-plugin", 309 | "-Xclang", 310 | SameTypePlugin, 311 | ); 312 | PRODUCT_BUNDLE_IDENTIFIER = io.vernonvan.github.SameTypeClangPlugin; 313 | PRODUCT_NAME = "$(TARGET_NAME)"; 314 | TARGETED_DEVICE_FAMILY = "1,2"; 315 | }; 316 | name = Debug; 317 | }; 318 | AD7FC9922085F54100830686 /* Release */ = { 319 | isa = XCBuildConfiguration; 320 | buildSettings = { 321 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 322 | CODE_SIGN_STYLE = Automatic; 323 | DEVELOPMENT_TEAM = R6964LXMCM; 324 | GCC_VERSION = com.apple.compilers.llvm.clang.hacked.compiler; 325 | INFOPLIST_FILE = SameTypeClangPlugin/Info.plist; 326 | LD_RUNPATH_SEARCH_PATHS = ( 327 | "$(inherited)", 328 | "@executable_path/Frameworks", 329 | ); 330 | OTHER_CFLAGS = ( 331 | "-Xclang", 332 | "-load", 333 | "-Xclang", 334 | /opt/llvm/llvm_build/Debug/lib/SameTypePlugin.dylib, 335 | "-Xclang", 336 | "-add-plugin", 337 | "-Xclang", 338 | SameTypePlugin, 339 | ); 340 | PRODUCT_BUNDLE_IDENTIFIER = io.vernonvan.github.SameTypeClangPlugin; 341 | PRODUCT_NAME = "$(TARGET_NAME)"; 342 | TARGETED_DEVICE_FAMILY = "1,2"; 343 | }; 344 | name = Release; 345 | }; 346 | /* End XCBuildConfiguration section */ 347 | 348 | /* Begin XCConfigurationList section */ 349 | AD7FC9752085F53E00830686 /* Build configuration list for PBXProject "SameTypeClangPlugin" */ = { 350 | isa = XCConfigurationList; 351 | buildConfigurations = ( 352 | AD7FC98E2085F54100830686 /* Debug */, 353 | AD7FC98F2085F54100830686 /* Release */, 354 | ); 355 | defaultConfigurationIsVisible = 0; 356 | defaultConfigurationName = Release; 357 | }; 358 | AD7FC9902085F54100830686 /* Build configuration list for PBXNativeTarget "SameTypeClangPlugin" */ = { 359 | isa = XCConfigurationList; 360 | buildConfigurations = ( 361 | AD7FC9912085F54100830686 /* Debug */, 362 | AD7FC9922085F54100830686 /* Release */, 363 | ); 364 | defaultConfigurationIsVisible = 0; 365 | defaultConfigurationName = Release; 366 | }; 367 | /* End XCConfigurationList section */ 368 | }; 369 | rootObject = AD7FC9722085F53E00830686 /* Project object */; 370 | } 371 | -------------------------------------------------------------------------------- /SameTypeClangPlugin.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SameTypeClangPlugin.xcodeproj/project.xcworkspace/xcuserdata/Vernon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VernonVan/SameTypeClangPlugin/fe0c48602efe92d1157a02fff9b94cfc04993ee6/SameTypeClangPlugin.xcodeproj/project.xcworkspace/xcuserdata/Vernon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /SameTypeClangPlugin.xcodeproj/xcuserdata/Vernon.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SameTypeClangPlugin.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /SameTypeClangPlugin/ClangPlugin/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_llvm_loadable_module(SameTypePlugin 2 | SameTypePlugin.cpp 3 | PLUGIN_TOOL clang 4 | ) 5 | 6 | if(LLVM_ENABLE_PLUGINS AND (WIN32 OR CYGWIN)) 7 | target_link_libraries(SameTypePlugin PRIVATE 8 | clangAST 9 | clangBasic 10 | clangFrontend 11 | clangLex 12 | LLVMSupport 13 | ) 14 | endif() -------------------------------------------------------------------------------- /SameTypeClangPlugin/ClangPlugin/SameTypePlugin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "clang/AST/AST.h" 3 | #include "clang/AST/ASTConsumer.h" 4 | #include "clang/ASTMatchers/ASTMatchers.h" 5 | #include "clang/ASTMatchers/ASTMatchFinder.h" 6 | #include "clang/Frontend/CompilerInstance.h" 7 | #include "clang/Frontend/FrontendPluginRegistry.h" 8 | using namespace clang; 9 | using namespace std; 10 | using namespace llvm; 11 | using namespace clang::ast_matchers; 12 | 13 | namespace SameTypePlugin 14 | { 15 | class SameTypeHandler : public MatchFinder::MatchCallback 16 | { 17 | private: 18 | CompilerInstance &Instance; 19 | ASTContext *context; 20 | std::vector propertyDeclVector; 21 | 22 | public: 23 | SameTypeHandler(CompilerInstance &Instance) : Instance(Instance) {} 24 | 25 | void setContext(ASTContext &context) 26 | { 27 | this->context = &context; 28 | } 29 | 30 | virtual void run(const MatchFinder::MatchResult &Result) 31 | { 32 | if (const ObjCPropertyDecl *propertyDecl = Result.Nodes.getNodeAs("objcPropertyDecl")) { 33 | if (isUserSourceDecl(propertyDecl)) { 34 | // 存储 Objective-C 类属性 35 | propertyDeclVector.push_back(const_cast(propertyDecl)); 36 | } 37 | } else if (const BinaryOperator *binaryOperator = Result.Nodes.getNodeAs("binaryOperator")) { 38 | if (isUserSourceStmt(binaryOperator) && binaryOperator->isAssignmentOp()) { 39 | ObjCPropertyRefExpr *leftExpr = dyn_cast_or_null(binaryOperator->getLHS()); 40 | OpaqueValueExpr *rightExpr = dyn_cast_or_null(binaryOperator->getRHS()); 41 | if (leftExpr && rightExpr) { 42 | std::string propertyName = leftExpr->getGetterSelector().getAsString(); 43 | if (ImplicitCastExpr *castExpr = dyn_cast_or_null(rightExpr->getSourceExpr())) { 44 | if (ObjCMessageExpr *messageExpr = dyn_cast_or_null(castExpr->getSubExpr())) { 45 | ObjCMethodDecl *methodDecl = messageExpr->getMethodDecl(); 46 | if (checkIfHasAttribute(methodDecl)) { 47 | for (Stmt *stmt : messageExpr->arguments()) { 48 | ObjCMessageExpr *callClassExpr = dyn_cast_or_null(stmt); 49 | if (callClassExpr && callClassExpr->getSelector().getAsString() == "class") { 50 | string leftType = removePtrString(getPropertyType(propertyName)); 51 | string rightType = removePtrString(callClassExpr->getClassReceiver().getAsString()); 52 | DiagnosticsEngine &diag = Instance.getDiagnostics(); 53 | if (leftType.find(rightType) == std::string::npos) { 54 | FixItHint fixItHint = FixItHint::CreateReplacement(callClassExpr->getReceiverRange(), leftType); 55 | diag.Report(binaryOperator->getLocStart(), diag.getCustomDiagID(DiagnosticsEngine::Warning, "类型不一致:左边:%0 右边:%1")) << leftType << rightType << fixItHint; 56 | } 57 | } 58 | } 59 | } 60 | } 61 | } 62 | } 63 | } 64 | } 65 | } 66 | 67 | string getPropertyType(const string propertyName) 68 | { 69 | vector::iterator iter; 70 | for (iter = propertyDeclVector.begin(); iter != propertyDeclVector.end(); iter++) { 71 | ObjCPropertyDecl *decl = *iter; 72 | if (decl->getName() == propertyName) { 73 | return decl->getType().getAsString(); 74 | } 75 | } 76 | return NULL; 77 | } 78 | 79 | string removePtrString(const string typeString) 80 | { 81 | size_t lastindex = typeString.find_last_of("*"); 82 | return typeString.substr(0, lastindex); 83 | } 84 | 85 | bool checkIfHasAttribute(ObjCMethodDecl *methodDecl) 86 | { 87 | for (Attr *attr : methodDecl->attrs()) { 88 | if (!strcmp(attr->getSpelling(), "objc_same_type")) { 89 | return true; 90 | } 91 | } 92 | return false; 93 | } 94 | 95 | bool isUserSourceDecl(const Decl *decl) 96 | { 97 | string filename = Instance.getSourceManager().getFilename(decl->getSourceRange().getBegin()).str(); 98 | return isUserSourceWithFilename(filename); 99 | } 100 | 101 | bool isUserSourceStmt(const Stmt *stmt) 102 | { 103 | string filename = Instance.getSourceManager().getFilename(stmt->getSourceRange().getBegin()).str(); 104 | return isUserSourceWithFilename(filename); 105 | } 106 | 107 | bool isUserSourceWithFilename(const string filename) 108 | { 109 | if (filename.empty()) 110 | return false; 111 | 112 | //非XCode中的源码都认为是用户源码 113 | if(filename.find("/Applications/Xcode.app/") == 0) 114 | return false; 115 | 116 | return true; 117 | } 118 | }; 119 | 120 | 121 | class SameTypePluginASTConsumer: public ASTConsumer 122 | { 123 | public: 124 | SameTypePluginASTConsumer(CompilerInstance &Instance) : handlerForSameType(Instance) 125 | { 126 | matcher.addMatcher(objcPropertyDecl().bind("objcPropertyDecl"), &handlerForSameType); 127 | 128 | matcher.addMatcher(binaryOperator(hasOperatorName("=")).bind("binaryOperator"), &handlerForSameType); 129 | } 130 | 131 | private: 132 | MatchFinder matcher; 133 | SameTypeHandler handlerForSameType; 134 | 135 | void HandleTranslationUnit(ASTContext &context) 136 | { 137 | handlerForSameType.setContext(context); 138 | matcher.matchAST(context); 139 | } 140 | 141 | }; 142 | 143 | class SameTypePluginASTAction: public PluginASTAction 144 | { 145 | public: 146 | unique_ptr CreateASTConsumer(CompilerInstance &Compiler, StringRef InFile) 147 | { 148 | return unique_ptr(new SameTypePluginASTConsumer(Compiler)); 149 | } 150 | 151 | bool ParseArgs(const CompilerInstance &CI, const std::vector &args) 152 | { 153 | return true; 154 | } 155 | }; 156 | } 157 | 158 | static clang::FrontendPluginRegistry::Add 159 | X("SameTypePlugin", "check parameter and return type"); 160 | -------------------------------------------------------------------------------- /SameTypeClangPlugin/DemoClass.h: -------------------------------------------------------------------------------- 1 | // 2 | // DemoClass.h 3 | // SameTypeClangPlugin 4 | // 5 | // Created by Vernon on 2018/4/17. 6 | // Copyright © 2018年 Vernon. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface DemoClass : NSObject 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /SameTypeClangPlugin/DemoClass.m: -------------------------------------------------------------------------------- 1 | // 2 | // DemoClass.m 3 | // SameTypeClangPlugin 4 | // 5 | // Created by Vernon on 2018/4/17. 6 | // Copyright © 2018年 Vernon. All rights reserved. 7 | // 8 | 9 | #import "DemoClass.h" 10 | 11 | @interface DemoClass () 12 | @property (nonatomic, strong) NSString *myString; 13 | @property (nonatomic, strong) NSArray *myArray; 14 | @end 15 | 16 | @implementation DemoClass 17 | 18 | - (void)someMethod 19 | { 20 | self.myString = [self modelOfClass:[NSString class]]; 21 | self.myArray = [self modelOfClass:[NSString class]]; 22 | } 23 | 24 | - (__kindof NSObject *)modelOfClass:(Class)modelClass __attribute__((objc_same_type)) 25 | { 26 | if ([modelClass isKindOfClass:[NSString class]]) { 27 | return [[NSString alloc] init]; 28 | } else if ([modelClass isKindOfClass:[NSArray class]]) { 29 | return [[NSArray alloc] init]; 30 | } 31 | return nil; 32 | } 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /SameTypeClangPlugin/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /SameTypeClangPlugin/other/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // SameTypeClangPlugin 4 | // 5 | // Created by Vernon on 2018/4/17. 6 | // Copyright © 2018年 Vernon. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /SameTypeClangPlugin/other/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // SameTypeClangPlugin 4 | // 5 | // Created by Vernon on 2018/4/17. 6 | // Copyright © 2018年 Vernon. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /SameTypeClangPlugin/other/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /SameTypeClangPlugin/other/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /SameTypeClangPlugin/other/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /SameTypeClangPlugin/other/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /SameTypeClangPlugin/other/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // SameTypeClangPlugin 4 | // 5 | // Created by Vernon on 2018/4/17. 6 | // Copyright © 2018年 Vernon. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | --------------------------------------------------------------------------------