Skip to content

Commit 79ca26a

Browse files
committed
[Clang] Add noalias to this pointer in C++ constructors
1 parent 4e81ee4 commit 79ca26a

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2731,8 +2731,8 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
27312731
llvm::AttributeSet::get(getLLVMContext(), Attrs);
27322732
}
27332733

2734-
// Apply `nonnull`, `dereferenceable(N)` and `align N` to the `this` argument,
2735-
// unless this is a thunk function.
2734+
// Apply `nonnull`, `dereferenceable(N)`, `align N` (and `noalias` for
2735+
// constructors) to the `this` argument, unless this is a thunk function.
27362736
// FIXME: fix this properly, https://reviews.llvm.org/D100388
27372737
if (FI.isInstanceMethod() && !IRFunctionArgs.hasInallocaArg() &&
27382738
!FI.arg_begin()->type->isVoidPointerType() && !IsThunk) {
@@ -2744,6 +2744,11 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
27442744

27452745
QualType ThisTy = FI.arg_begin()->type.getTypePtr()->getPointeeType();
27462746

2747+
// According to [class.cdtor]/2, the value of the object is unspecified if
2748+
// its elements are accessed not through `this`.
2749+
if (isa_and_nonnull<CXXConstructorDecl>(TargetDecl))
2750+
Attrs.addAttribute(llvm::Attribute::NoAlias);
2751+
27472752
if (!CodeGenOpts.NullPointerIsValid &&
27482753
getTypes().getTargetAddressSpace(FI.arg_begin()->type) == 0) {
27492754
Attrs.addAttribute(llvm::Attribute::NonNull);

0 commit comments

Comments
 (0)