[PATCH] D44346: [clang-tidy] Add Fuchsia checker for temporary objects

2018-03-12 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added inline comments.



Comment at: clang-tidy/fuchsia/FuchsiaTidyModule.cpp:44
+CheckFactories.registerCheck(
+"fuchsia-zx-temporary-objects");
   }

Do we want a zircon module instead? I'm wondering about people who enable 
modules by doing `fuchsia-*` and whether or not they would expect this check 
(and others for zircon) to be enabled.



Comment at: clang-tidy/fuchsia/ZxTemporaryObjectsCheck.cpp:46
+void ZxTemporaryObjectsCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *D = Result.Nodes.getNodeAs("temps")) {
+diag(D->getLocation(), "misuse of temporary object");

Elide braces.



Comment at: clang-tidy/fuchsia/ZxTemporaryObjectsCheck.cpp:47
+  if (const auto *D = Result.Nodes.getNodeAs("temps")) {
+diag(D->getLocation(), "misuse of temporary object");
+  }

I think this could be stated more clearly as "creating a temporary object of 
type %0 is prohibited" and pass in the temporary type. That will also help the 
user to identify what type is problematic in something like: `f(good_temp{}, 
bad_temp{}, good_temp{});`. I'm not tied to printing the type, but "misuse" 
suggests there's a better way to use the temporary object, which I don't think 
is a correct interpretation.



Comment at: clang-tidy/fuchsia/ZxTemporaryObjectsCheck.h:20
+
+/// Constructing of specific temporary objects in the Zircon kernel is
+/// discouraged. Takes the list of such discouraged temporary objects as a

Construction instead of constructing?


https://reviews.llvm.org/D44346



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D44346: [clang-tidy] Add Fuchsia checker for temporary objects

2018-03-09 Thread Eugene Zelenko via Phabricator via cfe-commits
Eugene.Zelenko added inline comments.



Comment at: clang-tidy/fuchsia/ZxTemporaryObjectsCheck.cpp:24
+  std::string QualifiedName = Node.getQualifiedNameAsString();
+  return llvm::any_of(Names,
+  [&](StringRef Name) { return QualifiedName == Name; });

Please include STLExtras.h and string.



Comment at: clang-tidy/fuchsia/ZxTemporaryObjectsCheck.h:31
+Names(utils::options::parseStringList(
+Options.get("Names", "::std::vector"))) {}
+  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;

This contradict documentation which claims that default is empty.



Comment at: docs/ReleaseNotes.rst:79
+  Warns on construction of specific temporary objects in the Zircon kernel.
+  Takes the list of such discouraged temporary objects as a parameter.
+

I think you could omit second statement.



Comment at: docs/clang-tidy/checks/fuchsia-zx-temporary-objects.rst:38
+
+   A semi-colon-separated list of fully-qualified names of C++ classes that 
should not be constructed as temporaries. Default is empty.

Please use 2 spaces indentation and 80 characters limit. Continuation should be 
indented too.


https://reviews.llvm.org/D44346



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D44346: [clang-tidy] Add Fuchsia checker for temporary objects

2018-03-09 Thread Julie Hockett via Phabricator via cfe-commits
juliehockett created this revision.
juliehockett added reviewers: aaron.ballman, hokein, ilya-biryukov.
juliehockett added a project: clang-tools-extra.
Herald added subscribers: xazax.hun, mgorny.

Adding a checker to fuchsia-zx (for zircon) to flag instances where specific 
objects are temporarily created.


https://reviews.llvm.org/D44346

Files:
  clang-tidy/fuchsia/CMakeLists.txt
  clang-tidy/fuchsia/FuchsiaTidyModule.cpp
  clang-tidy/fuchsia/ZxTemporaryObjectsCheck.cpp
  clang-tidy/fuchsia/ZxTemporaryObjectsCheck.h
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/fuchsia-zx-temporary-objects.rst
  docs/clang-tidy/checks/list.rst
  test/clang-tidy/fuchsia-zx-temporary-objects.cpp

Index: test/clang-tidy/fuchsia-zx-temporary-objects.cpp
===
--- /dev/null
+++ test/clang-tidy/fuchsia-zx-temporary-objects.cpp
@@ -0,0 +1,82 @@
+// RUN: %check_clang_tidy %s fuchsia-zx-temporary-objects %t -- \
+// RUN:   -config="{CheckOptions: [{key: fuchsia-zx-temporary-objects.Names, value: 'Foo;NS::Bar'}]}" \
+// RUN:   -header-filter=.* \
+// RUN: -- -std=c++11
+
+// Should flag instances of Foo, NS::Bar
+
+class Foo {
+public:
+  Foo() = default;
+  Foo(int Val) : Val(Val){};
+
+private:
+  int Val;
+};
+
+namespace NS {
+
+class Bar {
+public:
+  Bar() = default;
+  Bar(int Val) : Val(Val){};
+
+private:
+  int Val;
+};
+
+} // namespace NS
+
+class Bar {
+public:
+  Bar() = default;
+  Bar(int Val) : Val(Val){};
+
+private:
+  int Val;
+};
+
+int func(Foo F) { return 1; };
+
+int main() {
+  Foo F;
+  Foo *F2 = new Foo();
+  new Foo();
+  Foo();
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: misuse of temporary object
+  Foo F3 = Foo();
+  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: misuse of temporary object
+
+  Bar();
+  NS::Bar();
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: misuse of temporary object
+
+  int A = func(Foo());
+  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: misuse of temporary object
+
+  Foo F4(0);
+  Foo *F5 = new Foo(0);
+  new Foo(0);
+  Foo(0);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: misuse of temporary object
+  Foo F6 = Foo(0);
+  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: misuse of temporary object
+
+  Bar(0);
+  NS::Bar(0);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: misuse of temporary object
+
+  int B = func(Foo(0));
+  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: misuse of temporary object
+}
+
+namespace NS {
+
+void f() {
+  Bar();
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: misuse of temporary object
+  Bar(0);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: misuse of temporary object
+}
+
+} // namespace NS
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -90,6 +90,7 @@
fuchsia-statically-constructed-objects
fuchsia-trailing-return
fuchsia-virtual-inheritance
+   fuchsia-zx-temporary-objects
google-build-explicit-make-pair
google-build-namespaces
google-build-using-namespace
Index: docs/clang-tidy/checks/fuchsia-zx-temporary-objects.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/fuchsia-zx-temporary-objects.rst
@@ -0,0 +1,38 @@
+.. title:: clang-tidy - fuchsia-zx-temporary-objects
+
+fuchsia-zx-temporary-objects
+
+
+Warns on construction of specific temporary objects in the Zircon kernel.
+
+For example, given the list of classes "Foo" and "NS::Bar", all of the following will trigger the warning: 
+
+.. code-block:: c++
+
+  Foo();
+  Foo F = Foo();
+  func(Foo());
+
+  namespace NS {
+
+  Bar();
+
+  }
+
+With the same list, the following will not trigger the warning:
+
+.. code-block:: c++
+
+  Foo F;// Non-temporary construction okay
+  Foo F(param);			// Non-temporary construction okay
+  Foo *F = new Foo();	// New construction okay
+
+  Bar(); // Not NS::Bar, so okay
+  NS::Bar B;			// Non-temporary construction okay
+
+Options
+---
+
+.. option:: Names
+
+   A semi-colon-separated list of fully-qualified names of C++ classes that should not be constructed as temporaries. Default is empty.
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -72,6 +72,12 @@
   with looping constructs. Every backward jump is rejected. Forward jumps are
   only allowed in nested loops.
 
+- New `fuchsia-zx-temporary-objects
+  `_ check
+
+  Warns on construction of specific temporary objects in the Zircon kernel.
+  Takes the list of such discouraged temporary objects as a parameter.
+
 - New `fuchsia-multiple-inheritance
   `_ check
 
Index: clang-tidy/fuchsia/ZxTemporaryObjectsCheck.h
==