[PATCH] D67460: clang-tidy: modernize-use-using work with multi-argument templates

Conrad Poelman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 11 12:48:35 PDT 2019


poelmanc created this revision.
poelmanc added reviewers: alexfh, alexfh_.
poelmanc added a project: clang-tools-extra.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

clang-tidy's modernize-use-using feature is great! But if it finds any commas that are not within parentheses, it won't create a fix. That means it won't change lines like:
`  typedef std::pair<int, int> Point;`
to
`  using Point = std::pair<int, int>;`
or even:
`  typedef std::map<std::string, Foo> MyMap;`
`  typedef std::vector<int,MyCustomAllocator<int>> MyVector;`

This patch allows the fix to apply to lines with commas if they are within parentheses //or// angle brackets.

One test is include


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D67460

Files:
  clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
  clang-tools-extra/test/clang-tidy/modernize-use-using.cpp


Index: clang-tools-extra/test/clang-tidy/modernize-use-using.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/modernize-use-using.cpp
+++ clang-tools-extra/test/clang-tidy/modernize-use-using.cpp
@@ -182,4 +182,11 @@
 class E : public C<E> {
   void f() override { super::f(); }
 };
+
+template <typename T1, typename T2>
+class TwoArgTemplate {
+  typedef TwoArgTemplate<T1, T2> self;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef'
+  // CHECK-FIXES: using self = TwoArgTemplate<T1, T2>;
+};
 }
Index: clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -40,6 +40,7 @@
 
   Token Tok;
   int ParenLevel = 0;
+  int AngleBracketLevel = 0;
   bool FoundTypedef = false;
 
   while (!DeclLexer.LexFromRawLexer(Tok) && !Tok.is(tok::semi)) {
@@ -54,10 +55,16 @@
     case tok::r_paren:
       ParenLevel--;
       break;
+    case tok::less: // '<', start template
+      AngleBracketLevel++;
+      break;
+    case tok::greater: // '>', end template
+      AngleBracketLevel--;
+      break;
     case tok::comma:
-      if (ParenLevel == 0) {
-        // If there is comma and we are not between open parenthesis then it is
-        // two or more declarations in this chain.
+      if (ParenLevel == 0 && AngleBracketLevel == 0) {
+        // If there is comma and we are not between open parenthesis or between
+        // open angle brackets then it is two or more declarations in this chain.
         return false;
       }
       break;
@@ -88,8 +95,7 @@
   if (StartLoc.isMacroID() && IgnoreMacros)
     return;
 
-  auto Diag =
-      diag(StartLoc, "use 'using' instead of 'typedef'");
+  auto Diag = diag(StartLoc, "use 'using' instead of 'typedef'");
 
   // do not fix if there is macro or array
   if (MatchedDecl->getUnderlyingType()->isArrayType() || StartLoc.isMacroID())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67460.219765.patch
Type: text/x-patch
Size: 2078 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190911/541c5206/attachment.bin>


More information about the cfe-commits mailing list