benhamilton created this revision. benhamilton added reviewers: alexfh, Wizard, hokein. Herald added a subscriber: cfe-commits.
Previously, `google-readability-casting` would trigger for Objective-C++ code using C-style casts to or from Objective-C object types. The official Google Objective-C standard says Objective-C++ allows authors to mix the Objective-C style (which uses C-style casts) and C++ (which disallows it): http://google.github.io/styleguide/objcguide.html#style-matches-the-language So, to resolve this conflict, this diff updates `google-readability-casting` to ignore C-style casts to or from Objective-C object types. Test Plan: New tests added. Ran tests with: % make -j16 check-clang-tools Before diff, confirmed tests failed: https://reviews.llvm.org/P8081 After diff, confirrmed tests passed. Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D46659 Files: clang-tidy/google/AvoidCStyleCastsCheck.cpp test/clang-tidy/google-readability-casting.mm Index: test/clang-tidy/google-readability-casting.mm =================================================================== --- /dev/null +++ test/clang-tidy/google-readability-casting.mm @@ -0,0 +1,42 @@ +// RUN: clang-tidy %s -checks=-*,google-readability-casting -- \ +// RUN: -xobjective-c++ -fobjc-abi-version=2 -fobjc-arc | count 0 + +// Note: this test expects no diagnostics, but FileCheck cannot handle that, +// hence the use of | count 0. + +#define nil 0 + +@interface Foo +@end + +@protocol Proto +@end + +@interface Bar : Foo <Proto> +@end + +@interface Baz : Foo <Proto> +@end + +void foo() { + id nilObj = nil; + Foo *foo = (Foo *)nilObj; + Bar *bar = (Bar *)nilObj; + id ego = (id)foo; + foo = (Foo *)bar; + foo = (id)bar; + id<Proto> nilProto = (id<Proto>)bar; + ego = (id)nilProto; + bar = (Bar *)nilProto; + Foo<Proto> *fooProto = (Foo<Proto> *)bar; + Baz *baz = (Baz *)bar; + Class klass = (Class)nilObj; + ego = (id)klass; + void *voidStar = nullptr; + foo = (__bridge Foo *)voidStar; + nilProto = (__bridge id<Proto>)voidStar; + klass = (__bridge Class)voidStar; + voidStar = (__bridge void *)foo; + voidStar = (__bridge void *)nilProto; + voidStar = (__bridge void *)klass; +} Index: clang-tidy/google/AvoidCStyleCastsCheck.cpp =================================================================== --- clang-tidy/google/AvoidCStyleCastsCheck.cpp +++ clang-tidy/google/AvoidCStyleCastsCheck.cpp @@ -110,6 +110,10 @@ // compiled as C++. if (getCurrentMainFile().endswith(".c")) return; + // Ignore casts between Objective-C types. + if (SourceType->isObjCObjectPointerType() || + DestType->isObjCObjectPointerType()) + return; SourceManager &SM = *Result.SourceManager;
Index: test/clang-tidy/google-readability-casting.mm =================================================================== --- /dev/null +++ test/clang-tidy/google-readability-casting.mm @@ -0,0 +1,42 @@ +// RUN: clang-tidy %s -checks=-*,google-readability-casting -- \ +// RUN: -xobjective-c++ -fobjc-abi-version=2 -fobjc-arc | count 0 + +// Note: this test expects no diagnostics, but FileCheck cannot handle that, +// hence the use of | count 0. + +#define nil 0 + +@interface Foo +@end + +@protocol Proto +@end + +@interface Bar : Foo <Proto> +@end + +@interface Baz : Foo <Proto> +@end + +void foo() { + id nilObj = nil; + Foo *foo = (Foo *)nilObj; + Bar *bar = (Bar *)nilObj; + id ego = (id)foo; + foo = (Foo *)bar; + foo = (id)bar; + id<Proto> nilProto = (id<Proto>)bar; + ego = (id)nilProto; + bar = (Bar *)nilProto; + Foo<Proto> *fooProto = (Foo<Proto> *)bar; + Baz *baz = (Baz *)bar; + Class klass = (Class)nilObj; + ego = (id)klass; + void *voidStar = nullptr; + foo = (__bridge Foo *)voidStar; + nilProto = (__bridge id<Proto>)voidStar; + klass = (__bridge Class)voidStar; + voidStar = (__bridge void *)foo; + voidStar = (__bridge void *)nilProto; + voidStar = (__bridge void *)klass; +} Index: clang-tidy/google/AvoidCStyleCastsCheck.cpp =================================================================== --- clang-tidy/google/AvoidCStyleCastsCheck.cpp +++ clang-tidy/google/AvoidCStyleCastsCheck.cpp @@ -110,6 +110,10 @@ // compiled as C++. if (getCurrentMainFile().endswith(".c")) return; + // Ignore casts between Objective-C types. + if (SourceType->isObjCObjectPointerType() || + DestType->isObjCObjectPointerType()) + return; SourceManager &SM = *Result.SourceManager;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits