juliehockett updated this revision to Diff 128806.
juliehockett marked 3 inline comments as done.
juliehockett added a comment.

1. Narrowing check to only warn if the declaration is a global non-trivial 
object with explicit static storage, unless the object either has a `constexpr` 
constructor or the object has no explicit constructor.
2. Restricting check to only consider C++11 or higher.
3. Updating tests to reflect the above


https://reviews.llvm.org/D41546

Files:
  clang-tidy/fuchsia/CMakeLists.txt
  clang-tidy/fuchsia/FuchsiaTidyModule.cpp
  clang-tidy/fuchsia/StaticallyConstructedObjectsCheck.cpp
  clang-tidy/fuchsia/StaticallyConstructedObjectsCheck.h
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/fuchsia-statically-constructed-objects.rst
  docs/clang-tidy/checks/list.rst
  test/clang-tidy/fuchsia-statically-constructed-objects.cpp

Index: test/clang-tidy/fuchsia-statically-constructed-objects.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/fuchsia-statically-constructed-objects.cpp
@@ -0,0 +1,67 @@
+// RUN: %check_clang_tidy %s fuchsia-statically-constructed-objects %t
+
+// Trivial static is fine
+static int i;
+
+class ClassWithNoCtor {};
+
+class ClassWithCtor {
+public:
+  ClassWithCtor(int Val) : Val(Val) {}
+private:
+  int Val;
+};
+
+class ClassWithConstexpr {
+public:
+  ClassWithConstexpr(int Val1) : Val(Val) {}
+  constexpr ClassWithConstexpr() : Val(0) {}
+
+private:
+  int Val;
+};
+
+ClassWithNoCtor A;
+ClassWithConstexpr C;
+ClassWithConstexpr E(0);
+ClassWithCtor G(0);
+
+static ClassWithNoCtor A2;
+static ClassWithConstexpr C2;
+static ClassWithConstexpr E2(0);
+// CHECK-MESSAGES: [[@LINE-1]]:1: warning: explicit global static objects are disallowed: if possible, use a constexpr constructor instead [fuchsia-statically-constructed-objects]
+// CHECK-MESSAGES-NEXT:  static ClassWithConstexpr E2(0);
+static ClassWithCtor G2(0);
+// CHECK-MESSAGES: [[@LINE-1]]:1: warning: explicit global static objects are disallowed: if possible, use a constexpr constructor instead [fuchsia-statically-constructed-objects]
+// CHECK-MESSAGES-NEXT:  static ClassWithCtor G2(0);
+
+struct StructWithConstexpr { constexpr StructWithConstexpr(); };
+struct StructWithNoCtor {};
+struct StructWithCtor { StructWithCtor(); };
+
+StructWithNoCtor SNoCtor;
+StructWithConstexpr SConstexpr;
+StructWithCtor SCtor;
+
+static StructWithConstexpr SConstexpr2;
+static StructWithNoCtor SNoCtor2;
+static StructWithCtor SCtor2;
+// CHECK-MESSAGES: [[@LINE-1]]:1: warning: explicit global static objects are disallowed: if possible, use a constexpr constructor instead [fuchsia-statically-constructed-objects]
+// CHECK-MESSAGES-NEXT:  static StructWithCtor SCtor2;
+
+extern StructWithCtor SCtor3;
+
+class ClassWithStaticMember {
+private:
+  static StructWithNoCtor S;
+};
+
+ClassWithStaticMember Z();
+
+void f() {
+  // Function static is fine
+  static int i;
+  static ClassWithConstexpr E3(0);
+  static ClassWithCtor G3(0);
+  static StructWithCtor SCtor;
+}
Index: docs/clang-tidy/checks/list.rst
===================================================================
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -70,6 +70,7 @@
    cppcoreguidelines-special-member-functions
    fuchsia-default-arguments
    fuchsia-overloaded-operator
+   fuchsia-statically-constructed-objects
    fuchsia-virtual-inheritance
    google-build-explicit-make-pair
    google-build-namespaces
Index: docs/clang-tidy/checks/fuchsia-statically-constructed-objects.rst
===================================================================
--- /dev/null
+++ docs/clang-tidy/checks/fuchsia-statically-constructed-objects.rst
@@ -0,0 +1,41 @@
+.. title:: clang-tidy - fuchsia-statically-constructed-objects
+
+fuchsia-statically-constructed-objects
+======================================
+
+Warns if global non-trivial objects with explicit static storage are 
+constructed, unless the object either has a ``constexpr`` constructor or has no 
+explicit constructor.
+
+For example:
+
+.. code-block:: c++
+
+  class A {};
+
+  class B {
+  public:
+    B(int Val) : Val(Val) {}
+  private:
+    int Val;
+  };
+
+  class C {
+  public:
+    C(int Val) : Val(Val) {}
+    constexpr C() : Val(0) {}
+
+  private:
+    int Val;
+  };
+
+  static A a;         // No warning, as there is no explicit constructor
+  static C c(0);      // No warning, as constructor is constexpr
+
+  static B b(0);      // Warning, as constructor is not constexpr
+  static C c2(0, 1);  // Warning, as constructor is not constexpr
+  
+  static int i;       // No warning, as it is trivial
+  B b2(0);            // No warning, as it is not explicitly static
+
+See the features disallowed in Fuchsia at https://fuchsia.googlesource.com/zircon/+/master/docs/cxx.md
Index: docs/ReleaseNotes.rst
===================================================================
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -57,7 +57,12 @@
 Improvements to clang-tidy
 --------------------------
 
