[PATCH] D114823: Filter string_view from the nullptr diagnosis of bugprone-string-constructor to prevent duplicate warnings with bugprone-stringview-nullptr

CJ Johnson via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 30 15:19:03 PST 2021


CJ-Johnson created this revision.
CJ-Johnson added reviewers: ymandel, alexfh, njames93, hokein, whisperity.
Herald added subscribers: carlosgalvezp, rnkovacs.
CJ-Johnson requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Updates the check and tests to not diagnose null for string_view (but retains null for string)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D114823

Files:
  clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp
  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
@@ -74,10 +74,6 @@
   // CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
   std::string_view q5(kText3, 0x1000000);
   // 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() {
@@ -85,11 +81,6 @@
   // 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');
Index: clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp
@@ -121,17 +121,20 @@
   // Check the literal string constructor with char pointer.
   // [i.e. string (const char* s);]
   Finder->addMatcher(
-      traverse(TK_AsIs,
-               cxxConstructExpr(
-                   hasDeclaration(cxxConstructorDecl(ofClass(cxxRecordDecl(
-                       hasAnyName(removeNamespaces(StringNames)))))),
-                   hasArgument(0, expr().bind("from-ptr")),
-                   // do not match std::string(ptr, int)
-                   // match std::string(ptr, alloc)
-                   // match std::string(ptr)
-                   anyOf(hasArgument(1, unless(hasType(isInteger()))),
-                         argumentCountIs(1)))
-                   .bind("constructor")),
+      traverse(
+          TK_AsIs,
+          cxxConstructExpr(
+              hasDeclaration(cxxConstructorDecl(ofClass(anyOf(
+                  cxxRecordDecl(hasName("basic_string_view"))
+                      .bind("basic_string_view_decl"),
+                  cxxRecordDecl(hasAnyName(removeNamespaces(StringNames))))))),
+              hasArgument(0, expr().bind("from-ptr")),
+              // do not match std::string(ptr, int)
+              // match std::string(ptr, alloc)
+              // match std::string(ptr)
+              anyOf(hasArgument(1, unless(hasType(isInteger()))),
+                    argumentCountIs(1)))
+              .bind("constructor")),
       this);
 }
 
@@ -167,7 +170,13 @@
         Ptr->EvaluateAsRValue(ConstPtr, Ctx) &&
         ((ConstPtr.Val.isInt() && ConstPtr.Val.getInt().isZero()) ||
          (ConstPtr.Val.isLValue() && ConstPtr.Val.isNullPointer()))) {
-      diag(Loc, "constructing string from nullptr is undefined behaviour");
+      const auto *StringViewType =
+          Result.Nodes.getNodeAs<CXXRecordDecl>("basic_string_view_decl");
+      // Filter out basic_string_view to avoid conflicts with
+      // bugprone-stringview-nullptr
+      if (StringViewType == nullptr) {
+        diag(Loc, "constructing string from nullptr is undefined behaviour");
+      }
     }
   }
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114823.390834.patch
Type: text/x-patch
Size: 3549 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211130/80ce2a12/attachment.bin>


More information about the cfe-commits mailing list