10#include "clang/AST/ASTContext.h"
11#include "clang/Lex/Lexer.h"
12#include "clang/Tooling/FixIt.h"
13#include "llvm/ADT/SmallPtrSet.h"
20 Finder->addMatcher(callExpr(unless(isInTemplateInstantiation())).bind(
"call"),
28 if (
auto *Cast = dyn_cast<CastExpr>(
E))
29 if (Cast->getCastKind() == CK_LValueToRValue ||
30 Cast->getCastKind() == CK_NoOp)
39 return Cast->getCastKind() == CK_UserDefinedConversion ||
40 Cast->getCastKind() == CK_FloatingToBoolean ||
41 Cast->getCastKind() == CK_FloatingToIntegral ||
42 Cast->getCastKind() == CK_IntegralToBoolean ||
43 Cast->getCastKind() == CK_IntegralToFloating ||
44 Cast->getCastKind() == CK_MemberPointerToBoolean ||
45 Cast->getCastKind() == CK_PointerToBoolean ||
46 (Cast->getCastKind() == CK_IntegralCast &&
47 Cast->getSubExpr()->getType()->isBooleanType());
54 if (!L->isBuiltinType() || !R->isBuiltinType())
57 return (L->isFloatingType() && R->isFloatingType()) ||
58 (L->isIntegerType() && R->isIntegerType()) ||
59 (L->isBooleanType() && R->isBooleanType());
65 const QualType RFrom) {
66 if (LTo == RTo || LFrom == RFrom)
70 if (LTo == RFrom && REq)
74 if (RTo == LFrom && LEq)
84 const ASTContext &Ctx = *Result.Context;
85 const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"call");
87 llvm::SmallPtrSet<const Expr *, 4> UsedArgs;
88 for (
unsigned I = 1,
E = Call->getNumArgs(); I <
E; ++I) {
89 const Expr *LHS = Call->getArg(I - 1);
90 const Expr *RHS = Call->getArg(I);
94 if (UsedArgs.count(RHS))
115 LHSFrom->getType(), RHSFrom->getType()))
119 diag(Call->getBeginLoc(),
"argument with implicit conversion from %0 "
120 "to %1 followed by argument converted from "
121 "%2 to %3, potentially swapped arguments.")
122 << LHSFrom->getType() << LHS->getType() << RHSFrom->getType()
123 << RHS->getType() << tooling::fixit::createReplacement(*LHS, *RHS, Ctx)
124 << tooling::fixit::createReplacement(*RHS, *LHS, Ctx);
127 UsedArgs.insert(RHSCast);
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static bool isImplicitCastCandidate(const CastExpr *Cast)
Restrict the warning to implicit casts that are most likely accidental.
static const Expr * ignoreNoOpCasts(const Expr *E)
Look through lvalue to rvalue and nop casts.
static bool areArgumentsPotentiallySwapped(const QualType LTo, const QualType RTo, const QualType LFrom, const QualType RFrom)
static bool areTypesSemiEqual(const QualType L, const QualType R)