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 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 @@ private: 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; };";