[PATCH] D76761: [clang-tidy] Fix crash in readability-redundant-string-cstr

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 25 03:45:31 PDT 2020


njames93 created this revision.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76761

Files:
  clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp
@@ -220,3 +220,16 @@
   m1tp m1p2 = m1;
   m1p2(s.c_str());  
 }
+
+namespace PR45286 {
+struct Foo {
+  void func(const std::string &) {}
+};
+
+void bar() {
+  std::string Str{"aaa"};
+  Foo Foo;
+  Foo.func(Str.c_str());
+  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
+}
+} // namespace PR45286
Index: clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
@@ -83,24 +83,25 @@
 // Check that ParamDecl of CallExprDecl has rvalue type.
 static bool checkParamDeclOfAncestorCallExprHasRValueRefType(
     const Expr *TheCxxConstructExpr, ASTContext &Context) {
-  if (const clang::CallExpr *TheCallExpr =
-          tryGetCallExprAncestorForCxxConstructExpr(TheCxxConstructExpr,
-                                                    Context)) {
-    for (unsigned i = 0; i < TheCallExpr->getNumArgs(); ++i) {
-      const Expr *Arg = TheCallExpr->getArg(i);
-      if (Arg->getSourceRange() == TheCxxConstructExpr->getSourceRange()) {
-        if (const auto *TheCallExprFuncProto =
-                TheCallExpr->getCallee()
-                    ->getType()
-                    ->getPointeeType()
-                    ->getAs<FunctionProtoType>()) {
-          if (TheCallExprFuncProto->getParamType(i)->isRValueReferenceType())
-            return true;
-        }
-      }
+  const clang::CallExpr *TheCallExpr =
+      tryGetCallExprAncestorForCxxConstructExpr(TheCxxConstructExpr, Context);
+  if (!TheCallExpr)
+    return false;
+  for (unsigned I = 0; I < TheCallExpr->getNumArgs(); ++I) {
+    const Expr *Arg = TheCallExpr->getArg(I);
+    if (Arg->getSourceRange() != TheCxxConstructExpr->getSourceRange())
+      continue;
+    QualType CalleePointerType =
+        TheCallExpr->getCallee()->getType()->getPointeeType();
+    if (CalleePointerType.isNull())
+      // Not a pointer type, should probably avoid this
+      continue;
+    if (const auto *TheCallExprFuncProto =
+            CalleePointerType->getAs<FunctionProtoType>()) {
+      if (TheCallExprFuncProto->getParamType(I)->isRValueReferenceType())
+        return true;
     }
   }
-
   return false;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76761.252531.patch
Type: text/x-patch
Size: 2703 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200325/fc2aa9ea/attachment.bin>


More information about the cfe-commits mailing list