]> granicus.if.org Git - clang/commitdiff
Add a new flag and attributes to control static destructor registration
authorErik Pilkington <erik.pilkington@gmail.com>
Tue, 21 Aug 2018 17:24:06 +0000 (17:24 +0000)
committerErik Pilkington <erik.pilkington@gmail.com>
Tue, 21 Aug 2018 17:24:06 +0000 (17:24 +0000)
This commit adds the flag -fno-c++-static-destructors and the attributes
[[clang::no_destroy]] and [[clang::always_destroy]]. no_destroy specifies that a
specific static or thread duration variable shouldn't have it's destructor
registered, and is the default in -fno-c++-static-destructors mode.
always_destroy is the opposite, and is the default in -fc++-static-destructors
mode.

A variable whose destructor is disabled (either because of
-fno-c++-static-destructors or [[clang::no_destroy]]) doesn't count as a use of
the destructor, so we don't do any access checking or mark it referenced. We
also don't emit -Wexit-time-destructors for these variables.

rdar://21734598

Differential revision: https://reviews.llvm.org/D50994

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@340306 91177308-0d34-0410-b5e6-96231b3b80d8

18 files changed:
include/clang/AST/Decl.h
include/clang/Basic/Attr.td
include/clang/Basic/AttrDocs.td
include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Basic/LangOptions.def
include/clang/Driver/Options.td
lib/AST/Decl.cpp
lib/CodeGen/ItaniumCXXABI.cpp
lib/CodeGen/MicrosoftCXXABI.cpp
lib/Driver/ToolChains/Clang.cpp
lib/Frontend/CompilerInvocation.cpp
lib/Sema/SemaDeclAttr.cpp
lib/Sema/SemaDeclCXX.cpp
test/CodeGenCXX/always_destroy.cpp [new file with mode: 0644]
test/CodeGenCXX/no_destroy.cpp [new file with mode: 0644]
test/Misc/pragma-attribute-supported-attributes-list.test
test/SemaCXX/no_destroy.cpp [new file with mode: 0644]
test/SemaCXX/warn-exit-time-destructors.cpp

index 0c910e0ffbf8b5316acf4d6cb006e88e6a7da532..35978c155db719562cdd3d67eb86e6877e96c24c 100644 (file)
@@ -1469,6 +1469,9 @@ public:
   // has no definition within this source file.
   bool isKnownToBeDefined() const;
 
+  /// Do we need to emit an exit-time destructor for this variable?
+  bool isNoDestroy(const ASTContext &) const;
+
   // Implement isa/cast/dyncast/etc.
   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   static bool classofKind(Kind K) { return K >= firstVar && K <= lastVar; }
index 6d3c271c5b37ddf49acdcf451dbedabc65cb718a..e4377f2b7a4befb695e165b2a5aba56ddbf387e3 100644 (file)
@@ -3009,3 +3009,15 @@ def Reinitializes : InheritableAttr {
   let Subjects = SubjectList<[NonStaticNonConstCXXMethod], ErrorDiag>;
   let Documentation = [ReinitializesDocs];
 }
+
+def NoDestroy : InheritableAttr {
+  let Spellings = [Clang<"no_destroy", 0>];
+  let Subjects = SubjectList<[Var]>;
+  let Documentation = [NoDestroyDocs];
+}
+
+def AlwaysDestroy : InheritableAttr {
+  let Spellings = [Clang<"always_destroy", 0>];
+  let Subjects = SubjectList<[Var]>;
+  let Documentation = [AlwaysDestroyDocs];
+}
index 27334b535ad6fb37d888db54d638924dcad283c9..e9598df8b55c9803c3019e01db6cc13d03e3cc7d 100644 (file)
@@ -3486,3 +3486,22 @@ a container class:
   };
   }];
 }
