[clang] f54d42a - [clang-format] Don't put `noexcept` on empty line following constructor
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 5 03:36:09 PDT 2022
Author: Emilia Dreamer
Date: 2022-09-05T12:35:39+02:00
New Revision: f54d42abcf82e122ed0ac4688f01c1f4da5599f2
URL: https://github.com/llvm/llvm-project/commit/f54d42abcf82e122ed0ac4688f01c1f4da5599f2
DIFF: https://github.com/llvm/llvm-project/commit/f54d42abcf82e122ed0ac4688f01c1f4da5599f2.diff
LOG: [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
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 2a64a3bb9c413..663e8ae6a8986 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1924,7 +1924,7 @@ class AnnotatingParser {
!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)) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index d330c65470f0d..ce56570697cd1 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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);
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 6a10d679fbc72..21ef5c382a900 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -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 ¶meter) {}");
+ 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));
More information about the cfe-commits
mailing list