Skip to content

Commit 7694582

Browse files
Fix crash on switch conditions of non-integer types in templates
Clang currently crashes for switch statements inside a template when the condition is a non-integer field. The crash is due to incorrect type-dependency of field. Type-dependency of member expressions is currently set based on the containing class. This patch changes this for 'members of the current instantiation' to set the type dependency based on the member's type instead. A few lit tests started to fail once I applied this patch because errors are now diagnosed earlier (does not wait till instantiation). I've modified these tests in this patch as well. Patch fixes PR#40982 Differential Revision: https://reviews.llvm.org/D61027 llvm-svn: 368706
1 parent 0a04a06 commit 7694582

File tree

6 files changed

+27
-6
lines changed

6 files changed

+27
-6
lines changed

clang/lib/AST/Expr.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,15 @@ MemberExpr *MemberExpr::Create(
16691669
MemberExpr *E = new (Mem) MemberExpr(Base, IsArrow, OperatorLoc, MemberDecl,
16701670
NameInfo, T, VK, OK, NOUR);
16711671

1672+
if (FieldDecl *Field = dyn_cast<FieldDecl>(MemberDecl)) {
1673+
DeclContext *DC = MemberDecl->getDeclContext();
1674+
// dyn_cast_or_null is used to handle objC variables which do not
1675+
// have a declaration context.
1676+
CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC);
1677+
if (RD && RD->isDependentContext() && RD->isCurrentInstantiation(DC))
1678+
E->setTypeDependent(T->isDependentType());
1679+
}
1680+
16721681
if (HasQualOrFound) {
16731682
// FIXME: Wrong. We should be looking at the member declaration we found.
16741683
if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {

clang/lib/Sema/SemaChecking.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -14288,6 +14288,8 @@ void Sema::RefersToMemberWithReducedAlignment(
1428814288
bool AnyIsPacked = false;
1428914289
do {
1429014290
QualType BaseType = ME->getBase()->getType();
14291+
if (BaseType->isDependentType())
14292+
return;
1429114293
if (ME->isArrow())
1429214294
BaseType = BaseType->getPointeeType();
1429314295
RecordDecl *RD = BaseType->getAs<RecordType>()->getDecl();

clang/test/SemaTemplate/dependent-names.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,6 @@ namespace PR10187 {
273273
}
274274
int e[10];
275275
};
276-
void g() {
277-
S<int>().f(); // expected-note {{here}}
278-
}
279276
}
280277

281278
namespace A2 {

clang/test/SemaTemplate/enum-argument.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// RUN: %clang_cc1 -fsyntax-only -verify %s
2-
// expected-no-diagnostics
32

43
enum Enum { val = 1 };
54
template <Enum v> struct C {
@@ -31,7 +30,7 @@ namespace rdar8020920 {
3130
unsigned long long bitfield : e0;
3231

3332
void f(int j) {
34-
bitfield + j;
33+
bitfield + j; // expected-warning {{expression result unused}}
3534
}
3635
};
3736
}

clang/test/SemaTemplate/member-access-expr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ namespace test6 {
156156
void get(B **ptr) {
157157
// It's okay if at some point we figure out how to diagnose this
158158
// at instantiation time.
159-
*ptr = field;
159+
*ptr = field; // expected-error {{assigning to 'test6::B *' from incompatible type 'test6::A *}}
160160
}
161161
};
162162
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s
2+
3+
struct NOT_AN_INTEGRAL_TYPE {};
4+
5+
template <typename T>
6+
struct foo {
7+
NOT_AN_INTEGRAL_TYPE Bad;
8+
void run() {
9+
switch (Bad) { // expected-error {{statement requires expression of integer type ('NOT_AN_INTEGRAL_TYPE' invalid)}}
10+
case 0:
11+
break;
12+
}
13+
}
14+
};

0 commit comments

Comments
 (0)