[clang-tools-extra] r329730 - [clang-tidy] [modernize-use-auto] Add a threshold for minimal type name length to be replaced with 'auto'

Zinovy Nis via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 10 11:05:25 PDT 2018


Author: zinovy.nis
Date: Tue Apr 10 11:05:24 2018
New Revision: 329730

URL: http://llvm.org/viewvc/llvm-project?rev=329730&view=rev
Log:
[clang-tidy] [modernize-use-auto] Add a threshold for minimal type name length to be replaced with 'auto'

The threshold option is 'MinTypeNameLength' with default value '5'. 
With MinTypeNameLength == 5 'int'/'bool' and 'const int'/'const bool' 
will not be converted to 'auto', while 'unsigned' will be. 

Differential Revision: https://reviews.llvm.org/D45405

Added:
    clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-min-type-name-length.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.cpp
    clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.h
    clang-tools-extra/trunk/docs/ReleaseNotes.rst
    clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-use-auto.rst
    clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-cast-remove-stars.cpp
    clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-cast.cpp
    clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-new.cpp

Modified: clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.cpp?rev=329730&r1=329729&r2=329730&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.cpp Tue Apr 10 11:05:24 2018
@@ -11,6 +11,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Tooling/FixIt.h"
 
 using namespace clang;
 using namespace clang::ast_matchers;
@@ -286,10 +287,11 @@ StatementMatcher makeCombinedMatcher() {
 } // namespace
 
 UseAutoCheck::UseAutoCheck(StringRef Name, ClangTidyContext *Context)
-    : ClangTidyCheck(Name, Context),
-      RemoveStars(Options.get("RemoveStars", 0)) {}
+    : ClangTidyCheck(Name, Context), RemoveStars(Options.get("RemoveStars", 0)),
+      MinTypeNameLength(Options.get("MinTypeNameLength", 5)) {}
 
 void UseAutoCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "MinTypeNameLength", MinTypeNameLength);
   Options.store(Opts, "RemoveStars", RemoveStars ? 1 : 0);
 }
 
@@ -414,6 +416,12 @@ void UseAutoCheck::replaceExpr(
     Loc = Loc.getNextTypeLoc();
   }
   SourceRange Range(Loc.getSourceRange());
+
+  if (MinTypeNameLength != 0 &&
+      tooling::fixit::getText(Loc.getSourceRange(), FirstDecl->getASTContext())
+              .size() < MinTypeNameLength)
+    return;
+
   auto Diag = diag(Range.getBegin(), Message);
 
   // Space after 'auto' to handle cases where the '*' in the pointer type is

Modified: clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.h?rev=329730&r1=329729&r2=329730&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.h Tue Apr 10 11:05:24 2018
@@ -29,6 +29,7 @@ private:
                    llvm::function_ref<QualType(const Expr *)> GetType,
                    StringRef Message);
 
+  const unsigned int MinTypeNameLength;
   const bool RemoveStars;
 };
 

Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=329730&r1=329729&r2=329730&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Tue Apr 10 11:05:24 2018
@@ -57,6 +57,11 @@ The improvements are...
 Improvements to clang-tidy
 --------------------------
 
+- New option `MinTypeNameLength` for `modernize-use-auto` to limit the minimal
+  length of type names to be replaced with 'auto'. Use to skip replacing
+  short type names like 'int'/'bool' -> 'auto'. Default value is 5 which means
+  replace types with the name length >= 5 letters only (ex. double, unsigned).
+
 - New module `abseil` for checks related to the `Abseil <https://abseil.io>`_
   library.
 

Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-use-auto.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-use-auto.rst?rev=329730&r1=329729&r2=329730&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-use-auto.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-use-auto.rst Tue Apr 10 11:05:24 2018
@@ -194,3 +194,23 @@ Options
   // RemoveStars = 1
 
   auto my_first_pointer = new TypeName, my_second_pointer = new TypeName;
