[clang-format] Don't put `noexcept` on empty line following constructor

With the AlwaysBreakTemplateDeclarations option, having a constructor
template for a type consisting of all-uppercase letters with a
noexcept specifier would put said noexcept specifier on its own blank
line.

This is because the all-uppercase type is understood as a macro-like
attribute (such as DEPRECATED()), and noexcept is seen as the
declaration. However, noexcept is a keyword and cannot be an
identifier on its own.

Fixes https://github.com/llvm/llvm-project/issues/56216

Differential Revision: https://reviews.llvm.org/D132189
This commit is contained in:
Emilia Dreamer 2022-09-05 12:19:31 +02:00 committed by Björn Schäpers
parent 84c4efbc6d
commit f54d42abcf
3 changed files with 26 additions and 1 deletions

View File

@ -1924,7 +1924,7 @@ private:
!Current.Next->isBinaryOperator() &&
!Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
tok::comma, tok::period, tok::arrow,
tok::coloncolon)) {
tok::coloncolon, tok::kw_noexcept)) {
if (FormatToken *AfterParen = Current.MatchingParen->Next) {
// Make sure this isn't the return type of an Obj-C block declaration
if (AfterParen->isNot(tok::caret)) {

View File

@ -9607,6 +9607,15 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) {
verifyFormat("template <typename T> // T can be A, B or C.\n"
"struct C {};",
AlwaysBreak);
verifyFormat("template <typename T>\n"
"C(T) noexcept;",
AlwaysBreak);
verifyFormat("template <typename T>\n"
"ClassName(T) noexcept;",
AlwaysBreak);
verifyFormat("template <typename T>\n"
"POOR_NAME(T) noexcept;",
AlwaysBreak);
verifyFormat("template <enum E> class A {\n"
"public:\n"
" E *f();\n"
@ -9617,6 +9626,9 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) {
verifyFormat("template <typename T> class C {};", NeverBreak);
verifyFormat("template <typename T> void f();", NeverBreak);
verifyFormat("template <typename T> void f() {}", NeverBreak);
verifyFormat("template <typename T> C(T) noexcept;", NeverBreak);
verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak);
verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak);
verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
"bbbbbbbbbbbbbbbbbbbb) {}",
NeverBreak);

View File

@ -788,6 +788,19 @@ TEST_F(TokenAnnotatorTest, UnderstandsLambdas) {
EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_LambdaLBrace);
}
TEST_F(TokenAnnotatorTest, UnderstandsFunctionAnnotations) {
auto Tokens = annotate("template <typename T>\n"
"DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
"string OldFunction(const string &parameter) {}");
ASSERT_EQ(Tokens.size(), 20u) << Tokens;
EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_FunctionAnnotationRParen);
Tokens = annotate("template <typename T>\n"
"A(T) noexcept;");
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_Unknown);
}
TEST_F(TokenAnnotatorTest, UnderstandsVerilogOperators) {
auto Annotate = [this](llvm::StringRef Code) {
return annotate(Code, getLLVMStyle(FormatStyle::LK_Verilog));