[clang] [clang-format] Fix a bug in SpaceBeforeParensOptions (PR #98849)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Sun Jul 14 22:52:22 PDT 2024
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/98849
>From 24f3b4f03f217e578aa2a2585d7d1742b7d7c09b Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sun, 14 Jul 2024 17:52:23 -0700
Subject: [PATCH 1/2] [clang-format] Fix a bug in SpaceBeforeParensOptions
Handle constructors/destructors for AfterFunctionDeclarationName
and AfterFunctionDefinitionName.
Fixes #98812.
---
clang/lib/Format/TokenAnnotator.cpp | 15 +++++++--------
clang/unittests/Format/FormatTest.cpp | 4 ++--
2 files changed, 9 insertions(+), 10 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 1fd309afd697e..b6d6e52ccb8f8 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4713,14 +4713,13 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
if (Right.is(TT_OverloadedOperatorLParen))
return spaceRequiredBeforeParens(Right);
// Function declaration or definition
- if (Line.MightBeFunctionDecl && (Left.is(TT_FunctionDeclarationName))) {
- if (Line.mightBeFunctionDefinition()) {
- return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
- spaceRequiredBeforeParens(Right);
- } else {
- return Style.SpaceBeforeParensOptions.AfterFunctionDeclarationName ||
- spaceRequiredBeforeParens(Right);
- }
+ if (Line.MightBeFunctionDecl && Right.is(TT_FunctionDeclarationLParen)) {
+ if (spaceRequiredBeforeParens(Right))
+ return true;
+ const auto &Options = Style.SpaceBeforeParensOptions;
+ return Line.mightBeFunctionDefinition()
+ ? Options.AfterFunctionDefinitionName
+ : Options.AfterFunctionDeclarationName;
}
// Lambda
if (Line.Type != LT_PreprocessorDirective && Left.is(tok::r_square) &&
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 283843ad7ab47..911f0ea8e57e8 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -16875,7 +16875,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
verifyFormat("int f();", SpaceFuncDef);
verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef);
- verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
+ verifyFormat("A::A () : a(1) {}", SpaceFuncDef);
verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef);
verifyFormat("#define A(x) x", SpaceFuncDef);
@@ -16901,7 +16901,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
verifyFormat("T A::operator()() {}", SpaceFuncDef);
verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
verifyFormat("int x = int(y);", SpaceFuncDef);
- verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
+ verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}",
SpaceFuncDef);
FormatStyle SpaceIfMacros = getLLVMStyle();
>From 59d30d669d15770cea770a4b546de8601ad84105 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sun, 14 Jul 2024 22:51:17 -0700
Subject: [PATCH 2/2] Add a test case for #98820.
---
clang/unittests/Format/FormatTest.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 911f0ea8e57e8..d01ce137b8fcb 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -16901,6 +16901,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
verifyFormat("T A::operator()() {}", SpaceFuncDef);
verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
verifyFormat("int x = int(y);", SpaceFuncDef);
+ verifyFormat("void foo::bar () {}", SpaceFuncDef);
verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}",
SpaceFuncDef);
More information about the cfe-commits
mailing list