+
+.. option:: MinTypeNameLength
+
+   If the option is set to non-zero (default '5'), the check will ignore
+   type names having a length less than the option value.
+   The option affects expressions only, not iterators.
+
+.. code-block:: c++
+
+  // MinTypeNameLength = 0
+
+  int a = static_cast<int>(foo());            // ---> auto a = ...
+  bool b = new bool;                          // ---> auto b = ...
+  unsigned c = static_cast<unsigned>(foo());  // ---> auto c = ...
+
+  // MinTypeNameLength = 8
+
+  int a = static_cast<int>(foo());            // ---> int  a = ...
+  bool b = new bool;                          // ---> bool b = ...
+  unsigned c = static_cast<unsigned>(foo());  // ---> auto c = ...

Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-cast-remove-stars.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-cast-remove-stars.cpp?rev=329730&r1=329729&r2=329730&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-cast-remove-stars.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-cast-remove-stars.cpp Tue Apr 10 11:05:24 2018
@@ -1,5 +1,5 @@
 // RUN: %check_clang_tidy %s modernize-use-auto %t -- \
-// RUN:   -config="{CheckOptions: [{key: modernize-use-auto.RemoveStars, value: '1'}]}" \
+// RUN:   -config="{CheckOptions: [{key: modernize-use-auto.RemoveStars, value: '1'} , {key: modernize-use-auto.MinTypeNameLength, value: '0'}]}" \
 // RUN:   -- -std=c++11 -frtti
 
 struct A {

Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-cast.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-cast.cpp?rev=329730&r1=329729&r2=329730&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-cast.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-cast.cpp Tue Apr 10 11:05:24 2018
@@ -1,5 +1,6 @@
-// RUN: %check_clang_tidy %s modernize-use-auto %t -- -- \
-// RUN:   -std=c++11 -I %S/Inputs/modernize-use-auto -frtti
+// RUN: %check_clang_tidy %s modernize-use-auto %t -- \
+// RUN:   -config="{CheckOptions: [{key: modernize-use-auto.MinTypeNameLength, value: '0'}]}" \
+// RUN:   -- -std=c++11 -I %S/Inputs/modernize-use-auto -frtti
 
 struct A {
   virtual ~A() {}

Added: clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-min-type-name-length.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-min-type-name-length.cpp?rev=329730&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-min-type-name-length.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-min-type-name-length.cpp Tue Apr 10 11:05:24 2018
@@ -0,0 +1,30 @@
+// RUN: %check_clang_tidy %s modernize-use-auto %t -- \
+// RUN:   -config="{CheckOptions: [{key: modernize-use-auto.MinTypeNameLength, value: '5'}]}" \
+// RUN:   -- -std=c++11 -frtti
+
+extern int foo();
+
+using VeryVeryVeryLongTypeName = int;
+
+int bar() {
+  int a = static_cast<VeryVeryVeryLongTypeName>(foo());
+  // strlen('int') = 4 <  5, so skip it,
+  // even strlen('VeryVeryVeryLongTypeName') > 5.
+
+  unsigned b = static_cast<unsigned>(foo());
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto]
+  // CHECK-FIXES: auto b = static_cast<unsigned>(foo());
+
+  bool c = static_cast<bool>(foo());
+  // strlen('bool') = 4 <  5, so skip it.
+
+  const bool c1 = static_cast<const bool>(foo());
+  // strlen('bool') = 4 <  5, so skip it, even there's a 'const'.
+
+  unsigned long long ull = static_cast<unsigned long long>(foo());
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto]
+  // CHECK-FIXES: auto ull = static_cast<unsigned long long>(foo());
+
+  return 1;
+}
+

Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-new.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-new.cpp?rev=329730&r1=329729&r2=329730&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-new.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-new.cpp Tue Apr 10 11:05:24 2018
@@ -1,4 +1,6 @@
-// RUN: %check_clang_tidy %s modernize-use-auto %t
+// RUN: %check_clang_tidy %s modernize-use-auto %t -- \
+// RUN:   -config="{CheckOptions: [{key: modernize-use-auto.MinTypeNameLength, value: '0'}]}" \
+// RUN:   -- -std=c++11 -frtti
 
 class MyType {};
 




More information about the cfe-commits mailing list