-- ...
+- New `fuchsia-statically-constructed-objects
+  <http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-statically-constructed-objects.html>`_ check
+
+  Warns if global non-trivial objects with explicit static storage are 
+  constructed, unless the object either has a ``constexpr`` constructor or has 
+  no explicit constructor.
 
 Improvements to include-fixer
 -----------------------------
Index: clang-tidy/fuchsia/StaticallyConstructedObjectsCheck.h
===================================================================
--- /dev/null
+++ clang-tidy/fuchsia/StaticallyConstructedObjectsCheck.h
@@ -0,0 +1,37 @@
+//===--- StaticallyConstructedObjectsCheck.h - clang-tidy--------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_STATICALLY_CONSTRUCTED_OBJECTS_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_STATICALLY_CONSTRUCTED_OBJECTS_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace fuchsia {
+
+/// Constructing global non-trivial objects with explicit static storage is 
+/// disallowed, unless the object either has a constexpr constructor or has no 
+/// explicit constructor.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-statically-constructed-objects.html
+class StaticallyConstructedObjectsCheck : public ClangTidyCheck {
+public:
+  StaticallyConstructedObjectsCheck(StringRef Name, ClangTidyContext *Context)
+      : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace fuchsia
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_STATICALLY_CONSTRUCTED_OBJECTS_H
Index: clang-tidy/fuchsia/StaticallyConstructedObjectsCheck.cpp
===================================================================
--- /dev/null
+++ clang-tidy/fuchsia/StaticallyConstructedObjectsCheck.cpp
@@ -0,0 +1,56 @@
+//===--- StaticallyConstructedObjectsCheck.cpp - clang-tidy----------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "StaticallyConstructedObjectsCheck.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace fuchsia {
+
+AST_MATCHER(VarDecl, isGlobalStatic) {
+  return Node.getStorageDuration() == SD_Static && !Node.isLocalVarDecl();
+}
+
+void StaticallyConstructedObjectsCheck::registerMatchers(MatchFinder *Finder) {
+  // Constructing global non-trivial objects with explicit static storage is
+  // disallowed, unless the object either has a constexpr constructor or has no
+  // explicit constructor.
+
+  // Constexpr requires C++11 or later.
+  if (!getLangOpts().CPlusPlus11)
+    return;
+
+  Finder->addMatcher(
+      varDecl(allOf(
+                  // Match global statically stored objects...
+                  isGlobalStatic(),
+                  // ... that are explicitly static...
+                  isStaticStorageClass(),
+                  // ... that have C++ constructors...
+                  hasDescendant(cxxConstructExpr(unless(
+                      // ... unless it is constexpr.
+                      hasDeclaration(cxxConstructorDecl(isConstexpr())))))))
+          .bind("decl"),
+      this);
+}
+
+void StaticallyConstructedObjectsCheck::check(
+    const MatchFinder::MatchResult &Result) {
+  if (const auto *D = Result.Nodes.getNodeAs<VarDecl>("decl")) {
+    diag(D->getLocStart(), "explicit global static objects are disallowed: if "
+                           "possible, use a constexpr constructor instead "
+                           "[fuchsia-statically-constructed-objects]");
+  }
+}
+
+} // namespace fuchsia
+} // namespace tidy
+} // namespace clang
Index: clang-tidy/fuchsia/FuchsiaTidyModule.cpp
===================================================================
--- clang-tidy/fuchsia/FuchsiaTidyModule.cpp
+++ clang-tidy/fuchsia/FuchsiaTidyModule.cpp
@@ -12,6 +12,7 @@
 #include "../ClangTidyModuleRegistry.h"
 #include "DefaultArgumentsCheck.h"
 #include "OverloadedOperatorCheck.h"
+#include "StaticallyConstructedObjectsCheck.h"
 #include "VirtualInheritanceCheck.h"
 
 using namespace clang::ast_matchers;
@@ -28,6 +29,8 @@
         "fuchsia-default-arguments");
     CheckFactories.registerCheck<OverloadedOperatorCheck>(
         "fuchsia-overloaded-operator");
+    CheckFactories.registerCheck<StaticallyConstructedObjectsCheck>(
+        "fuchsia-statically-constructed-objects");
     CheckFactories.registerCheck<VirtualInheritanceCheck>(
         "fuchsia-virtual-inheritance");
   }
Index: clang-tidy/fuchsia/CMakeLists.txt
===================================================================
--- clang-tidy/fuchsia/CMakeLists.txt
+++ clang-tidy/fuchsia/CMakeLists.txt
@@ -4,6 +4,7 @@
   DefaultArgumentsCheck.cpp
   FuchsiaTidyModule.cpp
   OverloadedOperatorCheck.cpp
+  StaticallyConstructedObjectsCheck.cpp
   VirtualInheritanceCheck.cpp
 
   LINK_LIBS
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to