Skip to content

Commit eefc3d2

Browse files
el-evcor3ntin
andauthored
[clang][diagnostics] Fix fix-it hint parenthesis placement for fold expressions (#151790)
- Closes #151787 Insert the right parenthesis one token later to correctly enclose the expression. --------- Co-authored-by: Corentin Jabot <[email protected]>
1 parent 69aa6a0 commit eefc3d2

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ Improvements to Clang's diagnostics
123123
Moved the warning for a missing (though implied) attribute on a redeclaration into this group.
124124
Added a new warning in this group for the case where the attribute is missing/implicit on
125125
an override of a virtual method.
126+
- Fixed fix-it hint for fold expressions. Clang now correctly places the suggested right
127+
parenthesis when diagnosing malformed fold expressions. (#GH151787)
126128

127129
Improvements to Clang's time-trace
128130
----------------------------------

clang/lib/Sema/SemaTemplateVariadic.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,8 @@ static void CheckFoldOperand(Sema &S, Expr *E) {
13871387
S.Diag(E->getExprLoc(), diag::err_fold_expression_bad_operand)
13881388
<< E->getSourceRange()
13891389
<< FixItHint::CreateInsertion(E->getBeginLoc(), "(")
1390-
<< FixItHint::CreateInsertion(E->getEndLoc(), ")");
1390+
<< FixItHint::CreateInsertion(S.getLocForEndOfToken(E->getEndLoc()),
1391+
")");
13911392
}
13921393
}
13931394

clang/test/FixIt/fixit-c++17.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %clang_cc1 -verify -std=c++17 -pedantic-errors %s
2+
// RUN: cp %s %t
3+
// RUN: not %clang_cc1 -x c++ -std=c++17 -fixit %t
4+
// RUN: %clang_cc1 -Wall -pedantic-errors -x c++ -std=c++17 %t
5+
6+
/* This is a test of the various code modification hints that only
7+
apply in C++17. */
8+
template<int... args>
9+
int foo() {
10+
int a = (args + 1 + ...); // expected-error {{expression not permitted as operand of fold expression}}
11+
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:14-[[@LINE-1]]:14}:"("
12+
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:22-[[@LINE-2]]:22}:")"
13+
int b = (args + 123 + ...); // expected-error {{expression not permitted as operand of fold expression}}
14+
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:14-[[@LINE-1]]:14}:"("
15+
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:24-[[@LINE-2]]:24}:")"
16+
int c = (args + 1 + 2 + ...); // expected-error {{expression not permitted as operand of fold expression}}
17+
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:14-[[@LINE-1]]:14}:"("
18+
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:26-[[@LINE-2]]:26}:")"
19+
int e = (... + 1 + args); // expected-error {{expression not permitted as operand of fold expression}}
20+
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:20-[[@LINE-1]]:20}:"("
21+
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:28-[[@LINE-2]]:28}:")"
22+
int f = (1 + ... + args + 1); // expected-error {{expression not permitted as operand of fold expression}}
23+
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:24-[[@LINE-1]]:24}:"("
24+
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:32-[[@LINE-2]]:32}:")"
25+
int g = (args + 1 + ... + 1); // expected-error {{expression not permitted as operand of fold expression}}
26+
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:14-[[@LINE-1]]:14}:"("
27+
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:22-[[@LINE-2]]:22}:")"
28+
return a + b + c + e + f + g;
29+
}

0 commit comments

Comments
 (0)