Skip to content

Commit e0e07a7

Browse files
committed
Fix detection of __attribute__((may_alias)) to properly look through
type sugar. We previously missed the attribute in a lot of cases in C++, because there's often other type sugar there (eg, ElaboratedType).
1 parent cdf5cfe commit e0e07a7

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

clang/lib/CodeGen/CodeGenTBAA.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,18 @@ llvm::MDNode *CodeGenTBAA::getChar() {
7878

7979
static bool TypeHasMayAlias(QualType QTy) {
8080
// Tagged types have declarations, and therefore may have attributes.
81-
if (const TagType *TTy = dyn_cast<TagType>(QTy))
82-
return TTy->getDecl()->hasAttr<MayAliasAttr>();
81+
if (auto *TD = QTy->getAsTagDecl())
82+
if (TD->hasAttr<MayAliasAttr>())
83+
return true;
8384

84-
// Typedef types have declarations, and therefore may have attributes.
85-
if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
86-
if (TTy->getDecl()->hasAttr<MayAliasAttr>())
85+
// Also look for may_alias as a declaration attribute on a typedef.
86+
// FIXME: We should follow GCC and model may_alias as a type attribute
87+
// rather than as a declaration attribute.
88+
while (auto *TT = QTy->getAs<TypedefType>()) {
89+
if (TT->getDecl()->hasAttr<MayAliasAttr>())
8790
return true;
88-
// Also, their underlying types may have relevant attributes.
89-
return TypeHasMayAlias(TTy->desugar());
91+
QTy = TT->desugar();
9092
}
91-
9293
return false;
9394
}
9495

clang/test/CodeGenCXX/may_alias.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clang_cc1 %s -triple %itanium_abi_triple -emit-llvm -O2 -disable-llvm-passes -o - | FileCheck %s
2+
// RUN: %clang_cc1 %s -triple %ms_abi_triple -emit-llvm -O2 -disable-llvm-passes -o - | FileCheck %s
3+
4+
enum class __attribute__((may_alias)) E {};
5+
6+
template<typename T> struct A {
7+
using B __attribute__((may_alias)) = enum {};
8+
};
9+
10+
template<typename T> using Alias = typename A<T>::B;
11+
12+
// CHECK-LABEL: define {{.*}}foo
13+
// CHECK: load i{{[0-9]*}}, {{.*}}, !tbaa ![[MAY_ALIAS:[^ ,]*]]
14+
auto foo(E &r) { return r; }
15+
16+
// CHECK-LABEL: define {{.*}}goo
17+
// CHECK: load i{{[0-9]*}}, {{.*}}, !tbaa ![[MAY_ALIAS]]
18+
auto goo(A<int>::B &r) { return r; }
19+
20+
// CHECK-LABEL: define {{.*}}hoo
21+
// CHECK: load i{{[0-9]*}}, {{.*}}, !tbaa ![[MAY_ALIAS]]
22+
auto hoo(Alias<int> &r) { return r; }
23+
24+
// CHECK: ![[CHAR:.*]] = !{!"omnipotent char", !{{.*}}, i64 0}
25+
// CHECK: ![[MAY_ALIAS]] = !{![[CHAR]], ![[CHAR]], i64 0}

0 commit comments

Comments
 (0)