1 //===--- SwappedArgumentsCheck.cpp - clang-tidy ---------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "SwappedArgumentsCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/Lex/Lexer.h"
12 #include "clang/Tooling/FixIt.h"
13 #include "llvm/ADT/SmallPtrSet.h"
14
15 using namespace clang::ast_matchers;
16
17 namespace clang {
18 namespace tidy {
19 namespace bugprone {
20
registerMatchers(MatchFinder * Finder)21 void SwappedArgumentsCheck::registerMatchers(MatchFinder *Finder) {
22 Finder->addMatcher(callExpr().bind("call"), this);
23 }
24
25 /// Look through lvalue to rvalue and nop casts. This filters out
26 /// implicit conversions that have no effect on the input but block our view for
27 /// other implicit casts.
ignoreNoOpCasts(const Expr * E)28 static const Expr *ignoreNoOpCasts(const Expr *E) {
29 if (auto *Cast = dyn_cast<CastExpr>(E))
30 if (Cast->getCastKind() == CK_LValueToRValue ||
31 Cast->getCastKind() == CK_NoOp)
32 return ignoreNoOpCasts(Cast->getSubExpr());
33 return E;
34 }
35
36 /// Restrict the warning to implicit casts that are most likely
37 /// accidental. User defined or integral conversions fit in this category,
38 /// lvalue to rvalue or derived to base does not.
isImplicitCastCandidate(const CastExpr * Cast)39 static bool isImplicitCastCandidate(const CastExpr *Cast) {
40 return Cast->getCastKind() == CK_UserDefinedConversion ||
41 Cast->getCastKind() == CK_FloatingToBoolean ||
42 Cast->getCastKind() == CK_FloatingToIntegral ||
43 Cast->getCastKind() == CK_IntegralToBoolean ||
44 Cast->getCastKind() == CK_IntegralToFloating ||
45 Cast->getCastKind() == CK_MemberPointerToBoolean ||
46 Cast->getCastKind() == CK_PointerToBoolean;
47 }
48
check(const MatchFinder::MatchResult & Result)49 void SwappedArgumentsCheck::check(const MatchFinder::MatchResult &Result) {
50 const ASTContext &Ctx = *Result.Context;
51 const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
52
53 llvm::SmallPtrSet<const Expr *, 4> UsedArgs;
54 for (unsigned I = 1, E = Call->getNumArgs(); I < E; ++I) {
55 const Expr *LHS = Call->getArg(I - 1);
56 const Expr *RHS = Call->getArg(I);
57
58 // Only need to check RHS, as LHS has already been covered. We don't want to
59 // emit two warnings for a single argument.
60 if (UsedArgs.count(RHS))
61 continue;
62
63 const auto *LHSCast = dyn_cast<ImplicitCastExpr>(ignoreNoOpCasts(LHS));
64 const auto *RHSCast = dyn_cast<ImplicitCastExpr>(ignoreNoOpCasts(RHS));
65
66 // Look if this is a potentially swapped argument pair. First look for
67 // implicit casts.
68 if (!LHSCast || !RHSCast || !isImplicitCastCandidate(LHSCast) ||
69 !isImplicitCastCandidate(RHSCast))
70 continue;
71
72 // If the types that go into the implicit casts match the types of the other
73 // argument in the declaration there is a high probability that the
74 // arguments were swapped.
75 // TODO: We could make use of the edit distance between the argument name
76 // and the name of the passed variable in addition to this type based
77 // heuristic.
78 const Expr *LHSFrom = ignoreNoOpCasts(LHSCast->getSubExpr());
79 const Expr *RHSFrom = ignoreNoOpCasts(RHSCast->getSubExpr());
80 if (LHS->getType() == RHS->getType() ||
81 LHS->getType() != RHSFrom->getType() ||
82 RHS->getType() != LHSFrom->getType())
83 continue;
84
85 // Emit a warning and fix-its that swap the arguments.
86 diag(Call->getBeginLoc(), "argument with implicit conversion from %0 "
87 "to %1 followed by argument converted from "
88 "%2 to %3, potentially swapped arguments.")
89 << LHS->getType() << LHSFrom->getType() << RHS->getType()
90 << RHSFrom->getType()
91 << tooling::fixit::createReplacement(*LHS, *RHS, Ctx)
92 << tooling::fixit::createReplacement(*RHS, *LHS, Ctx);
93
94 // Remember that we emitted a warning for this argument.
95 UsedArgs.insert(RHSCast);
96 }
97 }
98
99 } // namespace bugprone
100 } // namespace tidy
101 } // namespace clang
102