[clang] 709fd98 - [clang-tidy] fix readability-redundant-member-init auto-fix of Function-try-block

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 19 15:04:12 PST 2020


Author: Alexander Lanin
Date: 2020-02-19T23:04:05Z
New Revision: 709fd989b644a80527e0f4a22503d54255bc095c

URL: https://github.com/llvm/llvm-project/commit/709fd989b644a80527e0f4a22503d54255bc095c
DIFF: https://github.com/llvm/llvm-project/commit/709fd989b644a80527e0f4a22503d54255bc095c.diff

LOG: [clang-tidy] fix readability-redundant-member-init auto-fix of Function-try-block

Summary: This fixes https://bugs.llvm.org/show_bug.cgi?id=39310

Reviewers: malcolm.parsons, ioeric

Reviewed By: malcolm.parsons

Subscribers: xazax.hun

Tags: #clang-format, #clang-tools-extra

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

Added: 
    

Modified: 
    clang-tools-extra/test/clang-tidy/checkers/modernize-use-default-member-init.cpp
    clang/lib/Format/TokenAnnotator.cpp
    clang/lib/Format/UnwrappedLineParser.cpp
    clang/unittests/Format/CleanupTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/test/clang-tidy/checkers/modernize-use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize-use-default-member-init.cpp
index 196eec91b8e8..84dfeb215f4f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize-use-default-member-init.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize-use-default-member-init.cpp
@@ -418,3 +418,17 @@ NegativeTemplateExisting<double> nted(0);
   };
 
 MACRO();
+
+
+class FunctionTryBlock {
+public:
+  FunctionTryBlock() try : i(5), k(8) {}
+  // CHECK-FIXES: FunctionTryBlock() try  {}
+  catch (...) {}
+
+private:
+  int i, k;
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'i' [modernize-use-default-member-init]
+  // CHECK-MESSAGES: :[[@LINE-2]]:10: warning: use default member initializer for 'k' [modernize-use-default-member-init]
+  // CHECK-FIXES: int i{5}, k{8};
+};

diff  --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 42963ca105a9..d71c4f470378 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -819,10 +819,15 @@ class AnnotatingParser {
         Tok->Type = TT_BitFieldColon;
       } else if (Contexts.size() == 1 &&
                  !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) {
-        if (Tok->getPreviousNonComment()->isOneOf(tok::r_paren,
-                                                  tok::kw_noexcept))
+        FormatToken *Prev = Tok->getPreviousNonComment();
+        if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept))
           Tok->Type = TT_CtorInitializerColon;
-        else
+        else if (Prev->is(tok::kw_try)) {
+          // Member initializer list within function try block.
+          FormatToken *PrevPrev = Prev->getPreviousNonComment();
+          if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
+            Tok->Type = TT_CtorInitializerColon;
+        } else
           Tok->Type = TT_InheritanceColon;
       } else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
                  (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||

diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index b455a7f2ebed..802bb514a38f 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1849,11 +1849,20 @@ void UnwrappedLineParser::parseTryCatch() {
   if (FormatTok->is(tok::colon)) {
     // We are in a function try block, what comes is an initializer list.
     nextToken();
+
+    // In case identifiers were removed by clang-tidy, what might follow is
+    // multiple commas in sequence - before the first identifier.
+    while (FormatTok->is(tok::comma))
+      nextToken();
+
     while (FormatTok->is(tok::identifier)) {
       nextToken();
       if (FormatTok->is(tok::l_paren))
         parseParens();
-      if (FormatTok->is(tok::comma))
+
+      // In case identifiers were removed by clang-tidy, what might follow is
+      // multiple commas in sequence - after the first identifier.
+      while (FormatTok->is(tok::comma))
         nextToken();
     }
   }

diff  --git a/clang/unittests/Format/CleanupTest.cpp b/clang/unittests/Format/CleanupTest.cpp
index b0c81b509d2a..70741d239c82 100644
--- a/clang/unittests/Format/CleanupTest.cpp
+++ b/clang/unittests/Format/CleanupTest.cpp
@@ -151,6 +151,31 @@ TEST_F(CleanupTest, CtorInitializationSimpleRedundantComma) {
   EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code));
 }
 
+// regression test for bug 39310
+TEST_F(CleanupTest, CtorInitializationSimpleRedundantCommaInFunctionTryBlock) {
+  std::string Code = "class A {\nA() try : , {} };";
+  std::string Expected = "class A {\nA() try  {} };";
+  EXPECT_EQ(Expected, cleanupAroundOffsets({21, 23}, Code));
+
+  Code = "class A {\nA() try : x(1), {} };";
+  Expected = "class A {\nA() try : x(1) {} };";
+  EXPECT_EQ(Expected, cleanupAroundOffsets({27}, Code));
+
+  Code = "class A {\nA() try :,,,,{} };";
+  Expected = "class A {\nA() try {} };";
+  EXPECT_EQ(Expected, cleanupAroundOffsets({19}, Code));
+
+  Code = "class A {\nA() try : x(1),,, {} };";
+  Expected = "class A {\nA() try : x(1) {} };";
+  EXPECT_EQ(Expected, cleanupAroundOffsets({27}, Code));
+
+  // Do not remove every comma following a colon as it simply doesn't make
+  // sense in some situations.
+  Code = "try : , {}";
+  Expected = "try : , {}";
+  EXPECT_EQ(Expected, cleanupAroundOffsets({8}, Code));
+}
+
 TEST_F(CleanupTest, CtorInitializationSimpleRedundantColon) {
   std::string Code = "class A {\nA() : =default; };";
   std::string Expected = "class A {\nA()  =default; };";


        


More information about the cfe-commits mailing list