1 //===--- SuspiciousCallArgumentCheck.h - clang-tidy -------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SUSPICIOUSCALLARGUMENTCHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SUSPICIOUSCALLARGUMENTCHECK_H
12 #include "../ClangTidyCheck.h"
13 #include "llvm/ADT/StringSet.h"
16 namespace clang::tidy::readability
{
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
22 /// For the user-facing documentation see:
23 /// http://clang.llvm.org/extra/clang-tidy/checks/readability/suspicious-call-argument.html
24 class SuspiciousCallArgumentCheck
: public ClangTidyCheck
{
25 enum class Heuristic
{
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.
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.
51 static constexpr std::size_t SmallVectorSize
= 8;
52 static constexpr std::size_t HeuristicCount
=
53 static_cast<std::size_t>(Heuristic::Dice
) + 1;
55 SuspiciousCallArgumentCheck(StringRef Name
, ClangTidyContext
*Context
);
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
;
61 const std::size_t MinimumIdentifierNameLength
;
63 /// The configuration for which heuristics were enabled.
64 SmallVector
<Heuristic
, HeuristicCount
> AppliedHeuristics
;
66 /// The lower and upper bounds for each heuristic, as configured by the user.
67 SmallVector
<std::pair
<int8_t, int8_t>, HeuristicCount
> ConfiguredBounds
;
69 /// The abbreviation-to-abbreviated map for the Abbreviation heuristic.
70 llvm::StringMap
<std::string
> AbbreviationDictionary
;
72 bool isHeuristicEnabled(Heuristic H
) const;
73 std::optional
<int8_t> getBound(Heuristic H
, BoundKind BK
) const;
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
;
81 void setParamNamesAndTypes(const FunctionDecl
*CalleeFuncDecl
);
83 void setArgNamesAndTypes(const CallExpr
*MatchedCallExpr
,
84 std::size_t InitialArgIndex
);
86 bool areParamAndArgComparable(std::size_t Position1
, std::size_t Position2
,
87 const ASTContext
&Ctx
) const;
89 bool areArgsSwapped(std::size_t Position1
, std::size_t Position2
) const;
91 bool areNamesSimilar(StringRef Arg
, StringRef Param
, Heuristic H
,
95 } // namespace clang::tidy::readability
97 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SUSPICIOUSCALLARGUMENTCHECK_H