[clang] 86b5eab - Allow parameter names to be elided in a function definition in C.
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 7 11:44:54 PDT 2020
Author: Aaron Ballman
Date: 2020-04-07T14:43:38-04:00
New Revision: 86b5eabfeab164dcb680f6690e7718e3d21ceeb5
URL: https://github.com/llvm/llvm-project/commit/86b5eabfeab164dcb680f6690e7718e3d21ceeb5
DIFF: https://github.com/llvm/llvm-project/commit/86b5eabfeab164dcb680f6690e7718e3d21ceeb5.diff
LOG: Allow parameter names to be elided in a function definition in C.
WG14 has adopted N2480 (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2480.pdf)
into C2x at the meetings last week, allowing parameter names of a function
definition to be elided. This patch relaxes the error so that C++ and C2x do not
diagnose this situation, and modes before C2x will allow it as an extension.
This also adds the same feature to ObjC blocks under the assumption that ObjC
wishes to follow the C standard in this regard.
Added:
Modified:
clang/include/clang/Basic/DiagnosticGroups.td
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaChecking.cpp
clang/lib/Sema/SemaExpr.cpp
clang/test/Sema/block-args.c
clang/test/Sema/c89.c
clang/test/Sema/function.c
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 1175476d609b..87444e5b6d98 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -985,6 +985,9 @@ def C11 : DiagGroup<"c11-extensions">;
// A warning group for warnings about using C99 features as extensions.
def C99 : DiagGroup<"c99-extensions", [C99Designator]>;
+// A warning group for warnings about using C2x features as extensions.
+def C2x : DiagGroup<"c2x-extensions">;
+
// A warning group for warnings about GCC extensions.
def GNU : DiagGroup<"gnu", [GNUAlignofExpression, GNUAnonymousStruct,
GNUAutoType,
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 148f23f362dc..35a7a05667fc 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -275,7 +275,9 @@ def err_bad_parameter_name : Error<
"%0 cannot be the name of a parameter">;
def err_bad_parameter_name_template_id : Error<
"parameter name cannot have template arguments">;
-def err_parameter_name_omitted : Error<"parameter name omitted">;
+def ext_parameter_name_omitted_c2x : ExtWarn<
+ "omitting the parameter name in a function definition is a C2x extension">,
+ InGroup<C2x>;
def err_anyx86_interrupt_attribute : Error<
"%select{x86|x86-64}0 'interrupt' attribute only applies to functions that "
"have %select{a 'void' return type|"
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 736a88782e1d..dc5147687376 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -12883,11 +12883,12 @@ bool Sema::CheckParmsForFunctionDef(ArrayRef<ParmVarDecl *> Parameters,
// C99 6.9.1p5: If the declarator includes a parameter type list, the
// declaration of each parameter shall include an identifier.
- if (CheckParameterNames &&
- Param->getIdentifier() == nullptr &&
- !Param->isImplicit() &&
- !getLangOpts().CPlusPlus)
- Diag(Param->getLocation(), diag::err_parameter_name_omitted);
+ if (CheckParameterNames && Param->getIdentifier() == nullptr &&
+ !Param->isImplicit() && !getLangOpts().CPlusPlus) {
+ // Diagnose this as an extension in C17 and earlier.
+ if (!getLangOpts().C2x)
+ Diag(Param->getLocation(), diag::ext_parameter_name_omitted_c2x);
+ }
// C99 6.7.5.3p12:
// If the function declarator is not part of a definition of that
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index a4f9c22138ee..c52c6fb67bf6 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -14646,11 +14646,12 @@ void Sema::ActOnBlockArguments(SourceLocation CaretLoc, Declarator &ParamInfo,
if (ExplicitSignature) {
for (unsigned I = 0, E = ExplicitSignature.getNumParams(); I != E; ++I) {
ParmVarDecl *Param = ExplicitSignature.getParam(I);
- if (Param->getIdentifier() == nullptr &&
- !Param->isImplicit() &&
- !Param->isInvalidDecl() &&
- !getLangOpts().CPlusPlus)
- Diag(Param->getLocation(), diag::err_parameter_name_omitted);
+ if (Param->getIdentifier() == nullptr && !Param->isImplicit() &&
+ !Param->isInvalidDecl() && !getLangOpts().CPlusPlus) {
+ // Diagnose this as an extension in C17 and earlier.
+ if (!getLangOpts().C2x)
+ Diag(Param->getLocation(), diag::ext_parameter_name_omitted_c2x);
+ }
Params.push_back(Param);
}
diff --git a/clang/test/Sema/block-args.c b/clang/test/Sema/block-args.c
index 69cf047a9ece..99e3220d5ac3 100644
--- a/clang/test/Sema/block-args.c
+++ b/clang/test/Sema/block-args.c
@@ -31,8 +31,8 @@ int main(int argc, char** argv) {
// radar 7528255
void f0() {
- ^(int, double d, char) {}(1, 1.34, 'a'); // expected-error {{parameter name omitted}} \
- // expected-error {{parameter name omitted}}
+ ^(int, double d, char) {}(1, 1.34, 'a'); // expected-warning {{omitting the parameter name in a function definition is a C2x extension}} \
+ // expected-warning {{omitting the parameter name in a function definition is a C2x extension}}
}
// rdar://problem/8962770
diff --git a/clang/test/Sema/c89.c b/clang/test/Sema/c89.c
index c9e81f1c41ca..89eda9362608 100644
--- a/clang/test/Sema/c89.c
+++ b/clang/test/Sema/c89.c
@@ -45,7 +45,7 @@ void test8(int, x); /* expected-warning {{declaration specifier missing, defaul
typedef int sometype;
int a(sometype, y) {return 0;} /* expected-warning {{declaration specifier missing, defaulting to 'int'}} \
- expected-error {{parameter name omitted}}*/
+ expected-warning {{omitting the parameter name in a function definition is a C2x extension}}*/
diff --git a/clang/test/Sema/function.c b/clang/test/Sema/function.c
index 26be61416b3c..69262bb6f2cb 100644
--- a/clang/test/Sema/function.c
+++ b/clang/test/Sema/function.c
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic
+// RUN: %clang_cc1 %s -fsyntax-only -verify -verify=c2x -pedantic
+// RUN: %clang_cc1 %s -fsyntax-only -std=c2x -verify -pedantic
// PR1892, PR11354
void f(double a[restrict][5]) { __typeof(a) x = 10; } // expected-warning {{(aka 'double (*restrict)[5]')}}
@@ -30,7 +31,7 @@ void t10(){}
void t11(){t10(1);} // expected-warning{{too many arguments}}
// PR3208
-void t12(int) {} // expected-error{{parameter name omitted}}
+void t12(int) {} // c2x-warning{{omitting the parameter name in a function definition is a C2x extension}}
// PR2790
void t13() {
@@ -80,7 +81,8 @@ typedef void fn_t(void);
fn_t t17;
// PR4049
-unknown_type t18(void*) { // expected-error {{unknown type name 'unknown_type'}} expected-error{{parameter name omitted}}
+unknown_type t18(void*) { // expected-error {{unknown type name 'unknown_type'}} \
+ // c2x-warning {{omitting the parameter name in a function definition is a C2x extension}}
}
unknown_type t19(int* P) { // expected-error {{unknown type name 'unknown_type'}}
More information about the cfe-commits
mailing list