From 76032b0e3f58d4abe8d00ac61ff1b2044e076ba7 Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Mon, 14 Feb 2022 08:53:36 -0500 Subject: [PATCH] Check for the overloadable attribute in all the appropriate syntactic locations When forming the function type from a declarator, we look for an overloadable attribute before issuing a diagnostic in C about a function signature containing only .... When the attribute is present, we allow such a declaration for compatibility with the overloading rules in C++. However, we were not looking for the attribute in all of the places it is legal to write it on a declarator and so we only accepted the signature in some forms and incorrectly rejected the signature in others. We now check for the attribute preceding the declarator instead of only being applied to the declarator directly. --- clang/docs/ReleaseNotes.rst | 3 +++ clang/lib/Sema/SemaType.cpp | 4 +++- clang/test/Sema/overloadable.c | 11 +++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 4a6395941267..26860d311802 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -77,6 +77,9 @@ Attribute Changes in Clang - Added support for parameter pack expansion in `clang::annotate`. +- The ``overloadable`` attribute can now be written in all of the syntactic + locations a declaration attribute may appear. Fixes PR53805. + Windows Support --------------- diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index ab47e9f03eaf..74969749e54a 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -5237,7 +5237,9 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state, // function is marked with the "overloadable" attribute. Scan // for this attribute now. if (!FTI.NumParams && FTI.isVariadic && !LangOpts.CPlusPlus) - if (!D.getAttributes().hasAttribute(ParsedAttr::AT_Overloadable)) + if (!D.getAttributes().hasAttribute(ParsedAttr::AT_Overloadable) && + !D.getDeclSpec().getAttributes().hasAttribute( + ParsedAttr::AT_Overloadable)) S.Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_param); if (FTI.NumParams && FTI.Params[0].Param == nullptr) { diff --git a/clang/test/Sema/overloadable.c b/clang/test/Sema/overloadable.c index b520d76f9e7e..e9bbdeab7f66 100644 --- a/clang/test/Sema/overloadable.c +++ b/clang/test/Sema/overloadable.c @@ -248,3 +248,14 @@ void typeof_function_is_not_a_pointer() { // if take_fn is passed a void (**)(void *), we'll get a warning. take_fn(fn); } + +// PR53805 +// We previously failed to consider the attribute being written before the +// declaration when considering whether to allow a variadic signature with no +// other parameters, and so we handled these cases differently. +__attribute__((overloadable)) void can_overload_1(...); // ok, was previously rejected +void can_overload_2(...) __attribute__((overloadable)); // ok +[[clang::overloadable]] void can_overload_3(...); // ok, was previously rejected +void can_overload_4 [[clang::overloadable]] (...); // ok +void cannot_overload(...) [[clang::overloadable]]; // expected-error {{ISO C requires a named parameter before '...'}} \ + // expected-error {{'overloadable' attribute cannot be applied to types}}