10 #include "clang/AST/ASTContext.h" 11 #include "clang/Lex/Lexer.h" 12 #include "clang/Tooling/FixIt.h" 13 #include "llvm/ADT/SmallPtrSet.h" 21 void SwappedArgumentsCheck::registerMatchers(MatchFinder *Finder) {
22 Finder->addMatcher(callExpr().bind(
"call"),
this);
29 if (
auto *Cast = dyn_cast<CastExpr>(E))
30 if (Cast->getCastKind() == CK_LValueToRValue ||
31 Cast->getCastKind() == CK_NoOp)
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;
49 void SwappedArgumentsCheck::check(
const MatchFinder::MatchResult &Result) {
50 const ASTContext &
Ctx = *Result.Context;
51 const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"call");
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);
60 if (UsedArgs.count(RHS))
80 if (LHS->getType() == RHS->getType() ||
81 LHS->getType() != RHSFrom->getType() ||
82 RHS->getType() != LHSFrom->getType())
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()
91 << tooling::fixit::createReplacement(*LHS, *RHS, Ctx)
92 << tooling::fixit::createReplacement(*RHS, *LHS, Ctx);
95 UsedArgs.insert(RHSCast);
static const Expr * ignoreNoOpCasts(const Expr *E)
Look through lvalue to rvalue and nop casts.
static bool isImplicitCastCandidate(const CastExpr *Cast)
Restrict the warning to implicit casts that are most likely accidental.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//