<div dir="ltr">You can't know that all compilers will optimize this away, can you?</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 22, 2015 at 1:38 AM, Yury Gribov <span dir="ltr"><<a href="mailto:tetra2005@gmail.com" target="_blank">tetra2005@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi rsmith, eli.friedman,<br>
<br>
This patch liberalizes -Wglobal-constructors to not emit warning if constructor is likely to be optimized away. This covers important LinkerInitialized idiom used e.g. in libsanitizer and Chromium:<br>
  enum LinkerInitialized { LINKER_INITIALIZED };<br>
  class Mutex {<br>
  public:<br>
    inline Mutex(base::LinkerInitialized) {}<br>
  };<br>
  Mutex mu(LINKER_INITIALIZED);  // Will be optimized away<br>
No regressions in check-all.<br>
<br>
<a href="http://reviews.llvm.org/D7122" target="_blank">http://reviews.llvm.org/D7122</a><br>
<br>
Files:<br>
  lib/Sema/SemaDecl.cpp<br>
  test/SemaCXX/warn-global-constructors.cpp<br>
<br>
Index: lib/Sema/SemaDecl.cpp<br>
===================================================================<br>
--- lib/Sema/SemaDecl.cpp<br>
+++ lib/Sema/SemaDecl.cpp<br>
@@ -9523,18 +9523,34 @@<br>
<br>
   if (!var->getDeclContext()->isDependentContext() &&<br>
       Init && !Init->isValueDependent()) {<br>
-    if (IsGlobal && !var->isConstexpr() &&<br>
-        !getDiagnostics().isIgnored(diag::warn_global_constructor,<br>
-                                    var->getLocation())) {<br>
+    do {<br>
+      if (!IsGlobal || var->isConstexpr() ||<br>
+          getDiagnostics().isIgnored(diag::warn_global_constructor,<br>
+                                      var->getLocation()))<br>
+        break;<br>
+<br>
       // Warn about globals which don't have a constant initializer.  Don't<br>
       // warn about globals with a non-trivial destructor because we already<br>
       // warned about them.<br>
       CXXRecordDecl *RD = baseType->getAsCXXRecordDecl();<br>
-      if (!(RD && !RD->hasTrivialDestructor()) &&<br>
-          !Init->isConstantInitializer(Context, baseType->isReferenceType()))<br>
-        Diag(var->getLocation(), diag::warn_global_constructor)<br>
-          << Init->getSourceRange();<br>
-    }<br>
+      if ((RD && !RD->hasTrivialDestructor()) ||<br>
+          Init->isConstantInitializer(Context, baseType->isReferenceType()))<br>
+        break;<br>
+<br>
+      CXXConstructExpr *CtorInit = dyn_cast_or_null<CXXConstructExpr>(Init);<br>
+      if (CtorInit) {<br>
+        // Do not warn if constructor will likely generate no code.<br>
+        CXXConstructorDecl *CtorDecl = CtorInit->getConstructor();<br>
+        Stmt *CtorBody = CtorDecl->getBody();<br>
+        bool HasEmptyBody = CtorBody && CtorDecl->isDefined() && cast<CompoundStmt>(CtorBody)->body_empty();<br>
+        bool HasNoInitializers = !CtorDecl->getNumCtorInitializers();<br>
+        if (HasEmptyBody && HasNoInitializers)<br>
+          break;<br>
+      }<br>
+<br>
+      Diag(var->getLocation(), diag::warn_global_constructor)<br>
+        << Init->getSourceRange();<br>
+    } while(0);<br>
<br>
     if (var->isConstexpr()) {<br>
       SmallVector<PartialDiagnosticAt, 8> Notes;<br>
Index: test/SemaCXX/warn-global-constructors.cpp<br>
===================================================================<br>
--- test/SemaCXX/warn-global-constructors.cpp<br>
+++ test/SemaCXX/warn-global-constructors.cpp<br>
@@ -121,6 +121,18 @@<br>
   E e;<br>
 }<br>
<br>
+namespace LinkerInitialized {<br>
+  enum LinkerInitialized { LINKER_INITIALIZED = 0 };<br>
+<br>
+  struct BlockingMutex {<br>
+    BlockingMutex(LinkerInitialized) {}<br>
+    BlockingMutex() {}<br>
+    char owner_;<br>
+  };<br>
+<br>
+  BlockingMutex mu(LINKER_INITIALIZED);  // elided<br>
+}<br>
+<br>
 namespace pr20420 {<br>
 // No warning is expected. This used to crash.<br>
 void *array_storage[1];<br>
<br>
EMAIL PREFERENCES<br>
  <a href="http://reviews.llvm.org/settings/panel/emailpreferences/" target="_blank">http://reviews.llvm.org/settings/panel/emailpreferences/</a><br>
<br>_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
<br></blockquote></div><br></div>