Skip to content

Commit a8d99bb

Browse files
committed
Disallow btf_type_tag in C++ mode
This was always intended to be disallowed in C++ (see the definition in Attr.td), but failed to add the correct checking code in SemaType.cpp to ensure it was rejected. Fixes llvm#106864
1 parent 6238159 commit a8d99bb

File tree

5 files changed

+25
-8
lines changed

5 files changed

+25
-8
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ Attribute Changes in Clang
225225
more cases where the returned reference outlives the object.
226226
(#GH100567)
227227

228+
- Now correctly diagnosing use of ``btf_type_tag`` in C++ and ignoring it; this
229+
attribute is a C-only attribute, and was causing crashes with template
230+
instantiation by accidentally allowing it in C++ in some circumstances.
231+
(#GH106864)
232+
228233
Improvements to Clang's diagnostics
229234
-----------------------------------
230235

clang/lib/Sema/SemaType.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6490,6 +6490,15 @@ static void HandleBTFTypeTagAttribute(QualType &Type, const ParsedAttr &Attr,
64906490
TypeProcessingState &State) {
64916491
Sema &S = State.getSema();
64926492

6493+
// This attribute is only supported in C.
6494+
// FIXME: we should implement checkCommonAttributeFeatures() in SemaAttr.cpp
6495+
// such that it handles type attributes, and then call that from
6496+
// processTypeAttrs() instead of one-off checks like this.
6497+
if (!Attr.diagnoseLangOpts(S)) {
6498+
Attr.setInvalid();
6499+
return;
6500+
}
6501+
64936502
// Check the number of attribute arguments.
64946503
if (Attr.getNumArgs() != 1) {
64956504
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)

clang/test/Sema/attr-btf_type_tag.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s
2+
// RUN: %clang_cc1 -fsyntax-only -verify=c -x c %s
3+
4+
// c-no-diagnostics
5+
6+
// Ensure that we diagnose the attribute as ignored in C++ but not in C.
7+
#ifdef __cplusplus
8+
static_assert(__builtin_is_implicit_lifetime(int __attribute__((btf_type_tag("user"))) *)); // expected-warning {{'btf_type_tag' attribute ignored}}
9+
#endif
10+
int __attribute__((btf_type_tag("user"))) *ptr; // expected-warning {{'btf_type_tag' attribute ignored}}
11+

clang/test/SemaCXX/sugar-common-types.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,6 @@ N t19 = 0 ? (__underlying_type(EnumsX::X)){} : (__underlying_type(EnumsY::Y)){};
9090
N t20 = 0 ? (__underlying_type(EnumsX::X)){} : (__underlying_type(EnumsY::X)){};
9191
// expected-error@-1 {{rvalue of type '__underlying_type(Enums::X)' (aka 'int')}}
9292

93-
using SBTF1 = SS1 [[clang::btf_type_tag("1")]];
94-
using SBTF2 = ::SS1 [[clang::btf_type_tag("1")]];
95-
using SBTF3 = ::SS1 [[clang::btf_type_tag("2")]];
96-
97-
N t21 = 0 ? (SBTF1){} : (SBTF3){}; // expected-error {{from 'SS1'}}
98-
N t22 = 0 ? (SBTF1){} : (SBTF2){}; // expected-error {{from 'SS1 __attribute__((btf_type_tag("1")))' (aka 'SS1')}}
99-
10093
using QX = const SB1 *;
10194
using QY = const ::SB1 *;
10295
N t23 = 0 ? (QX){} : (QY){}; // expected-error {{rvalue of type 'const SB1 *' (aka 'const SS1 *')}}

clang/test/SemaCXX/type-traits.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2052,7 +2052,6 @@ void is_implicit_lifetime(int n) {
20522052
static_assert(__builtin_is_implicit_lifetime(float4));
20532053
static_assert(__builtin_is_implicit_lifetime(align_value_int));
20542054
static_assert(__builtin_is_implicit_lifetime(int[[clang::annotate_type("category2")]] *));
2055-
static_assert(__builtin_is_implicit_lifetime(int __attribute__((btf_type_tag("user"))) *));
20562055
static_assert(__builtin_is_implicit_lifetime(EnforceReadOnlyPlacement));
20572056
static_assert(__builtin_is_implicit_lifetime(int __attribute__((noderef)) *));
20582057
static_assert(__builtin_is_implicit_lifetime(TypeVisibility));

0 commit comments

Comments
 (0)