+
+def AlwaysDestroyDocs : Documentation {
+  let Category = DocCatVariable;
+  let Content = [{
+The ``always_destroy`` attribute specifies that a variable with static or thread
+storage duration should have its exit-time destructor run. This attribute is the
+default unless clang was invoked with -fno-c++-static-destructors.
+  }];
+}
+
+def NoDestroyDocs : Documentation {
+  let Category = DocCatVariable;
+  let Content = [{
+The ``no_destroy`` attribute specifies that a variable with static or thread
+storage duration shouldn't have its exit-time destructor run. Annotating every
+static and thread duration variable with this attribute is equivalent to
+invoking clang with -fno-c++-static-destructors.
+  }];
+}
index 94cf03e19ee384feac0fc509725a1ff719edf262..2f1bd94614dcb421777d0f64b0bae5fb1d250192 100644 (file)
@@ -1809,6 +1809,10 @@ def err_destructor_expr_type_mismatch : Error<
 def note_destructor_type_here : Note<
   "type %0 is declared here">;
 
+def err_destroy_attr_on_non_static_var : Error<
+  "%select{no_destroy|always_destroy}0 attribute can only be applied to a"
+  " variable with static or thread storage duration">;
+
 def err_destructor_template : Error<
   "destructor cannot be declared as a template">;
 
index fc38af5b03f6ee6ec9119dd365d3cc3740bb4ba3..8c2865b3e4912e450ae5ba1797956b9e74cc3494 100644 (file)
@@ -308,6 +308,8 @@ LANGOPT(FixedPoint, 1, 0, "fixed point types")
 LANGOPT(PaddingOnUnsignedFixedPoint, 1, 0,
         "unsigned fixed point types having one extra padding bit")
 
+LANGOPT(RegisterStaticDestructors, 1, 1, "Register C++ static destructors")
+
 #undef LANGOPT
 #undef COMPATIBLE_LANGOPT
 #undef BENIGN_LANGOPT
index 2dbf7c52eb3bd012370c74e31c19c4831b1bf9ab..6e45f2aef6390520bf733fcc18e62af2be39e676 100644 (file)
@@ -898,6 +898,13 @@ def ffixed_point : Flag<["-"], "ffixed-point">, Group<f_Group>,
                    Flags<[CC1Option]>, HelpText<"Enable fixed point types">;
 def fno_fixed_point : Flag<["-"], "fno-fixed-point">, Group<f_Group>,
                       HelpText<"Disable fixed point types">;
+def fcxx_static_destructors : Flag<["-"], "fc++-static-destructors">,
+  Group<f_Group>,
+  HelpText<"Enable C++ static destructor registration (the default)">;
+def fno_cxx_static_destructors : Flag<["-"], "fno-c++-static-destructors">,
+  Group<f_Group>,
+  Flags<[CC1Option]>,
+  HelpText<"Disable C++ static destructor registration">;
 
 // Begin sanitizer flags. These should all be core options exposed in all driver
 // modes.
index 8ce960ff5ffc813fc9c3be3e9a182bdf71f0003f..b6d35f4260c73844be18c415ab213a6bf5d5a05e 100644 (file)
@@ -2449,6 +2449,12 @@ bool VarDecl::isKnownToBeDefined() const {
   return hasDefinition();
 }
 
+bool VarDecl::isNoDestroy(const ASTContext &Ctx) const {
+  return hasGlobalStorage() && (hasAttr<NoDestroyAttr>() ||
+                                (!Ctx.getLangOpts().RegisterStaticDestructors &&
+                                 !hasAttr<AlwaysDestroyAttr>()));
+}
+
 MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
   if (isStaticDataMember())
     // FIXME: Remove ?
index 5b430741ed9e26006f1a95ea5764eaa45180a707..b2a8f5a945f1fbc26cb6accfc5e95b407433ab78 100644 (file)
@@ -2342,6 +2342,9 @@ void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
                                        const VarDecl &D,
                                        llvm::Constant *dtor,
                                        llvm::Constant *addr) {
+  if (D.isNoDestroy(CGM.getContext()))
+    return;
+
   // Use __cxa_atexit if available.
   if (CGM.getCodeGenOpts().CXAAtExit)
     return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind());
