[PATCH] D120555: [clang-tidy] Fix `readability-suspicious-call-argument` crash for arguments without name-like identifier

Whisperity via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 25 05:25:18 PST 2022


whisperity created this revision.
whisperity added reviewers: aaron.ballman, steakhal.
whisperity added a project: clang-tools-extra.
Herald added subscribers: carlosgalvezp, martong, gamesh411, Szelethus, dkrupp, rnkovacs, xazax.hun.
whisperity requested review of this revision.
Herald added a subscriber: cfe-commits.

As originally reported by @steakhal in #54074 <http://github.com/llvm/llvm-project/issues/54074>, the name extraction logic in `readability-suspicious-call-argument` crashes if the argument passed to a function was a function call to a non-trivially named entity (e.g. an operator).

Fixed this crash case by ignoring such constructs and considering them as having no name.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120555

Files:
  clang-tools-extra/clang-tidy/readability/SuspiciousCallArgumentCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/readability-suspicious-call-argument.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/readability-suspicious-call-argument.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/readability-suspicious-call-argument.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability-suspicious-call-argument.cpp
@@ -485,3 +485,32 @@
 
   return 0;
 }
+
+namespace Issue_54074 {
+
+class T {};
+using OperatorTy = int(const T &, const T &);
+int operator-(const T &, const T &);
+
+template <typename U>
+struct Wrap {
+  Wrap(U);
+};
+
+template <typename V>
+void wrapTaker(V, Wrap<OperatorTy>);
+
+template <typename V>
+void wrapTaker(V aaaaa, V bbbbb, Wrap<OperatorTy>);
+
+void test() {
+  wrapTaker(0, operator-);
+  // NO-WARN. No crash!
+
+  int aaaaa = 4, bbbbb = 8;
+  wrapTaker(bbbbb, aaaaa, operator-);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 1st argument 'bbbbb' (passed to 'aaaaa') looks like it might be swapped with the 2nd, 'aaaaa' (passed to 'bbbbb')
+  // CHECK-MESSAGES: :[[@LINE-9]]:6: note: in the call to 'wrapTaker<int>', declared here
+}
+
+} // namespace Issue_54074
Index: clang-tools-extra/clang-tidy/readability/SuspiciousCallArgumentCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/SuspiciousCallArgumentCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/SuspiciousCallArgumentCheck.cpp
@@ -711,23 +711,28 @@
 
   for (std::size_t I = InitialArgIndex, J = MatchedCallExpr->getNumArgs();
        I < J; ++I) {
+    assert(ArgTypes.size() == I - InitialArgIndex &&
+           ArgNames.size() == ArgTypes.size() &&
+           "Every iteration must put an element into the vectors!");
+
     if (const auto *ArgExpr = dyn_cast<DeclRefExpr>(
             MatchedCallExpr->getArg(I)->IgnoreUnlessSpelledInSource())) {
       if (const auto *Var = dyn_cast<VarDecl>(ArgExpr->getDecl())) {
         ArgTypes.push_back(Var->getType());
         ArgNames.push_back(Var->getName());
-      } else if (const auto *FCall =
-                     dyn_cast<FunctionDecl>(ArgExpr->getDecl())) {
-        ArgTypes.push_back(FCall->getType());
-        ArgNames.push_back(FCall->getName());
-      } else {
-        ArgTypes.push_back(QualType());
-        ArgNames.push_back(StringRef());
+        continue;
+      }
+      if (const auto *FCall = dyn_cast<FunctionDecl>(ArgExpr->getDecl())) {
+        if (FCall->getNameInfo().getName().isIdentifier()) {
+          ArgTypes.push_back(FCall->getType());
+          ArgNames.push_back(FCall->getName());
+          continue;
+        }
       }
-    } else {
-      ArgTypes.push_back(QualType());
-      ArgNames.push_back(StringRef());
     }
+
+    ArgTypes.push_back(QualType());
+    ArgNames.push_back(StringRef());
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120555.411386.patch
Type: text/x-patch
Size: 2804 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220225/273aa409/attachment.bin>


More information about the cfe-commits mailing list