Skip to content

Commit

Permalink
Disallow use of __has_c_attribute in C++ mode.
Browse files Browse the repository at this point in the history
__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.
  • Loading branch information
AaronBallman committed Nov 25, 2019
1 parent 214683f commit d930ed1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion clang/lib/Lex/PPMacroExpansion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
6 changes: 6 additions & 0 deletions clang/test/Preprocessor/has_c_attribute.c
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
8 changes: 8 additions & 0 deletions clang/test/Preprocessor/has_c_attribute.cpp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d930ed1

Please sign in to comment.