|
| 1 | +//===--- SwappedArgumentsCheck.cpp - clang-tidy ---------------------------===// |
| 2 | +// |
| 3 | +// The LLVM Compiler Infrastructure |
| 4 | +// |
| 5 | +// This file is distributed under the University of Illinois Open Source |
| 6 | +// License. See LICENSE.TXT for details. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#include "SwappedArgumentsCheck.h" |
| 11 | +#include "clang/AST/ASTContext.h" |
| 12 | +#include "clang/Lex/Lexer.h" |
| 13 | +#include "llvm/ADT/SmallPtrSet.h" |
| 14 | + |
| 15 | +using namespace clang::ast_matchers; |
| 16 | + |
| 17 | +namespace clang { |
| 18 | +namespace tidy { |
| 19 | + |
| 20 | +void SwappedArgumentsCheck::registerMatchers(MatchFinder *Finder) { |
| 21 | + Finder->addMatcher(callExpr().bind("call"), this); |
| 22 | +} |
| 23 | + |
| 24 | +/// \brief Look through lvalue to rvalue and nop casts. This filters out |
| 25 | +/// implicit conversions that have no effect on the input but block our view for |
| 26 | +/// other implicit casts. |
| 27 | +static const Expr *ignoreNoOpCasts(const Expr *E) { |
| 28 | + if (auto *Cast = dyn_cast<CastExpr>(E)) |
| 29 | + if (Cast->getCastKind() == CK_LValueToRValue || |
| 30 | + Cast->getCastKind() == CK_NoOp) |
| 31 | + return ignoreNoOpCasts(Cast->getSubExpr()); |
| 32 | + return E; |
| 33 | +} |
| 34 | + |
| 35 | +/// \brief Restrict the warning to implicit casts that are most likely |
| 36 | +/// accidental. User defined or integral conversions fit in this category, |
| 37 | +/// lvalue to rvalue or derived to base does not. |
| 38 | +static bool isImplicitCastCandidate(const CastExpr *Cast) { |
| 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 | +} |
| 47 | + |
| 48 | +/// \brief Get a StringRef representing a SourceRange. |
| 49 | +static StringRef getAsString(const MatchFinder::MatchResult &Result, |
| 50 | + SourceRange R) { |
| 51 | + const SourceManager &SM = *Result.SourceManager; |
| 52 | + // Don't even try to resolve macro or include contraptions. Not worth emitting |
| 53 | + // a fixit for. |
| 54 | + if (R.getBegin().isMacroID() || |
| 55 | + !SM.isWrittenInSameFile(R.getBegin(), R.getEnd())) |
| 56 | + return StringRef(); |
| 57 | + |
| 58 | + const char *Begin = SM.getCharacterData(R.getBegin()); |
| 59 | + const char *End = SM.getCharacterData(Lexer::getLocForEndOfToken( |
| 60 | + R.getEnd(), 0, SM, Result.Context->getLangOpts())); |
| 61 | + |
| 62 | + return StringRef(Begin, End - Begin); |
| 63 | +} |
| 64 | + |
| 65 | +void SwappedArgumentsCheck::check(const MatchFinder::MatchResult &Result) { |
| 66 | + auto *Call = Result.Nodes.getStmtAs<CallExpr>("call"); |
| 67 | + |
| 68 | + llvm::SmallPtrSet<const Expr *, 4> UsedArgs; |
| 69 | + for (unsigned I = 1, E = Call->getNumArgs(); I < E; ++I) { |
| 70 | + const Expr *LHS = Call->getArg(I - 1); |
| 71 | + const Expr *RHS = Call->getArg(I); |
| 72 | + |
| 73 | + // Only need to check RHS, as LHS has already been covered. We don't want to |
| 74 | + // emit two warnings for a single argument. |
| 75 | + if (UsedArgs.count(RHS)) |
| 76 | + continue; |
| 77 | + |
| 78 | + const auto *LHSCast = dyn_cast<ImplicitCastExpr>(ignoreNoOpCasts(LHS)); |
| 79 | + const auto *RHSCast = dyn_cast<ImplicitCastExpr>(ignoreNoOpCasts(RHS)); |
| 80 | + |
| 81 | + // Look if this is a potentially swapped argument pair. First look for |
| 82 | + // implicit casts. |
| 83 | + if (!LHSCast || !RHSCast || !isImplicitCastCandidate(LHSCast) || |
| 84 | + !isImplicitCastCandidate(RHSCast)) |
| 85 | + continue; |
| 86 | + |
| 87 | + // If the types that go into the implicit casts match the types of the other |
| 88 | + // argument in the declaration there is a high probability that the |
| 89 | + // arguments were swapped. |
| 90 | + // TODO: We could make use of the edit distance between the argument name |
| 91 | + // and the name of the passed variable in addition to this type based |
| 92 | + // heuristic. |
| 93 | + const Expr *LHSFrom = ignoreNoOpCasts(LHSCast->getSubExpr()); |
| 94 | + const Expr *RHSFrom = ignoreNoOpCasts(RHSCast->getSubExpr()); |
| 95 | + if (LHS->getType() == RHS->getType() || |
| 96 | + LHS->getType() != RHSFrom->getType() || |
| 97 | + RHS->getType() != LHSFrom->getType()) |
| 98 | + continue; |
| 99 | + |
| 100 | + // Emit a warning and fix-its that swap the arguments. |
| 101 | + SourceRange LHSRange = LHS->getSourceRange(), |
| 102 | + RHSRange = RHS->getSourceRange(); |
| 103 | + auto D = |
| 104 | + diag(Call->getLocStart(), "argument with implicit conversion from %0 " |
| 105 | + "to %1 followed by argument converted from " |
| 106 | + "%2 to %3, potentially swapped arguments.") |
| 107 | + << LHS->getType() << LHSFrom->getType() << RHS->getType() |
| 108 | + << RHSFrom->getType() << LHSRange << RHSRange; |
| 109 | + |
| 110 | + StringRef RHSString = getAsString(Result, RHSRange); |
| 111 | + StringRef LHSString = getAsString(Result, LHSRange); |
| 112 | + if (!LHSString.empty() && !RHSString.empty()) { |
| 113 | + D << FixItHint::CreateReplacement( |
| 114 | + CharSourceRange::getTokenRange(LHSRange), RHSString) |
| 115 | + << FixItHint::CreateReplacement( |
| 116 | + CharSourceRange::getTokenRange(RHSRange), LHSString); |
| 117 | + } |
| 118 | + |
| 119 | + // Remember that we emitted a warning for this argument. |
| 120 | + UsedArgs.insert(RHSCast); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace tidy |
| 125 | +} // namespace clang |
0 commit comments