Skip to content

Commit

Permalink
[Sema] Avoid emitting warnings for constant destruction.
Browse files Browse the repository at this point in the history
Fixes #62436.

Reviewed By: rsmith

Differential Revision: https://reviews.llvm.org/D149713
  • Loading branch information
pkasting authored and xgupta committed May 6, 2023
1 parent e8e8707 commit dd6a58b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15732,7 +15732,8 @@ void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) {
}
}

if (!VD->hasGlobalStorage()) return;
if (!VD->hasGlobalStorage() || !VD->needsDestruction(Context))
return;

// Emit warning for non-trivial dtor in global scope (a real global,
// class-static, function-static).
Expand Down
22 changes: 21 additions & 1 deletion clang/test/SemaCXX/warn-exit-time-destructors.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wexit-time-destructors %s -verify
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wexit-time-destructors %s -verify=expected,cxx11
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wexit-time-destructors %s -verify=expected

namespace test1 {
struct A { ~A(); };
Expand Down Expand Up @@ -48,3 +49,22 @@ namespace test4 {
struct A { ~A(); };
[[clang::no_destroy]] A a; // no warning
}

namespace test5 {
#if __cplusplus >= 202002L
#define CPP20_CONSTEXPR constexpr
#else
#define CPP20_CONSTEXPR
#endif
struct S {
CPP20_CONSTEXPR ~S() {}
};
S s; // cxx11-warning {{exit-time destructor}}

struct T {
CPP20_CONSTEXPR ~T() { if (b) {} }
bool b;
};
T t; // expected-warning {{exit-time destructor}}
#undef CPP20_CONSTEXPR
}
22 changes: 21 additions & 1 deletion clang/test/SemaCXX/warn-global-constructors.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s -verify
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s -verify=expected,cxx11
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wglobal-constructors %s -verify=expected

int opaque_int();

Expand Down Expand Up @@ -145,3 +146,22 @@ namespace bitfields {
const HasUnnamedBitfield nonConstexprConst{1}; // expected-warning {{global constructor}}
HasUnnamedBitfield nonConstexprMutable{1}; // expected-warning {{global constructor}}
}

namespace test7 {
#if __cplusplus >= 202002L
#define CPP20_CONSTEXPR constexpr
#else
#define CPP20_CONSTEXPR
#endif
struct S {
CPP20_CONSTEXPR ~S() {}
};
S s; // cxx11-warning {{global destructor}}

struct T {
CPP20_CONSTEXPR ~T() { if (b) {} }
bool b;
};
T t; // expected-warning {{global destructor}}
#undef CPP20_CONSTEXPR
}

0 comments on commit dd6a58b

Please sign in to comment.