[PATCH] D88314: Added llvm-string-referencing check

2020-10-09 Thread Bogdan Serea via Phabricator via cfe-commits
bogser01 updated this revision to Diff 297355.
bogser01 added a comment.

Rebase diff


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D88314

Files:
  clang-tools-extra/clang-tidy/add_new_check.py
  clang-tools-extra/clang-tidy/llvm/CMakeLists.txt
  clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.cpp
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
  clang-tools-extra/docs/clang-tidy/Contributing.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
  clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
@@ -0,0 +1,66 @@
+// RUN: %check_clang_tidy %s llvm-string-referencing %t
+
+namespace std {
+class string {};
+class u18_string_t;
+
+} // namespace std
+
+namespace llvm {
+class StringRef;
+} // namespace llvm
+
+class String;
+
+namespace A {
+using namespace std;
+void f(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
+// CHECK-FIXES: void f(llvm::StringRef P);{{$}}
+} // namespace A
+
+namespace B {
+using std::string;
+void f1(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
+// CHECK-FIXES: void f1(llvm::StringRef P);{{$}}
+} // namespace B
+
+void f2(std::string, int, const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
+// CHECK-FIXES: void f2(std::string, int, llvm::StringRef );{{$}}
+void f2(std::string P, int x, const std::string ) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
+  // CHECK-FIXES: void f2(std::string P, int x, llvm::StringRef P2) {{{$}}
+  return;
+}
+
+void f3(const std::string , const std::string );
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
+// CHECK-MESSAGES: :[[@LINE-2]]:32: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
+// CHECK-FIXES: void f3(llvm::StringRef P1, llvm::StringRef P2);{{$}}
+
+struct St {
+  void operator=(const std::string ) const {
+// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
+// CHECK-FIXES: void operator=(llvm::StringRef Val) const {{{$}}
+return;
+  }
+};
+
+void f7(const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
+// CHECK-FIXES: void f7(llvm::StringRef );{{$}}
+
+// Functions below this line should not trigger the check
+void f1(std::string );
+
+void f4(std::string *P);
+
+void f5(String );
+
+void f6(llvm::StringRef P);
+
+void f9(std::u18_string_t );
+
+void f10(const std::string &);
Index: clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
===
--- /dev/null
+++ clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
@@ -0,0 +1,17 @@
+.. title:: clang-tidy - llvm-string-referencing
+
+llvm-string-referencing
+===
+
+The StringRef data type represents a reference to a constant string (a 
+character array and a length) and supports the common operations available
+on std::string. The LLVM Programmer's Manual recommends the use of StringRef
+instead 'const std::string &'
+https://llvm.org/docs/ProgrammersManual.html#id14
+
+The check warns about uses of 'const std::string &' as a function parameter 
+type and provides an appropriate fix. 
+
+IMPORTANT NOTE: not all std::string member functions are available from 
+StringRef so applying the fix should be considered on a case-by-case basis.
+
Index: clang-tools-extra/docs/clang-tidy/checks/list.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -192,6 +192,7 @@
`llvm-namespace-comment `_,
`llvm-prefer-isa-or-dyn-cast-in-conditionals `_, "Yes"
`llvm-prefer-register-over-unsigned `_, "Yes"
+   

[PATCH] D88314: Added llvm-string-referencing check

2020-10-09 Thread Bogdan Serea via Phabricator via cfe-commits
bogser01 added a comment.

@aaron.ballman Thank you for picking up this review! Running the check over the 
entire LLVM causes ~74K warnings across 430 files. As to the false positive 
rate it's tricky to measure. Based on previous analysis on //flang// codebase, 
I would say roughly 50% of the fixes would cause compiler errors when applied.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D88314

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


[PATCH] D88314: Added llvm-string-referencing check

2020-10-09 Thread Bogdan Serea via Phabricator via cfe-commits
bogser01 updated this revision to Diff 297246.
bogser01 added a comment.

Added documentation & Nit fixes


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D88314

Files:
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.cpp
  clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
  clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
@@ -15,41 +15,41 @@
 namespace A {
 using namespace std;
 void f(const string );
-// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
 // CHECK-FIXES: void f(llvm::StringRef P);{{$}}
 } // namespace A
 
 namespace B {
 using std::string;
 void f1(const string );
-// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
 // CHECK-FIXES: void f1(llvm::StringRef P);{{$}}
 } // namespace B
 
 void f2(std::string, int, const std::string &);
-// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
 // CHECK-FIXES: void f2(std::string, int, llvm::StringRef );{{$}}
 void f2(std::string P, int x, const std::string ) {
-  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
   // CHECK-FIXES: void f2(std::string P, int x, llvm::StringRef P2) {{{$}}
   return;
 }
 
 void f3(const std::string , const std::string );
-// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
-// CHECK-MESSAGES: :[[@LINE-2]]:32: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
+// CHECK-MESSAGES: :[[@LINE-2]]:32: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
 // CHECK-FIXES: void f3(llvm::StringRef P1, llvm::StringRef P2);{{$}}
 
 struct St {
   void operator=(const std::string ) const {
-// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
 // CHECK-FIXES: void operator=(llvm::StringRef Val) const {{{$}}
 return;
   }
 };
 
 void f7(const std::string &);
-// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter of type 'const std::string &' could potentially be replaced with 'llvm::StringRef' [llvm-string-referencing]
 // CHECK-FIXES: void f7(llvm::StringRef );{{$}}
 
 // Functions below this line should not trigger the check
Index: clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
+++ clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
@@ -3,4 +3,15 @@
 llvm-string-referencing
 ===
 
-FIXME: Describe what patterns does the check detect and why. Give examples.
+The StringRef data type represents a reference to a constant string (a 
+character array and a length) and supports the common operations available
+on std::string. The LLVM Programmer's Manual recommends the use of StringRef
+instead 'const std::string &'
+https://llvm.org/docs/ProgrammersManual.html#id14
+
+The check warns about uses of 'const 

[PATCH] D88314: Added llvm-string-referencing check

2020-10-02 Thread Bogdan Serea via Phabricator via cfe-commits
bogser01 added a comment.

@alexfh does this look alright?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D88314

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


[PATCH] D88314: Added llvm-string-referencing check

2020-10-02 Thread Bogdan Serea via Phabricator via cfe-commits
bogser01 updated this revision to Diff 295811.
bogser01 added a comment.

Remove conflict markers 2


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D88314

Files:
  clang-tools-extra/clang-tidy/llvm/CMakeLists.txt
  clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.cpp
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
  clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
@@ -0,0 +1,66 @@
+// RUN: %check_clang_tidy %s llvm-string-referencing %t
+
+namespace std {
+class string {};
+class u18_string_t;
+
+} // namespace std
+
+namespace llvm {
+class StringRef;
+} // namespace llvm
+
+class String;
+
+namespace A {
+using namespace std;
+void f(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f(llvm::StringRef P);{{$}}
+} // namespace A
+
+namespace B {
+using std::string;
+void f1(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f1(llvm::StringRef P);{{$}}
+} // namespace B
+
+void f2(std::string, int, const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f2(std::string, int, llvm::StringRef );{{$}}
+void f2(std::string P, int x, const std::string ) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+  // CHECK-FIXES: void f2(std::string P, int x, llvm::StringRef P2) {{{$}}
+  return;
+}
+
+void f3(const std::string , const std::string );
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-MESSAGES: :[[@LINE-2]]:32: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f3(llvm::StringRef P1, llvm::StringRef P2);{{$}}
+
+struct St {
+  void operator=(const std::string ) const {
+// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void operator=(llvm::StringRef Val) const {{{$}}
+return;
+  }
+};
+
+void f7(const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f7(llvm::StringRef );{{$}}
+
+// Functions below this line should not trigger the check
+void f1(std::string );
+
+void f4(std::string *P);
+
+void f5(String );
+
+void f6(llvm::StringRef P);
+
+void f9(std::u18_string_t );
+
+void f10(const std::string &);
Index: clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
===
--- /dev/null
+++ clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
@@ -0,0 +1,6 @@
+.. title:: clang-tidy - llvm-string-referencing
+
+llvm-string-referencing
+===
+
+FIXME: Describe what patterns does the check detect and why. Give examples.
Index: clang-tools-extra/docs/clang-tidy/checks/list.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -192,6 +192,7 @@
`llvm-namespace-comment `_,
`llvm-prefer-isa-or-dyn-cast-in-conditionals `_, "Yes"
`llvm-prefer-register-over-unsigned `_, "Yes"
+   `llvm-string-referencing `_, "Yes"
`llvm-twine-local `_, "Yes"
`llvmlibc-callee-namespace `_,
`llvmlibc-implementation-in-namespace `_,
Index: clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
===
--- /dev/null
+++ clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
@@ -0,0 +1,46 @@
+//===--- StringReferencingCheck.h - clang-tidy --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//

[PATCH] D88314: Added llvm-string-referencing check

2020-10-02 Thread Bogdan Serea via Phabricator via cfe-commits
bogser01 updated this revision to Diff 295810.
bogser01 added a comment.

Removed residual conflict markers


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D88314

Files:
  clang-tools-extra/clang-tidy/llvm/CMakeLists.txt
  clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.cpp
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
  clang-tools-extra/docs/clang-tidy/Contributing.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
  clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
@@ -0,0 +1,66 @@
+// RUN: %check_clang_tidy %s llvm-string-referencing %t
+
+namespace std {
+class string {};
+class u18_string_t;
+
+} // namespace std
+
+namespace llvm {
+class StringRef;
+} // namespace llvm
+
+class String;
+
+namespace A {
+using namespace std;
+void f(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f(llvm::StringRef P);{{$}}
+} // namespace A
+
+namespace B {
+using std::string;
+void f1(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f1(llvm::StringRef P);{{$}}
+} // namespace B
+
+void f2(std::string, int, const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f2(std::string, int, llvm::StringRef );{{$}}
+void f2(std::string P, int x, const std::string ) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+  // CHECK-FIXES: void f2(std::string P, int x, llvm::StringRef P2) {{{$}}
+  return;
+}
+
+void f3(const std::string , const std::string );
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-MESSAGES: :[[@LINE-2]]:32: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f3(llvm::StringRef P1, llvm::StringRef P2);{{$}}
+
+struct St {
+  void operator=(const std::string ) const {
+// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void operator=(llvm::StringRef Val) const {{{$}}
+return;
+  }
+};
+
+void f7(const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f7(llvm::StringRef );{{$}}
+
+// Functions below this line should not trigger the check
+void f1(std::string );
+
+void f4(std::string *P);
+
+void f5(String );
+
+void f6(llvm::StringRef P);
+
+void f9(std::u18_string_t );
+
+void f10(const std::string &);
Index: clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
===
--- /dev/null
+++ clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
@@ -0,0 +1,6 @@
+.. title:: clang-tidy - llvm-string-referencing
+
+llvm-string-referencing
+===
+
+FIXME: Describe what patterns does the check detect and why. Give examples.
Index: clang-tools-extra/docs/clang-tidy/checks/list.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -192,6 +192,7 @@
`llvm-namespace-comment `_,
`llvm-prefer-isa-or-dyn-cast-in-conditionals `_, "Yes"
`llvm-prefer-register-over-unsigned `_, "Yes"
+   `llvm-string-referencing `_, "Yes"
`llvm-twine-local `_, "Yes"
`llvmlibc-callee-namespace `_,
`llvmlibc-implementation-in-namespace `_,
Index: clang-tools-extra/docs/clang-tidy/Contributing.rst
===
--- clang-tools-extra/docs/clang-tidy/Contributing.rst
+++ clang-tools-extra/docs/clang-tidy/Contributing.rst
@@ -201,8 +201,13 @@
   }
 
   void AwesomeFunctionNamesCheck::check(const MatchFinder::MatchResult ) {
+<<< HEAD
+const auto *MatchedDecl = Result.Nodes.getNodeAs("x");
+if ((!MatchedDecl->getIdentifier()) || MatchedDecl->getName().startswith("awesome_"))
+===
 const auto 

[PATCH] D88314: Added llvm-string-referencing check

2020-10-01 Thread Bogdan Serea via Phabricator via cfe-commits
bogser01 updated this revision to Diff 295580.
bogser01 added a comment.

Fixed unit test 2


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D88314

Files:
  clang-tools-extra/clang-tidy/add_new_check.py
  clang-tools-extra/clang-tidy/llvm/CMakeLists.txt
  clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.cpp
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
  clang-tools-extra/docs/clang-tidy/Contributing.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
  clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
@@ -0,0 +1,66 @@
+// RUN: %check_clang_tidy %s llvm-string-referencing %t
+
+namespace std {
+class string {};
+class u18_string_t;
+
+} // namespace std
+
+namespace llvm {
+class StringRef;
+} // namespace llvm
+
+class String;
+
+namespace A {
+using namespace std;
+void f(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f(llvm::StringRef P);{{$}}
+} // namespace A
+
+namespace B {
+using std::string;
+void f1(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f1(llvm::StringRef P);{{$}}
+} // namespace B
+
+void f2(std::string, int, const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f2(std::string, int, llvm::StringRef );{{$}}
+void f2(std::string P, int x, const std::string ) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+  // CHECK-FIXES: void f2(std::string P, int x, llvm::StringRef P2) {{{$}}
+  return;
+}
+
+void f3(const std::string , const std::string );
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-MESSAGES: :[[@LINE-2]]:32: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f3(llvm::StringRef P1, llvm::StringRef P2);{{$}}
+
+struct St {
+  void operator=(const std::string ) const {
+// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void operator=(llvm::StringRef Val) const {{{$}}
+return;
+  }
+};
+
+void f7(const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f7(llvm::StringRef );{{$}}
+
+// Functions below this line should not trigger the check
+void f1(std::string );
+
+void f4(std::string *P);
+
+void f5(String );
+
+void f6(llvm::StringRef P);
+
+void f9(std::u18_string_t );
+
+void f10(const std::string &);
Index: clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
===
--- /dev/null
+++ clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
@@ -0,0 +1,6 @@
+.. title:: clang-tidy - llvm-string-referencing
+
+llvm-string-referencing
+===
+
+FIXME: Describe what patterns does the check detect and why. Give examples.
Index: clang-tools-extra/docs/clang-tidy/checks/list.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -192,6 +192,7 @@
`llvm-namespace-comment `_,
`llvm-prefer-isa-or-dyn-cast-in-conditionals `_, "Yes"
`llvm-prefer-register-over-unsigned `_, "Yes"
+   `llvm-string-referencing `_, "Yes"
`llvm-twine-local `_, "Yes"
`llvmlibc-callee-namespace `_,
`llvmlibc-implementation-in-namespace `_,
Index: clang-tools-extra/docs/clang-tidy/Contributing.rst
===
--- clang-tools-extra/docs/clang-tidy/Contributing.rst
+++ clang-tools-extra/docs/clang-tidy/Contributing.rst
@@ -201,8 +201,13 @@
   }
 
   void AwesomeFunctionNamesCheck::check(const MatchFinder::MatchResult ) {
+<<< HEAD
+const auto *MatchedDecl = Result.Nodes.getNodeAs("x");
+if ((!MatchedDecl->getIdentifier()) || MatchedDecl->getName().startswith("awesome_"))

[PATCH] D88314: Added llvm-string-referencing check

2020-10-01 Thread Bogdan Serea via Phabricator via cfe-commits
bogser01 updated this revision to Diff 295553.
bogser01 added a comment.

Fixed failing unit test


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D88314

Files:
  clang-tools-extra/clang-tidy/llvm/CMakeLists.txt
  clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.cpp
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
  clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
@@ -0,0 +1,66 @@
+// RUN: %check_clang_tidy %s llvm-string-referencing %t
+
+namespace std {
+class string {};
+class u18_string_t;
+
+} // namespace std
+
+namespace llvm {
+class StringRef;
+} // namespace llvm
+
+class String;
+
+namespace A {
+using namespace std;
+void f(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f(llvm::StringRef P);{{$}}
+} // namespace A
+
+namespace B {
+using std::string;
+void f1(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f1(llvm::StringRef P);{{$}}
+} // namespace B
+
+void f2(std::string, int, const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f2(std::string, int, llvm::StringRef );{{$}}
+void f2(std::string P, int x, const std::string ) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+  // CHECK-FIXES: void f2(std::string P, int x, llvm::StringRef P2) {{{$}}
+  return;
+}
+
+void f3(const std::string , const std::string );
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-MESSAGES: :[[@LINE-2]]:32: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f3(llvm::StringRef P1, llvm::StringRef P2);{{$}}
+
+struct St {
+  void operator=(const std::string ) const {
+// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void operator=(llvm::StringRef Val) const {{{$}}
+return;
+  }
+};
+
+void f7(const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f7(llvm::StringRef );{{$}}
+
+// Functions below this line should not trigger the check
+void f1(std::string );
+
+void f4(std::string *P);
+
+void f5(String );
+
+void f6(llvm::StringRef P);
+
+void f9(std::u18_string_t );
+
+void f10(const std::string &);
Index: clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
===
--- /dev/null
+++ clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
@@ -0,0 +1,6 @@
+.. title:: clang-tidy - llvm-string-referencing
+
+llvm-string-referencing
+===
+
+FIXME: Describe what patterns does the check detect and why. Give examples.
Index: clang-tools-extra/docs/clang-tidy/checks/list.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -192,6 +192,7 @@
`llvm-namespace-comment `_,
`llvm-prefer-isa-or-dyn-cast-in-conditionals `_, "Yes"
`llvm-prefer-register-over-unsigned `_, "Yes"
+   `llvm-string-referencing `_, "Yes"
`llvm-twine-local `_, "Yes"
`llvmlibc-callee-namespace `_,
`llvmlibc-implementation-in-namespace `_,
Index: clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
===
--- /dev/null
+++ clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
@@ -0,0 +1,46 @@
+//===--- StringReferencingCheck.h - clang-tidy --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//

[PATCH] D88314: Added llvm-string-referencing check

2020-09-25 Thread Bogdan Serea via Phabricator via cfe-commits
bogser01 created this revision.
Herald added subscribers: cfe-commits, mgorny.
Herald added a project: clang.
bogser01 requested review of this revision.

Clang-tidy pass detecting the use of const std::string& references.

Use of llvm::StringRef is recommended in the LLVM Programmer's Manual instead:
https://llvm.org/docs/ProgrammersManual.html#the-stringref-class


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D88314

Files:
  clang-tools-extra/clang-tidy/llvm/CMakeLists.txt
  clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.cpp
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
  clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
@@ -0,0 +1,66 @@
+// RUN: %check_clang_tidy %s llvm-string-referencing %t
+
+namespace std {
+class string {};
+class u18_string_t;
+
+} // namespace std
+
+namespace llvm {
+class StringRef;
+} // namespace llvm
+
+class String;
+
+namespace A {
+using namespace std;
+void f(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f(llvm::StringRef P);{{$}}
+} // namespace A
+
+namespace B {
+using std::string;
+void f1(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f1(llvm::StringRef P);{{$}}
+} // namespace B
+
+void f2(std::string, int, const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f2(std::string, int, llvm::StringRef );{{$}}
+void f2(std::string P, int x, const std::string ) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+  // CHECK-FIXES: void f2(std::string P, int x, llvm::StringRef P2) {{{$}}
+  return;
+}
+
+void f3(const std::string , const std::string );
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-MESSAGES: :[[@LINE-2]]:32: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f3(llvm::StringRef P1, llvm::StringRef P2);{{$}}
+
+struct St {
+  void operator=(const std::string ) const {
+// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void operator=(llvm::StringRef Val) const {{{$}}
+return;
+  }
+};
+
+void f7(const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f7(llvm::StringRef );{{$}}
+
+// Functions below this line should not trigger the check
+void f1(std::string );
+
+void f4(std::string *P);
+
+void f5(String );
+
+void f6(llvm::StringRef P);
+
+void f9(std::u18_string_t );
+
+void f10(const std::string &);
Index: clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
===
--- /dev/null
+++ clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
@@ -0,0 +1,6 @@
+.. title:: clang-tidy - llvm-string-referencing
+
+llvm-string-referencing
+===
+
+FIXME: Describe what patterns does the check detect and why. Give examples.
Index: clang-tools-extra/docs/clang-tidy/checks/list.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -192,6 +192,7 @@
`llvm-namespace-comment `_,
`llvm-prefer-isa-or-dyn-cast-in-conditionals `_, "Yes"
`llvm-prefer-register-over-unsigned `_, "Yes"
+   `llvm-string-referencing `_, "Yes"
`llvm-twine-local `_, "Yes"
`llvmlibc-callee-namespace `_,
`llvmlibc-implementation-in-namespace `_,
Index: clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
===
--- /dev/null
+++ clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
@@ -0,0 +1,46 @@
+//===--- StringReferencingCheck.h - clang-tidy --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.

[PATCH] D88311: Added llvm-string-referencing check

2020-09-25 Thread Bogdan Serea via Phabricator via cfe-commits
bogser01 updated this revision to Diff 294331.
bogser01 added a comment.
Herald added a subscriber: mgorny.

Changed upstream


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D88311

Files:
  clang-tools-extra/clang-tidy/llvm/CMakeLists.txt
  clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.cpp
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
  clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
@@ -0,0 +1,66 @@
+// RUN: %check_clang_tidy %s llvm-string-referencing %t
+
+namespace std {
+class string {};
+class u18_string_t;
+
+} // namespace std
+
+namespace llvm {
+class StringRef;
+} // namespace llvm
+
+class String;
+
+namespace A {
+using namespace std;
+void f(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f(llvm::StringRef P);{{$}}
+} // namespace A
+
+namespace B {
+using std::string;
+void f1(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f1(llvm::StringRef P);{{$}}
+} // namespace B
+
+void f2(std::string, int, const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f2(std::string, int, llvm::StringRef );{{$}}
+void f2(std::string P, int x, const std::string ) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+  // CHECK-FIXES: void f2(std::string P, int x, llvm::StringRef P2) {{{$}}
+  return;
+}
+
+void f3(const std::string , const std::string );
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-MESSAGES: :[[@LINE-2]]:32: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f3(llvm::StringRef P1, llvm::StringRef P2);{{$}}
+
+struct St {
+  void operator=(const std::string ) const {
+// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void operator=(llvm::StringRef Val) const {{{$}}
+return;
+  }
+};
+
+void f7(const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f7(llvm::StringRef );{{$}}
+
+// Functions below this line should not trigger the check
+void f1(std::string );
+
+void f4(std::string *P);
+
+void f5(String );
+
+void f6(llvm::StringRef P);
+
+void f9(std::u18_string_t );
+
+void f10(const std::string &);
Index: clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
===
--- /dev/null
+++ clang-tools-extra/docs/clang-tidy/checks/llvm-string-referencing.rst
@@ -0,0 +1,6 @@
+.. title:: clang-tidy - llvm-string-referencing
+
+llvm-string-referencing
+===
+
+FIXME: Describe what patterns does the check detect and why. Give examples.
Index: clang-tools-extra/docs/clang-tidy/checks/list.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -192,6 +192,7 @@
`llvm-namespace-comment `_,
`llvm-prefer-isa-or-dyn-cast-in-conditionals `_, "Yes"
`llvm-prefer-register-over-unsigned `_, "Yes"
+   `llvm-string-referencing `_, "Yes"
`llvm-twine-local `_, "Yes"
`llvmlibc-callee-namespace `_,
`llvmlibc-implementation-in-namespace `_,
Index: clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
===
--- /dev/null
+++ clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
@@ -0,0 +1,46 @@
+//===--- StringReferencingCheck.h - clang-tidy --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//

[PATCH] D88311: Added llvm-string-referencing check

2020-09-25 Thread Bogdan Serea via Phabricator via cfe-commits
bogser01 created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
bogser01 requested review of this revision.

Clang-tidy pass detecting the use of const std::string& references.

Use of llvm::StringRef is recommended in the LLVM Programmer's Manual instead:
https://llvm.org/docs/ProgrammersManual.html#the-stringref-class


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D88311

Files:
  .gitignore
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.cpp
  clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
  clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/llvm-string-referencing.cpp
@@ -1,23 +1,66 @@
 // RUN: %check_clang_tidy %s llvm-string-referencing %t
 
-namespace std{ 
-class string;
+namespace std {
+class string {};
+class u18_string_t;
+
 } // namespace std
 
-namespace llvm{
-class StringRef;
+namespace llvm {
+class StringRef;
 } // namespace llvm
 
-void f(std::string& P){
-// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: Use of std::string& is against LLVM guidelines [llvm-string-referencing]
-// CHECK-FIXES: void f(llvm::StringRef P){{{$}}
-return;
+class String;
+
+namespace A {
+using namespace std;
+void f(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f(llvm::StringRef P);{{$}}
+} // namespace A
+
+namespace B {
+using std::string;
+void f1(const string );
+// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f1(llvm::StringRef P);{{$}}
+} // namespace B
+
+void f2(std::string, int, const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f2(std::string, int, llvm::StringRef );{{$}}
+void f2(std::string P, int x, const std::string ) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+  // CHECK-FIXES: void f2(std::string P, int x, llvm::StringRef P2) {{{$}}
+  return;
 }
 
-void f2(int x, std::string& P){
-// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: Use of std::string& is against LLVM guidelines [llvm-string-referencing]
-// CHECK-FIXES: void f2(int x, llvm::StringRef P){{{$}}
+void f3(const std::string , const std::string );
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-MESSAGES: :[[@LINE-2]]:32: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f3(llvm::StringRef P1, llvm::StringRef P2);{{$}}
+
+struct St {
+  void operator=(const std::string ) const {
+// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void operator=(llvm::StringRef Val) const {{{$}}
 return;
-}
+  }
+};
+
+void f7(const std::string &);
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: Use of const std::string & is discouraged in the LLVM Programmer's Manual [llvm-string-referencing]
+// CHECK-FIXES: void f7(llvm::StringRef );{{$}}
+
+// Functions below this line should not trigger the check
+void f1(std::string );
+
+void f4(std::string *P);
+
+void f5(String );
+
+void f6(llvm::StringRef P);
+
+void f9(std::u18_string_t );
 
-void f3(llvm::StringRef P);
+void f10(const std::string &);
Index: clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
===
--- clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
+++ clang-tools-extra/clang-tidy/llvm/StringReferencingCheck.h
@@ -10,22 +10,33 @@
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_STRINGREFERENCINGCHECK_H
 
 #include "../ClangTidyCheck.h"
+#include "../utils/IncludeInserter.h"
+#include "clang/Frontend/CompilerInstance.h"
 
 namespace clang {
 namespace tidy {
 namespace llvm_check {
 
-
-/// LLVM guidelines say that llvm::StringRef should be used for function parameters instead of references to std::string.
+/// LThe LLVM Programmer's Manual recommends that llvm::StringRef should be
+/// used for function parameters instead of references to const std::string:
+/// https://llvm.org/docs/ProgrammersManual.html#the-stringref-class
 /// For the user-facing documentation see:
 /// http://clang.llvm.org/extra/clang-tidy/checks/llvm-string-referencing.html
 

[PATCH] D85218: In clang-tidy base checks prevent anonymous functions from triggering assertions

2020-08-04 Thread Bogdan Serea via Phabricator via cfe-commits
bogser01 created this revision.
bogser01 added reviewers: djasper, alexfh.
bogser01 added a project: clang-tools-extra.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
bogser01 requested review of this revision.

Skeleton checks generated by clang-tidy add_check.py cause assertions to fail 
when run over anonymous functions(lambda functions). This patch introduces an 
additional check to verify that the target function is not anonymous before 
calling getName(). 
The code snippet from the [[ 
https://clang.llvm.org/extra/clang-tidy/Contributing.html | clang-tidy tutorial 
 ]]is also updated.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85218

Files:
  clang-tools-extra/clang-tidy/add_new_check.py
  clang-tools-extra/docs/clang-tidy/Contributing.rst


Index: clang-tools-extra/docs/clang-tidy/Contributing.rst
===
--- clang-tools-extra/docs/clang-tidy/Contributing.rst
+++ clang-tools-extra/docs/clang-tidy/Contributing.rst
@@ -201,8 +201,8 @@
   }
 
   void AwesomeFunctionNamesCheck::check(const MatchFinder::MatchResult 
) {
-const auto *MatchedDecl = Result.Nodes.getNodeAs("x");
-if (MatchedDecl->getName().startswith("awesome_"))
+const auto *MatchedDecl = Result.Nodes.getNodeAs("x");
+if ((!MatchedDecl->getIdentifier()) || 
MatchedDecl->getName().startswith("awesome_"))
   return;
 diag(MatchedDecl->getLocation(), "function %0 is insufficiently awesome")
 << MatchedDecl
Index: clang-tools-extra/clang-tidy/add_new_check.py
===
--- clang-tools-extra/clang-tidy/add_new_check.py
+++ clang-tools-extra/clang-tidy/add_new_check.py
@@ -136,7 +136,7 @@
 void %(check_name)s::check(const MatchFinder::MatchResult ) {
   // FIXME: Add callback implementation.
   const auto *MatchedDecl = Result.Nodes.getNodeAs("x");
-  if (MatchedDecl->getName().startswith("awesome_"))
+  if ((!MatchedDecl->getIdentifier()) || 
MatchedDecl->getName().startswith("awesome_"))
 return;
   diag(MatchedDecl->getLocation(), "function %%0 is insufficiently awesome")
   << MatchedDecl;


Index: clang-tools-extra/docs/clang-tidy/Contributing.rst
===
--- clang-tools-extra/docs/clang-tidy/Contributing.rst
+++ clang-tools-extra/docs/clang-tidy/Contributing.rst
@@ -201,8 +201,8 @@
   }
 
   void AwesomeFunctionNamesCheck::check(const MatchFinder::MatchResult ) {
-const auto *MatchedDecl = Result.Nodes.getNodeAs("x");
-if (MatchedDecl->getName().startswith("awesome_"))
+const auto *MatchedDecl = Result.Nodes.getNodeAs("x");
+if ((!MatchedDecl->getIdentifier()) || MatchedDecl->getName().startswith("awesome_"))
   return;
 diag(MatchedDecl->getLocation(), "function %0 is insufficiently awesome")
 << MatchedDecl
Index: clang-tools-extra/clang-tidy/add_new_check.py
===
--- clang-tools-extra/clang-tidy/add_new_check.py
+++ clang-tools-extra/clang-tidy/add_new_check.py
@@ -136,7 +136,7 @@
 void %(check_name)s::check(const MatchFinder::MatchResult ) {
   // FIXME: Add callback implementation.
   const auto *MatchedDecl = Result.Nodes.getNodeAs("x");
-  if (MatchedDecl->getName().startswith("awesome_"))
+  if ((!MatchedDecl->getIdentifier()) || MatchedDecl->getName().startswith("awesome_"))
 return;
   diag(MatchedDecl->getLocation(), "function %%0 is insufficiently awesome")
   << MatchedDecl;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits