[clang] d930ed1 - Disallow use of __has_c_attribute in C++ mode.
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 25 16:07:21 PST 2019
On Mon, Nov 25, 2019 at 5:59 PM James Y Knight <jyknight at google.com> wrote:
>
> Isn't this unnecessarily annoying to users? You have the same syntax to use the attributes, and the attributes are expected to be compatible when named the same way, but you can't use the same #if conditional to check for availability, when writing a header intended to work in both modes?
Yes, I do think it's needlessly annoying to users.
WG21 adopted __has_cpp_attribute for C++2a based on the existing Clang
implementation. WG14 is looking to add a similar feature testing
macro, but cannot use __has_cpp_attribute due to the name and so I
proposed __has_c_attribute for parity with __has_cpp_attribute and
implemented it in Clang. After a lot of discussions with users,
committee members, and other implementers, I realized this is silly:
we could have a single macro that handles both C and C++ depending on
the language mode being compiled for. Because of that, as a liaison I
filed an NB comment with WG21 to have them reconsider the name
__has_cpp_attribute before it shipped in a standard so that we could
use a language-neutral name for both languages. In Belfast, EWG
reaffirmed that they want the name __has_cpp_attribute. They found my
arguments to be unpersuasive and didn't think it would be possible for
the semantics of attributes to align between the languages, or to
commit to such a guarantee (despite WG14 having a chartered mandate to
be compatible with features adopted from C++).
Rather than leaving C out in the cold because EWG came to the decision
they did, we're kind of stuck with __has_c_attribute.
One problem this does solve is that __has_cpp_attribute and
__has_c_attribute both return a numeric value that is date-like for
standard attributes, but those standard attributes are adopted into
different standards at different times. Because returned values are
different, it makes use harder when dealing with attribute
modifications over time. This isn't impossible to solve, we could
introduce macros for the return values and base their values on which
language mode is being preprocessed, but we no longer have to.
~Aaron
>
> On Mon, Nov 25, 2019 at 5:35 PM Aaron Ballman via cfe-commits <cfe-commits at lists.llvm.org> wrote:
>>
>>
>> Author: Aaron Ballman
>> Date: 2019-11-25T17:35:12-05:00
>> New Revision: d930ed1acc0ea49d4b3aae7e95b4c6d9cd310578
>>
>> URL: https://github.com/llvm/llvm-project/commit/d930ed1acc0ea49d4b3aae7e95b4c6d9cd310578
>> DIFF: https://github.com/llvm/llvm-project/commit/d930ed1acc0ea49d4b3aae7e95b4c6d9cd310578.diff
>>
>> LOG: Disallow use of __has_c_attribute in C++ mode.
>>
>> __has_cpp_attribute is not available in C mode, and __has_c_attribute
>> should not be available in C++ mode. This also adds a test to
>> demonstrate that we properly handle scoped attribute tokens even in C
>> mode.
>>
>> Added:
>> clang/test/Preprocessor/has_c_attribute.cpp
>>
>> Modified:
>> clang/lib/Lex/PPMacroExpansion.cpp
>> clang/test/Preprocessor/has_c_attribute.c
>>
>> Removed:
>>
>>
>>
>> ################################################################################
>> diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
>> index e6e00b1c1700..a69c4dbb3a2a 100644
>> --- a/clang/lib/Lex/PPMacroExpansion.cpp
>> +++ b/clang/lib/Lex/PPMacroExpansion.cpp
>> @@ -370,7 +370,11 @@ void Preprocessor::RegisterBuiltinMacros() {
>> Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension");
>> Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");
>> Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute");
>> - Ident__has_c_attribute = RegisterBuiltinMacro(*this, "__has_c_attribute");
>> + if (!LangOpts.CPlusPlus)
>> + Ident__has_c_attribute = RegisterBuiltinMacro(*this, "__has_c_attribute");
>> + else
>> + Ident__has_c_attribute = nullptr;
>> +
>> Ident__has_declspec = RegisterBuiltinMacro(*this, "__has_declspec_attribute");
>> Ident__has_include = RegisterBuiltinMacro(*this, "__has_include");
>> Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
>>
>> diff --git a/clang/test/Preprocessor/has_c_attribute.c b/clang/test/Preprocessor/has_c_attribute.c
>> index 843a67a2646c..f8b0b364faa5 100644
>> --- a/clang/test/Preprocessor/has_c_attribute.c
>> +++ b/clang/test/Preprocessor/has_c_attribute.c
>> @@ -1,4 +1,5 @@
>> // RUN: %clang_cc1 -fdouble-square-bracket-attributes -std=c11 -E %s -o - | FileCheck %s
>> +// RUN: %clang_cc1 -std=c2x -E %s -o - | FileCheck %s
>>
>> // CHECK: has_fallthrough
>> #if __has_c_attribute(fallthrough)
>> @@ -14,3 +15,8 @@
>> #if __has_c_attribute(__nodiscard__)
>> int has_nodiscard_underscore();
>> #endif
>> +
>> +// CHECK: has_clang_annotate
>> +#if __has_c_attribute(clang::annotate)
>> + int has_clang_annotate();
>> +#endif
>>
>> diff --git a/clang/test/Preprocessor/has_c_attribute.cpp b/clang/test/Preprocessor/has_c_attribute.cpp
>> new file mode 100644
>> index 000000000000..0bde73067178
>> --- /dev/null
>> +++ b/clang/test/Preprocessor/has_c_attribute.cpp
>> @@ -0,0 +1,8 @@
>> +// RUN: %clang_cc1 -std=c++11 %s -verify
>> +
>> +#if __has_c_attribute(fallthrough) // expected-error {{function-like macro '__has_c_attribute' is not defined}}
>> +#endif
>> +
>> +#if __has_c_attribute(gnu::transparent_union) // expected-error {{function-like macro '__has_c_attribute' is not defined}}
>> +#endif
>> +
>>
>>
>>
>> _______________________________________________
>> cfe-commits mailing list
>> cfe-commits at lists.llvm.org
>> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
More information about the cfe-commits
mailing list