diff --git a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp index 80cc475b07a1..4c7c164b9060 100644 --- a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp @@ -100,7 +100,8 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { } // The rest of this check is only relevant to C++. - if (!getLangOpts().CPlusPlus) + // We also disable it for Objective-C++. + if (!getLangOpts().CPlusPlus || getLangOpts().ObjC1 || getLangOpts().ObjC2) return; // Ignore code inside extern "C" {} blocks. if (!match(expr(hasAncestor(linkageSpecDecl())), *CastExpr, *Result.Context) diff --git a/clang-tools-extra/test/clang-tidy/google-readability-casting.mm b/clang-tools-extra/test/clang-tidy/google-readability-casting.mm new file mode 100644 index 000000000000..b987dc2279aa --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/google-readability-casting.mm @@ -0,0 +1,179 @@ +// 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. + +bool g() { return false; } + +enum Enum { Enum1 }; +struct X {}; +struct Y : public X {}; + +void f(int a, double b, const char *cpc, const void *cpv, X *pX) { + + typedef const char *Typedef1; + typedef const char *Typedef2; + Typedef1 t1; + (Typedef2)t1; + (const char*)t1; + (Typedef1)cpc; + + typedef char Char; + char *pc; + Char *pChar = (Char*)pc; + + (Char)*cpc; + + (char)*pChar; + + (const char*)cpv; + + char *pc2 = (char*)(cpc + 33); + + const char &crc = *cpc; + char &rc = (char&)crc; + + char &rc2 = (char&)*cpc; + + char ** const* const* ppcpcpc; + char ****ppppc = (char****)ppcpcpc; + + char ***pppc = (char***)*(ppcpcpc); + + char ***pppc2 = (char***)(*ppcpcpc); + + char *pc5 = (char*)(const char*)(cpv); + + int b1 = (int)b; + b1 = (const int&)b; + + b1 = (int) b; + + b1 = (int) b; + + b1 = (int) (b); + + b1 = (int) (b); + + Y *pB = (Y*)pX; + Y &rB = (Y&)*pX; + + const char *pc3 = (const char*)cpv; + + char *pc4 = (char*)cpv; + + b1 = (int)Enum1; + + Enum e = (Enum)b1; + + int b2 = int(b); + int b3 = static_cast(b); + int b4 = b; + double aa = a; + (void)b2; + return (void)g(); +} + +template +void template_function(T t, int n) { + int i = (int)t; +} + +template +struct TemplateStruct { + void f(T t, int n) { + int k = (int)t; + } +}; + +void test_templates() { + template_function(1, 42); + template_function(1.0, 42); + TemplateStruct().f(1, 42); + TemplateStruct().f(1.0, 42); +} + +extern "C" { +void extern_c_code(const char *cpc) { + char *pc = (char*)cpc; +} +} + +#define CAST(type, value) (type)(value) +void macros(double d) { + int i = CAST(int, d); +} + +enum E { E1 = 1 }; +template +struct A { + // Usage of template argument e = E1 is represented as (E)1 in the AST for + // some reason. We have a special treatment of this case to avoid warnings + // here. + static const E ee = e; +}; +struct B : public A {}; + + +void overloaded_function(); +void overloaded_function(int); + +template +void g(Fn fn) { + fn(); +} + +void function_casts() { + typedef void (*FnPtrVoid)(); + typedef void (&FnRefVoid)(); + typedef void (&FnRefInt)(int); + + g((void (*)())overloaded_function); + g((void (*)())&overloaded_function); + g((void (&)())overloaded_function); + + g((FnPtrVoid)overloaded_function); + g((FnPtrVoid)&overloaded_function); + g((FnRefVoid)overloaded_function); + + FnPtrVoid fn0 = (void (*)())&overloaded_function; + FnPtrVoid fn1 = (void (*)())overloaded_function; + FnPtrVoid fn1a = (FnPtrVoid)overloaded_function; + FnRefInt fn2 = (void (&)(int))overloaded_function; + auto fn3 = (void (*)())&overloaded_function; + auto fn4 = (void (*)())overloaded_function; + auto fn5 = (void (&)(int))overloaded_function; + + void (*fn6)() = (void (*)())&overloaded_function; + void (*fn7)() = (void (*)())overloaded_function; + void (*fn8)() = (FnPtrVoid)overloaded_function; + void (&fn9)(int) = (void (&)(int))overloaded_function; + + void (*correct1)() = static_cast(overloaded_function); + FnPtrVoid correct2 = static_cast(&overloaded_function); + FnRefInt correct3 = static_cast(overloaded_function); +} + +struct S { + S(const char *); +}; +struct ConvertibleToS { + operator S() const; +}; +struct ConvertibleToSRef { + operator const S&() const; +}; + +void conversions() { + //auto s1 = (const S&)""; + auto s2 = (S)""; + auto s2a = (struct S)""; + auto s2b = (const S)""; + ConvertibleToS c; + auto s3 = (const S&)c; + auto s4 = (S)c; + ConvertibleToSRef cr; + auto s5 = (const S&)cr; + auto s6 = (S)cr; +}