[clang] [clang][diagnostics] Fix fix-it hint parenthesis placement for fold expressions (PR #151790)
Iris Shi via cfe-commits
cfe-commits at lists.llvm.org
Sat Aug 2 00:16:55 PDT 2025
https://github.com/el-ev updated https://github.com/llvm/llvm-project/pull/151790
>From 12e745a621a7fbd30c0991bf52598455b886f37e Mon Sep 17 00:00:00 2001
From: Iris Shi <0.0 at owo.li>
Date: Sat, 2 Aug 2025 12:02:49 +0800
Subject: [PATCH 1/2] [clang][diagnostics] Fix fix-it hint parenthesis
placement for fold expressions
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/Sema/SemaTemplateVariadic.cpp | 2 +-
clang/test/FixIt/fixit-c++17.cpp | 13 +++++++++++++
3 files changed, 16 insertions(+), 1 deletion(-)
create mode 100644 clang/test/FixIt/fixit-c++17.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 247d784739f4d..d049211298347 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -123,6 +123,8 @@ Improvements to Clang's diagnostics
Moved the warning for a missing (though implied) attribute on a redeclaration into this group.
Added a new warning in this group for the case where the attribute is missing/implicit on
an override of a virtual method.
+- Fixed fix-it hint for fold expressions. Clang now correctly places the suggested right
+ parenthesis when diagnosing malformed fold expressions.
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/lib/Sema/SemaTemplateVariadic.cpp b/clang/lib/Sema/SemaTemplateVariadic.cpp
index 572dbf2e7393f..83f989222cb20 100644
--- a/clang/lib/Sema/SemaTemplateVariadic.cpp
+++ b/clang/lib/Sema/SemaTemplateVariadic.cpp
@@ -1387,7 +1387,7 @@ static void CheckFoldOperand(Sema &S, Expr *E) {
S.Diag(E->getExprLoc(), diag::err_fold_expression_bad_operand)
<< E->getSourceRange()
<< FixItHint::CreateInsertion(E->getBeginLoc(), "(")
- << FixItHint::CreateInsertion(E->getEndLoc(), ")");
+ << FixItHint::CreateInsertion(E->getEndLoc().getLocWithOffset(1), ")");
}
}
diff --git a/clang/test/FixIt/fixit-c++17.cpp b/clang/test/FixIt/fixit-c++17.cpp
new file mode 100644
index 0000000000000..76d4268a2981f
--- /dev/null
+++ b/clang/test/FixIt/fixit-c++17.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -verify -std=c++17 -pedantic-errors %s
+// RUN: cp %s %t
+// RUN: not %clang_cc1 -x c++ -std=c++17 -fixit %t
+// RUN: %clang_cc1 -Wall -pedantic-errors -x c++ -std=c++17 %t
+
+/* This is a test of the various code modification hints that only
+ apply in C++17. */
+template<int... args>
+int foo() {
+ return (args + 1 + ...); // expected-error {{expression not permitted as operand of fold expression}}
+}
+// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"("
+// CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:21-[[@LINE-3]]:21}:")"
>From 5a7b87324546fbaefd9c196417694614258f0d0d Mon Sep 17 00:00:00 2001
From: Iris Shi <0.0 at owo.li>
Date: Sat, 2 Aug 2025 15:14:28 +0800
Subject: [PATCH 2/2] address review comments
---
clang/lib/Sema/SemaTemplateVariadic.cpp | 2 +-
clang/test/FixIt/fixit-c++17.cpp | 22 +++++++++++++++++++---
2 files changed, 20 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Sema/SemaTemplateVariadic.cpp b/clang/lib/Sema/SemaTemplateVariadic.cpp
index 83f989222cb20..0a251a0650eb3 100644
--- a/clang/lib/Sema/SemaTemplateVariadic.cpp
+++ b/clang/lib/Sema/SemaTemplateVariadic.cpp
@@ -1387,7 +1387,7 @@ static void CheckFoldOperand(Sema &S, Expr *E) {
S.Diag(E->getExprLoc(), diag::err_fold_expression_bad_operand)
<< E->getSourceRange()
<< FixItHint::CreateInsertion(E->getBeginLoc(), "(")
- << FixItHint::CreateInsertion(E->getEndLoc().getLocWithOffset(1), ")");
+ << FixItHint::CreateInsertion(S.getLocForEndOfToken(E->getEndLoc()), ")");
}
}
diff --git a/clang/test/FixIt/fixit-c++17.cpp b/clang/test/FixIt/fixit-c++17.cpp
index 76d4268a2981f..26c3bb94d8f7d 100644
--- a/clang/test/FixIt/fixit-c++17.cpp
+++ b/clang/test/FixIt/fixit-c++17.cpp
@@ -7,7 +7,23 @@
apply in C++17. */
template<int... args>
int foo() {
- return (args + 1 + ...); // expected-error {{expression not permitted as operand of fold expression}}
+ int a = (args + 1 + ...); // expected-error {{expression not permitted as operand of fold expression}}
+ // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:14-[[@LINE-1]]:14}:"("
+ // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:22-[[@LINE-2]]:22}:")"
+ int b = (args + 123 + ...); // expected-error {{expression not permitted as operand of fold expression}}
+ // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:14-[[@LINE-1]]:14}:"("
+ // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:24-[[@LINE-2]]:24}:")"
+ int c = (args + 1 + 2 + ...); // expected-error {{expression not permitted as operand of fold expression}}
+ // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:14-[[@LINE-1]]:14}:"("
+ // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:26-[[@LINE-2]]:26}:")"
+ int e = (... + 1 + args); // expected-error {{expression not permitted as operand of fold expression}}
+ // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:20-[[@LINE-1]]:20}:"("
+ // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:28-[[@LINE-2]]:28}:")"
+ int f = (1 + ... + args + 1); // expected-error {{expression not permitted as operand of fold expression}}
+ // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:24-[[@LINE-1]]:24}:"("
+ // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:32-[[@LINE-2]]:32}:")"
+ int g = (args + 1 + ... + 1); // expected-error {{expression not permitted as operand of fold expression}}
+ // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:14-[[@LINE-1]]:14}:"("
+ // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:22-[[@LINE-2]]:22}:")"
+ return a + b + c + e + f + g;
}
-// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"("
-// CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:21-[[@LINE-3]]:21}:")"
More information about the cfe-commits
mailing list