Debian Codegraph @

llvm-project/clang-tools-extra/clang-tidy/bugprone/SwappedArgumentsCheck.cpp

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