Skip to content

Commit 5050a8d

Browse files
committed
Fix __has_cpp_attribute expansion to produce trailing L and (where
necessary) leading whitespace. Simplify unit test and extend to cover no_unique_address attribute. llvm-svn: 364102
1 parent f5a5785 commit 5050a8d

File tree

2 files changed

+77
-87
lines changed

2 files changed

+77
-87
lines changed

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,9 +1330,13 @@ static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
13301330

13311331
// The last ')' has been reached; return the value if one found or
13321332
// a diagnostic and a dummy value.
1333-
if (Result.hasValue())
1333+
if (Result.hasValue()) {
13341334
OS << Result.getValue();
1335-
else {
1335+
// For strict conformance to __has_cpp_attribute rules, use 'L'
1336+
// suffix for dated literals.
1337+
if (Result.getValue() > 1)
1338+
OS << 'L';
1339+
} else {
13361340
OS << 0;
13371341
if (!SuppressDiagnostic)
13381342
PP.Diag(Tok.getLocation(), diag::err_too_few_args_in_macro_invoc);
@@ -1454,6 +1458,8 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
14541458
// Set up the return result.
14551459
Tok.setIdentifierInfo(nullptr);
14561460
Tok.clearFlag(Token::NeedsCleaning);
1461+
bool IsAtStartOfLine = Tok.isAtStartOfLine();
1462+
bool HasLeadingSpace = Tok.hasLeadingSpace();
14571463

14581464
if (II == Ident__LINE__) {
14591465
// C99 6.10.8: "__LINE__: The presumed line number (within the current
@@ -1807,6 +1813,8 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
18071813
llvm_unreachable("Unknown identifier!");
18081814
}
18091815
CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
1816+
Tok.setFlagValue(Token::StartOfLine, IsAtStartOfLine);
1817+
Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
18101818
}
18111819

18121820
void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
Lines changed: 67 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,80 @@
1-
// RUN: %clang_cc1 -triple i386-unknown-unknown -fms-compatibility -std=c++11 -E %s -o - | FileCheck %s
1+
// RUN: %clang_cc1 -triple i386-unknown-unknown -fms-compatibility -std=c++11 -E %s -o - | FileCheck %s --check-prefixes=CHECK,ITANIUM --implicit-check-not=:
2+
// RUN: %clang_cc1 -triple i386-windows -fms-compatibility -std=c++11 -E %s -o - | FileCheck %s --check-prefixes=CHECK,WINDOWS --implicit-check-not=:
23

3-
// CHECK: has_cxx11_carries_dep
4-
#if __has_cpp_attribute(carries_dependency)
5-
int has_cxx11_carries_dep();
6-
#endif
4+
#define CXX11(x) x: __has_cpp_attribute(x)
75

8-
// CHECK: has_clang_fallthrough_1
9-
#if __has_cpp_attribute(clang::fallthrough)
10-
int has_clang_fallthrough_1();
11-
#endif
6+
// CHECK: clang::fallthrough: 201603L
7+
CXX11(clang::fallthrough)
128

13-
// CHECK: does_not_have_selectany
14-
#if !__has_cpp_attribute(selectany)
15-
int does_not_have_selectany();
16-
#endif
9+
// CHECK: selectany: 0
10+
CXX11(selectany)
1711

1812
// The attribute name can be bracketed with double underscores.
19-
// CHECK: has_clang_fallthrough_2
20-
#if __has_cpp_attribute(clang::__fallthrough__)
21-
int has_clang_fallthrough_2();
22-
#endif
13+
// CHECK: clang::__fallthrough__: 201603L
14+
CXX11(clang::__fallthrough__)
2315

2416
// The scope cannot be bracketed with double underscores unless it is
2517
// for gnu or clang.
26-
// CHECK: does_not_have___gsl___suppress
27-
#if !__has_cpp_attribute(__gsl__::suppress)
28-
int does_not_have___gsl___suppress();
29-
#endif
18+
// CHECK: __gsl__::suppress: 0
19+
CXX11(__gsl__::suppress)
3020

3121
// We do somewhat support the __clang__ vendor namespace, but it is a
3222
// predefined macro and thus we encourage users to use _Clang instead.
3323
// Because of this, we do not support __has_cpp_attribute for that
3424
// vendor namespace.
35-
// CHECK: does_not_have___clang___fallthrough
36-
#if !__has_cpp_attribute(__clang__::fallthrough)
37-
int does_not_have___clang___fallthrough();
38-
#endif
39-
40-
// CHECK: does_have_Clang_fallthrough
41-
#if __has_cpp_attribute(_Clang::fallthrough)
42-
int does_have_Clang_fallthrough();
43-
#endif
44-
45-
// CHECK: has_gnu_const
46-
#if __has_cpp_attribute(__gnu__::__const__)
47-
int has_gnu_const();
48-
#endif
49-
50-
// Test that C++11, target-specific attributes behave properly.
51-
52-
// CHECK: does_not_have_mips16
53-
#if !__has_cpp_attribute(gnu::mips16)
54-
int does_not_have_mips16();
55-
#endif
56-
57-
// Test that the version numbers of attributes listed in SD-6 are supported
58-
// correctly.
59-
60-
// CHECK: has_cxx11_carries_dep_vers
61-
#if __has_cpp_attribute(carries_dependency) == 200809
62-
int has_cxx11_carries_dep_vers();
63-
#endif
64-
65-
// CHECK: has_cxx11_noreturn_vers
66-
#if __has_cpp_attribute(noreturn) == 200809
67-
int has_cxx11_noreturn_vers();
68-
#endif
69-
70-
// CHECK: has_cxx14_deprecated_vers
71-
#if __has_cpp_attribute(deprecated) == 201309
72-
int has_cxx14_deprecated_vers();
73-
#endif
74-
75-
// CHECK: has_cxx1z_nodiscard
76-
#if __has_cpp_attribute(nodiscard) == 201603
77-
int has_cxx1z_nodiscard();
78-
#endif
79-
80-
// CHECK: has_cxx1z_fallthrough
81-
#if __has_cpp_attribute(fallthrough) == 201603
82-
int has_cxx1z_fallthrough();
83-
#endif
84-
85-
// CHECK: has_declspec_uuid
86-
#if __has_declspec_attribute(uuid)
87-
int has_declspec_uuid();
88-
#endif
89-
90-
// CHECK: has_declspec_uuid2
91-
#if __has_declspec_attribute(__uuid__)
92-
int has_declspec_uuid2();
93-
#endif
94-
95-
// CHECK: does_not_have_declspec_fallthrough
96-
#if !__has_declspec_attribute(fallthrough)
97-
int does_not_have_declspec_fallthrough();
98-
#endif
25+
//
26+
// Note, we can't use CXX11 here because it will expand __clang__ to 1
27+
// too early.
28+
// CHECK: 1::fallthrough: 0
29+
__clang__::fallthrough: __has_cpp_attribute(__clang__::fallthrough)
30+
31+
// CHECK: _Clang::fallthrough: 201603L
32+
CXX11(_Clang::fallthrough)
33+
34+
// CHECK: __gnu__::__const__: 1
35+
CXX11(__gnu__::__const__)
36+
37+
// Test that C++11, target-specific attributes behave properly.
38+
39+
// CHECK: gnu::mips16: 0
40+
CXX11(gnu::mips16)
41+
42+
// Test for standard attributes as listed in C++2a [cpp.cond] paragraph 6.
43+
44+
CXX11(assert)
45+
CXX11(carries_dependency)
46+
CXX11(deprecated)
47+
CXX11(ensures)
48+
CXX11(expects)
49+
CXX11(fallthrough)
50+
CXX11(likely)
51+
CXX11(maybe_unused)
52+
CXX11(no_unique_address)
53+
CXX11(nodiscard)
54+
CXX11(noreturn)
55+
CXX11(unlikely)
56+
// FIXME(201806L) CHECK: assert: 0
57+
// CHECK: carries_dependency: 200809L
58+
// CHECK: deprecated: 201309L
59+
// FIXME(201806L) CHECK: ensures: 0
60+
// FIXME(201806L) CHECK: expects: 0
61+
// CHECK: fallthrough: 201603L
62+
// FIXME(201803L) CHECK: likely: 0
63+
// CHECK: maybe_unused: 201603L
64+
// ITANIUM: no_unique_address: 201803L
65+
// WINDOWS: no_unique_address: 0
66+
// CHECK: nodiscard: 201603L
67+
// CHECK: noreturn: 200809L
68+
// FIXME(201803L) CHECK: unlikely: 0
69+
70+
// Test for Microsoft __declspec attributes
71+
72+
#define DECLSPEC(x) x: __has_declspec_attribute(x)
73+
74+
// CHECK: uuid: 1
75+
// CHECK: __uuid__: 1
76+
DECLSPEC(uuid)
77+
DECLSPEC(__uuid__)
78+
79+
// CHECK: fallthrough: 0
80+
DECLSPEC(fallthrough)

0 commit comments

Comments
 (0)