11 #include "clang/AST/ASTContext.h"
12 #include "clang/Lex/Lexer.h"
13 #include "llvm/ADT/SmallPtrSet.h"
15 using namespace clang::ast_matchers;
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;
52 const SourceManager &
SM = *Result.SourceManager;
55 if (R.getBegin().isMacroID() ||
56 !SM.isWrittenInSameFile(R.getBegin(), R.getEnd()))
59 const char *Begin = SM.getCharacterData(R.getBegin());
60 const char *End = SM.getCharacterData(Lexer::getLocForEndOfToken(
61 R.getEnd(), 0,
SM, Result.Context->getLangOpts()));
63 return StringRef(Begin, End - Begin);
66 void SwappedArgumentsCheck::check(
const MatchFinder::MatchResult &
Result) {
67 auto *Call = Result.Nodes.getStmtAs<CallExpr>(
"call");
69 llvm::SmallPtrSet<const Expr *, 4> UsedArgs;
70 for (
unsigned I = 1, E = Call->getNumArgs(); I < E; ++I) {
71 const Expr *LHS = Call->getArg(I - 1);
72 const Expr *RHS = Call->getArg(I);
76 if (UsedArgs.count(RHS))
96 if (LHS->getType() == RHS->getType() ||
97 LHS->getType() != RHSFrom->getType() ||
98 RHS->getType() != LHSFrom->getType())
102 SourceRange LHSRange = LHS->getSourceRange(),
103 RHSRange = RHS->getSourceRange();
105 diag(Call->getLocStart(),
"argument with implicit conversion from %0 "
106 "to %1 followed by argument converted from "
107 "%2 to %3, potentially swapped arguments.")
108 << LHS->getType() << LHSFrom->getType() << RHS->getType()
109 << RHSFrom->getType() << LHSRange << RHSRange;
111 StringRef RHSString =
getAsString(Result, RHSRange);
112 StringRef LHSString =
getAsString(Result, LHSRange);
113 if (!LHSString.empty() && !RHSString.empty()) {
114 D << FixItHint::CreateReplacement(
115 CharSourceRange::getTokenRange(LHSRange), RHSString)
116 << FixItHint::CreateReplacement(
117 CharSourceRange::getTokenRange(RHSRange), LHSString);
121 UsedArgs.insert(RHSCast);
static const Expr * ignoreNoOpCasts(const Expr *E)
Look through lvalue to rvalue and nop casts.
std::unique_ptr< ast_matchers::MatchFinder > Finder
static StringRef getAsString(const MatchFinder::MatchResult &Result, SourceRange R)
Get a StringRef representing a SourceRange.
static bool isImplicitCastCandidate(const CastExpr *Cast)
Restrict the warning to implicit casts that are most likely accidental.