1 //===--- SuspiciousCallArgumentCheck.h - clang-tidy -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SUSPICIOUSCALLARGUMENTCHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SUSPICIOUSCALLARGUMENTCHECK_H
11 
12 #include "../ClangTidyCheck.h"
13 #include "llvm/ADT/StringSet.h"
14 
15 namespace clang {
16 namespace tidy {
17 namespace readability {
18 
19 /// Finds function calls where the arguments passed are provided out of order,
20 /// based on the difference between the argument name and the parameter names
21 /// of the function.
22 ///
23 /// For the user-facing documentation see:
24 /// http://clang.llvm.org/extra/clang-tidy/checks/readability/suspicious-call-argument.html
25 class SuspiciousCallArgumentCheck : public ClangTidyCheck {
26   enum class Heuristic {
27     Equality,
28     Abbreviation,
29     Prefix,
30     Suffix,
31     Substring,
32     Levenshtein,
33     JaroWinkler,
34     Dice
35   };
36 
37   /// When applying a heuristic, the value of this enum decides which kind of
38   /// bound will be selected from the bounds configured for the heuristic.
39   /// This only applies to heuristics that can take bounds.
40   enum class BoundKind {
41     /// Check for dissimilarity of the names. Names are deemed dissimilar if
42     /// the similarity measurement is **below** the configured threshold.
43     DissimilarBelow,
44 
45     /// Check for similarity of the names. Names are deemed similar if the
46     /// similarity measurement (the result of heuristic) is **above** the
47     /// configured threshold.
48     SimilarAbove
49   };
50 
51 public:
52   static constexpr std::size_t SmallVectorSize = 8;
53   static constexpr std::size_t HeuristicCount =
54       static_cast<std::size_t>(Heuristic::Dice) + 1;
55 
56   SuspiciousCallArgumentCheck(StringRef Name, ClangTidyContext *Context);
57   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
58   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
59   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
60 
61 private:
62   const std::size_t MinimumIdentifierNameLength;
63 
64   /// The configuration for which heuristics were enabled.
65   SmallVector<Heuristic, HeuristicCount> AppliedHeuristics;
66 
67   /// The lower and upper bounds for each heuristic, as configured by the user.
68   SmallVector<std::pair<int8_t, int8_t>, HeuristicCount> ConfiguredBounds;
69 
70   /// The abbreviation-to-abbreviated map for the Abbreviation heuristic.
71   llvm::StringMap<std::string> AbbreviationDictionary;
72 
73   bool isHeuristicEnabled(Heuristic H) const;
74   Optional<int8_t> getBound(Heuristic H, BoundKind BK) const;
75 
76   // Runtime information of the currently analyzed function call.
77   SmallVector<QualType, SmallVectorSize> ArgTypes;
78   SmallVector<StringRef, SmallVectorSize> ArgNames;
79   SmallVector<QualType, SmallVectorSize> ParamTypes;
80   SmallVector<StringRef, SmallVectorSize> ParamNames;
81 
82   void setParamNamesAndTypes(const FunctionDecl *CalleeFuncDecl);
83 
84   void setArgNamesAndTypes(const CallExpr *MatchedCallExpr,
85                            std::size_t InitialArgIndex);
86 
87   bool areParamAndArgComparable(std::size_t Position1, std::size_t Position2,
88                                 const ASTContext &Ctx) const;
89 
90   bool areArgsSwapped(std::size_t Position1, std::size_t Position2) const;
91 
92   bool areNamesSimilar(StringRef Arg, StringRef Param, Heuristic H,
93                        BoundKind BK) const;
94 };
95 
96 } // namespace readability
97 } // namespace tidy
98 } // namespace clang
99 
100 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SUSPICIOUSCALLARGUMENTCHECK_H
101