Skip to content

Commit 54fab18

Browse files
committed
[clang-format] Require space before noexcept qualifier
This brings the noexcept qualifier more visually in line with the other keyword qualifiers, such as "final" and "override". Originally reported as llvm#44542, it was closed as "working by design" and reinforcing tests were added as part of a218706. The exact spacing depended on the `PointerAlignment` option, where the default value of `Right` would leave no space. This patch seeks to change this behaviour, regardless of the configured `PointerAlignment` option (matching the previous behaviour of the `Left` option). Closes llvm#59729 Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay Differential Revision: https://reviews.llvm.org/D140767
1 parent 7f56488 commit 54fab18

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3530,7 +3530,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
35303530
if (Right.is(TT_BlockComment))
35313531
return true;
35323532
// foo() -> const Bar * override/final
3533-
if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final) &&
3533+
if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final,
3534+
tok::kw_noexcept) &&
35343535
!Right.is(TT_StartOfName)) {
35353536
return true;
35363537
}

clang/unittests/Format/FormatTest.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10566,10 +10566,10 @@ TEST_F(FormatTest, UnderstandsOverloadedOperators) {
1056610566

1056710567
TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
1056810568
verifyFormat("void A::b() && {}");
10569-
verifyFormat("void A::b() &&noexcept {}");
10569+
verifyFormat("void A::b() && noexcept {}");
1057010570
verifyFormat("Deleted &operator=(const Deleted &) & = default;");
1057110571
verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
10572-
verifyFormat("Deleted &operator=(const Deleted &) &noexcept = default;");
10572+
verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;");
1057310573
verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
1057410574
verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
1057510575
verifyFormat("Deleted &operator=(const Deleted &) &;");
@@ -10579,16 +10579,16 @@ TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
1057910579
verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
1058010580
verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
1058110581
verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
10582-
verifyFormat("SomeType MemberFunction(const Deleted &) &&noexcept {}");
10582+
verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}");
1058310583
verifyFormat("void Fn(T const &) const &;");
1058410584
verifyFormat("void Fn(T const volatile &&) const volatile &&;");
10585-
verifyFormat("void Fn(T const volatile &&) const volatile &&noexcept;");
10585+
verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;");
1058610586
verifyFormat("template <typename T>\n"
1058710587
"void F(T) && = delete;",
1058810588
getGoogleStyle());
1058910589
verifyFormat("template <typename T> void operator=(T) &;");
1059010590
verifyFormat("template <typename T> void operator=(T) const &;");
10591-
verifyFormat("template <typename T> void operator=(T) &noexcept;");
10591+
verifyFormat("template <typename T> void operator=(T) & noexcept;");
1059210592
verifyFormat("template <typename T> void operator=(T) & = default;");
1059310593
verifyFormat("template <typename T> void operator=(T) &&;");
1059410594
verifyFormat("template <typename T> void operator=(T) && = delete;");
@@ -10678,31 +10678,31 @@ TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
1067810678

1067910679
verifyFormat("struct f {\n"
1068010680
" template <class T>\n"
10681-
" int &foo(const std::string &str) &noexcept {}\n"
10681+
" int &foo(const std::string &str) & noexcept {}\n"
1068210682
"};",
1068310683
BreakTemplate);
1068410684

1068510685
verifyFormat("struct f {\n"
1068610686
" template <class T>\n"
10687-
" int &foo(const std::string &str) &&noexcept {}\n"
10687+
" int &foo(const std::string &str) && noexcept {}\n"
1068810688
"};",
1068910689
BreakTemplate);
1069010690

1069110691
verifyFormat("struct f {\n"
1069210692
" template <class T>\n"
10693-
" int &foo(const std::string &str) const &noexcept {}\n"
10693+
" int &foo(const std::string &str) const & noexcept {}\n"
1069410694
"};",
1069510695
BreakTemplate);
1069610696

1069710697
verifyFormat("struct f {\n"
1069810698
" template <class T>\n"
10699-
" int &foo(const std::string &str) const &noexcept {}\n"
10699+
" int &foo(const std::string &str) const & noexcept {}\n"
1070010700
"};",
1070110701
BreakTemplate);
1070210702

1070310703
verifyFormat("struct f {\n"
1070410704
" template <class T>\n"
10705-
" auto foo(const std::string &str) &&noexcept -> int & {}\n"
10705+
" auto foo(const std::string &str) && noexcept -> int & {}\n"
1070610706
"};",
1070710707
BreakTemplate);
1070810708

0 commit comments

Comments
 (0)