This is an archive of the discontinued LLVM Phabricator instance.

[clang] [lex] Return false from __has_declspec_attribute() if not explicitly enabled
ClosedPublic

Authored by tbaeder on Jan 12 2021, 1:53 AM.

Details

Summary

Currently, projects can check for __has_declspec_attribute() and use it accordingly, like so:

#if __has_declspec_attribute(noreturn)
__declspec(noreturn)
#endif
static void foo(void) {}

(with maybe an additional defined(__has_declspec_attribute) check around).

However, clang will by default accept this code and return true for __has_declspec_attribute(noreturn), just to error out because declspec attributes have not been enabled explicitly:

~ ❯ clang ./declspec.c
./declspec.c:3:1: error: '__declspec' attributes are not enabled; use '-fdeclspec' or '-fms-extensions' to enable support for __declspec attributes
__declspec(noreturn)
^
1 error generated.

I think it would be better if clang simply returned false for __has_declspec_attribute() checks if declspec attributes have not been enabled.

Diff Detail

Event Timeline

tbaeder requested review of this revision.Jan 12 2021, 1:53 AM
tbaeder created this revision.
aaron.ballman accepted this revision.Jan 12 2021, 4:51 AM

Good catch, especially given that we document this as only working if the target supports the attribute (https://clang.llvm.org/docs/LanguageExtensions.html#has-declspec-attribute). LGTM with a few minor nits.

clang/lib/Lex/PPMacroExpansion.cpp
1697
clang/test/Preprocessor/has_declspec_attribute.c
7
This revision is now accepted and ready to land.Jan 12 2021, 4:51 AM
tbaeder updated this revision to Diff 316069.Jan 12 2021, 5:41 AM

Thanks for the quick review. I've made the requested changes and added a proper commit message. I noticed I forgot that before.

tbaeder marked 2 inline comments as done.Jan 12 2021, 5:47 AM
aaron.ballman accepted this revision.Jan 12 2021, 6:10 AM

LGTM, thank you!

I've commit this on your behalf in ef3800e82169c674219501d9ac09ef12b28e6359, thank you for the patch!