Skip to content

Commit b1dc62f

Browse files
authored
[clang]Treat arguments to builtin type traits as template type arguments (#87132)
This change improves error messages for builtins in case of empty parentheses. Fixes #86997
1 parent 6f7976c commit b1dc62f

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

clang/lib/Parse/ParseExprCXX.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3910,10 +3910,10 @@ ExprResult Parser::ParseTypeTrait() {
39103910
SmallVector<ParsedType, 2> Args;
39113911
do {
39123912
// Parse the next type.
3913-
TypeResult Ty =
3914-
ParseTypeName(/*SourceRange=*/nullptr,
3915-
getLangOpts().CPlusPlus ? DeclaratorContext::TemplateArg
3916-
: DeclaratorContext::TypeName);
3913+
TypeResult Ty = ParseTypeName(/*SourceRange=*/nullptr,
3914+
getLangOpts().CPlusPlus
3915+
? DeclaratorContext::TemplateTypeArg
3916+
: DeclaratorContext::TypeName);
39173917
if (Ty.isInvalid()) {
39183918
Parens.skipToEnd();
39193919
return ExprError();
@@ -3955,8 +3955,8 @@ ExprResult Parser::ParseArrayTypeTrait() {
39553955
if (T.expectAndConsume())
39563956
return ExprError();
39573957

3958-
TypeResult Ty =
3959-
ParseTypeName(/*SourceRange=*/nullptr, DeclaratorContext::TemplateArg);
3958+
TypeResult Ty = ParseTypeName(/*SourceRange=*/nullptr,
3959+
DeclaratorContext::TemplateTypeArg);
39603960
if (Ty.isInvalid()) {
39613961
SkipUntil(tok::comma, StopAtSemi);
39623962
SkipUntil(tok::r_paren, StopAtSemi);

clang/test/Sema/static-assert.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// RUN: %clang_cc1 -std=c11 -Wgnu-folding-constant -fsyntax-only -verify=expected,c %s
2-
// RUN: %clang_cc1 -fms-compatibility -Wgnu-folding-constant -DMS -fsyntax-only -verify=expected,ms,c %s
3-
// RUN: %clang_cc1 -std=c99 -pedantic -Wgnu-folding-constant -fsyntax-only -verify=expected,ext,c %s
1+
// RUN: %clang_cc1 -std=c11 -Wgnu-folding-constant -fsyntax-only -verify %s
2+
// RUN: %clang_cc1 -fms-compatibility -Wgnu-folding-constant -DMS -fsyntax-only -verify=expected,ms %s
3+
// RUN: %clang_cc1 -std=c99 -pedantic -Wgnu-folding-constant -fsyntax-only -verify=expected,ext %s
44
// RUN: %clang_cc1 -xc++ -std=c++11 -pedantic -fsyntax-only -verify=expected,ext,cxx %s
55

66
_Static_assert("foo", "string is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}
@@ -57,8 +57,7 @@ UNION(char[2], short) u2 = { .one = { 'a', 'b' } }; // ext-warning 3 {{'_Static_
5757
typedef UNION(char, short) U3; // expected-error {{static assertion failed due to requirement 'sizeof(char) == sizeof(short)': type size mismatch}} \
5858
// expected-note{{evaluates to '1 == 2'}} \
5959
// ext-warning 3 {{'_Static_assert' is a C11 extension}}
60-
typedef UNION(float, 0.5f) U4; // c-error {{expected a type}} \
61-
// cxx-error {{type name requires a specifier or qualifier}} \
60+
typedef UNION(float, 0.5f) U4; // expected-error {{expected a type}} \
6261
// ext-warning 3 {{'_Static_assert' is a C11 extension}}
6362

6463
// After defining the assert macro in MS-compatibility mode, we should

clang/test/SemaCXX/builtins.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ using ConstMemFnType = int (Dummy::*)() const;
7676

7777
void foo() {}
7878

79+
void test_builtin_empty_parentheses_diags() {
80+
__is_trivially_copyable(); // expected-error {{expected a type}}
81+
__is_trivially_copyable(1); // expected-error {{expected a type}}
82+
}
83+
7984
void test_builtin_launder_diags(void *vp, const void *cvp, FnType *fnp,
8085
MemFnType mfp, ConstMemFnType cmfp, int (&Arr)[5]) {
8186
__builtin_launder(vp); // expected-error {{void pointer argument to '__builtin_launder' is not allowed}}

clang/test/SemaCXX/deprecated-builtins.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ void f() {
1717
a = __has_trivial_destructor(A); // expected-warning-re {{__has_trivial_destructor {{.*}} use __is_trivially_destructible}}
1818

1919
}
20+
21+
void test_builtin_empty_parentheses_diags(void) {
22+
__has_nothrow_copy(); // expected-error {{expected a type}}
23+
__has_nothrow_copy(1); // expected-error {{expected a type}}
24+
}

0 commit comments

Comments
 (0)