xref: /llvm-project/clang-tools-extra/clang-tidy/readability/SuspiciousCallArgumentCheck.h (revision 4718da506091a37ca4863d979bc541e359b79b10)
173e4b5cfSWhisperity //===--- SuspiciousCallArgumentCheck.h - clang-tidy -------------*- C++ -*-===//
273e4b5cfSWhisperity //
373e4b5cfSWhisperity // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
473e4b5cfSWhisperity // See https://llvm.org/LICENSE.txt for license information.
573e4b5cfSWhisperity // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
673e4b5cfSWhisperity //
773e4b5cfSWhisperity //===----------------------------------------------------------------------===//
873e4b5cfSWhisperity 
973e4b5cfSWhisperity #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SUSPICIOUSCALLARGUMENTCHECK_H
1073e4b5cfSWhisperity #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SUSPICIOUSCALLARGUMENTCHECK_H
1173e4b5cfSWhisperity 
1273e4b5cfSWhisperity #include "../ClangTidyCheck.h"
1373e4b5cfSWhisperity #include "llvm/ADT/StringSet.h"
1471f55735SKazu Hirata #include <optional>
1573e4b5cfSWhisperity 
16*4718da50SCarlos Galvez namespace clang::tidy::readability {
1773e4b5cfSWhisperity 
1873e4b5cfSWhisperity /// Finds function calls where the arguments passed are provided out of order,
1973e4b5cfSWhisperity /// based on the difference between the argument name and the parameter names
2073e4b5cfSWhisperity /// of the function.
2173e4b5cfSWhisperity ///
2273e4b5cfSWhisperity /// For the user-facing documentation see:
236e566bc5SRichard /// http://clang.llvm.org/extra/clang-tidy/checks/readability/suspicious-call-argument.html
2473e4b5cfSWhisperity class SuspiciousCallArgumentCheck : public ClangTidyCheck {
2573e4b5cfSWhisperity   enum class Heuristic {
2673e4b5cfSWhisperity     Equality,
2773e4b5cfSWhisperity     Abbreviation,
2873e4b5cfSWhisperity     Prefix,
2973e4b5cfSWhisperity     Suffix,
3073e4b5cfSWhisperity     Substring,
3173e4b5cfSWhisperity     Levenshtein,
3273e4b5cfSWhisperity     JaroWinkler,
3373e4b5cfSWhisperity     Dice
3473e4b5cfSWhisperity   };
3573e4b5cfSWhisperity 
3673e4b5cfSWhisperity   /// When applying a heuristic, the value of this enum decides which kind of
3773e4b5cfSWhisperity   /// bound will be selected from the bounds configured for the heuristic.
3873e4b5cfSWhisperity   /// This only applies to heuristics that can take bounds.
3973e4b5cfSWhisperity   enum class BoundKind {
4073e4b5cfSWhisperity     /// Check for dissimilarity of the names. Names are deemed dissimilar if
4173e4b5cfSWhisperity     /// the similarity measurement is **below** the configured threshold.
4273e4b5cfSWhisperity     DissimilarBelow,
4373e4b5cfSWhisperity 
4473e4b5cfSWhisperity     /// Check for similarity of the names. Names are deemed similar if the
4573e4b5cfSWhisperity     /// similarity measurement (the result of heuristic) is **above** the
4673e4b5cfSWhisperity     /// configured threshold.
4773e4b5cfSWhisperity     SimilarAbove
4873e4b5cfSWhisperity   };
4973e4b5cfSWhisperity 
5073e4b5cfSWhisperity public:
5173e4b5cfSWhisperity   static constexpr std::size_t SmallVectorSize = 8;
5273e4b5cfSWhisperity   static constexpr std::size_t HeuristicCount =
5373e4b5cfSWhisperity       static_cast<std::size_t>(Heuristic::Dice) + 1;
5473e4b5cfSWhisperity 
5573e4b5cfSWhisperity   SuspiciousCallArgumentCheck(StringRef Name, ClangTidyContext *Context);
5673e4b5cfSWhisperity   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
5773e4b5cfSWhisperity   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
5873e4b5cfSWhisperity   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
5973e4b5cfSWhisperity 
6073e4b5cfSWhisperity private:
6173e4b5cfSWhisperity   const std::size_t MinimumIdentifierNameLength;
6273e4b5cfSWhisperity 
6373e4b5cfSWhisperity   /// The configuration for which heuristics were enabled.
6473e4b5cfSWhisperity   SmallVector<Heuristic, HeuristicCount> AppliedHeuristics;
6573e4b5cfSWhisperity 
6673e4b5cfSWhisperity   /// The lower and upper bounds for each heuristic, as configured by the user.
6773e4b5cfSWhisperity   SmallVector<std::pair<int8_t, int8_t>, HeuristicCount> ConfiguredBounds;
6873e4b5cfSWhisperity 
6973e4b5cfSWhisperity   /// The abbreviation-to-abbreviated map for the Abbreviation heuristic.
7073e4b5cfSWhisperity   llvm::StringMap<std::string> AbbreviationDictionary;
7173e4b5cfSWhisperity 
7273e4b5cfSWhisperity   bool isHeuristicEnabled(Heuristic H) const;
73f71ffd3bSKazu Hirata   std::optional<int8_t> getBound(Heuristic H, BoundKind BK) const;
7473e4b5cfSWhisperity 
7573e4b5cfSWhisperity   // Runtime information of the currently analyzed function call.
7673e4b5cfSWhisperity   SmallVector<QualType, SmallVectorSize> ArgTypes;
7773e4b5cfSWhisperity   SmallVector<StringRef, SmallVectorSize> ArgNames;
7873e4b5cfSWhisperity   SmallVector<QualType, SmallVectorSize> ParamTypes;
7973e4b5cfSWhisperity   SmallVector<StringRef, SmallVectorSize> ParamNames;
8073e4b5cfSWhisperity 
8173e4b5cfSWhisperity   void setParamNamesAndTypes(const FunctionDecl *CalleeFuncDecl);
8273e4b5cfSWhisperity 
8373e4b5cfSWhisperity   void setArgNamesAndTypes(const CallExpr *MatchedCallExpr,
8473e4b5cfSWhisperity                            std::size_t InitialArgIndex);
8573e4b5cfSWhisperity 
8673e4b5cfSWhisperity   bool areParamAndArgComparable(std::size_t Position1, std::size_t Position2,
8773e4b5cfSWhisperity                                 const ASTContext &Ctx) const;
8873e4b5cfSWhisperity 
8973e4b5cfSWhisperity   bool areArgsSwapped(std::size_t Position1, std::size_t Position2) const;
9073e4b5cfSWhisperity 
9173e4b5cfSWhisperity   bool areNamesSimilar(StringRef Arg, StringRef Param, Heuristic H,
9273e4b5cfSWhisperity                        BoundKind BK) const;
9373e4b5cfSWhisperity };
9473e4b5cfSWhisperity 
95*4718da50SCarlos Galvez } // namespace clang::tidy::readability
9673e4b5cfSWhisperity 
9773e4b5cfSWhisperity #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SUSPICIOUSCALLARGUMENTCHECK_H
98