Skip to content

Commit 7599484

Browse files
author
Melanie Blower
committed
Reapply "Fix crash on switch conditions of non-integer types in templates"
This patch reapplies commit 7694582. The first version broke buildbots due to clang-tidy test fails. The fails are because some errors in templates are now diagnosed earlier (does not wait till instantiation). I have modified the tests to add checks for these diagnostics/prevent these diagnostics. There are no additional code changes. Summary of code changes: Clang currently crashes for switch statements inside a template when the condition is a non-integer field member because contextual implicit conversion is skipped when parsing the condition. This conversion is however later checked in an assert when the case statement is handled. The conversion is skipped when parsing the condition because the field member is set as type-dependent based on its containing class. This patch sets the type dependency based on the field's type instead. This patch fixes Bug 40982. Reviewers: rnk, gribozavr2 Patch by: Elizabeth Andrews (eandrews) Differential revision: https://reviews.llvm.org/D69950
1 parent 7f92d66 commit 7599484

File tree

9 files changed

+32
-8
lines changed

9 files changed

+32
-8
lines changed

clang-tools-extra/test/clang-tidy/checkers/bugprone-string-integer-assignment.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ struct S {
103103
static constexpr T t = 0x8000;
104104
std::string s;
105105
void f(char c) { s += c | static_cast<int>(t); }
106+
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: an integer is interpreted as a chara
107+
// CHECK-FIXES: {{^}} void f(char c) { s += std::to_string(c | static_cast<int>(t)); }
106108
};
107109

108110
template S<int>;

clang-tools-extra/test/clang-tidy/checkers/misc-unused-parameters.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ struct a {
233233
template <class>
234234
class d {
235235
a e;
236-
void f() { e.b(); }
236+
void f() { e.b(0); }
237237
};
238238
} // namespace
239239
} // namespace PR38055

clang/lib/AST/Expr.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,15 @@ MemberExpr *MemberExpr::Create(
16751675
MemberExpr *E = new (Mem) MemberExpr(Base, IsArrow, OperatorLoc, MemberDecl,
16761676
NameInfo, T, VK, OK, NOUR);
16771677

1678+
if (FieldDecl *Field = dyn_cast<FieldDecl>(MemberDecl)) {
1679+
DeclContext *DC = MemberDecl->getDeclContext();
1680+
// dyn_cast_or_null is used to handle objC variables which do not
1681+
// have a declaration context.
1682+
CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC);
1683+
if (RD && RD->isDependentContext() && RD->isCurrentInstantiation(DC))
1684+
E->setTypeDependent(T->isDependentType());
1685+
}
1686+
16781687
if (HasQualOrFound) {
16791688
// FIXME: Wrong. We should be looking at the member declaration we found.
16801689
if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {

clang/lib/Sema/SemaChecking.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14682,6 +14682,8 @@ void Sema::RefersToMemberWithReducedAlignment(
1468214682
bool AnyIsPacked = false;
1468314683
do {
1468414684
QualType BaseType = ME->getBase()->getType();
14685+
if (BaseType->isDependentType())
14686+
return;
1468514687
if (ME->isArrow())
1468614688
BaseType = BaseType->getPointeeType();
1468714689
RecordDecl *RD = BaseType->castAs<RecordType>()->getDecl();

clang/test/SemaCXX/constant-expression-cxx2a.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace std {
1818
[[nodiscard]] void *operator new(std::size_t, std::align_val_t, const std::nothrow_t&) noexcept;
1919
[[nodiscard]] void *operator new[](std::size_t, const std::nothrow_t&) noexcept;
2020
[[nodiscard]] void *operator new[](std::size_t, std::align_val_t, const std::nothrow_t&) noexcept;
21+
[[nodiscard]] void *operator new[](std::size_t, std::align_val_t);
2122
void operator delete(void*, const std::nothrow_t&) noexcept;
2223
void operator delete(void*, std::align_val_t, const std::nothrow_t&) noexcept;
2324
void operator delete[](void*, const std::nothrow_t&) noexcept;
@@ -1050,7 +1051,7 @@ namespace dynamic_alloc {
10501051
// Ensure that we don't try to evaluate these for overflow and crash. These
10511052
// are all value-dependent expressions.
10521053
p = new char[n];
1053-
p = new (n) char[n];
1054+
p = new ((std::align_val_t)n) char[n];
10541055
p = new char(n);
10551056
}
10561057
}

clang/test/SemaTemplate/dependent-names.cpp

Lines changed: 0 additions & 3 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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
}
Lines changed: 14 additions & 0 deletions
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)