[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
Wed Jan 12 14:05:01 PST 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rG81c330e23dd4: Filter string_view from the nullptr diagnosis of bugprone-string-constructor to… (authored by CJ-Johnson).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D114823

Files:
  clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp
  clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h


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
@@ -30,6 +30,7 @@
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
 
 private:
+  const bool IsStringviewNullptrCheckEnabled;
   const bool WarnOnLargeLength;
   const unsigned int LargeLengthThreshold;
   std::vector<std::string> StringNames;
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
@@ -43,6 +43,8 @@
 StringConstructorCheck::StringConstructorCheck(StringRef Name,
                                                ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
+      IsStringviewNullptrCheckEnabled(
+          Context->isCheckEnabled("bugprone-stringview-nullptr")),
       WarnOnLargeLength(Options.get("WarnOnLargeLength", true)),
       LargeLengthThreshold(Options.get("LargeLengthThreshold", 0x800000)),
       StringNames(utils::options::parseStringList(
@@ -121,17 +123,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,6 +172,12 @@
         Ptr->EvaluateAsRValue(ConstPtr, Ctx) &&
         ((ConstPtr.Val.isInt() && ConstPtr.Val.getInt().isZero()) ||
          (ConstPtr.Val.isLValue() && ConstPtr.Val.isNullPointer()))) {
+      if (IsStringviewNullptrCheckEnabled &&
+          Result.Nodes.getNodeAs<CXXRecordDecl>("basic_string_view_decl")) {
+        // Filter out `basic_string_view` to avoid conflicts with
+        // `bugprone-stringview-nullptr`
+        return;
+      }
       diag(Loc, "constructing string from nullptr is undefined behaviour");
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114823.399450.patch
Type: text/x-patch
Size: 3305 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220112/f7dd7b23/attachment.bin>


More information about the cfe-commits mailing list