Commit 878a24ee authored by Elizabeth Andrews's avatar Elizabeth Andrews
Browse files

Reapply "Fix crash on switch conditions of non-integer types in templates"

This patch reapplies commit 75994846. 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.
parent f139ae3d
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
// RUN: %check_clang_tidy %s bugprone-string-integer-assignment %t
// RUN: %check_clang_tidy %s bugprone-string-integer-assignment %t -- -- -fno-delayed-template-parsing

namespace std {
template<typename T>
@@ -103,6 +103,8 @@ struct S {
  static constexpr T t = 0x8000;
  std::string s;
  void f(char c) { s += c | static_cast<int>(t); }
  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: an integer is interpreted as a chara
  // CHECK-FIXES: {{^}}  void f(char c) { s += std::to_string(c | static_cast<int>(t)); } 
};

template S<int>;
+1 −1
Original line number Diff line number Diff line
@@ -233,7 +233,7 @@ struct a {
template <class>
class d {
  a e;
  void f() { e.b(); }
  void f() { e.b(0); }
};
}  // namespace
}  // namespace PR38055
+9 −0
Original line number Diff line number Diff line
@@ -1678,6 +1678,15 @@ MemberExpr *MemberExpr::Create(
  MemberExpr *E = new (Mem) MemberExpr(Base, IsArrow, OperatorLoc, MemberDecl,
                                       NameInfo, T, VK, OK, NOUR);

  if (FieldDecl *Field = dyn_cast<FieldDecl>(MemberDecl)) {
    DeclContext *DC = MemberDecl->getDeclContext();
    // dyn_cast_or_null is used to handle objC variables which do not
    // have a declaration context.
    CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC);
    if (RD && RD->isDependentContext() && RD->isCurrentInstantiation(DC))
      E->setTypeDependent(T->isDependentType());
  }

  if (HasQualOrFound) {
    // FIXME: Wrong. We should be looking at the member declaration we found.
    if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
+2 −0
Original line number Diff line number Diff line
@@ -14706,6 +14706,8 @@ void Sema::RefersToMemberWithReducedAlignment(
  bool AnyIsPacked = false;
  do {
    QualType BaseType = ME->getBase()->getType();
    if (BaseType->isDependentType())
      return;
    if (ME->isArrow())
      BaseType = BaseType->getPointeeType();
    RecordDecl *RD = BaseType->castAs<RecordType>()->getDecl();
+2 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ namespace std {
[[nodiscard]] void *operator new(std::size_t, std::align_val_t, const std::nothrow_t&) noexcept;
[[nodiscard]] void *operator new[](std::size_t, const std::nothrow_t&) noexcept;
[[nodiscard]] void *operator new[](std::size_t, std::align_val_t, const std::nothrow_t&) noexcept;
[[nodiscard]] void *operator new[](std::size_t, std::align_val_t);
void operator delete(void*, const std::nothrow_t&) noexcept;
void operator delete(void*, std::align_val_t, const std::nothrow_t&) noexcept;
void operator delete[](void*, const std::nothrow_t&) noexcept;
@@ -1050,7 +1051,7 @@ namespace dynamic_alloc {
    // Ensure that we don't try to evaluate these for overflow and crash. These
    // are all value-dependent expressions.
    p = new char[n];
    p = new (n) char[n];
    p = new ((std::align_val_t)n) char[n];
    p = new char(n);
  }
}
Loading