index 059adb78ca302e89da322492e04463654bb40ad3..968c171c71b6111ac537c36d4b29ee9c6ec70ab7 100644 (file)
@@ -2240,6 +2240,9 @@ static void emitGlobalDtorWithTLRegDtor(CodeGenFunction &CGF, const VarDecl &VD,
 void MicrosoftCXXABI::registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
                                          llvm::Constant *Dtor,
                                          llvm::Constant *Addr) {
+  if (D.isNoDestroy(CGM.getContext()))
+    return;
+
   if (D.getTLSKind())
     return emitGlobalDtorWithTLRegDtor(CGF, D, Dtor, Addr);
 
index 68ad03d7ef19137859c520781c8b4ba5f3f7437a..135d40fc7008f68d23ea769bc865605f1530aebc 100644 (file)
@@ -4832,6 +4832,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
                    options::OPT_fno_complete_member_pointers, false))
     CmdArgs.push_back("-fcomplete-member-pointers");
 
+  if (!Args.hasFlag(options::OPT_fcxx_static_destructors,
+                    options::OPT_fno_cxx_static_destructors, true))
+    CmdArgs.push_back("-fno-c++-static-destructors");
+
   if (Arg *A = Args.getLastArg(options::OPT_moutline,
                                options::OPT_mno_outline)) {
     if (A->getOption().matches(options::OPT_moutline)) {
index ded2d421995e632c7449a801b59d44874a81cc6f..74476bbe5e307fe9dbb792f718808b791916e509 100644 (file)
@@ -2772,6 +2772,8 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
   // -fallow-editor-placeholders
   Opts.AllowEditorPlaceholders = Args.hasArg(OPT_fallow_editor_placeholders);
 
+  Opts.RegisterStaticDestructors = !Args.hasArg(OPT_fno_cxx_static_destructors);
+
   if (Arg *A = Args.getLastArg(OPT_fclang_abi_compat_EQ)) {
     Opts.setClangABICompat(LangOptions::ClangABI::Latest);
 
index 39d7019b259a434d442f7180eefbddfed4a446d4..27eb533360cc7d76287e60c8b0222fbf940843af 100644 (file)
@@ -5917,6 +5917,20 @@ static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
       AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
 }
 
+static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) {
+  if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->hasGlobalStorage()) {
+    S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var)
+        << (A.getKind() == ParsedAttr::AT_AlwaysDestroy);
+    return;
+  }
+
+  if (A.getKind() == ParsedAttr::AT_AlwaysDestroy) {
+    handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A);
+  } else {
+    handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A);
+  }
+}
+
 //===----------------------------------------------------------------------===//
 // Top Level Sema Entry Points
 //===----------------------------------------------------------------------===//
@@ -6587,6 +6601,11 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
   case ParsedAttr::AT_Reinitializes:
     handleSimpleAttribute<ReinitializesAttr>(S, D, AL);
     break;
+
+  case ParsedAttr::AT_AlwaysDestroy:
+  case ParsedAttr::AT_NoDestroy:
+    handleDestroyAttr(S, D, AL);
+    break;
   }
 }
 
