[PATCH] D92992: [clang-tidy] Add make_unique_for_overwrite/make_shared_for_overwrite check.

2020-12-09 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly created this revision.
ckennelly added reviewers: hokein, njames93, alexfh, aaron.ballman.
Herald added subscribers: xazax.hun, mgorny.
ckennelly requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

std::make_unique_for_overwrite and std::make_shared_for_overwrite were
introduced in C++20.

These default initialize the values, unlike std::make_unique /
std::make_shared, which value initialize.  The latter can cause
unexpected performance regressions when trivial types (such as int) are
subsequently value initialized.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92992

Files:
  clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
  clang-tools-extra/clang-tidy/modernize/MakeSharedCheck.cpp
  clang-tools-extra/clang-tidy/modernize/MakeSharedForOverwriteCheck.cpp
  clang-tools-extra/clang-tidy/modernize/MakeSharedForOverwriteCheck.h
  clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
  clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
  clang-tools-extra/clang-tidy/modernize/MakeUniqueCheck.cpp
  clang-tools-extra/clang-tidy/modernize/MakeUniqueForOverwriteCheck.cpp
  clang-tools-extra/clang-tidy/modernize/MakeUniqueForOverwriteCheck.h
  clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  
clang-tools-extra/docs/clang-tidy/checks/modernize-make-shared-for-overwrite.rst
  
clang-tools-extra/docs/clang-tidy/checks/modernize-make-unique-for-overwrite.rst
  
clang-tools-extra/test/clang-tidy/checkers/modernize-make-shared-for-overwrite.cpp
  
clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique-for-overwrite.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique-for-overwrite.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique-for-overwrite.cpp
@@ -0,0 +1,476 @@
+// RUN: %check_clang_tidy -std=c++20-or-later %s modernize-make-unique-for-overwrite %t -- -- -I %S/Inputs/modernize-smart-ptr
+
+#include "initializer_list.h"
+#include "unique_ptr.h"
+// CHECK-FIXES: #include 
+
+struct Base {
+  Base();
+  Base(int, int);
+};
+
+struct Derived : public Base {
+  Derived();
+  Derived(int, int);
+};
+
+struct APair {
+  int a, b;
+};
+
+struct DPair {
+  DPair() : a(0), b(0) {}
+  DPair(int x, int y) : a(y), b(x) {}
+  int a, b;
+};
+
+template 
+struct MyVector {
+  MyVector(std::initializer_list);
+};
+
+struct Empty {};
+
+struct E {
+  E(std::initializer_list);
+  E();
+};
+
+struct F {
+  F(std::initializer_list);
+  F();
+  int a;
+};
+
+struct G {
+  G(std::initializer_list);
+  G(int);
+};
+
+struct H {
+  H(std::vector);
+  H(std::vector &, double);
+  H(MyVector, int);
+};
+
+struct I {
+  I(G);
+};
+
+struct J {
+  J(E e, int);
+};
+
+namespace {
+class Foo {};
+} // namespace
+
+namespace bar {
+class Bar {};
+} // namespace bar
+
+template 
+using unique_ptr_ = std::unique_ptr;
+
+void *operator new(__SIZE_TYPE__ Count, void *Ptr);
+
+int g(std::unique_ptr P);
+
+std::unique_ptr getPointer() {
+  return std::unique_ptr(new Base);
+}
+
+std::unique_ptr getPointerValue() {
+  return std::unique_ptr(new Base());
+}
+
+void basic() {
+  std::unique_ptr P1 = std::unique_ptr(new int());
+  std::unique_ptr P2 = std::unique_ptr(new int);
+  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique_for_overwrite instead [modernize-make-unique-for-overwrite]
+  // CHECK-FIXES: std::unique_ptr P2 = std::make_unique_for_overwrite();
+
+  P1.reset(new int());
+  P2.reset(new int);
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique_for_overwrite instead [modernize-make-unique-for-overwrite]
+  // CHECK-FIXES: P2 = std::make_unique_for_overwrite();
+
+  P1 = std::unique_ptr(new int());
+  P2 = std::unique_ptr(new int);
+  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique_for_overwrite instead [modernize-make-unique-for-overwrite]
+  // CHECK-FIXES: P2 = std::make_unique_for_overwrite();
+
+  P1.reset(new int());
+  P2.reset(new int);
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique_for_overwrite instead [modernize-make-unique-for-overwrite]
+  // CHECK-FIXES: P2 = std::make_unique_for_overwrite();
+
+  // With auto.
+  auto P4 = std::unique_ptr(new int());
+  auto P5 = std::unique_ptr(new int);
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique_for_overwrite instead
+  // CHECK-FIXES: auto P5 = std::make_unique_for_overwrite();
+
+  std::unique_ptr P6 = std::unique_ptr((new int()));
+  std::unique_ptr P7 = std::unique_ptr((new int));
+  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique_for_overwrite instead [modernize-make-unique-for-overwrite]
+  // CHECK-FIXES: std::unique_ptr P7 = std::make_unique_for_overwrite();
+
+  P4.reset((new int()));
+  P5.reset((new int));
+  // CHECK-MESSA

[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-12-08 Thread Chris Kennelly via Phabricator via cfe-commits
This revision was not accepted when it landed; it landed in state "Needs 
Review".
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG8d2c095e5a6b: [clang-tidy] Omit std::make_unique/make_shared 
for default initialization. (authored by ckennelly).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

Files:
  clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
  clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
  clang-tools-extra/docs/clang-tidy/checks/modernize-make-shared.rst
  clang-tools-extra/docs/clang-tidy/checks/modernize-make-unique.rst
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-shared.cpp
  
clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique-default-init.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
@@ -83,49 +83,60 @@
   // CHECK-FIXES: return std::make_unique();
 }
 
+std::unique_ptr getPointerValue() {
+  return std::unique_ptr(new Base());
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead
+  // CHECK-FIXES: return std::make_unique();
+}
+
 void basic() {
   std::unique_ptr P1 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: std::unique_ptr P1 = std::make_unique();
+  std::unique_ptr P2 = std::unique_ptr(new int);
 
   P1.reset(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P1 = std::make_unique();
+  P2.reset(new int);
 
   P1 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P1 = std::make_unique();
+  P1 = std::unique_ptr(new int);
 
-  // Without parenthesis.
-  std::unique_ptr P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P2 = std::make_unique();
+  // Without parenthesis, default initialization.
+  std::unique_ptr P3 = std::unique_ptr(new int);
 
   P2.reset(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   // With auto.
-  auto P3 = std::unique_ptr(new int());
+  auto P4 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
-  // CHECK-FIXES: auto P3 = std::make_unique();
+  // CHECK-FIXES: auto P4 = std::make_unique();
+  auto P5 = std::unique_ptr(new int);
 
-  std::unique_ptr P4 = std::unique_ptr((new int));
+  std::unique_ptr P6 = std::unique_ptr((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P4 = std::make_unique();
-  P4.reset((new int));
+  // CHECK-FIXES: std::unique_ptr P6 = std::make_unique();
+  std::unique_ptr P7 = std::unique_ptr((new int));
+
+  P4.reset((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P4 = std::make_unique();
-  std::unique_ptr P5 = std::unique_ptrnew int;
+  P5.reset((new int));
+
+  std::unique_ptr P8 = std::unique_ptrnew int();
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P5 = std::make_unique();
-  P5.reset(new int);
+  // CHECK-FIXES: std::unique_ptr P8 = std::make_unique();
+  std::unique_ptr P9 = std::unique_ptrnew int;
+
+  P5.reset(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P5 = std::make_unique();
+  P6.reset(new int);
 
   {
 // No std.
@@ -133,40 +144,55 @@
 unique_ptr Q = unique_ptr(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use std::make_unique instead
 // CHECK-FIXES: unique_ptr Q = std::make_unique();
+unique_ptr P = unique_ptr(new int);
 
 Q = unique_ptr(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
 // CHECK-FIXES: Q = std::make_unique();
+
+P = unique_ptr(new int);
   }
 
   std::unique_ptr R(new int());
+  std::unique_ptr S(new int);
 
  

[PATCH] D92810: [clang-tidy] Recognize single character needles for absl::StrContains.

2020-12-08 Thread Chris Kennelly via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG16622d535c02: [clang-tidy] Recognize single character 
needles for absl::StrContains. (authored by ckennelly).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D92810/new/

https://reviews.llvm.org/D92810

Files:
  clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp


Index: 
clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
===
--- 
clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
+++ 
clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
@@ -226,17 +226,21 @@
   // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(asv, cc);{{$}}
 }
 
-// Confirms that it does *not* match when the parameter to find() is a char,
-// because absl::StrContains is not implemented for char.
-void no_char_param_tests() {
+void char_param_tests() {
   std::string ss;
   ss.find('c') == std::string::npos;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StrContains instead of
+  // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(ss, 'c');{{$}}
 
   std::string_view ssv;
   ssv.find('c') == std::string_view::npos;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StrContains instead of
+  // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(ssv, 'c');{{$}}
 
   absl::string_view asv;
   asv.find('c') == absl::string_view::npos;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StrContains instead of
+  // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(asv, 'c');{{$}}
 }
 
 #define FOO(a, b, c, d) ((a).find(b) == std::string::npos ? (c) : (d))
Index: clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
===
--- clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
+++ clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
@@ -31,6 +31,8 @@
 using ::clang::transformer::node;
 using ::clang::transformer::RewriteRule;
 
+AST_MATCHER(Type, isCharType) { return Node.isCharType(); }
+
 static const char DefaultStringLikeClasses[] = "::std::basic_string;"
"::std::basic_string_view;"
"::absl::string_view";
@@ -58,13 +60,15 @@
   hasUnqualifiedDesugaredType(recordType(hasDeclaration(StringLikeClass)));
   auto CharStarType =
   hasUnqualifiedDesugaredType(pointerType(pointee(isAnyCharacter(;
+  auto CharType = hasUnqualifiedDesugaredType(isCharType());
   auto StringNpos = declRefExpr(
   to(varDecl(hasName("npos"), hasDeclContext(StringLikeClass;
   auto StringFind = cxxMemberCallExpr(
   callee(cxxMethodDecl(
   hasName("find"),
-  hasParameter(0, parmVarDecl(anyOf(hasType(StringType),
-hasType(CharStarType)),
+  hasParameter(
+  0, parmVarDecl(anyOf(hasType(StringType), hasType(CharStarType),
+   hasType(CharType)),
   on(hasType(StringType)), hasArgument(0, 
expr().bind("parameter_to_find")),
   anyOf(hasArgument(1, integerLiteral(equals(0))),
 hasArgument(1, cxxDefaultArgExpr())),


Index: clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
@@ -226,17 +226,21 @@
   // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(asv, cc);{{$}}
 }
 
-// Confirms that it does *not* match when the parameter to find() is a char,
-// because absl::StrContains is not implemented for char.
-void no_char_param_tests() {
+void char_param_tests() {
   std::string ss;
   ss.find('c') == std::string::npos;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StrContains instead of
+  // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(ss, 'c');{{$}}
 
   std::string_view ssv;
   ssv.find('c') == std::string_view::npos;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StrContains instead of
+  // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(ssv, 'c');{{$}}
 
   absl::string_view asv;
   asv.find('c') == absl::string_view::npos;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StrContains instead of
+  // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(asv, 'c');{{$}}
 }
 
 #define FOO(a, b, c, d) ((a).find(b) == std::string::npos ? (c) : (d))
Index: clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
==

[PATCH] D92810: [clang-tidy] Recognize single character needles for absl::StrContains.

2020-12-07 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly created this revision.
ckennelly added reviewers: EricWF, ymandel, hokein.
Herald added a subscriber: xazax.hun.
ckennelly requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Commit fbdff6f3ae0b in the Abseil tree adds an overload for
absl::StrContains to accept a single character needle for optimized
lookups.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92810

Files:
  clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp


Index: 
clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
===
--- 
clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
+++ 
clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
@@ -226,17 +226,21 @@
   // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(asv, cc);{{$}}
 }
 
-// Confirms that it does *not* match when the parameter to find() is a char,
-// because absl::StrContains is not implemented for char.
-void no_char_param_tests() {
+void char_param_tests() {
   std::string ss;
   ss.find('c') == std::string::npos;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StrContains instead of
+  // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(ss, 'c');{{$}}
 
   std::string_view ssv;
   ssv.find('c') == std::string_view::npos;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StrContains instead of
+  // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(ssv, 'c');{{$}}
 
   absl::string_view asv;
   asv.find('c') == absl::string_view::npos;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StrContains instead of
+  // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(asv, 'c');{{$}}
 }
 
 #define FOO(a, b, c, d) ((a).find(b) == std::string::npos ? (c) : (d))
Index: clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
===
--- clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
+++ clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
@@ -31,6 +31,8 @@
 using ::clang::transformer::node;
 using ::clang::transformer::RewriteRule;
 
+AST_MATCHER(Type, isCharType) { return Node.isCharType(); }
+
 static const char DefaultStringLikeClasses[] = "::std::basic_string;"
"::std::basic_string_view;"
"::absl::string_view";
@@ -58,13 +60,15 @@
   hasUnqualifiedDesugaredType(recordType(hasDeclaration(StringLikeClass)));
   auto CharStarType =
   hasUnqualifiedDesugaredType(pointerType(pointee(isAnyCharacter(;
+  auto CharType = hasUnqualifiedDesugaredType(isCharType());
   auto StringNpos = declRefExpr(
   to(varDecl(hasName("npos"), hasDeclContext(StringLikeClass;
   auto StringFind = cxxMemberCallExpr(
   callee(cxxMethodDecl(
   hasName("find"),
-  hasParameter(0, parmVarDecl(anyOf(hasType(StringType),
-hasType(CharStarType)),
+  hasParameter(
+  0, parmVarDecl(anyOf(hasType(StringType), hasType(CharStarType),
+   hasType(CharType)),
   on(hasType(StringType)), hasArgument(0, 
expr().bind("parameter_to_find")),
   anyOf(hasArgument(1, integerLiteral(equals(0))),
 hasArgument(1, cxxDefaultArgExpr())),


Index: clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
@@ -226,17 +226,21 @@
   // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(asv, cc);{{$}}
 }
 
-// Confirms that it does *not* match when the parameter to find() is a char,
-// because absl::StrContains is not implemented for char.
-void no_char_param_tests() {
+void char_param_tests() {
   std::string ss;
   ss.find('c') == std::string::npos;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StrContains instead of
+  // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(ss, 'c');{{$}}
 
   std::string_view ssv;
   ssv.find('c') == std::string_view::npos;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StrContains instead of
+  // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(ssv, 'c');{{$}}
 
   absl::string_view asv;
   asv.find('c') == absl::string_view::npos;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StrContains instead of
+  // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(asv, 'c');{{$}}
 }
 
 #define FOO(a, b, c, d) ((a).find(b) == std::string::npos ? (c) : (d))
Index: clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
==

[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-12-07 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added a comment.

@njames93: Any additional comments?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

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


[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-12-07 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added a comment.

In D90392#2436639 , @hokein wrote:

> thanks, I think this patch is good, feel free to land it (and sorry for the 
> delay).
>
> As @lebedev.ri commented, would be nice to mention the motivation of the 
> change (`make_shared_for_overwrite`?) in the description.

Done


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

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


[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-12-07 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly updated this revision to Diff 309936.
ckennelly edited the summary of this revision.
ckennelly added a comment.

updating commit message


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

Files:
  clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
  clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
  clang-tools-extra/docs/clang-tidy/checks/modernize-make-shared.rst
  clang-tools-extra/docs/clang-tidy/checks/modernize-make-unique.rst
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-shared.cpp
  
clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique-default-init.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
@@ -83,49 +83,60 @@
   // CHECK-FIXES: return std::make_unique();
 }
 
+std::unique_ptr getPointerValue() {
+  return std::unique_ptr(new Base());
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead
+  // CHECK-FIXES: return std::make_unique();
+}
+
 void basic() {
   std::unique_ptr P1 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: std::unique_ptr P1 = std::make_unique();
+  std::unique_ptr P2 = std::unique_ptr(new int);
 
   P1.reset(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P1 = std::make_unique();
+  P2.reset(new int);
 
   P1 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P1 = std::make_unique();
+  P1 = std::unique_ptr(new int);
 
-  // Without parenthesis.
-  std::unique_ptr P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P2 = std::make_unique();
+  // Without parenthesis, default initialization.
+  std::unique_ptr P3 = std::unique_ptr(new int);
 
   P2.reset(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   // With auto.
-  auto P3 = std::unique_ptr(new int());
+  auto P4 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
-  // CHECK-FIXES: auto P3 = std::make_unique();
+  // CHECK-FIXES: auto P4 = std::make_unique();
+  auto P5 = std::unique_ptr(new int);
 
-  std::unique_ptr P4 = std::unique_ptr((new int));
+  std::unique_ptr P6 = std::unique_ptr((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P4 = std::make_unique();
-  P4.reset((new int));
+  // CHECK-FIXES: std::unique_ptr P6 = std::make_unique();
+  std::unique_ptr P7 = std::unique_ptr((new int));
+
+  P4.reset((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P4 = std::make_unique();
-  std::unique_ptr P5 = std::unique_ptrnew int;
+  P5.reset((new int));
+
+  std::unique_ptr P8 = std::unique_ptrnew int();
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P5 = std::make_unique();
-  P5.reset(new int);
+  // CHECK-FIXES: std::unique_ptr P8 = std::make_unique();
+  std::unique_ptr P9 = std::unique_ptrnew int;
+
+  P5.reset(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P5 = std::make_unique();
+  P6.reset(new int);
 
   {
 // No std.
@@ -133,40 +144,55 @@
 unique_ptr Q = unique_ptr(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use std::make_unique instead
 // CHECK-FIXES: unique_ptr Q = std::make_unique();
+unique_ptr P = unique_ptr(new int);
 
 Q = unique_ptr(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
 // CHECK-FIXES: Q = std::make_unique();
+
+P = unique_ptr(new int);
   }
 
   std::unique_ptr R(new int());
+  std::unique_ptr S(new int);
 
   // Create the unique_ptr as a parameter to a function.
   int T = g(std::unique_ptr(new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
   // CHECK-FIXES: int T

[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-12-07 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly updated this revision to Diff 309934.
ckennelly added a comment.

rebase


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

Files:
  clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
  clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
  clang-tools-extra/docs/clang-tidy/checks/modernize-make-shared.rst
  clang-tools-extra/docs/clang-tidy/checks/modernize-make-unique.rst
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-shared.cpp
  
clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique-default-init.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
@@ -83,49 +83,60 @@
   // CHECK-FIXES: return std::make_unique();
 }
 
+std::unique_ptr getPointerValue() {
+  return std::unique_ptr(new Base());
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead
+  // CHECK-FIXES: return std::make_unique();
+}
+
 void basic() {
   std::unique_ptr P1 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: std::unique_ptr P1 = std::make_unique();
+  std::unique_ptr P2 = std::unique_ptr(new int);
 
   P1.reset(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P1 = std::make_unique();
+  P2.reset(new int);
 
   P1 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P1 = std::make_unique();
+  P1 = std::unique_ptr(new int);
 
-  // Without parenthesis.
-  std::unique_ptr P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P2 = std::make_unique();
+  // Without parenthesis, default initialization.
+  std::unique_ptr P3 = std::unique_ptr(new int);
 
   P2.reset(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   // With auto.
-  auto P3 = std::unique_ptr(new int());
+  auto P4 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
-  // CHECK-FIXES: auto P3 = std::make_unique();
+  // CHECK-FIXES: auto P4 = std::make_unique();
+  auto P5 = std::unique_ptr(new int);
 
-  std::unique_ptr P4 = std::unique_ptr((new int));
+  std::unique_ptr P6 = std::unique_ptr((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P4 = std::make_unique();
-  P4.reset((new int));
+  // CHECK-FIXES: std::unique_ptr P6 = std::make_unique();
+  std::unique_ptr P7 = std::unique_ptr((new int));
+
+  P4.reset((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P4 = std::make_unique();
-  std::unique_ptr P5 = std::unique_ptrnew int;
+  P5.reset((new int));
+
+  std::unique_ptr P8 = std::unique_ptrnew int();
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P5 = std::make_unique();
-  P5.reset(new int);
+  // CHECK-FIXES: std::unique_ptr P8 = std::make_unique();
+  std::unique_ptr P9 = std::unique_ptrnew int;
+
+  P5.reset(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P5 = std::make_unique();
+  P6.reset(new int);
 
   {
 // No std.
@@ -133,40 +144,55 @@
 unique_ptr Q = unique_ptr(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use std::make_unique instead
 // CHECK-FIXES: unique_ptr Q = std::make_unique();
+unique_ptr P = unique_ptr(new int);
 
 Q = unique_ptr(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
 // CHECK-FIXES: Q = std::make_unique();
+
+P = unique_ptr(new int);
   }
 
   std::unique_ptr R(new int());
+  std::unique_ptr S(new int);
 
   // Create the unique_ptr as a parameter to a function.
   int T = g(std::unique_ptr(new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
   // CHECK-FIXES: int T = g(std::make_unique());
+  T = g(std::unique_ptr(new int));
 

[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-12-06 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added a comment.

Ping


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

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


[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-11-28 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added a comment.

Ping


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

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


[PATCH] D91009: [clang-tidy] Include std::basic_string_view in readability-redundant-string-init.

2020-11-20 Thread Chris Kennelly via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGe4f9b5d442a2: [clang-tidy] Include std::basic_string_view in 
readability-redundant-string… (authored by ckennelly).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91009/new/

https://reviews.llvm.org/D91009

Files:
  clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
  
clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
@@ -1,7 +1,7 @@
 // RUN: %check_clang_tidy -std=c++11,c++14 %s readability-redundant-string-init %t \
 // RUN:   -config="{CheckOptions: \
 // RUN: [{key: readability-redundant-string-init.StringNames, \
-// RUN:   value: '::std::basic_string;our::TestString'}] \
+// RUN:   value: '::std::basic_string;::std::basic_string_view;our::TestString'}] \
 // RUN: }"
 // FIXME: Fix the checker to work in C++17 mode.
 
@@ -19,6 +19,20 @@
 };
 typedef basic_string string;
 typedef basic_string wstring;
+
+template , typename A = std::allocator>
+struct basic_string_view {
+  using size_type = decltype(sizeof(0));
+
+  basic_string_view();
+  basic_string_view(const basic_string_view &);
+  basic_string_view(const C *, size_type);
+  basic_string_view(const C *);
+  template 
+  basic_string_view(It, End);
+};
+typedef basic_string_view string_view;
+typedef basic_string_view wstring_view;
 }
 
 void f() {
@@ -48,6 +62,33 @@
   std::string z;
 }
 
+void fview() {
+  std::string_view a = "";
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization [readability-redundant-string-init]
+  // CHECK-FIXES: std::string_view a;
+  std::string_view b("");
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view b;
+  std::string_view c = R"()";
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view c;
+  std::string_view d(R"()");
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view d;
+  std::string_view e{""};
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view e;
+  std::string_view f = {""};
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view f;
+
+  std::string_view u = "u";
+  std::string_view w("w");
+  std::string_view x = R"(x)";
+  std::string_view y(R"(y)");
+  std::string_view z;
+}
+
 void g() {
   std::wstring a = L"";
   // CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
@@ -69,6 +110,33 @@
   std::wstring z;
 }
 
+void gview() {
+  std::wstring_view a = L"";
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization [readability-redundant-string-init]
+  // CHECK-FIXES: std::wstring_view a;
+  std::wstring_view b(L"");
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view b;
+  std::wstring_view c = L"";
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view c;
+  std::wstring_view d(L"");
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view d;
+  std::wstring_view e{L""};
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view e;
+  std::wstring_view f = {L""};
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view f;
+
+  std::wstring_view u = L"u";
+  std::wstring_view w(L"w");
+  std::wstring_view x = LR"(x)";
+  std::wstring_view y(LR"(y)");
+  std::wstring_view z;
+}
+
 template 
 void templ() {
   std::string s = "";
@@ -274,7 +342,6 @@
   // CHECK-MESSAGES: [[@LINE-2]]:23: warning: redundant string initialization
   // CHECK-FIXES:  Foo(float)  {}
 
-
   // Check how it handles removing some redundant initializers while leaving
   // valid initializers intact.
   Foo(std::string Arg) : A(Arg), B(""), C("NonEmpty"), D(R"()"), E("") {}
Index: clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
+++ clang-tools-extra/doc

[PATCH] D91009: [clang-tidy] Include std::basic_string_view in readability-redundant-string-init.

2020-11-19 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added inline comments.



Comment at: 
clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp:65
 
+void fview() {
+  std::string_view a = "";

aaron.ballman wrote:
> Can you also add tests for `wstring_view`?
> 
> Also, perhaps one for `std::string_view foo{};` (to remove the spurious `{}`)
Done for `wstring_view`

re `std::string_view foo{}`, it looks like we don't even match `std::string{}`, 
https://godbolt.org/z/zaafrb


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91009/new/

https://reviews.llvm.org/D91009

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


[PATCH] D91009: [clang-tidy] Include std::basic_string_view in readability-redundant-string-init.

2020-11-19 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly updated this revision to Diff 306591.
ckennelly marked 3 inline comments as done.
ckennelly added a comment.

Applied review feedback


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91009/new/

https://reviews.llvm.org/D91009

Files:
  clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
  
clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
@@ -1,7 +1,7 @@
 // RUN: %check_clang_tidy -std=c++11,c++14 %s readability-redundant-string-init %t \
 // RUN:   -config="{CheckOptions: \
 // RUN: [{key: readability-redundant-string-init.StringNames, \
-// RUN:   value: '::std::basic_string;our::TestString'}] \
+// RUN:   value: '::std::basic_string;::std::basic_string_view;our::TestString'}] \
 // RUN: }"
 // FIXME: Fix the checker to work in C++17 mode.
 
@@ -19,6 +19,20 @@
 };
 typedef basic_string string;
 typedef basic_string wstring;
+
+template , typename A = std::allocator>
+struct basic_string_view {
+  using size_type = decltype(sizeof(0));
+
+  basic_string_view();
+  basic_string_view(const basic_string_view &);
+  basic_string_view(const C *, size_type);
+  basic_string_view(const C *);
+  template 
+  basic_string_view(It, End);
+};
+typedef basic_string_view string_view;
+typedef basic_string_view wstring_view;
 }
 
 void f() {
@@ -48,6 +62,33 @@
   std::string z;
 }
 
+void fview() {
+  std::string_view a = "";
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization [readability-redundant-string-init]
+  // CHECK-FIXES: std::string_view a;
+  std::string_view b("");
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view b;
+  std::string_view c = R"()";
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view c;
+  std::string_view d(R"()");
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view d;
+  std::string_view e{""};
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view e;
+  std::string_view f = {""};
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view f;
+
+  std::string_view u = "u";
+  std::string_view w("w");
+  std::string_view x = R"(x)";
+  std::string_view y(R"(y)");
+  std::string_view z;
+}
+
 void g() {
   std::wstring a = L"";
   // CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
@@ -69,6 +110,33 @@
   std::wstring z;
 }
 
+void gview() {
+  std::wstring_view a = L"";
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization [readability-redundant-string-init]
+  // CHECK-FIXES: std::wstring_view a;
+  std::wstring_view b(L"");
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view b;
+  std::wstring_view c = L"";
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view c;
+  std::wstring_view d(L"");
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view d;
+  std::wstring_view e{L""};
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view e;
+  std::wstring_view f = {L""};
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view f;
+
+  std::wstring_view u = L"u";
+  std::wstring_view w(L"w");
+  std::wstring_view x = LR"(x)";
+  std::wstring_view y(LR"(y)");
+  std::wstring_view z;
+}
+
 template 
 void templ() {
   std::string s = "";
@@ -274,7 +342,6 @@
   // CHECK-MESSAGES: [[@LINE-2]]:23: warning: redundant string initialization
   // CHECK-FIXES:  Foo(float)  {}
 
-
   // Check how it handles removing some redundant initializers while leaving
   // valid initializers intact.
   Foo(std::string Arg) : A(Arg), B(""), C("NonEmpty"), D(R"()"), E("") {}
Index: clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
+++ clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
@@ -19,12 +19,21 @@
   std::string a;
   std::string b;
 
+  //

[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-11-18 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly updated this revision to Diff 306302.
ckennelly added a comment.

Added option to allow rewrites for default to value initialization


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

Files:
  clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
  clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
  clang-tools-extra/docs/clang-tidy/checks/modernize-make-shared.rst
  clang-tools-extra/docs/clang-tidy/checks/modernize-make-unique.rst
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-shared.cpp
  
clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique-default-init.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
@@ -83,49 +83,60 @@
   // CHECK-FIXES: return std::make_unique();
 }
 
+std::unique_ptr getPointerValue() {
+  return std::unique_ptr(new Base());
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead
+  // CHECK-FIXES: return std::make_unique();
+}
+
 void basic() {
   std::unique_ptr P1 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: std::unique_ptr P1 = std::make_unique();
+  std::unique_ptr P2 = std::unique_ptr(new int);
 
   P1.reset(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P1 = std::make_unique();
+  P2.reset(new int);
 
   P1 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P1 = std::make_unique();
+  P1 = std::unique_ptr(new int);
 
-  // Without parenthesis.
-  std::unique_ptr P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P2 = std::make_unique();
+  // Without parenthesis, default initialization.
+  std::unique_ptr P3 = std::unique_ptr(new int);
 
   P2.reset(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   // With auto.
-  auto P3 = std::unique_ptr(new int());
+  auto P4 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
-  // CHECK-FIXES: auto P3 = std::make_unique();
+  // CHECK-FIXES: auto P4 = std::make_unique();
+  auto P5 = std::unique_ptr(new int);
 
-  std::unique_ptr P4 = std::unique_ptr((new int));
+  std::unique_ptr P6 = std::unique_ptr((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P4 = std::make_unique();
-  P4.reset((new int));
+  // CHECK-FIXES: std::unique_ptr P6 = std::make_unique();
+  std::unique_ptr P7 = std::unique_ptr((new int));
+
+  P4.reset((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P4 = std::make_unique();
-  std::unique_ptr P5 = std::unique_ptrnew int;
+  P5.reset((new int));
+
+  std::unique_ptr P8 = std::unique_ptrnew int();
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P5 = std::make_unique();
-  P5.reset(new int);
+  // CHECK-FIXES: std::unique_ptr P8 = std::make_unique();
+  std::unique_ptr P9 = std::unique_ptrnew int;
+
+  P5.reset(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P5 = std::make_unique();
+  P6.reset(new int);
 
   {
 // No std.
@@ -133,40 +144,55 @@
 unique_ptr Q = unique_ptr(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use std::make_unique instead
 // CHECK-FIXES: unique_ptr Q = std::make_unique();
+unique_ptr P = unique_ptr(new int);
 
 Q = unique_ptr(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
 // CHECK-FIXES: Q = std::make_unique();
+
+P = unique_ptr(new int);
   }
 
   std::unique_ptr R(new int());
+  std::unique_ptr S(new int);
 
   // Create the unique_ptr as a parameter to a function.
   int T = g(std::unique_ptr(new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
   // CHECK-FIXES: int T = g

[PATCH] D91015: [clang-tidy] Extend bugprone-string-constructor-check to std::string_view.

2020-11-18 Thread Chris Kennelly via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG25f5406f0875: [clang-tidy] Extend 
bugprone-string-constructor-check to std::string_view. (authored by ckennelly).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91015/new/

https://reviews.llvm.org/D91015

Files:
  clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp
  clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
  clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
  clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp
@@ -14,6 +14,15 @@
 };
 typedef basic_string string;
 typedef basic_string wstring;
+
+template >
+struct basic_string_view {
+  basic_string_view();
+  basic_string_view(const C *, unsigned int size);
+  basic_string_view(const C *);
+};
+typedef basic_string_view string_view;
+typedef basic_string_view wstring_view;
 }
 
 const char* kText = "";
@@ -52,11 +61,35 @@
   // CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructing string from nullptr is undefined behaviour
 }
 
+void TestView() {
+  std::string_view q0("test", 0);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructor creating an empty string
+  std::string_view q1(kText, -4);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: negative value used as length parameter
+  std::string_view q2("test", 200);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
+  std::string_view q3(kText, 200);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
+  std::string_view q4(kText2, 200);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
+  std::string_view q5(kText3, 0x100);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: suspicious large length parameter
+  std::string_view q6(nullptr);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructing string from nullptr is undefined behaviour
+  std::string_view q7 = 0;
+  // CHECK-MESSAGES: [[@LINE-1]]:25: warning: constructing string from nullptr is undefined behaviour
+}
+
 std::string StringFromZero() {
   return 0;
   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: constructing string from nullptr is undefined behaviour
 }
 
+std::string_view StringViewFromZero() {
+  return 0;
+  // CHECK-MESSAGES: [[@LINE-1]]:10: warning: constructing string from nullptr is undefined behaviour
+}
+
 void Valid() {
   std::string empty();
   std::string str(4, 'x');
@@ -64,6 +97,11 @@
   std::string s1("test", 4);
   std::string s2("test", 3);
   std::string s3("test");
+
+  std::string_view emptyv();
+  std::string_view sv1("test", 4);
+  std::string_view sv2("test", 3);
+  std::string_view sv3("test");
 }
 
 namespace instantiation_dependent_exprs {
@@ -71,5 +109,6 @@
 struct S {
   bool x;
   std::string f() { return x ? "a" : "b"; }
+  std::string_view g() { return x ? "a" : "b"; }
 };
 }
Index: clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
+++ clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
@@ -21,6 +21,7 @@
 .. code-block:: c++
 
   std::string("test", 200);   // Will include random characters after "test".
+  std::string_view("test", 200);
 
 Creating an empty string from constructors with parameters is considered
 suspicious. The programmer should use the empty constructor instead.
@@ -30,6 +31,7 @@
 .. code-block:: c++
 
   std::string("test", 0);   // Creation of an empty string.
+  std::string_view("test", 0);
 
 Options
 ---
@@ -42,3 +44,12 @@
 .. option::  LargeLengthThreshold
 
An integer specifying the large length threshold. Default is `0x80`.
+
+.. option:: StringNames
+
+Default is `::std::basic_string;::std::basic_string_view`.
+
+Semicolon-delimited list of class names to apply this check to.
+By default `::std::basic_string` applies to ``std::string`` and
+``std::wstring``. Set to e.g. `::std::basic_string;llvm::StringRef;QString`
+to perform this check on custom classes.
Index: clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
===
--- clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
+++ clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
@@ -32,6 +32,7 @@
 private:
   const bool WarnOnLargeLength;
   const unsigned int LargeLengthThreshold;
+  std::vector StringNames;
 };
 
 } // n

[PATCH] D91009: [clang-tidy] Include std::basic_string_view in readability-redundant-string-init.

2020-11-16 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added a comment.

Ping


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91009/new/

https://reviews.llvm.org/D91009

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


[PATCH] D91015: [clang-tidy] Extend bugprone-string-constructor-check to std::string_view.

2020-11-16 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added inline comments.



Comment at: clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp:65
+  const auto hasStringCtorName =
+  hasAnyNameStdString(removeNamespaces(StringNames));
+

aaron.ballman wrote:
> It took me a while to realize that this functionality is really about trying 
> to get to the name of the constructor for a given type -- perhaps this should 
> be a single function called `getCtorNameFromType()` or something?
> 
> Actually, given that this is a narrowing matcher from a `CXXConstructExpr`, 
> can't you look at `CXXConstructExpr::getConstructor()` to get the 
> `CXXConstructorDecl` and check the name from there? That seems cleaner than 
> trying to identify the constructor name through string matching.
I applied ymandel's suggestion, which I think simplified things a bit.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91015/new/

https://reviews.llvm.org/D91015

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


[PATCH] D91015: [clang-tidy] Extend bugprone-string-constructor-check to std::string_view.

2020-11-16 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly updated this revision to Diff 305652.
ckennelly marked 5 inline comments as done.
ckennelly added a comment.

Applied review comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91015/new/

https://reviews.llvm.org/D91015

Files:
  clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp
  clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
  clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
  clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp
@@ -14,6 +14,15 @@
 };
 typedef basic_string string;
 typedef basic_string wstring;
+
+template >
+struct basic_string_view {
+  basic_string_view();
+  basic_string_view(const C *, unsigned int size);
+  basic_string_view(const C *);
+};
+typedef basic_string_view string_view;
+typedef basic_string_view wstring_view;
 }
 
 const char* kText = "";
@@ -52,11 +61,35 @@
   // CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructing string from nullptr is undefined behaviour
 }
 
+void TestView() {
+  std::string_view q0("test", 0);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructor creating an empty string
+  std::string_view q1(kText, -4);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: negative value used as length parameter
+  std::string_view q2("test", 200);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
+  std::string_view q3(kText, 200);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
+  std::string_view q4(kText2, 200);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
+  std::string_view q5(kText3, 0x100);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: suspicious large length parameter
+  std::string_view q6(nullptr);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructing string from nullptr is undefined behaviour
+  std::string_view q7 = 0;
+  // CHECK-MESSAGES: [[@LINE-1]]:25: warning: constructing string from nullptr is undefined behaviour
+}
+
 std::string StringFromZero() {
   return 0;
   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: constructing string from nullptr is undefined behaviour
 }
 
+std::string_view StringViewFromZero() {
+  return 0;
+  // CHECK-MESSAGES: [[@LINE-1]]:10: warning: constructing string from nullptr is undefined behaviour
+}
+
 void Valid() {
   std::string empty();
   std::string str(4, 'x');
@@ -64,6 +97,11 @@
   std::string s1("test", 4);
   std::string s2("test", 3);
   std::string s3("test");
+
+  std::string_view emptyv();
+  std::string_view sv1("test", 4);
+  std::string_view sv2("test", 3);
+  std::string_view sv3("test");
 }
 
 namespace instantiation_dependent_exprs {
@@ -71,5 +109,6 @@
 struct S {
   bool x;
   std::string f() { return x ? "a" : "b"; }
+  std::string_view g() { return x ? "a" : "b"; }
 };
 }
Index: clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
+++ clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
@@ -21,6 +21,7 @@
 .. code-block:: c++
 
   std::string("test", 200);   // Will include random characters after "test".
+  std::string_view("test", 200);
 
 Creating an empty string from constructors with parameters is considered
 suspicious. The programmer should use the empty constructor instead.
@@ -30,6 +31,7 @@
 .. code-block:: c++
 
   std::string("test", 0);   // Creation of an empty string.
+  std::string_view("test", 0);
 
 Options
 ---
@@ -42,3 +44,12 @@
 .. option::  LargeLengthThreshold
 
An integer specifying the large length threshold. Default is `0x80`.
+
+.. option:: StringNames
+
+Default is `::std::basic_string;::std::basic_string_view`.
+
+Semicolon-delimited list of class names to apply this check to.
+By default `::std::basic_string` applies to ``std::string`` and
+``std::wstring``. Set to e.g. `::std::basic_string;llvm::StringRef;QString`
+to perform this check on custom classes.
Index: clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
===
--- clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
+++ clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
@@ -32,6 +32,7 @@
 private:
   const bool WarnOnLargeLength;
   const unsigned int LargeLengthThreshold;
+  std::vector StringNames;
 };
 
 } // namespace bugprone
Index: clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp
=

[PATCH] D91015: [clang-tidy] Extend bugprone-string-constructor-check to std::string_view.

2020-11-16 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly updated this revision to Diff 305649.
ckennelly added a comment.

Applied review feedback


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91015/new/

https://reviews.llvm.org/D91015

Files:
  clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp
  clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
  clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
  clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp
@@ -14,6 +14,15 @@
 };
 typedef basic_string string;
 typedef basic_string wstring;
+
+template >
+struct basic_string_view {
+  basic_string_view();
+  basic_string_view(const C *, unsigned int size);
+  basic_string_view(const C *);
+};
+typedef basic_string_view string_view;
+typedef basic_string_view wstring_view;
 }
 
 const char* kText = "";
@@ -52,11 +61,35 @@
   // CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructing string from nullptr is undefined behaviour
 }
 
+void TestView() {
+  std::string_view q0("test", 0);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructor creating an empty string
+  std::string_view q1(kText, -4);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: negative value used as length parameter
+  std::string_view q2("test", 200);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
+  std::string_view q3(kText, 200);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
+  std::string_view q4(kText2, 200);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
+  std::string_view q5(kText3, 0x100);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: suspicious large length parameter
+  std::string_view q6(nullptr);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructing string from nullptr is undefined behaviour
+  std::string_view q7 = 0;
+  // CHECK-MESSAGES: [[@LINE-1]]:25: warning: constructing string from nullptr is undefined behaviour
+}
+
 std::string StringFromZero() {
   return 0;
   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: constructing string from nullptr is undefined behaviour
 }
 
+std::string_view StringViewFromZero() {
+  return 0;
+  // CHECK-MESSAGES: [[@LINE-1]]:10: warning: constructing string from nullptr is undefined behaviour
+}
+
 void Valid() {
   std::string empty();
   std::string str(4, 'x');
@@ -64,6 +97,11 @@
   std::string s1("test", 4);
   std::string s2("test", 3);
   std::string s3("test");
+
+  std::string_view emptyv();
+  std::string_view sv1("test", 4);
+  std::string_view sv2("test", 3);
+  std::string_view sv3("test");
 }
 
 namespace instantiation_dependent_exprs {
@@ -71,5 +109,6 @@
 struct S {
   bool x;
   std::string f() { return x ? "a" : "b"; }
+  std::string_view g() { return x ? "a" : "b"; }
 };
 }
Index: clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
+++ clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
@@ -21,6 +21,7 @@
 .. code-block:: c++
 
   std::string("test", 200);   // Will include random characters after "test".
+  std::string_view("test", 200);
 
 Creating an empty string from constructors with parameters is considered
 suspicious. The programmer should use the empty constructor instead.
@@ -30,6 +31,7 @@
 .. code-block:: c++
 
   std::string("test", 0);   // Creation of an empty string.
+  std::string_view("test", 0);
 
 Options
 ---
@@ -42,3 +44,12 @@
 .. option::  LargeLengthThreshold
 
An integer specifying the large length threshold. Default is `0x80`.
+
+.. option:: StringNames
+
+Default is `::std::basic_string;::std::basic_string_view`.
+
+Semicolon-delimited list of class names to apply this check to.
+By default `::std::basic_string` applies to ``std::string`` and
+``std::wstring``. Set to e.g. `::std::basic_string;llvm::StringRef;QString`
+to perform this check on custom classes.
Index: clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
===
--- clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
+++ clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
@@ -32,6 +32,7 @@
 private:
   const bool WarnOnLargeLength;
   const unsigned int LargeLengthThreshold;
+  std::vector StringNames;
 };
 
 } // namespace bugprone
Index: clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp
===
--- c

[PATCH] D91009: [clang-tidy] Include std::basic_string_view in readability-redundant-string-init.

2020-11-09 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly updated this revision to Diff 304034.
ckennelly added a comment.

Applied feedback from review

- reduced test cases to verify functionality
- updated release notes
- updated docs


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91009/new/

https://reviews.llvm.org/D91009

Files:
  clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
  
clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
@@ -1,7 +1,7 @@
 // RUN: %check_clang_tidy -std=c++11,c++14 %s readability-redundant-string-init %t \
 // RUN:   -config="{CheckOptions: \
 // RUN: [{key: readability-redundant-string-init.StringNames, \
-// RUN:   value: '::std::basic_string;our::TestString'}] \
+// RUN:   value: '::std::basic_string;::std::basic_string_view;our::TestString'}] \
 // RUN: }"
 // FIXME: Fix the checker to work in C++17 mode.
 
@@ -19,6 +19,20 @@
 };
 typedef basic_string string;
 typedef basic_string wstring;
+
+template , typename A = std::allocator>
+struct basic_string_view {
+  using size_type = unsigned;
+
+  basic_string_view();
+  basic_string_view(const basic_string_view &);
+  basic_string_view(const C *, size_type);
+  basic_string_view(const C *, const A &a = A());
+  template 
+  basic_string_view(It, End);
+};
+typedef basic_string_view string_view;
+typedef basic_string_view wstring_view;
 }
 
 void f() {
@@ -48,6 +62,33 @@
   std::string z;
 }
 
+void fview() {
+  std::string_view a = "";
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization [readability-redundant-string-init]
+  // CHECK-FIXES: std::string_view a;
+  std::string_view b("");
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view b;
+  std::string_view c = R"()";
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view c;
+  std::string_view d(R"()");
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view d;
+  std::string_view e{""};
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view e;
+  std::string_view f = {""};
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view f;
+
+  std::string_view u = "u";
+  std::string_view w("w");
+  std::string_view x = R"(x)";
+  std::string_view y(R"(y)");
+  std::string_view z;
+}
+
 void g() {
   std::wstring a = L"";
   // CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
@@ -274,7 +315,6 @@
   // CHECK-MESSAGES: [[@LINE-2]]:23: warning: redundant string initialization
   // CHECK-FIXES:  Foo(float)  {}
 
-
   // Check how it handles removing some redundant initializers while leaving
   // valid initializers intact.
   Foo(std::string Arg) : A(Arg), B(""), C("NonEmpty"), D(R"()"), E("") {}
Index: clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
+++ clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
@@ -19,12 +19,21 @@
   std::string a;
   std::string b;
 
+  // Initializing a string_view with an empty string literal produces an
+  // instance that compares equal to string_view().
+  std::string_view a = "";
+  std::string_view b("");
+
+  // becomes
+  std::string_view a;
+  std::string_view b;
+
 Options
 ---
 
 .. option:: StringNames
 
-Default is `::std::basic_string`.
+Default is `::std::basic_string;::std::basic_string_view`.
 
 Semicolon-delimited list of class names to apply this check to.
 By default `::std::basic_string` applies to ``std::string`` and
Index: clang-tools-extra/docs/ReleaseNotes.rst
===
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -146,6 +146,11 @@
 - Removed `google-runtime-references` check because the rule it checks does
   not exist in the Google Style Guide anymore.
 
+- Improved :doc:`readability-redundant-string-init
+  ` check.
+
+  Added `std::basic_string_view` to default list of ``string``-like types.
+
 Improvements to include-fixer
 -
 
Index: clang-tools-extra/clang-tidy/readability/Redunda

[PATCH] D91009: [clang-tidy] Include std::basic_string_view in readability-redundant-string-init.

2020-11-09 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added a comment.

In D91009#2381590 , @njames93 wrote:

> You don't really need to duplicate every single `std::string` test. Just 
> enough so that it detects it, kind of like `::out::TestString`. Also can you 
> update the docs and release notes to explain this behaviour.

Done


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91009/new/

https://reviews.llvm.org/D91009

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


[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-11-09 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added a comment.

njames93: Ping




Comment at: 
clang-tools-extra/test/clang-tidy/checkers/modernize-make-shared.cpp:51
   // CHECK-FIXES: std::shared_ptr P1 = std::make_shared();
+  std::shared_ptr P2 = std::shared_ptr(new int);
 

steveire wrote:
> ckennelly wrote:
> > steveire wrote:
> > > I'm a bit confused. Why don't we want to transform this?
> > `std::make_shared()` is equivalent to `std::shared_ptr(new 
> > int())`.  This value initializes (read: zeroes out the value).
> > 
> > The statement here is only default initializing (read: leaves the value 
> > uninitialized).  For this use case, we should use 
> > `std::make_shared_for_overwrite` when it is available.
> I see. Did you consider an option to transform these anyway? It seems at 
> least as likely that for real-world code bases they are not initialized 
> accidentally (as opposed to being a deliberate choice as assumed here).
The default initialization case for arrays was added because it caused several 
performance regressions.  I want to get the complete initialization check 
implemented, which will then make the `make_unique_for_overwrite` rewrite 
straight forward (just `!initializating`).

An option could then be added in a subsequent commit.



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

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


[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-11-07 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added inline comments.



Comment at: 
clang-tools-extra/test/clang-tidy/checkers/modernize-make-shared.cpp:51
   // CHECK-FIXES: std::shared_ptr P1 = std::make_shared();
+  std::shared_ptr P2 = std::shared_ptr(new int);
 

steveire wrote:
> I'm a bit confused. Why don't we want to transform this?
`std::make_shared()` is equivalent to `std::shared_ptr(new int())`.  
This value initializes (read: zeroes out the value).

The statement here is only default initializing (read: leaves the value 
uninitialized).  For this use case, we should use 
`std::make_shared_for_overwrite` when it is available.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

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


[PATCH] D91015: Extend bugprone-string-constructor-check to std::string_view.

2020-11-07 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly created this revision.
ckennelly added reviewers: EricWF, ymandel.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
ckennelly requested review of this revision.

This allows for matching the constructors std::string has in common with
std::string_view.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D91015

Files:
  clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp
  clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
  clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
  clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-string-constructor.cpp
@@ -14,6 +14,15 @@
 };
 typedef basic_string string;
 typedef basic_string wstring;
+
+template >
+struct basic_string_view {
+  basic_string_view();
+  basic_string_view(const C *, unsigned int size);
+  basic_string_view(const C *);
+};
+typedef basic_string_view string_view;
+typedef basic_string_view wstring_view;
 }
 
 const char* kText = "";
@@ -52,11 +61,35 @@
   // CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructing string from nullptr is undefined behaviour
 }
 
+void TestView() {
+  std::string_view q0("test", 0);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructor creating an empty string
+  std::string_view q1(kText, -4);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: negative value used as length parameter
+  std::string_view q2("test", 200);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
+  std::string_view q3(kText, 200);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
+  std::string_view q4(kText2, 200);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
+  std::string_view q5(kText3, 0x100);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: suspicious large length parameter
+  std::string_view q6(nullptr);
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructing string from nullptr is undefined behaviour
+  std::string_view q7 = 0;
+  // CHECK-MESSAGES: [[@LINE-1]]:25: warning: constructing string from nullptr is undefined behaviour
+}
+
 std::string StringFromZero() {
   return 0;
   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: constructing string from nullptr is undefined behaviour
 }
 
+std::string_view StringViewFromZero() {
+  return 0;
+  // CHECK-MESSAGES: [[@LINE-1]]:10: warning: constructing string from nullptr is undefined behaviour
+}
+
 void Valid() {
   std::string empty();
   std::string str(4, 'x');
@@ -64,6 +97,11 @@
   std::string s1("test", 4);
   std::string s2("test", 3);
   std::string s3("test");
+
+  std::string_view emptyv();
+  std::string_view sv1("test", 4);
+  std::string_view sv2("test", 3);
+  std::string_view sv3("test");
 }
 
 namespace instantiation_dependent_exprs {
@@ -71,5 +109,6 @@
 struct S {
   bool x;
   std::string f() { return x ? "a" : "b"; }
+  std::string_view g() { return x ? "a" : "b"; }
 };
 }
Index: clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
+++ clang-tools-extra/docs/clang-tidy/checks/bugprone-string-constructor.rst
@@ -21,6 +21,7 @@
 .. code-block:: c++
 
   std::string("test", 200);   // Will include random characters after "test".
+  std::string_view("test", 200);
 
 Creating an empty string from constructors with parameters is considered
 suspicious. The programmer should use the empty constructor instead.
@@ -30,6 +31,7 @@
 .. code-block:: c++
 
   std::string("test", 0);   // Creation of an empty string.
+  std::string_view("test", 0);
 
 Options
 ---
@@ -42,3 +44,12 @@
 .. option::  LargeLengthThreshold
 
An integer specifying the large length threshold. Default is `0x80`.
+
+.. option:: StringNames
+
+Default is `::std::basic_string;::std::basic_string_view`.
+
+Semicolon-delimited list of class names to apply this check to.
+By default `::std::basic_string` applies to ``std::string`` and
+``std::wstring``. Set to e.g. `::std::basic_string;llvm::StringRef;QString`
+to perform this check on custom classes.
Index: clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
===
--- clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
+++ clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
@@ -32,6 +32,7 @@
 private:
   const bool WarnOnLargeLength;
   const unsigned int LargeLengthThreshold;
+  std::vector StringNames;
 };
 
 } // namespace bugprone
Index: clang-tools-extra/c

[PATCH] D91009: Include std::basic_string_view in readability-redundant-string-init.

2020-11-07 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly created this revision.
ckennelly added reviewers: EricWF, ymandel.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
ckennelly requested review of this revision.

std::string_view("") produces a string_view instance that compares
equal to std::string_view(), but requires more complex initialization
(storing the address of the string literal, rather than zeroing).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D91009

Files:
  clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
  
clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
@@ -1,7 +1,7 @@
 // RUN: %check_clang_tidy -std=c++11,c++14 %s readability-redundant-string-init %t \
 // RUN:   -config="{CheckOptions: \
 // RUN: [{key: readability-redundant-string-init.StringNames, \
-// RUN:   value: '::std::basic_string;our::TestString'}] \
+// RUN:   value: '::std::basic_string;::std::basic_string_view;our::TestString'}] \
 // RUN: }"
 // FIXME: Fix the checker to work in C++17 mode.
 
@@ -19,6 +19,20 @@
 };
 typedef basic_string string;
 typedef basic_string wstring;
+
+template , typename A = std::allocator>
+struct basic_string_view {
+  using size_type = unsigned;
+
+  basic_string_view();
+  basic_string_view(const basic_string_view &);
+  basic_string_view(const C *, size_type);
+  basic_string_view(const C *, const A &a = A());
+  template 
+  basic_string_view(It, End);
+};
+typedef basic_string_view string_view;
+typedef basic_string_view wstring_view;
 }
 
 void f() {
@@ -48,6 +62,33 @@
   std::string z;
 }
 
+void fview() {
+  std::string_view a = "";
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization [readability-redundant-string-init]
+  // CHECK-FIXES: std::string_view a;
+  std::string_view b("");
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view b;
+  std::string_view c = R"()";
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view c;
+  std::string_view d(R"()");
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view d;
+  std::string_view e{""};
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view e;
+  std::string_view f = {""};
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view f;
+
+  std::string_view u = "u";
+  std::string_view w("w");
+  std::string_view x = R"(x)";
+  std::string_view y(R"(y)");
+  std::string_view z;
+}
+
 void g() {
   std::wstring a = L"";
   // CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
@@ -69,6 +110,27 @@
   std::wstring z;
 }
 
+void gview() {
+  std::wstring_view a = L"";
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view a;
+  std::wstring_view b(L"");
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view b;
+  std::wstring_view c = LR"()";
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view c;
+  std::wstring_view d(LR"()");
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view d;
+
+  std::wstring_view u = L"u";
+  std::wstring_view w(L"w");
+  std::wstring_view x = LR"(x)";
+  std::wstring_view y(LR"(y)");
+  std::wstring_view z;
+}
+
 template 
 void templ() {
   std::string s = "";
@@ -121,6 +183,35 @@
   DECL_STRING(h, "u");
 }
 
+typedef std::string_view MyStringView;
+#define STRINGVIEW MyStringView
+#define DECL_STRINGVIEW(name, val) STRINGVIEW name = val
+
+void iview() {
+  MyStringView a = "";
+  // CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
+  // CHECK-FIXES: MyStringView a;
+  STRINGVIEW b = "";
+  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: redundant string initialization
+  // CHECK-FIXES: STRINGVIEW b;
+  MyStringView c = ""
+   ""
+   "";
+  // CHECK-MESSAGES: [[@LINE-3]]:16: warning: redundant string initialization
+  // CHECK-FIXES: MyStringView c;
+  STRINGVIEW d = ""
+ ""
+ "";
+  // CHECK-MESSAGES: [[@LINE-3]]:14: warning: redundant string initialization
+  // CHECK-FIXES: STRINGVIEW d;
+  DECL_STRINGVIEW(e, "");
+  // CHECK-MESSAGES: [[@LINE-1]]:19: warning: redundant string initialization
+
+  MyStringView f = "u";
+

[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-11-03 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly updated this revision to Diff 302731.
ckennelly added a comment.

Expand tests to cover value and default initialization


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

Files:
  clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-shared.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
@@ -83,49 +83,60 @@
   // CHECK-FIXES: return std::make_unique();
 }
 
+std::unique_ptr getPointerValue() {
+  return std::unique_ptr(new Base());
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead
+  // CHECK-FIXES: return std::make_unique();
+}
+
 void basic() {
   std::unique_ptr P1 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: std::unique_ptr P1 = std::make_unique();
+  std::unique_ptr P2 = std::unique_ptr(new int);
 
   P1.reset(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P1 = std::make_unique();
+  P2.reset(new int);
 
   P1 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P1 = std::make_unique();
+  P1 = std::unique_ptr(new int);
 
-  // Without parenthesis.
-  std::unique_ptr P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P2 = std::make_unique();
+  // Without parenthesis, default initialization.
+  std::unique_ptr P3 = std::unique_ptr(new int);
 
   P2.reset(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   // With auto.
-  auto P3 = std::unique_ptr(new int());
+  auto P4 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
-  // CHECK-FIXES: auto P3 = std::make_unique();
+  // CHECK-FIXES: auto P4 = std::make_unique();
+  auto P5 = std::unique_ptr(new int);
 
-  std::unique_ptr P4 = std::unique_ptr((new int));
+  std::unique_ptr P6 = std::unique_ptr((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P4 = std::make_unique();
-  P4.reset((new int));
+  // CHECK-FIXES: std::unique_ptr P6 = std::make_unique();
+  std::unique_ptr P7 = std::unique_ptr((new int));
+
+  P4.reset((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P4 = std::make_unique();
-  std::unique_ptr P5 = std::unique_ptrnew int;
+  P5.reset((new int));
+
+  std::unique_ptr P8 = std::unique_ptrnew int();
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P5 = std::make_unique();
-  P5.reset(new int);
+  // CHECK-FIXES: std::unique_ptr P8 = std::make_unique();
+  std::unique_ptr P9 = std::unique_ptrnew int;
+
+  P5.reset(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P5 = std::make_unique();
+  P6.reset(new int);
 
   {
 // No std.
@@ -133,40 +144,55 @@
 unique_ptr Q = unique_ptr(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use std::make_unique instead
 // CHECK-FIXES: unique_ptr Q = std::make_unique();
+unique_ptr P = unique_ptr(new int);
 
 Q = unique_ptr(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
 // CHECK-FIXES: Q = std::make_unique();
+
+P = unique_ptr(new int);
   }
 
   std::unique_ptr R(new int());
+  std::unique_ptr S(new int);
 
   // Create the unique_ptr as a parameter to a function.
   int T = g(std::unique_ptr(new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
   // CHECK-FIXES: int T = g(std::make_unique());
+  T = g(std::unique_ptr(new int));
 
   // Only replace if the type in the template is the same as the type returned
   // by the new operator.
   auto Pderived = std::unique_ptr(new Derived());
+  auto PderivedNoparen = std::unique_ptr(new Derived);
 
   // OK to replace 

[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-11-03 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added a comment.

Ping


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

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


[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-10-29 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added a comment.

In D90392#2362327 , @lebedev.ri wrote:

> In D90392#2362308 , @ckennelly wrote:
>
>> In D90392#2362118 , @njames93 wrote:
>>
>>> IIUC, this is handling the case where `Ptr.reset(new int)` which is 
>>> different to `Ptr.reset(new int())` because the former doesn't initialise 
>>> the int while the latter default(zero) initialises it.
>>> If that's correct I still think we should still warn about this case, but 
>>> don't suggest an auto-fix as that will change behaviour.
>>> Maybe put a note explaining why it can't be auto-fixed.
>>
>> I disagree with printing a warning but not a fix.
>>
>> These uses should migrate to 
>> `std::make_unique_for_overwrite`/`std::make_shared_for_overwrite`.  I am 
>> planning on sending a follow-up patch for that but want to avoid the 
>> existing make-unique/make-shared checks try to migrate default 
>> initialization use cases.
>
> Reminder that there is more than one version of C++ standard, and users are 
> not obligated to be using some particular version, and the checks should not 
> be making that decision for user.

Understood, but replacing `std::unique_ptr(new int)` with 
`std::make_unique()` does change program behavior.  Once the code migrates 
to value initialization, it can be hard to determine later whether that 
initialization ends up being unnecessary when C++20 or an analogue for 
`std::make_unique_for_overwrite` is available.

I'm hesitant to nudge users towards the behavior change when alternatives would 
be available (maintain the status quo, have C++20, preadopt a 
`make_unique_for_overwrite` helper like we do with this clang-tidy for 
pre-C++14 builds).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

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


[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-10-29 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added a comment.

In D90392#2362118 , @njames93 wrote:

> IIUC, this is handling the case where `Ptr.reset(new int)` which is different 
> to `Ptr.reset(new int())` because the former doesn't initialise the int while 
> the latter default(zero) initialises it.
> If that's correct I still think we should still warn about this case, but 
> don't suggest an auto-fix as that will change behaviour.
> Maybe put a note explaining why it can't be auto-fixed.

I disagree with printing a warning but not a fix.

These uses should migrate to 
`std::make_unique_for_overwrite`/`std::make_shared_for_overwrite`.  I am 
planning on sending a follow-up patch for that but want to avoid the existing 
make-unique/make-shared checks try to migrate default initialization use cases.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

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


[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-10-29 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly updated this revision to Diff 301649.
ckennelly marked 3 inline comments as done.
ckennelly added a comment.

Apply review feedback from njames93


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

Files:
  clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-shared.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
@@ -96,34 +96,28 @@
   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P1 = std::make_unique();
 
-  // Without parenthesis.
+  // Without parenthesis, default initialization.
   std::unique_ptr P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P2 = std::make_unique();
 
   P2.reset(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   // With auto.
   auto P3 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
   // CHECK-FIXES: auto P3 = std::make_unique();
 
-  std::unique_ptr P4 = std::unique_ptr((new int));
+  std::unique_ptr P4 = std::unique_ptr((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: std::unique_ptr P4 = std::make_unique();
-  P4.reset((new int));
+  P4.reset((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P4 = std::make_unique();
-  std::unique_ptr P5 = std::unique_ptrnew int;
+  std::unique_ptr P5 = std::unique_ptrnew int();
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: std::unique_ptr P5 = std::make_unique();
-  P5.reset(new int);
+  P5.reset(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P5 = std::make_unique();
 
@@ -137,6 +131,8 @@
 Q = unique_ptr(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
 // CHECK-FIXES: Q = std::make_unique();
+
+Q = unique_ptr(new int);
   }
 
   std::unique_ptr R(new int());
@@ -446,12 +442,12 @@
   // CHECK-FIXES: FFs = std::make_unique(Num2);
 
   std::unique_ptr FI;
-  FI.reset(new int[5]()); // default initialization.
+  FI.reset(new int[5]()); // value initialization.
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning:
   // CHECK-FIXES: FI = std::make_unique(5);
 
   // The check doesn't give warnings and fixes for cases where the original new
-  // expression doesn't do any initialization.
+  // expression does default initialization.
   FI.reset(new int[5]);
   FI.reset(new int[Num]);
   FI.reset(new int[Num2]);
@@ -459,13 +455,13 @@
 
 void aliases() {
   typedef std::unique_ptr IntPtr;
-  IntPtr Typedef = IntPtr(new int);
+  IntPtr Typedef = IntPtr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use std::make_unique instead
   // CHECK-FIXES: IntPtr Typedef = std::make_unique();
 
   // We use 'bool' instead of '_Bool'.
   typedef std::unique_ptr BoolPtr;
-  BoolPtr BoolType = BoolPtr(new bool);
+  BoolPtr BoolType = BoolPtr(new bool());
   // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use std::make_unique instead
   // CHECK-FIXES: BoolPtr BoolType = std::make_unique();
 
@@ -476,12 +472,12 @@
 // CHECK-FIXES: BasePtr StructType = std::make_unique();
 
 #define PTR unique_ptr
-  std::unique_ptr Macro = std::PTR(new int);
+  std::unique_ptr Macro = std::PTR(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead
 // CHECK-FIXES: std::unique_ptr Macro = std::make_unique();
 #undef PTR
 
-  std::unique_ptr Using = unique_ptr_(new int);
+  std::unique_ptr Using = unique_ptr_(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead
   // CHECK-FIXES: std::unique_ptr Using = std::make_unique();
 }
@@ -505,7 +501,7 @@
   Nest.reset(new std::unique_ptr(new int));
   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead
   // CHECK-FIXES: Nest = std::make_unique>(new int);
-  Nest->reset(new int);
+  Nest->reset(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:9: warn

[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-10-29 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly updated this revision to Diff 301648.
ckennelly added a comment.

Apply feedback from ymandel


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90392/new/

https://reviews.llvm.org/D90392

Files:
  clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-shared.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
@@ -96,34 +96,28 @@
   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P1 = std::make_unique();
 
-  // Without parenthesis.
+  // Without parenthesis, default initialization.
   std::unique_ptr P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P2 = std::make_unique();
 
   P2.reset(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   // With auto.
   auto P3 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
   // CHECK-FIXES: auto P3 = std::make_unique();
 
-  std::unique_ptr P4 = std::unique_ptr((new int));
+  std::unique_ptr P4 = std::unique_ptr((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: std::unique_ptr P4 = std::make_unique();
-  P4.reset((new int));
+  P4.reset((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P4 = std::make_unique();
-  std::unique_ptr P5 = std::unique_ptrnew int;
+  std::unique_ptr P5 = std::unique_ptrnew int();
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: std::unique_ptr P5 = std::make_unique();
-  P5.reset(new int);
+  P5.reset(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P5 = std::make_unique();
 
@@ -137,6 +131,8 @@
 Q = unique_ptr(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
 // CHECK-FIXES: Q = std::make_unique();
+
+Q = unique_ptr(new int);
   }
 
   std::unique_ptr R(new int());
@@ -446,12 +442,12 @@
   // CHECK-FIXES: FFs = std::make_unique(Num2);
 
   std::unique_ptr FI;
-  FI.reset(new int[5]()); // default initialization.
+  FI.reset(new int[5]()); // value initialization.
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning:
   // CHECK-FIXES: FI = std::make_unique(5);
 
   // The check doesn't give warnings and fixes for cases where the original new
-  // expression doesn't do any initialization.
+  // expression does default initialization.
   FI.reset(new int[5]);
   FI.reset(new int[Num]);
   FI.reset(new int[Num2]);
@@ -459,13 +455,13 @@
 
 void aliases() {
   typedef std::unique_ptr IntPtr;
-  IntPtr Typedef = IntPtr(new int);
+  IntPtr Typedef = IntPtr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use std::make_unique instead
   // CHECK-FIXES: IntPtr Typedef = std::make_unique();
 
   // We use 'bool' instead of '_Bool'.
   typedef std::unique_ptr BoolPtr;
-  BoolPtr BoolType = BoolPtr(new bool);
+  BoolPtr BoolType = BoolPtr(new bool());
   // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use std::make_unique instead
   // CHECK-FIXES: BoolPtr BoolType = std::make_unique();
 
@@ -476,12 +472,12 @@
 // CHECK-FIXES: BasePtr StructType = std::make_unique();
 
 #define PTR unique_ptr
-  std::unique_ptr Macro = std::PTR(new int);
+  std::unique_ptr Macro = std::PTR(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead
 // CHECK-FIXES: std::unique_ptr Macro = std::make_unique();
 #undef PTR
 
-  std::unique_ptr Using = unique_ptr_(new int);
+  std::unique_ptr Using = unique_ptr_(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead
   // CHECK-FIXES: std::unique_ptr Using = std::make_unique();
 }
@@ -505,7 +501,7 @@
   Nest.reset(new std::unique_ptr(new int));
   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead
   // CHECK-FIXES: Nest = std::make_unique>(new int);
-  Nest->reset(new int);
+  Nest->reset(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
   // CHECK-FIXES:

[PATCH] D90392: [clang-tidy] Omit std::make_unique/make_shared for default initialization.

2020-10-29 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly created this revision.
ckennelly added reviewers: EricWF, ymandel.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.
ckennelly requested review of this revision.

This extends the check for default initialization in arrays added in
547f89d6070 to include scalar types and exclude them from the suggested fix for
make_unique/make_shared.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D90392

Files:
  clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-shared.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp
@@ -96,34 +96,28 @@
   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P1 = std::make_unique();
 
-  // Without parenthesis.
+  // Without parenthesis, default initialization.
   std::unique_ptr P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: std::unique_ptr P2 = std::make_unique();
 
   P2.reset(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   P2 = std::unique_ptr(new int);
-  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
-  // CHECK-FIXES: P2 = std::make_unique();
 
   // With auto.
   auto P3 = std::unique_ptr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
   // CHECK-FIXES: auto P3 = std::make_unique();
 
-  std::unique_ptr P4 = std::unique_ptr((new int));
+  std::unique_ptr P4 = std::unique_ptr((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: std::unique_ptr P4 = std::make_unique();
-  P4.reset((new int));
+  P4.reset((new int()));
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P4 = std::make_unique();
-  std::unique_ptr P5 = std::unique_ptrnew int;
+  std::unique_ptr P5 = std::unique_ptrnew int();
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: std::unique_ptr P5 = std::make_unique();
-  P5.reset(new int);
+  P5.reset(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
   // CHECK-FIXES: P5 = std::make_unique();
 
@@ -137,6 +131,8 @@
 Q = unique_ptr(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
 // CHECK-FIXES: Q = std::make_unique();
+
+Q = unique_ptr(new int);
   }
 
   std::unique_ptr R(new int());
@@ -446,12 +442,12 @@
   // CHECK-FIXES: FFs = std::make_unique(Num2);
 
   std::unique_ptr FI;
-  FI.reset(new int[5]()); // default initialization.
+  FI.reset(new int[5]()); // value initialization.
   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning:
   // CHECK-FIXES: FI = std::make_unique(5);
 
   // The check doesn't give warnings and fixes for cases where the original new
-  // expression doesn't do any initialization.
+  // expression does default initialization.
   FI.reset(new int[5]);
   FI.reset(new int[Num]);
   FI.reset(new int[Num2]);
@@ -459,13 +455,13 @@
 
 void aliases() {
   typedef std::unique_ptr IntPtr;
-  IntPtr Typedef = IntPtr(new int);
+  IntPtr Typedef = IntPtr(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use std::make_unique instead
   // CHECK-FIXES: IntPtr Typedef = std::make_unique();
 
   // We use 'bool' instead of '_Bool'.
   typedef std::unique_ptr BoolPtr;
-  BoolPtr BoolType = BoolPtr(new bool);
+  BoolPtr BoolType = BoolPtr(new bool());
   // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use std::make_unique instead
   // CHECK-FIXES: BoolPtr BoolType = std::make_unique();
 
@@ -476,12 +472,12 @@
 // CHECK-FIXES: BasePtr StructType = std::make_unique();
 
 #define PTR unique_ptr
-  std::unique_ptr Macro = std::PTR(new int);
+  std::unique_ptr Macro = std::PTR(new int());
 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead
 // CHECK-FIXES: std::unique_ptr Macro = std::make_unique();
 #undef PTR
 
-  std::unique_ptr Using = unique_ptr_(new int);
+  std::unique_ptr Using = unique_ptr_(new int());
   // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead
   // CHECK-FIXES: std::unique_ptr Using = std::make_unique();
 }
@@ -505,7 +501,7 @@
   Nest.reset(new std::unique_ptr(new int));
   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique 

[PATCH] D55741: Implementation Feature Test Macros for P0722R3

2019-01-18 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added a comment.

In D55741#1361040 , @jwakely wrote:

> I'd be happy to restrict this to > C++17 only (which is automatically the 
> case when using G++ because there's no `-fdestroying-delete` to enable it, 
> you only get it with `-std=c++2a`,  `-std=gnu++2a` etc.)


I'd like to see this available earlier (say >=C++17) so that it is possible to 
pre-adopt the feature before switching to C++2a (assuming appropriate language 
support for the feature from the compiler).  Otherwise, it's necessary to open 
up `std` to get to a simple type that happens to have magic language 
implications.


Repository:
  rC Clang

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D55741/new/

https://reviews.llvm.org/D55741



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


[PATCH] D55741: Implementation Feature Test Macros for P0722R3

2019-01-16 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added a comment.

Conditioning off of C++17 (or part C++11 and part C++17) seems reasonable, 
although we may want consistency with libc++ (discussion on 
https://reviews.llvm.org/D55840).


Repository:
  rC Clang

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D55741/new/

https://reviews.llvm.org/D55741



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


[PATCH] D55741: Implementation Feature Test Macros for P0722R3

2019-01-11 Thread Chris Kennelly via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rC350934: Implementation Feature Test Macros for P0722R3 
(authored by ckennelly, committed by ).

Repository:
  rC Clang

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D55741/new/

https://reviews.llvm.org/D55741

Files:
  lib/Frontend/InitPreprocessor.cpp
  test/Lexer/cxx-features.cpp
  www/cxx_status.html


Index: www/cxx_status.html
===
--- www/cxx_status.html
+++ www/cxx_status.html
@@ -1054,9 +1054,9 @@
 Available in Clang?
  
 
-  SD-6: SG10 feature test recommendations
-  http://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations";>SD-6
-  N/A
+  SD-6: SG10 feature test recommendations
+  http://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations";>SD-6
+  N/A
   
 Clang 3.4 (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3745";>N3745)
   
@@ -1081,6 +1081,11 @@
 Clang 7 (http://wg21.link/p0096r5";>P0096R5)
   
 
+
+  
+WIP (http://wg21.link/p1353r0";>P1353R0)
+  
+
 

[PATCH] D55741: Implementation Feature Test Macros for P0722R3

2018-12-17 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly added a comment.

Yes, I need someone to commit for me.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D55741/new/

https://reviews.llvm.org/D55741



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


[PATCH] D55741: Implementation Feature Test Macros for P0722R3

2018-12-16 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly updated this revision to Diff 178423.
ckennelly added a comment.

Added update to cxx_status.html for partial implementation of feature test 
macros adopted in San Diego


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D55741/new/

https://reviews.llvm.org/D55741

Files:
  lib/Frontend/InitPreprocessor.cpp
  test/Lexer/cxx-features.cpp
  www/cxx_status.html


Index: www/cxx_status.html
===
--- www/cxx_status.html
+++ www/cxx_status.html
@@ -1054,9 +1054,9 @@
 Available in Clang?
  
 
-  SD-6: SG10 feature test recommendations
-  http://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations";>SD-6
-  N/A
+  SD-6: SG10 feature test recommendations
+  http://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations";>SD-6
+  N/A
   
 Clang 3.4 (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3745";>N3745)
   
@@ -1081,6 +1081,11 @@
 Clang 7 (http://wg21.link/p0096r5";>P0096R5)
   
 
+
+  
+WIP (http://wg21.link/p1353r0";>P1353R0)
+  
+
 

[PATCH] D55741: Implementation Feature Test Macros for P0722R3

2018-12-15 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly created this revision.
ckennelly added a reviewer: rsmith.
Herald added a subscriber: cfe-commits.

P1353R0, adopted in San Diego, specified an implementation feature test macro 
for destroying delete (P0722R3).

The implementation of the feature (https://reviews.llvm.org/rL315662) is not 
guarded behind a flag, so the macro is not conditional on language version.


Repository:
  rC Clang

https://reviews.llvm.org/D55741

Files:
  lib/Frontend/InitPreprocessor.cpp
  test/Lexer/cxx-features.cpp


Index: test/Lexer/cxx-features.cpp
===
--- test/Lexer/cxx-features.cpp
+++ test/Lexer/cxx-features.cpp
@@ -34,6 +34,10 @@
 #error "wrong value for __cpp_char8_t"
 #endif
 
+#if check(impl_destroying_delete, 201806, 201806, 201806, 201806, 201806)
+#error "wrong value for __cpp_impl_destroying_delete"
+#endif
+
 // --- C++17 features ---
 
 #if check(hex_float, 0, 0, 0, 201603, 201603)
Index: lib/Frontend/InitPreprocessor.cpp
===
--- lib/Frontend/InitPreprocessor.cpp
+++ lib/Frontend/InitPreprocessor.cpp
@@ -543,6 +543,7 @@
   // C++20 features.
   if (LangOpts.Char8)
 Builder.defineMacro("__cpp_char8_t", "201811L");
+  Builder.defineMacro("__cpp_impl_destroying_delete", "201806L");
 
   // TS features.
   if (LangOpts.ConceptsTS)


Index: test/Lexer/cxx-features.cpp
===
--- test/Lexer/cxx-features.cpp
+++ test/Lexer/cxx-features.cpp
@@ -34,6 +34,10 @@
 #error "wrong value for __cpp_char8_t"
 #endif
 
+#if check(impl_destroying_delete, 201806, 201806, 201806, 201806, 201806)
+#error "wrong value for __cpp_impl_destroying_delete"
+#endif
+
 // --- C++17 features ---
 
 #if check(hex_float, 0, 0, 0, 201603, 201603)
Index: lib/Frontend/InitPreprocessor.cpp
===
--- lib/Frontend/InitPreprocessor.cpp
+++ lib/Frontend/InitPreprocessor.cpp
@@ -543,6 +543,7 @@
   // C++20 features.
   if (LangOpts.Char8)
 Builder.defineMacro("__cpp_char8_t", "201811L");
+  Builder.defineMacro("__cpp_impl_destroying_delete", "201806L");
 
   // TS features.
   if (LangOpts.ConceptsTS)
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D41746: Make std::get_temporary_buffer respect overaligned types when possible

2018-01-18 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly marked an inline comment as done.
ckennelly added a comment.

I don't have commit access, so can this be committed to trunk?


Repository:
  rCXX libc++

https://reviews.llvm.org/D41746



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


[PATCH] D41746: Make std::get_temporary_buffer respect overaligned types when possible

2018-01-12 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly marked 2 inline comments as done.
ckennelly added a comment.

In https://reviews.llvm.org/D41746#973941, @EricWF wrote:

> This LGTM minus nits.
>
> Is there a LWG issue or paper that specifies this change? or is it just a 
> general bug fix?


This is just a general bug fix.




Comment at: include/memory:2007
+#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
+if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+{

EricWF wrote:
> Please use the `alignment_of` trait; since that's always available within the 
> library, but different spellings of the `alignof` keyword aren't.
I also guarded for __STDCPP_DEFAULT_NEW_ALIGNMENT__


Repository:
  rCXX libc++

https://reviews.llvm.org/D41746



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


[PATCH] D41746: Make std::get_temporary_buffer respect overaligned types when possible

2018-01-12 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly updated this revision to Diff 129698.

Repository:
  rCXX libc++

https://reviews.llvm.org/D41746

Files:
  include/memory
  test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp

Index: test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp
===
--- /dev/null
+++ test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp
@@ -0,0 +1,33 @@
+//===--===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===--===//
+
+// 
+
+// template 
+//   pair
+//   get_temporary_buffer(ptrdiff_t n);
+//
+// template 
+//   void
+//   return_temporary_buffer(T* p);
+
+#include 
+#include 
+
+struct alignas(32) A {
+int field;
+};
+
+int main()
+{
+std::pair ip = std::get_temporary_buffer(5);
+assert(!(ip.first == nullptr) ^ (ip.second == 0));
+assert(reinterpret_cast(ip.first) % alignof(A) == 0);
+std::return_temporary_buffer(ip.first);
+}
Index: include/memory
===
--- include/memory
+++ include/memory
@@ -2003,7 +2003,38 @@
 __n = __m;
 while (__n > 0)
 {
+#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
+#if defined(__STDCPP_DEFAULT_NEW_ALIGNMENT__)
+if (std::alignment_of<_Tp>::value > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+#else
+if (std::alignment_of<_Tp>::value >
+std::alignment_of::value)
+#endif
+{
+std::align_val_t __al =
+std::align_val_t(std::alignment_of<_Tp>::value);
+__r.first = static_cast<_Tp*>(::operator new(
+__n * sizeof(_Tp), __al, nothrow));
+} else {
+__r.first = static_cast<_Tp*>(::operator new(
+__n * sizeof(_Tp), nothrow));
+}
+#else
+#if defined(__STDCPP_DEFAULT_NEW_ALIGNMENT__)
+if (std::alignment_of<_Tp>::value > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+#else
+if (std::alignment_of<_Tp>::value >
+std::alignment_of::value)
+#endif
+{
+// Since aligned operator new is unavailable, return an empty
+// buffer rather than one with invalid alignment.
+return __r;
+}
+
 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
+#endif
+
 if (__r.first)
 {
 __r.second = __n;
@@ -2016,7 +2047,23 @@
 
 template 
 inline _LIBCPP_INLINE_VISIBILITY
-void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
+void return_temporary_buffer(_Tp* __p) _NOEXCEPT
+{
+#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
+#if defined(__STDCPP_DEFAULT_NEW_ALIGNMENT__)
+if (std::alignment_of<_Tp>::value > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+#else
+if (std::alignment_of<_Tp>::value >
+std::alignment_of::value)
+#endif
+{
+std::align_val_t __al = std::align_val_t(std::alignment_of<_Tp>::value);
+::operator delete(__p, __al);
+return;
+}
+#endif
+::operator delete(__p);
+}
 
 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
 template 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D41746: Make std::get_temporary_buffer respect overaligned types when possible

2018-01-04 Thread Chris Kennelly via Phabricator via cfe-commits
ckennelly created this revision.
ckennelly added reviewers: EricWF, mclow.lists.

Repository:
  rCXX libc++

https://reviews.llvm.org/D41746

Files:
  include/memory
  test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp


Index: test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp
===
--- /dev/null
+++ test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp
@@ -0,0 +1,33 @@
+//===--===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===--===//
+
+// 
+
+// template 
+//   pair
+//   get_temporary_buffer(ptrdiff_t n);
+//
+// template 
+//   void
+//   return_temporary_buffer(T* p);
+
+#include 
+#include 
+
+struct alignas(32) A {
+int field;
+};
+
+int main()
+{
+std::pair ip = std::get_temporary_buffer(5);
+assert(!(ip.first == nullptr) ^ (ip.second == 0));
+assert(reinterpret_cast(ip.first) % alignof(A) == 0);
+std::return_temporary_buffer(ip.first);
+}
Index: include/memory
===
--- include/memory
+++ include/memory
@@ -2003,7 +2003,31 @@
 __n = __m;
 while (__n > 0)
 {
+#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
+if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+{
+std::align_val_t __al = std::align_val_t(alignof(_Tp));
+__r.first = static_cast<_Tp*>(::operator new(
+__n * sizeof(_Tp), __al, nothrow));
+} else {
+__r.first = static_cast<_Tp*>(::operator new(
+__n * sizeof(_Tp), nothrow));
+}
+#else
+#if defined(__STDCPP_DEFAULT_NEW_ALIGNMENT__)
+if (__alignof__(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+#else
+if (__alignof__(_Tp) > __alignof__(std::max_align_t))
+#endif
+{
+// Since aligned operator new is unavailable, return an empty
+// buffer rather than one with invalid alignment.
+return __r;
+}
+
 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), 
nothrow));
+#endif
+
 if (__r.first)
 {
 __r.second = __n;
@@ -2016,7 +2040,18 @@
 
 template 
 inline _LIBCPP_INLINE_VISIBILITY
-void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
+void return_temporary_buffer(_Tp* __p) _NOEXCEPT
+{
+#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
+if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+{
+std::align_val_t __al = std::align_val_t(alignof(_Tp));
+::operator delete(__p, __al);
+return;
+}
+#endif
+::operator delete(__p);
+}
 
 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
 template 


Index: test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp
===
--- /dev/null
+++ test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp
@@ -0,0 +1,33 @@
+//===--===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===--===//
+
+// 
+
+// template 
+//   pair
+//   get_temporary_buffer(ptrdiff_t n);
+//
+// template 
+//   void
+//   return_temporary_buffer(T* p);
+
+#include 
+#include 
+
+struct alignas(32) A {
+int field;
+};
+
+int main()
+{
+std::pair ip = std::get_temporary_buffer(5);
+assert(!(ip.first == nullptr) ^ (ip.second == 0));
+assert(reinterpret_cast(ip.first) % alignof(A) == 0);
+std::return_temporary_buffer(ip.first);
+}
Index: include/memory
===
--- include/memory
+++ include/memory
@@ -2003,7 +2003,31 @@
 __n = __m;
 while (__n > 0)
 {
+#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
+if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+{
+std::align_val_t __al = std::align_val_t(alignof(_Tp));
+__r.first = static_cast<_Tp*>(::operator new(
+__n * sizeof(_Tp), __al, nothrow));
+} else {
+__r.first = static_cast<_Tp*>(::operator new(
+__n * sizeof(_Tp), nothrow));
+}
+#else
+#if defined(__STDCPP_DEFAULT_NEW_ALIGNMENT__)
+if (__alignof__(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+#else
+if (__alignof__(_Tp) > __alignof__(std::max_align_t))
+#endif
+{
+// Since aligned operator new is unavailable, return an empty
+