clang-tools 20.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#include <optional>
15
17
18/// Finds function calls where the arguments passed are provided out of order,
19/// based on the difference between the argument name and the parameter names
20/// of the function.
21///
22/// For the user-facing documentation see:
23/// http://clang.llvm.org/extra/clang-tidy/checks/readability/suspicious-call-argument.html
25 enum class Heuristic {
26 Equality,
27 Abbreviation,
28 Prefix,
29 Suffix,
30 Substring,
31 Levenshtein,
32 JaroWinkler,
33 Dice
34 };
35
36 /// When applying a heuristic, the value of this enum decides which kind of
37 /// bound will be selected from the bounds configured for the heuristic.
38 /// This only applies to heuristics that can take bounds.
39 enum class BoundKind {
40 /// Check for dissimilarity of the names. Names are deemed dissimilar if
41 /// the similarity measurement is **below** the configured threshold.
43
44 /// Check for similarity of the names. Names are deemed similar if the
45 /// similarity measurement (the result of heuristic) is **above** the
46 /// configured threshold.
48 };
49
50public:
51 static constexpr std::size_t SmallVectorSize = 8;
52 static constexpr std::size_t HeuristicCount =
53 static_cast<std::size_t>(Heuristic::Dice) + 1;
54
56 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
57 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
58 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
59
60private:
61 const std::size_t MinimumIdentifierNameLength;
62
63 /// The configuration for which heuristics were enabled.
64 SmallVector<Heuristic, HeuristicCount> AppliedHeuristics;
65
66 /// The lower and upper bounds for each heuristic, as configured by the user.
67 SmallVector<std::pair<int8_t, int8_t>, HeuristicCount> ConfiguredBounds;
68
69 /// The abbreviation-to-abbreviated map for the Abbreviation heuristic.
70 llvm::StringMap<std::string> AbbreviationDictionary;
71
72 bool isHeuristicEnabled(Heuristic H) const;
73 std::optional<int8_t> getBound(Heuristic H, BoundKind BK) const;
74
75 // Runtime information of the currently analyzed function call.
76 SmallVector<QualType, SmallVectorSize> ArgTypes;
77 SmallVector<StringRef, SmallVectorSize> ArgNames;
78 SmallVector<QualType, SmallVectorSize> ParamTypes;
79 SmallVector<StringRef, SmallVectorSize> ParamNames;
80
81 void setParamNamesAndTypes(const FunctionDecl *CalleeFuncDecl);
82
83 void setArgNamesAndTypes(const CallExpr *MatchedCallExpr,
84 std::size_t InitialArgIndex);
85
86 bool areParamAndArgComparable(std::size_t Position1, std::size_t Position2,
87 const ASTContext &Ctx) const;
88
89 bool areArgsSwapped(std::size_t Position1, std::size_t Position2) const;
90
91 bool areNamesSimilar(StringRef Arg, StringRef Param, Heuristic H,
92 BoundKind BK) const;
93};
94
95} // namespace clang::tidy::readability
96
97#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SUSPICIOUSCALLARGUMENTCHECK_H
std::string Suffix
Definition: AddUsing.cpp:132
llvm::SmallString< 256U > Name
const int8_t SimilarAbove
The lower bound of % of similarity the two string must have to be considered similar.
const int8_t DissimilarBelow
The upper bound of % of similarity the two strings might have to be considered dissimilar.
Base class for all clang-tidy checks.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Finds function calls where the arguments passed are provided out of order, based on the difference be...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
llvm::StringMap< ClangTidyValue > OptionMap