index 3d8da1f797b4858f3b43ca1bd09fbd107b696045..61a7730d9b0aee66a7b90cd31b96c8a53b823187 100644 (file)
@@ -12910,6 +12910,9 @@ void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) {
   if (ClassDecl->hasIrrelevantDestructor()) return;
   if (ClassDecl->isDependentContext()) return;
 
+  if (VD->isNoDestroy(getASTContext()))
+    return;
+
   CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl);
   MarkFunctionReferenced(VD->getLocation(), Destructor);
   CheckDestructorAccess(VD->getLocation(), Destructor,
diff --git a/test/CodeGenCXX/always_destroy.cpp b/test/CodeGenCXX/always_destroy.cpp
new file mode 100644 (file)
index 0000000..e84c4cf
--- /dev/null
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 %s -fno-c++-static-destructors -emit-llvm -triple x86_64-apple-macosx10.13.0 -o - | FileCheck %s
+
+struct NonTrivial {
+  ~NonTrivial();
+};
+
+// CHECK-NOT: __cxa_atexit{{.*}}_ZN10NonTrivialD1Ev
+NonTrivial nt1;
+// CHECK-NOT: _tlv_atexit{{.*}}_ZN10NonTrivialD1Ev
+thread_local NonTrivial nt2;
+
+struct NonTrivial2 {
+  ~NonTrivial2();
+};
+
+// CHECK: __cxa_atexit{{.*}}_ZN11NonTrivial2D1Ev
+[[clang::always_destroy]] NonTrivial2 nt21;
+// CHECK: _tlv_atexit{{.*}}_ZN11NonTrivial2D1Ev
+[[clang::always_destroy]] thread_local NonTrivial2 nt22;
+
+void f() {
+  // CHECK: __cxa_atexit{{.*}}_ZN11NonTrivial2D1Ev
+  [[clang::always_destroy]] static NonTrivial2 nt21;
+  // CHECK: _tlv_atexit{{.*}}_ZN11NonTrivial2D1Ev
+  [[clang::always_destroy]] thread_local NonTrivial2 nt22;
+}
+
+// CHECK-NOT: __cxa_atexit{{.*}}_ZN10NonTrivialD1Ev
+[[clang::no_destroy]] NonTrivial nt3;
+// CHECK-NOT: _tlv_atexit{{.*}}_ZN10NonTrivialD1Ev
+[[clang::no_destroy]] thread_local NonTrivial nt4;
diff --git a/test/CodeGenCXX/no_destroy.cpp b/test/CodeGenCXX/no_destroy.cpp
new file mode 100644 (file)
index 0000000..93e6ce1
--- /dev/null
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 %s -emit-llvm -triple x86_64-apple-macosx10.13.0 -o - | FileCheck %s
+
+struct NonTrivial {
+  ~NonTrivial();
+};
+
+// CHECK-NOT: __cxa_atexit{{.*}}_ZN10NonTrivialD1Ev
+[[clang::no_destroy]] NonTrivial nt1;
+// CHECK-NOT: _tlv_atexit{{.*}}_ZN10NonTrivialD1Ev
+[[clang::no_destroy]] thread_local NonTrivial nt2;
+
+struct NonTrivial2 {
+  ~NonTrivial2();
+};
+
+// CHECK: __cxa_atexit{{.*}}_ZN11NonTrivial2D1Ev
+NonTrivial2 nt21;
+// CHECK: _tlv_atexit{{.*}}_ZN11NonTrivial2D1Ev
+thread_local NonTrivial2 nt22;
+
+void f() {
+  // CHECK: __cxa_atexit{{.*}}_ZN11NonTrivial2D1Ev
+  static NonTrivial2 nt21;
+  // CHECK: _tlv_atexit{{.*}}_ZN11NonTrivial2D1Ev
+  thread_local NonTrivial2 nt22;
+}
+
+// CHECK: __cxa_atexit{{.*}}_ZN10NonTrivialD1Ev
+[[clang::always_destroy]] NonTrivial nt3;
+// CHECK: _tlv_atexit{{.*}}_ZN10NonTrivialD1Ev
+[[clang::always_destroy]] thread_local NonTrivial nt4;
index 4a3846be8d9bac2e1cddd51269637c93d8a34814..d2c1fe6d27b27db08daf83c7b7339e7cba5a60a5 100644 (file)
@@ -2,7 +2,7 @@
 
 // The number of supported attributes should never go down!
 
-// CHECK: #pragma clang attribute supports 72 attributes:
+// CHECK: #pragma clang attribute supports 74 attributes:
 // CHECK-NEXT: AMDGPUFlatWorkGroupSize (SubjectMatchRule_function)
 // CHECK-NEXT: AMDGPUNumSGPR (SubjectMatchRule_function)
 // CHECK-NEXT: AMDGPUNumVGPR (SubjectMatchRule_function)
@@ -11,6 +11,7 @@
 // CHECK-NEXT: AbiTag (SubjectMatchRule_record_not_is_union, SubjectMatchRule_variable, SubjectMatchRule_function, SubjectMatchRule_namespace)
 // CHECK-NEXT: AlignValue (SubjectMatchRule_variable, SubjectMatchRule_type_alias)
 // CHECK-NEXT: AllocSize (SubjectMatchRule_function)
+// CHECK-NEXT: AlwaysDestroy (SubjectMatchRule_variable)
 // CHECK-NEXT: Annotate ()
 // CHECK-NEXT: AnyX86NoCfCheck (SubjectMatchRule_hasType_functionType)
 // CHECK-NEXT: AssumeAligned (SubjectMatchRule_objc_method, SubjectMatchRule_function)
@@ -38,6 +39,7 @@
 // CHECK-NEXT: MipsLongCall (SubjectMatchRule_function)
 // CHECK-NEXT: MipsShortCall (SubjectMatchRule_function)
 // CHECK-NEXT: NoDebug (SubjectMatchRule_hasType_functionType, SubjectMatchRule_objc_method, SubjectMatchRule_variable_not_is_parameter)
+// CHECK-NEXT: NoDestroy (SubjectMatchRule_variable)
 // CHECK-NEXT: NoDuplicate (SubjectMatchRule_function)
 // CHECK-NEXT: NoEscape (SubjectMatchRule_variable_is_parameter)
 // CHECK-NEXT: NoMicroMips (SubjectMatchRule_function)
diff --git a/test/SemaCXX/no_destroy.cpp b/test/SemaCXX/no_destroy.cpp
new file mode 100644 (file)
index 0000000..bdb8077
--- /dev/null
@@ -0,0 +1,40 @@
+// RUN: %clang_cc1 -DNO_DTORS -fno-c++-static-destructors -verify %s
+// RUN: %clang_cc1 -verify %s
+
+struct SecretDestructor {
+#ifndef NO_DTORS
+  // expected-note@+2 4 {{private}}
+#endif
+private: ~SecretDestructor(); // expected-note 2 {{private}}
+};
+
+SecretDestructor sd1;
+thread_local SecretDestructor sd2;
+void locals() {
+  static SecretDestructor sd3;
+  thread_local SecretDestructor sd4;
+}
+
+#ifndef NO_DTORS
+// SecretDestructor sd1;                  // expected-error@-8 {{private}}
+// thread_local SecretDestructor sd2;     // expected-error@-8 {{private}}
+// void locals() {
+//   static SecretDestructor sd3;         // expected-error@-8 {{private}}
+//   thread_local SecretDestructor sd4;   // expected-error@-8 {{private}}
+// }
+#endif
+
+[[clang::always_destroy]] SecretDestructor sd6; // expected-error{{private}}
+[[clang::always_destroy]] thread_local SecretDestructor sd7; // expected-error{{private}}
+
+[[clang::no_destroy]] SecretDestructor sd8;
+
+int main() {
+  [[clang::no_destroy]] int p; // expected-error{{no_destroy attribute can only be applied to a variable with static or thread storage duration}}
+  [[clang::always_destroy]] int p2; // expected-error{{always_destroy attribute can only be applied to a variable with static or thread storage duration}}
+  [[clang::no_destroy]] static int p3;
+  [[clang::always_destroy]] static int p4;
+}
+
+[[clang::always_destroy]] [[clang::no_destroy]] int p; // expected-error{{'no_destroy' and 'always_destroy' attributes are not compatible}} // expected-note{{here}}
+[[clang::no_destroy]] [[clang::always_destroy]] int p2; // expected-error{{'always_destroy' and 'no_destroy' attributes are not compatible}} // expected-note{{here}}
index 124576aa95b6d74d690d96da712d77739b3e0d7b..17b3a9406369fa9975f5622219c0cf5dc75b0639 100644 (file)
@@ -43,3 +43,8 @@ namespace test3 {
   };
   E e;
 }
+
+namespace test4 {
+struct A { ~A(); };
+[[clang::no_destroy]] A a; // no warning
+}