[clang-tools-extra] r299340 - Fixes for modernize-use-using check:

Krystyna Gajczyk via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 2 12:12:21 PDT 2017


Author: krystynka
Date: Sun Apr  2 14:12:20 2017
New Revision: 299340

URL: http://llvm.org/viewvc/llvm-project?rev=299340&view=rev
Log:
Fixes for modernize-use-using check:
- removed unnessacary namespaces
- added option to print warning in macros
- no fix for typedef with array
- removed "void" word from functions with 0 parameters

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

Added:
    clang-tools-extra/trunk/test/clang-tidy/modernize-use-using-macros.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/modernize/UseUsingCheck.cpp
    clang-tools-extra/trunk/clang-tidy/modernize/UseUsingCheck.h
    clang-tools-extra/trunk/test/clang-tidy/modernize-use-using.cpp

Modified: clang-tools-extra/trunk/clang-tidy/modernize/UseUsingCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseUsingCheck.cpp?rev=299340&r1=299339&r2=299340&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/UseUsingCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/UseUsingCheck.cpp Sun Apr  2 14:12:20 2017
@@ -17,6 +17,10 @@ namespace clang {
 namespace tidy {
 namespace modernize {
 
+UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context)
+    : ClangTidyCheck(Name, Context),
+      IgnoreMacros(Options.get("IgnoreMacros", true)) {}
+
 void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus11)
     return;
@@ -79,18 +83,28 @@ void UseUsingCheck::check(const MatchFin
   auto &Context = *Result.Context;
   auto &SM = *Result.SourceManager;
 
+  SourceLocation StartLoc = MatchedDecl->getLocStart();
+
+  if (StartLoc.isMacroID() && IgnoreMacros)
+    return;
+
   auto Diag =
-      diag(MatchedDecl->getLocStart(), "use 'using' instead of 'typedef'");
+      diag(StartLoc, "use 'using' instead of 'typedef'");
 
-  SourceLocation StartLoc = MatchedDecl->getLocStart();
-  if (StartLoc.isMacroID())
+  // do not fix if there is macro or array
+  if (MatchedDecl->getUnderlyingType()->isArrayType() || StartLoc.isMacroID())
     return;
 
   if (CheckRemoval(SM, StartLoc, Context)) {
+    auto printPolicy = PrintingPolicy(getLangOpts());
+    printPolicy.SuppressScope = true;
+    printPolicy.ConstantArraySizeAsWritten = true;
+    printPolicy.UseVoidForZeroParams = false;
+
     Diag << FixItHint::CreateReplacement(
         MatchedDecl->getSourceRange(),
         "using " + MatchedDecl->getNameAsString() + " = " +
-            MatchedDecl->getUnderlyingType().getAsString(getLangOpts()));
+            MatchedDecl->getUnderlyingType().getAsString(printPolicy));
   }
 }
 

Modified: clang-tools-extra/trunk/clang-tidy/modernize/UseUsingCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseUsingCheck.h?rev=299340&r1=299339&r2=299340&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/UseUsingCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/UseUsingCheck.h Sun Apr  2 14:12:20 2017
@@ -21,9 +21,14 @@ namespace modernize {
 /// For the user-facing documentation see:
 /// http://clang.llvm.org/extra/clang-tidy/checks/modernize-use-using.html
 class UseUsingCheck : public ClangTidyCheck {
+
+  const bool IgnoreMacros;
+
 public:
-  UseUsingCheck(StringRef Name, ClangTidyContext *Context)
-      : ClangTidyCheck(Name, Context) {}
+  UseUsingCheck(StringRef Name, ClangTidyContext *Context);
+  void storeOptions(ClangTidyOptions::OptionMap &Opts) override {
+    Options.store(Opts, "IgnoreMacros", IgnoreMacros);
+  }
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
 };

Added: clang-tools-extra/trunk/test/clang-tidy/modernize-use-using-macros.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-using-macros.cpp?rev=299340&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-use-using-macros.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-using-macros.cpp Sun Apr  2 14:12:20 2017
@@ -0,0 +1,23 @@
+// RUN: %check_clang_tidy %s modernize-use-using %t -- \
+// RUN:   -config="{CheckOptions: [{key: modernize-use-using.IgnoreMacros, value: 0}]}" \
+// RUN:   -- -std=c++11 -I %S/Inputs/modernize-use-using
+
+#define CODE typedef int INT
+
+CODE;
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
+// CHECK-FIXES: #define CODE typedef int INT
+// CHECK-FIXES: CODE;
+
+struct Foo;
+#define Bar Baz
+typedef Foo Bar;
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
+// CHECK-FIXES: #define Bar Baz
+// CHECK-FIXES: using Baz = Foo;
+
+#define TYPEDEF typedef
+TYPEDEF Foo Bak;
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
+// CHECK-FIXES: #define TYPEDEF typedef
+// CHECK-FIXES: TYPEDEF Foo Bak;

Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-use-using.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-using.cpp?rev=299340&r1=299339&r2=299340&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-use-using.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-using.cpp Sun Apr  2 14:12:20 2017
@@ -89,7 +89,6 @@ typedef int bla1, bla2, bla3;
 #define CODE typedef int INT
 
 CODE;
-// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
 // CHECK-FIXES: #define CODE typedef int INT
 // CHECK-FIXES: CODE;
 
@@ -102,7 +101,6 @@ typedef Foo Bar;
 
 #define TYPEDEF typedef
 TYPEDEF Foo Bak;
-// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
 // CHECK-FIXES: #define TYPEDEF typedef
 // CHECK-FIXES: TYPEDEF Foo Bak;
 
@@ -148,3 +146,18 @@ struct Q2 { int c; } typedef S3;
 struct { int d; } typedef S4;
 // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
 // CHECK-FIXES: struct { int d; } typedef S4;
+
+namespace my_space {
+  class my_cclass {};
+  typedef my_cclass FuncType;
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef'
+// CHECK-FIXES: using FuncType = my_cclass;
+}
+
+#define lol 4
+typedef unsigned Map[lol]; 
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
+
+typedef void (*fun_type)();
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
+// CHECK-FIXES: using fun_type = void (*)();




More information about the cfe-commits mailing list