Skip to content

Commit

Permalink
[Clang][Sema] Earlier type checking for builtin unary operators
Browse files Browse the repository at this point in the history
  • Loading branch information
sdkrystian committed May 3, 2024
1 parent 5d9889a commit 1b3476d
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 29 deletions.
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
22 changes: 13 additions & 9 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,12 +671,12 @@ ExprResult Sema::DefaultLvalueConversion(Expr *E) {

// We don't want to throw lvalue-to-rvalue casts on top of
// expressions of certain types in C++.
if (getLangOpts().CPlusPlus &&
(E->getType() == Context.OverloadTy ||
// FIXME: This is a hack! We want the lvalue-to-rvalue conversion applied
// to pointer types even if the pointee type is dependent.
(T->isDependentType() && !T->isPointerType()) || T->isRecordType()))
return E;
if (getLangOpts().CPlusPlus) {
if (T == Context.OverloadTy || T->isRecordType() ||
(T->isDependentType() && !T->isAnyPointerType() &&
!T->isMemberPointerType()))
return E;
}

// The C standard is actually really unclear on this point, and
// DR106 tells us what the result should be but not why. It's
Expand Down Expand Up @@ -11116,7 +11116,7 @@ static bool checkArithmeticIncompletePointerType(Sema &S, SourceLocation Loc,
if (const AtomicType *ResAtomicType = ResType->getAs<AtomicType>())
ResType = ResAtomicType->getValueType();

assert(ResType->isAnyPointerType() && !ResType->isDependentType());
assert(ResType->isAnyPointerType());
QualType PointeeTy = ResType->getPointeeType();
return S.RequireCompleteSizedType(
Loc, PointeeTy,
Expand Down Expand Up @@ -14287,7 +14287,9 @@ static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op,
ExprObjectKind &OK,
SourceLocation OpLoc,
bool IsInc, bool IsPrefix) {
if (Op->isTypeDependent())
if (Op->isTypeDependent() &&
(Op->hasPlaceholderType() ||
Op->getType()->isSpecificBuiltinType(BuiltinType::Dependent)))
return S.Context.DependentTy;

QualType ResType = Op->getType();
Expand Down Expand Up @@ -14725,7 +14727,9 @@ static void RecordModifiableNonNullParam(Sema &S, const Expr *Exp) {
static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK,
SourceLocation OpLoc,
bool IsAfterAmp = false) {
if (Op->isTypeDependent())
if (Op->isTypeDependent() &&
(Op->hasPlaceholderType() ||
Op->getType()->isSpecificBuiltinType(BuiltinType::Dependent)))
return S.Context.DependentTy;

ExprResult ConvResult = S.UsualUnaryConversions(Op);
Expand Down
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
8 changes: 5 additions & 3 deletions clang/test/CXX/over/over.built/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ struct A{};

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

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

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

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
12 changes: 6 additions & 6 deletions clang/test/SemaTemplate/class-template-spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int test_specs(A<float, float> *a1, A<float, int> *a2) {
return a1->x + a2->y;
}

int test_incomplete_specs(A<double, double> *a1,
int test_incomplete_specs(A<double, double> *a1,
A<double> *a2)
{
(void)a1->x; // expected-error{{member access into incomplete type}}
Expand All @@ -39,7 +39,7 @@ template <> struct X<int, int> { int foo(); }; // #1
template <> struct X<float> { int bar(); }; // #2

typedef int int_type;
void testme(X<int_type> *x1, X<float, int> *x2) {
void testme(X<int_type> *x1, X<float, int> *x2) {
(void)x1->foo(); // okay: refers to #1
(void)x2->bar(); // okay: refers to #2
}
Expand All @@ -53,7 +53,7 @@ struct A<char> {
A<char>::A() { }

// Make sure we can see specializations defined before the primary template.
namespace N{
namespace N{
template<typename T> struct A0;
}

Expand Down Expand Up @@ -97,7 +97,7 @@ namespace M {
template<> struct ::A<long double>; // expected-error{{must occur at global scope}}
}

template<> struct N::B<char> {
template<> struct N::B<char> {
int testf(int x) { return f(x); }
};

Expand Down Expand Up @@ -138,9 +138,9 @@ namespace PR18009 {

template <typename T> struct C {
template <int N, int M> struct S;
template <int N> struct S<N, N ? **(T(*)[N])0 : 0> {}; // expected-error {{depends on a template parameter of the partial specialization}}
template <int N> struct S<N, N ? **(T(*)[N])0 : 0> {}; // ok
};
C<int> c; // expected-note {{in instantiation of}}
C<int> c;

template<int A> struct outer {
template<int B, int C> struct inner {};
Expand Down

0 comments on commit 1b3476d

Please sign in to comment.