Skip to content

Commit 878a24e

Browse files
Reapply "Fix crash on switch conditions of non-integer types in templates"
This patch reapplies commit 7599484. Patch was reverted due to a clang-tidy test fail on Windows. The test has been modified. There are no additional code changes. Patch was tested with ninja check-all on Windows and Linux. 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.
1 parent f139ae3 commit 878a24e

File tree

9 files changed

+33
-9
lines changed

9 files changed

+33
-9
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %check_clang_tidy %s bugprone-string-integer-assignment %t
1+
// RUN: %check_clang_tidy %s bugprone-string-integer-assignment %t -- -- -fno-delayed-template-parsing
22

33
namespace std {
44
template<typename T>
@@ -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
@@ -1678,6 +1678,15 @@ MemberExpr *MemberExpr::Create(
16781678
MemberExpr *E = new (Mem) MemberExpr(Base, IsArrow, OperatorLoc, MemberDecl,
16791679
NameInfo, T, VK, OK, NOUR);
16801680

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

clang/lib/Sema/SemaChecking.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14706,6 +14706,8 @@ void Sema::RefersToMemberWithReducedAlignment(
1470614706
bool AnyIsPacked = false;
1470714707
do {
1470814708
QualType BaseType = ME->getBase()->getType();
14709+
if (BaseType->isDependentType())
14710+
return;
1470914711
if (ME->isArrow())
1471014712
BaseType = BaseType->getPointeeType();
1471114713
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)