From 5f70c473c9c0c7a7911534c7826e01c1c632c3cd Mon Sep 17 00:00:00 2001 From: Stephane Moore Date: Wed, 20 Mar 2019 23:05:00 +0000 Subject: [PATCH] =?UTF-8?q?[clang-tidy]=20Disable=20google-runtime-int=20i?= =?UTF-8?q?n=20Objective-C++=20=F0=9F=94=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: In contrast to Google C++, Objective-C often uses built-in integer types other than `int`. In fact, the Objective-C runtime itself defines the types NSInteger¹ and NSUInteger² which are variant types depending on the target architecture. The Objective-C style guide indicates that usage of system types with variant sizes is appropriate when handling values provided by system interfaces³. Objective-C++ is commonly the result of conversion from Objective-C to Objective-C++ for the purpose of integrating C++ functionality. The opposite of Objective-C++ being used to expose Objective-C functionality to C++ is less common, potentially because Objective-C has a signficantly more uneven presence on different platforms compared to C++. This generally predisposes Objective-C++ to commonly being more Objective-C than C++. Forcing Objective-C++ developers to perform conversions between variant system types and fixed size integer types depending on target architecture when Objective-C++ commonly uses variant system types from Objective-C is likely to lead to more bugs and overhead than benefit. For that reason, this change proposes to disable google-runtime-int in Objective-C++. [1] https://developer.apple.com/documentation/objectivec/nsinteger?language=objc [2] https://developer.apple.com/documentation/objectivec/nsuinteger?language=objc [3] "Types long, NSInteger, NSUInteger, and CGFloat vary in size between 32- and 64-bit builds. Use of these types is appropriate when handling values exposed by system interfaces, but they should be avoided for most other computations." https://github.com/google/styleguide/blob/gh-pages/objcguide.md#types-with-inconsistent-sizes Subscribers: xazax.hun, jdoerfert, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D59336 llvm-svn: 356627 --- .../clang-tidy/google/IntegerTypesCheck.cpp | 4 ++- clang-tools-extra/docs/ReleaseNotes.rst | 3 ++ .../test/clang-tidy/google-runtime-int.m | 32 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/test/clang-tidy/google-runtime-int.m diff --git a/clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp b/clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp index c9a1f3beaebf..fb6fd3be3dfb 100644 --- a/clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp @@ -54,7 +54,9 @@ void IntegerTypesCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { void IntegerTypesCheck::registerMatchers(MatchFinder *Finder) { // Find all TypeLocs. The relevant Style Guide rule only applies to C++. - if (!getLangOpts().CPlusPlus) + // This check is also not applied in Objective-C++ sources as Objective-C + // often uses built-in integer types other than `int`. + if (!getLangOpts().CPlusPlus || getLangOpts().ObjC) return; // Match any integer types, unless they are passed to a printf-based API: // diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 76e850232f21..f69d3e705585 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -115,6 +115,9 @@ Improvements to clang-tidy `CommentUserDefiniedLiterals`, `CommentStringLiterals`, `CommentCharacterLiterals` & `CommentNullPtrs` options. +- The :doc:`google-runtime-int ` + check has been disabled in Objective-C++. + - The `Acronyms` and `IncludeDefaultAcronyms` options for the :doc:`objc-property-declaration ` check have been removed. diff --git a/clang-tools-extra/test/clang-tidy/google-runtime-int.m b/clang-tools-extra/test/clang-tidy/google-runtime-int.m new file mode 100644 index 000000000000..dd1225c9c5b9 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/google-runtime-int.m @@ -0,0 +1,32 @@ +// RUN: clang-tidy -checks=-*,google-runtime-int %s 2>&1 -- | count 0 +// RUN: clang-tidy -checks=-*,google-runtime-int %s 2>&1 -- -x objective-c++ | count 0 + +typedef long NSInteger; +typedef unsigned long NSUInteger; + +@interface NSString +@property(readonly) NSInteger integerValue; +@property(readonly) long long longLongValue; +@property(readonly) NSUInteger length; +@end + +NSInteger Foo(NSString *s) { + return [s integerValue]; +} + +long long Bar(NSString *s) { + return [s longLongValue]; +} + +NSUInteger Baz(NSString *s) { + return [s length]; +} + +unsigned short NSSwapShort(unsigned short inv); + +long DoSomeMath(long a, short b) { + short c = NSSwapShort(b); + long a2 = a * 5L; + return a2 + c; +} +