clang-tools  14.0.0git
SuspiciousCallArgumentCheck.h
Go to the documentation of this file.
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
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.
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.
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 
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
clang::tidy::readability::SuspiciousCallArgumentCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: SuspiciousCallArgumentCheck.cpp:611
DissimilarBelow
const int8_t DissimilarBelow
The upper bound of % of similarity the two strings might have to be considered dissimilar.
Definition: SuspiciousCallArgumentCheck.cpp:32
Suffix
std::string Suffix
Definition: AddUsing.cpp:112
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::readability::SuspiciousCallArgumentCheck
Finds function calls where the arguments passed are provided out of order, based on the difference be...
Definition: SuspiciousCallArgumentCheck.h:25
Ctx
Context Ctx
Definition: TUScheduler.cpp:454
clang::tidy::readability::SuspiciousCallArgumentCheck::SuspiciousCallArgumentCheck
SuspiciousCallArgumentCheck(StringRef Name, ClangTidyContext *Context)
Definition: SuspiciousCallArgumentCheck.cpp:499
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::tidy::readability::SuspiciousCallArgumentCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: SuspiciousCallArgumentCheck.cpp:601
clang::tidy::readability::SuspiciousCallArgumentCheck::SmallVectorSize
static constexpr std::size_t SmallVectorSize
Definition: SuspiciousCallArgumentCheck.h:52
clang::tidy::readability::SuspiciousCallArgumentCheck::HeuristicCount
static constexpr std::size_t HeuristicCount
Definition: SuspiciousCallArgumentCheck.h:53
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:76
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::readability::SuspiciousCallArgumentCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: SuspiciousCallArgumentCheck.cpp:539
SimilarAbove
const int8_t SimilarAbove
The lower bound of % of similarity the two string must have to be considered similar.
Definition: SuspiciousCallArgumentCheck.cpp:38