[clang] Diagnose the code with trailing comma in the function call. (PR #125232)
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 31 07:59:04 PST 2025
https://github.com/hokein updated https://github.com/llvm/llvm-project/pull/125232
>From 5ecb2de4ba92619dc0bee89e06db5203e256ea42 Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Fri, 31 Jan 2025 14:59:39 +0100
Subject: [PATCH 1/2] Diagnose the code with trailing comma in the function
call.
---
clang/include/clang/Basic/DiagnosticParseKinds.td | 2 ++
clang/lib/Parse/ParseExpr.cpp | 3 +++
clang/test/Parser/recovery.cpp | 7 +++++++
3 files changed, 12 insertions(+)
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index c513dab810d1f5..f116ef114bb361 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -703,6 +703,8 @@ def ext_consteval_if : ExtWarn<
def warn_cxx20_compat_consteval_if : Warning<
"consteval if is incompatible with C++ standards before C++23">,
InGroup<CXXPre23Compat>, DefaultIgnore;
+def err_extraneous_trailing_comma : Error<
+ "extraneous trailing comma">;
def ext_init_statement : ExtWarn<
"'%select{if|switch}0' initialization statements are a C++17 extension">,
diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp
index aa8b3870a188c6..e31ef7d404a222 100644
--- a/clang/lib/Parse/ParseExpr.cpp
+++ b/clang/lib/Parse/ParseExpr.cpp
@@ -2237,6 +2237,9 @@ Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
RunSignatureHelp();
LHS = ExprError();
+ } else if (!HasError && HasTrailingComma) {
+ // FIXME: add a FIXIT to remove the trailing comma.
+ Diag(Tok, diag::err_extraneous_trailing_comma);
} else if (LHS.isInvalid()) {
for (auto &E : ArgExprs)
Actions.CorrectDelayedTyposInExpr(E);
diff --git a/clang/test/Parser/recovery.cpp b/clang/test/Parser/recovery.cpp
index 4e2811c4cac926..e318461e1961da 100644
--- a/clang/test/Parser/recovery.cpp
+++ b/clang/test/Parser/recovery.cpp
@@ -215,3 +215,10 @@ struct ::template foo, struct ::template bar; // expected-error 2 {{expected ide
struct ::foo struct::; // expected-error {{no struct named 'foo' in the global namespace}} expected-error {{expected identifier}}
class :: : {} a; // expected-error {{expected identifier}} expected-error {{expected class name}}
}
+
+namespace GH125225 {
+void func(int);
+void k() {
+ func(1, ); // expected-error {{extraneous trailing comma}}
+}
+}
>From 8f8693aab7b567ff923d6b5b65ac3a35b9132627 Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Fri, 31 Jan 2025 16:53:58 +0100
Subject: [PATCH 2/2] Address review comment
---
clang/include/clang/Basic/DiagnosticParseKinds.td | 2 --
clang/lib/Parse/ParseExpr.cpp | 3 +--
clang/test/Parser/recovery.cpp | 2 +-
3 files changed, 2 insertions(+), 5 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index f116ef114bb361..c513dab810d1f5 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -703,8 +703,6 @@ def ext_consteval_if : ExtWarn<
def warn_cxx20_compat_consteval_if : Warning<
"consteval if is incompatible with C++ standards before C++23">,
InGroup<CXXPre23Compat>, DefaultIgnore;
-def err_extraneous_trailing_comma : Error<
- "extraneous trailing comma">;
def ext_init_statement : ExtWarn<
"'%select{if|switch}0' initialization statements are a C++17 extension">,
diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp
index e31ef7d404a222..0cadede51a9b3d 100644
--- a/clang/lib/Parse/ParseExpr.cpp
+++ b/clang/lib/Parse/ParseExpr.cpp
@@ -2238,8 +2238,7 @@ Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
RunSignatureHelp();
LHS = ExprError();
} else if (!HasError && HasTrailingComma) {
- // FIXME: add a FIXIT to remove the trailing comma.
- Diag(Tok, diag::err_extraneous_trailing_comma);
+ Diag(Tok, diag::err_expected_expression);
} else if (LHS.isInvalid()) {
for (auto &E : ArgExprs)
Actions.CorrectDelayedTyposInExpr(E);
diff --git a/clang/test/Parser/recovery.cpp b/clang/test/Parser/recovery.cpp
index e318461e1961da..2fce67a52c6b61 100644
--- a/clang/test/Parser/recovery.cpp
+++ b/clang/test/Parser/recovery.cpp
@@ -219,6 +219,6 @@ class :: : {} a; // expected-error {{expected identifier}} expected-error {{exp
namespace GH125225 {
void func(int);
void k() {
- func(1, ); // expected-error {{extraneous trailing comma}}
+ func(1, ); // expected-error {{expected expression}}
}
}
More information about the cfe-commits
mailing list