Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clang][Sema] Earlier type checking for builtin unary operators #90500

Merged
merged 4 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ C++ Specific Potentially Breaking Changes

- Clang now rejects pointer to member from parenthesized expression in unevaluated context such as ``decltype(&(foo::bar))``. (#GH40906).

- Clang now performs semantic analysis for unary operators with dependent operands
that are known to be of non-class non-enumeration type prior to instantiation.

ABI Changes in This Version
---------------------------
- Fixed Microsoft name mangling of implicitly defined variables used for thread
Expand Down
5 changes: 4 additions & 1 deletion clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -8044,7 +8044,10 @@ inline bool Type::isUndeducedType() const {
/// Determines whether this is a type for which one can define
/// an overloaded operator.
inline bool Type::isOverloadableType() const {
return isDependentType() || isRecordType() || isEnumeralType();
if (!CanonicalType->isDependentType())
return isRecordType() || isEnumeralType();
return !isArrayType() && !isFunctionType() && !isAnyPointerType() &&
!isMemberPointerType();
}

/// Determines whether this type is written as a typedef-name.
Expand Down
354 changes: 175 additions & 179 deletions clang/lib/Sema/SemaExpr.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions clang/test/AST/ast-dump-expr-json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4261,9 +4261,9 @@ void TestNonADLCall3() {
// CHECK-NEXT: }
// CHECK-NEXT: },
// CHECK-NEXT: "type": {
// CHECK-NEXT: "qualType": "<dependent type>"
// CHECK-NEXT: "qualType": "V"
// CHECK-NEXT: },
// CHECK-NEXT: "valueCategory": "prvalue",
// CHECK-NEXT: "valueCategory": "lvalue",
// CHECK-NEXT: "isPostfix": false,
// CHECK-NEXT: "opcode": "*",
// CHECK-NEXT: "canOverflow": false,
Expand Down
2 changes: 1 addition & 1 deletion clang/test/AST/ast-dump-expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ void PrimaryExpressions(Ts... a) {
// CHECK-NEXT: CompoundStmt
// CHECK-NEXT: FieldDecl 0x{{[^ ]*}} <col:8> col:8 implicit 'V'
// CHECK-NEXT: ParenListExpr 0x{{[^ ]*}} <col:8> 'NULL TYPE'
// CHECK-NEXT: UnaryOperator 0x{{[^ ]*}} <col:8> '<dependent type>' prefix '*' cannot overflow
// CHECK-NEXT: UnaryOperator 0x{{[^ ]*}} <col:8> 'V' lvalue prefix '*' cannot overflow
// CHECK-NEXT: CXXThisExpr 0x{{[^ ]*}} <col:8> 'V *' this
}
};
Expand Down
2 changes: 1 addition & 1 deletion clang/test/AST/ast-dump-lambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ template <typename... Ts> void test(Ts... a) {
// CHECK-NEXT: | | | `-CompoundStmt {{.*}} <col:15, col:16>
// CHECK-NEXT: | | `-FieldDecl {{.*}} <col:8> col:8{{( imported)?}} implicit 'V'
// CHECK-NEXT: | |-ParenListExpr {{.*}} <col:8> 'NULL TYPE'
// CHECK-NEXT: | | `-UnaryOperator {{.*}} <col:8> '<dependent type>' prefix '*' cannot overflow
// CHECK-NEXT: | | `-UnaryOperator {{.*}} <col:8> 'V' lvalue prefix '*' cannot overflow
// CHECK-NEXT: | | `-CXXThisExpr {{.*}} <col:8> 'V *' this
// CHECK-NEXT: | `-CompoundStmt {{.*}} <col:15, col:16>
// CHECK-NEXT: |-DeclStmt {{.*}} <line:22:3, col:11>
Expand Down
65 changes: 65 additions & 0 deletions clang/test/CXX/expr/expr.unary/expr.unary.general/p1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// RUN: %clang_cc1 -Wno-unused -fsyntax-only %s -verify

struct A {
void operator*();
void operator+();
void operator-();
void operator!();
void operator~();
void operator&();
void operator++();
void operator--();
};

struct B { };

template<typename T, typename U>
void dependent(T t, T* pt, T U::* mpt, T(&ft)(), T(&at)[4]) {
*t;
+t;
-t;
!t;
~t;
&t;
++t;
--t;

*pt;
+pt;
-pt; // expected-error {{invalid argument type 'T *' to unary expression}}
!pt;
~pt; // expected-error {{invalid argument type 'T *' to unary expression}}
&pt;
++pt;
--pt;

*mpt; // expected-error {{indirection requires pointer operand ('T U::*' invalid)}}
+mpt; // expected-error {{invalid argument type 'T U::*' to unary expression}}
-mpt; // expected-error {{invalid argument type 'T U::*' to unary expression}}
!mpt;
~mpt; // expected-error {{invalid argument type 'T U::*' to unary expression}}
&mpt;
++mpt; // expected-error {{cannot increment value of type 'T U::*'}}
--mpt; // expected-error {{cannot decrement value of type 'T U::*'}}

*ft;
+ft;
-ft; // expected-error {{invalid argument type 'T (*)()' to unary expression}}
!ft;
~ft; // expected-error {{invalid argument type 'T (*)()' to unary expression}}
&ft;
++ft; // expected-error {{cannot increment value of type 'T ()'}}
--ft; // expected-error {{cannot decrement value of type 'T ()'}}

*at;
+at;
-at; // expected-error {{invalid argument type 'T *' to unary expression}}
!at;
~at; // expected-error {{invalid argument type 'T *' to unary expression}}
&at;
++at; // expected-error {{cannot increment value of type 'T[4]'}}
--at; // expected-error {{cannot decrement value of type 'T[4]'}}
}

// Make sure we only emit diagnostics once.
template void dependent(A t, A* pt, A B::* mpt, A(&ft)(), A(&at)[4]);
158 changes: 128 additions & 30 deletions clang/test/CXX/over/over.built/ast.cpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,139 @@
// RUN: %clang_cc1 -std=c++17 -ast-dump %s -ast-dump-filter Test | FileCheck %s
// RUN: %clang_cc1 -std=c++17 -Wno-unused -ast-dump %s -ast-dump-filter Test | FileCheck %s

struct A{};
namespace Test {
template<typename T, typename U>
void Unary(T t, T* pt, T U::* mpt, T(&ft)(), T(&at)[4]) {
// CHECK: UnaryOperator {{.*}} '<dependent type>' lvalue prefix '*' cannot overflow
// CHECK-NEXT: DeclRefExpr {{.*}} 'T' lvalue ParmVar {{.*}} 't' 'T'
*t;

template <typename T, typename U>
auto Test(T* pt, U* pu) {
// CHECK: UnaryOperator {{.*}} '<dependent type>' lvalue prefix '*'
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
(void)*pt;
// CHECK: UnaryOperator {{.*}} '<dependent type>' prefix '+' cannot overflow
// CHECK-NEXT: DeclRefExpr {{.*}} 'T' lvalue ParmVar {{.*}} 't' 'T'
+t;

// CHECK: UnaryOperator {{.*}} '<dependent type>' lvalue prefix '++'
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
(void)(++pt);
// CHECK: UnaryOperator {{.*}} '<dependent type>' prefix '-' cannot overflow
// CHECK-NEXT: DeclRefExpr {{.*}} 'T' lvalue ParmVar {{.*}} 't' 'T'
-t;

// CHECK: UnaryOperator {{.*}} '<dependent type>' prefix '+'
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
(void)(+pt);
// CHECK: UnaryOperator {{.*}} '<dependent type>' prefix '!' cannot overflow
// CHECK-NEXT: DeclRefExpr {{.*}} 'T' lvalue ParmVar {{.*}} 't' 'T'
!t;

// CHECK: BinaryOperator {{.*}} '<dependent type>' '+'
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
// CHECK-NEXT: IntegerLiteral {{.*}} 'int' 3
(void)(pt + 3);
// CHECK: UnaryOperator {{.*}} '<dependent type>' prefix '~' cannot overflow
// CHECK-NEXT: DeclRefExpr {{.*}} 'T' lvalue ParmVar {{.*}} 't' 'T'
~t;

// CHECK: BinaryOperator {{.*}} '<dependent type>' '-'
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
(void)(pt - pt);
// CHECK: UnaryOperator {{.*}} '<dependent type>' prefix '&' cannot overflow
// CHECK-NEXT: DeclRefExpr {{.*}} 'T' lvalue ParmVar {{.*}} 't' 'T'
&t;

// CHECK: BinaryOperator {{.*}} '<dependent type>' '-'
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
// CHECK-NEXT: DeclRefExpr {{.*}} 'U *' lvalue ParmVar {{.*}} 'pu' 'U *'
(void)(pt - pu);
// CHECK: UnaryOperator {{.*}} '<dependent type>' lvalue prefix '++' cannot overflow
// CHECK-NEXT: DeclRefExpr {{.*}} 'T' lvalue ParmVar {{.*}} 't' 'T'
++t;

// CHECK: BinaryOperator {{.*}} '<dependent type>' '=='
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
// CHECK-NEXT: DeclRefExpr {{.*}} 'U *' lvalue ParmVar {{.*}} 'pu' 'U *'
(void)(pt == pu);
// CHECK: UnaryOperator {{.*}} '<dependent type>' lvalue prefix '--' cannot overflow
// CHECK-NEXT: DeclRefExpr {{.*}} 'T' lvalue ParmVar {{.*}} 't' 'T'
--t;

}
// CHECK: UnaryOperator {{.*}} 'T' lvalue prefix '*' cannot overflow
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'T *' <LValueToRValue>
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
*pt;

// CHECK: UnaryOperator {{.*}} 'T *' prefix '+' cannot overflow
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'T *' <LValueToRValue>
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
+pt;

// CHECK: UnaryOperator {{.*}} 'bool' prefix '!' cannot overflow
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'bool' <PointerToBoolean>
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'T *' <LValueToRValue>
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
!pt;

// CHECK: UnaryOperator {{.*}} '<dependent type>' prefix '&' cannot overflow
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
&pt;

// CHECK: UnaryOperator {{.*}} 'T *' lvalue prefix '++' cannot overflow
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
++pt;

// CHECK: UnaryOperator {{.*}} 'T *' lvalue prefix '--' cannot overflow
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
--pt;

// CHECK: UnaryOperator {{.*}} 'bool' prefix '!' cannot overflow
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'bool' <MemberPointerToBoolean>
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'T U::*' <LValueToRValue>
// CHECK-NEXT: DeclRefExpr {{.*}} 'T U::*' lvalue ParmVar {{.*}} 'mpt' 'T U::*'
!mpt;

// CHECK: UnaryOperator {{.*}} '<dependent type>' prefix '&' cannot overflow
// CHECK-NEXT: DeclRefExpr {{.*}} 'T U::*' lvalue ParmVar {{.*}} 'mpt' 'T U::*'
&mpt;

// CHECK: UnaryOperator {{.*}} 'T ()' lvalue prefix '*' cannot overflow
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'T (*)()' <FunctionToPointerDecay>
// CHECK-NEXT: DeclRefExpr {{.*}} 'T ()' lvalue ParmVar {{.*}} 'ft' 'T (&)()'
*ft;

// CHECK: UnaryOperator {{.*}} 'T (*)()' prefix '+' cannot overflow
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'T (*)()' <FunctionToPointerDecay>
// CHECK-NEXT: DeclRefExpr {{.*}} 'T ()' lvalue ParmVar {{.*}} 'ft' 'T (&)()'
+ft;

// CHECK: UnaryOperator {{.*}} 'bool' prefix '!' cannot overflow
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'bool' <PointerToBoolean>
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'T (*)()' <FunctionToPointerDecay>
// CHECK-NEXT: DeclRefExpr {{.*}} 'T ()' lvalue ParmVar {{.*}} 'ft' 'T (&)()'
!ft;

// CHECK: UnaryOperator {{.*}} '<dependent type>' prefix '&' cannot overflow
// CHECK-NEXT: DeclRefExpr {{.*}} 'T ()' lvalue ParmVar {{.*}} 'ft' 'T (&)()'
&ft;

// CHECK: UnaryOperator {{.*}} 'T' lvalue prefix '*' cannot overflow
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'T *' <ArrayToPointerDecay>
// CHECK-NEXT: DeclRefExpr {{.*}} 'T[4]' lvalue ParmVar {{.*}} 'at' 'T (&)[4]'
*at;

// CHECK: UnaryOperator {{.*}} 'T *' prefix '+' cannot overflow
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'T *' <ArrayToPointerDecay>
// CHECK-NEXT: DeclRefExpr {{.*}} 'T[4]' lvalue ParmVar {{.*}} 'at' 'T (&)[4]'
+at;

// CHECK: UnaryOperator {{.*}} 'bool' prefix '!' cannot overflow
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'bool' <PointerToBoolean>
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'T *' <ArrayToPointerDecay>
// CHECK-NEXT: DeclRefExpr {{.*}} 'T[4]' lvalue ParmVar {{.*}} 'at' 'T (&)[4]'
!at;

// CHECK: UnaryOperator {{.*}} '<dependent type>' prefix '&' cannot overflow
// CHECK-NEXT: DeclRefExpr {{.*}} 'T[4]' lvalue ParmVar {{.*}} 'at' 'T (&)[4]'
&at;
}

template<typename T, typename U>
void Binary(T* pt, U* pu) {
// CHECK: BinaryOperator {{.*}} '<dependent type>' '+'
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
// CHECK-NEXT: IntegerLiteral {{.*}} 'int' 3
pt + 3;

// CHECK: BinaryOperator {{.*}} '<dependent type>' '-'
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
pt - pt;

// CHECK: BinaryOperator {{.*}} '<dependent type>' '-'
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
// CHECK-NEXT: DeclRefExpr {{.*}} 'U *' lvalue ParmVar {{.*}} 'pu' 'U *'
pt - pu;

// CHECK: BinaryOperator {{.*}} '<dependent type>' '=='
// CHECK-NEXT: DeclRefExpr {{.*}} 'T *' lvalue ParmVar {{.*}} 'pt' 'T *'
// CHECK-NEXT: DeclRefExpr {{.*}} 'U *' lvalue ParmVar {{.*}} 'pu' 'U *'
pt == pu;
}
} // namespace Test
2 changes: 1 addition & 1 deletion clang/test/CXX/over/over.built/p10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ void f(int i, float f, bool b, char c, int* pi, A* pa, T* pt) {

(void)-pi; // expected-error {{invalid argument type}}
(void)-pa; // expected-error {{invalid argument type}}
(void)-pt; // FIXME: we should be able to give an error here.
(void)-pt; // expected-error {{invalid argument type}}
}

2 changes: 1 addition & 1 deletion clang/test/CXX/over/over.built/p11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ void f(int i, float f, bool b, char c, int* pi, T* pt) {
(void)~b;
(void)~c;
(void)~pi; // expected-error {{invalid argument type}}
(void)~pt; // FIXME: we should be able to give an error here.
(void)~pt; // expected-error {{invalid argument type}}
}

25 changes: 10 additions & 15 deletions clang/test/CXX/temp/temp.res/temp.dep/temp.dep.type/p4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,17 +357,14 @@ namespace N0 {
a->A::f4(); // expected-error{{no member named 'f4' in 'N0::A'}}
a->B::A::f4(); // expected-error{{no member named 'f4' in 'N0::A'}}

// FIXME: An overloaded unary 'operator*' is built for these
// even though the operand is a pointer (to a dependent type).
// Type::isOverloadableType should return false for such cases.
(*this).x4;
(*this).B::x4;
(*this).A::x4;
(*this).B::A::x4;
(*this).f4();
(*this).B::f4();
(*this).A::f4();
(*this).B::A::f4();
(*this).x4; // expected-error{{no member named 'x4' in 'B<T>'}}
(*this).B::x4; // expected-error{{no member named 'x4' in 'B<T>'}}
(*this).A::x4; // expected-error{{no member named 'x4' in 'N0::A'}}
(*this).B::A::x4; // expected-error{{no member named 'x4' in 'N0::A'}}
(*this).f4(); // expected-error{{no member named 'f4' in 'B<T>'}}
(*this).B::f4(); // expected-error{{no member named 'f4' in 'B<T>'}}
(*this).A::f4(); // expected-error{{no member named 'f4' in 'N0::A'}}
(*this).B::A::f4(); // expected-error{{no member named 'f4' in 'N0::A'}}

b.x4; // expected-error{{no member named 'x4' in 'B<T>'}}
b.B::x4; // expected-error{{no member named 'x4' in 'B<T>'}}
Expand Down Expand Up @@ -399,15 +396,13 @@ namespace N1 {
f<0>();
this->f<0>();
a->f<0>();
// FIXME: This should not require 'template'!
(*this).f<0>(); // expected-error{{missing 'template' keyword prior to dependent template name 'f'}}
(*this).f<0>();
b.f<0>();

x.f<0>();
this->x.f<0>();
a->x.f<0>();
// FIXME: This should not require 'template'!
(*this).x.f<0>(); // expected-error{{missing 'template' keyword prior to dependent template name 'f'}}
(*this).x.f<0>();
b.x.f<0>();

// FIXME: None of these should require 'template'!
Expand Down
4 changes: 2 additions & 2 deletions clang/test/Frontend/noderef_templates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#define NODEREF __attribute__((noderef))

template <typename T>
int func(T NODEREF *a) { // expected-note 2 {{a declared here}}
return *a + 1; // expected-warning 2 {{dereferencing a; was declared with a 'noderef' type}}
int func(T NODEREF *a) { // expected-note 3 {{a declared here}}
return *a + 1; // expected-warning 3 {{dereferencing a; was declared with a 'noderef' type}}
}

void func() {
Expand Down
6 changes: 2 additions & 4 deletions clang/test/SemaCXX/cxx2b-deducing-this.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct S {
// new and delete are implicitly static
void *operator new(this unsigned long); // expected-error{{an explicit object parameter cannot appear in a static function}}
void operator delete(this void*); // expected-error{{an explicit object parameter cannot appear in a static function}}

void g(this auto) const; // expected-error{{explicit object member function cannot have 'const' qualifier}}
void h(this auto) &; // expected-error{{explicit object member function cannot have '&' qualifier}}
void i(this auto) &&; // expected-error{{explicit object member function cannot have '&&' qualifier}}
Expand Down Expand Up @@ -198,9 +198,7 @@ void func(int i) {
void TestMutationInLambda() {
[i = 0](this auto &&){ i++; }();
[i = 0](this auto){ i++; }();
[i = 0](this const auto&){ i++; }();
// expected-error@-1 {{cannot assign to a variable captured by copy in a non-mutable lambda}}
// expected-note@-2 {{in instantiation of}}
[i = 0](this const auto&){ i++; }(); // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}

int x;
const auto l1 = [x](this auto&) { x = 42; }; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
Expand Down
Loading
Loading