From dd6a58babc853d180b9a7e5bd709ae12477da004 Mon Sep 17 00:00:00 2001 From: Peter Kasting Date: Sat, 6 May 2023 20:12:14 +0530 Subject: [PATCH] [Sema] Avoid emitting warnings for constant destruction. Fixes https://github.com/llvm/llvm-project/issues/62436. Reviewed By: rsmith Differential Revision: https://reviews.llvm.org/D149713 --- clang/lib/Sema/SemaDeclCXX.cpp | 3 ++- .../SemaCXX/warn-exit-time-destructors.cpp | 22 ++++++++++++++++++- .../test/SemaCXX/warn-global-constructors.cpp | 22 ++++++++++++++++++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index dcb3e676b0ba48..31936bce786213 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -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). diff --git a/clang/test/SemaCXX/warn-exit-time-destructors.cpp b/clang/test/SemaCXX/warn-exit-time-destructors.cpp index 17b3a9406369fa..2f14243cb48c47 100644 --- a/clang/test/SemaCXX/warn-exit-time-destructors.cpp +++ b/clang/test/SemaCXX/warn-exit-time-destructors.cpp @@ -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(); }; @@ -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 +} diff --git a/clang/test/SemaCXX/warn-global-constructors.cpp b/clang/test/SemaCXX/warn-global-constructors.cpp index 430f239a3ed7e1..765654b5e9f4fe 100644 --- a/clang/test/SemaCXX/warn-global-constructors.cpp +++ b/clang/test/SemaCXX/warn-global-constructors.cpp @@ -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(); @@ -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 +}