From d930ed1acc0ea49d4b3aae7e95b4c6d9cd310578 Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Mon, 25 Nov 2019 17:32:07 -0500 Subject: [PATCH] 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. --- clang/lib/Lex/PPMacroExpansion.cpp | 6 +++++- clang/test/Preprocessor/has_c_attribute.c | 6 ++++++ clang/test/Preprocessor/has_c_attribute.cpp | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 clang/test/Preprocessor/has_c_attribute.cpp diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index e6e00b1..a69c4db 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 843a67a..f8b0b36 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 0000000..0bde730 --- /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 + -- 2.7.4