OSDN Git Service

Return false from __has_declspec_attribute() if not explicitly enabled
authorTimm Bäder <tbaeder@redhat.com>
Tue, 12 Jan 2021 18:18:13 +0000 (13:18 -0500)
committerAaron Ballman <aaron@aaronballman.com>
Tue, 12 Jan 2021 18:20:08 +0000 (13:20 -0500)
Currently, projects can check for __has_declspec_attribute() and use
it accordingly, but the check for __has_declspec_attribute will return
true even if declspec attributes are not enabled for the target.

This changes Clang to instead return false when declspec attributes are
not supported for the target.

clang/lib/Lex/PPMacroExpansion.cpp

index 3969630..43d31d6 100644 (file)
@@ -1693,8 +1693,14 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
       [this](Token &Tok, bool &HasLexedNextToken) -> int {
         IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
                                            diag::err_feature_check_malformed);
-        return II ? hasAttribute(AttrSyntax::Declspec, nullptr, II,
-                                 getTargetInfo(), getLangOpts()) : 0;
+        if (II) {
+          const LangOptions &LangOpts = getLangOpts();
+          return LangOpts.DeclSpecKeyword &&
+                 hasAttribute(AttrSyntax::Declspec, nullptr, II,
+                              getTargetInfo(), LangOpts);
+        }
+
+        return false;
       });
   } else if (II == Ident__has_cpp_attribute ||
              II == Ident__has_c_attribute) {