[llvm-branch-commits] [clang-tools-extra] e89602b - [clang-tidy] Fix `readability-suspicious-call-argument` crash for arguments without name-like identifier
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Feb 25 07:53:50 PST 2022
Author: Whisperity
Date: 2022-02-25T16:30:13+01:00
New Revision: e89602b7b2ec12f20f2618cefb864c2b22d0048a
URL: https://github.com/llvm/llvm-project/commit/e89602b7b2ec12f20f2618cefb864c2b22d0048a
DIFF: https://github.com/llvm/llvm-project/commit/e89602b7b2ec12f20f2618cefb864c2b22d0048a.diff
LOG: [clang-tidy] Fix `readability-suspicious-call-argument` crash for arguments without name-like identifier
As originally reported by @steakhal in
http://github.com/llvm/llvm-project/issues/54074, the name extraction logic of
`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.
Reviewed By: aaron.ballman, steakhal
Differential Revision: http://reviews.llvm.org/D120555
(Cherry-picked from commit 416e689ecda66616da855c82f7ec652657730c6a)
Added:
Modified:
clang-tools-extra/clang-tidy/readability/SuspiciousCallArgumentCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/readability-suspicious-call-argument.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/SuspiciousCallArgumentCheck.cpp b/clang-tools-extra/clang-tidy/readability/SuspiciousCallArgumentCheck.cpp
index 4d7c3451acc7a..ac6bda3ff09ff 100644
--- a/clang-tools-extra/clang-tidy/readability/SuspiciousCallArgumentCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/SuspiciousCallArgumentCheck.cpp
@@ -711,23 +711,28 @@ void SuspiciousCallArgumentCheck::setArgNamesAndTypes(
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());
}
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 59aff131f6c22..a794983708cfc 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -352,6 +352,10 @@ Changes in existing checks
return statements associated with ``case``, ``default`` and labeled
statements.
+- Fixed a crash in :doc:`readability-suspicious-call-argument
+ <clang-tidy/checks/readability-suspicious-call-argument>` related to passing
+ arguments that refer to program elements without a trivial identifier.
+
Removed checks
^^^^^^^^^^^^^^
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability-suspicious-call-argument.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-suspicious-call-argument.cpp
index 2597ee3b9e030..edd3591517af3 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability-suspicious-call-argument.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability-suspicious-call-argument.cpp
@@ -485,3 +485,32 @@ int main() {
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
More information about the llvm-branch-commits
mailing list