Skip to content

[Clang] [Sema] Handle this in __restrict-qualified member functions properly #83187

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

Merged
merged 6 commits into from
Feb 29, 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
5 changes: 5 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ Bug Fixes to C++ Support
lookup searches the bases of an incomplete class.
- Fix a crash when an unresolved overload set is encountered on the RHS of a ``.*`` operator.
(`#53815 <https://github.com/llvm/llvm-project/issues/53815>`_)
- In ``__restrict``-qualified member functions, attach ``__restrict`` to the pointer type of
``this`` rather than the pointee type.
Fixes (`#82941 <https://github.com/llvm/llvm-project/issues/82941>`_),
(`#42411 <https://github.com/llvm/llvm-project/issues/42411>`_), and
(`#18121 <https://github.com/llvm/llvm-project/issues/18121>`_).

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
15 changes: 13 additions & 2 deletions clang/lib/AST/DeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2543,8 +2543,19 @@ QualType CXXMethodDecl::getThisType(const FunctionProtoType *FPT,
const CXXRecordDecl *Decl) {
ASTContext &C = Decl->getASTContext();
QualType ObjectTy = ::getThisObjectType(C, FPT, Decl);
return C.getLangOpts().HLSL ? C.getLValueReferenceType(ObjectTy)
: C.getPointerType(ObjectTy);

// Unlike 'const' and 'volatile', a '__restrict' qualifier must be
// attached to the pointer type, not the pointee.
bool Restrict = FPT->getMethodQuals().hasRestrict();
if (Restrict)
ObjectTy.removeLocalRestrict();

ObjectTy = C.getLangOpts().HLSL ? C.getLValueReferenceType(ObjectTy)
: C.getPointerType(ObjectTy);

if (Restrict)
ObjectTy.addRestrict();
return ObjectTy;
}

QualType CXXMethodDecl::getThisType() const {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ static QualType adjustCVQualifiersForCXXThisWithinLambda(
: nullptr;
}
}
return ASTCtx.getPointerType(ClassType);
return ThisTy;
}

QualType Sema::getCurrentThisType() {
Expand Down
69 changes: 69 additions & 0 deletions clang/test/SemaCXX/restrict-this.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// RUN: %clang_cc1 -verify -fsyntax-only %s
// expected-no-diagnostics

struct C {
void f() __restrict {
static_assert(__is_same(decltype(this), C *__restrict));
(void) [this]() {
static_assert(__is_same(decltype(this), C *__restrict));
(void) [this]() { static_assert(__is_same(decltype(this), C *__restrict)); };

// By-value capture means 'this' is now a different object; do not
// make it __restrict.
(void) [*this]() { static_assert(__is_same(decltype(this), const C *)); };
(void) [*this]() mutable { static_assert(__is_same(decltype(this), C *)); };
};
}
};

template <typename T> struct TC {
void f() __restrict {
static_assert(__is_same(decltype(this), TC<int> *__restrict));
(void) [this]() {
static_assert(__is_same(decltype(this), TC<int> *__restrict));
(void) [this]() { static_assert(__is_same(decltype(this), TC<int> *__restrict)); };

// By-value capture means 'this' is now a different object; do not
// make it __restrict.
(void) [*this]() { static_assert(__is_same(decltype(this), const TC<int> *)); };
(void) [*this]() mutable { static_assert(__is_same(decltype(this), TC<int> *)); };
};
}
};

void f() {
TC<int>{}.f();
}

namespace gh18121 {
struct Foo {
void member() __restrict {
Foo *__restrict This = this;
}
};
}

namespace gh42411 {
struct foo {
int v;
void f() const __restrict {
static_assert(__is_same(decltype((v)), const int&));
(void) [this]() { static_assert(__is_same(decltype((v)), const int&)); };
}
};
}

namespace gh82941 {
void f(int& x) {
(void)x;
}

class C {
int x;
void g() __restrict;
};

void C::g() __restrict {
f(this->x);
}
}