Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:22

0001 //===--- SuspiciousCallArgumentCheck.h - clang-tidy -------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SUSPICIOUSCALLARGUMENTCHECK_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SUSPICIOUSCALLARGUMENTCHECK_H
0011 
0012 #include "../ClangTidyCheck.h"
0013 #include "llvm/ADT/StringSet.h"
0014 #include <optional>
0015 
0016 namespace clang::tidy::readability {
0017 
0018 /// Finds function calls where the arguments passed are provided out of order,
0019 /// based on the difference between the argument name and the parameter names
0020 /// of the function.
0021 ///
0022 /// For the user-facing documentation see:
0023 /// http://clang.llvm.org/extra/clang-tidy/checks/readability/suspicious-call-argument.html
0024 class SuspiciousCallArgumentCheck : public ClangTidyCheck {
0025   enum class Heuristic {
0026     Equality,
0027     Abbreviation,
0028     Prefix,
0029     Suffix,
0030     Substring,
0031     Levenshtein,
0032     JaroWinkler,
0033     Dice
0034   };
0035 
0036   /// When applying a heuristic, the value of this enum decides which kind of
0037   /// bound will be selected from the bounds configured for the heuristic.
0038   /// This only applies to heuristics that can take bounds.
0039   enum class BoundKind {
0040     /// Check for dissimilarity of the names. Names are deemed dissimilar if
0041     /// the similarity measurement is **below** the configured threshold.
0042     DissimilarBelow,
0043 
0044     /// Check for similarity of the names. Names are deemed similar if the
0045     /// similarity measurement (the result of heuristic) is **above** the
0046     /// configured threshold.
0047     SimilarAbove
0048   };
0049 
0050 public:
0051   static constexpr std::size_t SmallVectorSize = 8;
0052   static constexpr std::size_t HeuristicCount =
0053       static_cast<std::size_t>(Heuristic::Dice) + 1;
0054 
0055   SuspiciousCallArgumentCheck(StringRef Name, ClangTidyContext *Context);
0056   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
0057   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
0058   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
0059 
0060 private:
0061   const std::size_t MinimumIdentifierNameLength;
0062 
0063   /// The configuration for which heuristics were enabled.
0064   SmallVector<Heuristic, HeuristicCount> AppliedHeuristics;
0065 
0066   /// The lower and upper bounds for each heuristic, as configured by the user.
0067   SmallVector<std::pair<int8_t, int8_t>, HeuristicCount> ConfiguredBounds;
0068 
0069   /// The abbreviation-to-abbreviated map for the Abbreviation heuristic.
0070   llvm::StringMap<std::string> AbbreviationDictionary;
0071 
0072   bool isHeuristicEnabled(Heuristic H) const;
0073   std::optional<int8_t> getBound(Heuristic H, BoundKind BK) const;
0074 
0075   // Runtime information of the currently analyzed function call.
0076   SmallVector<QualType, SmallVectorSize> ArgTypes;
0077   SmallVector<StringRef, SmallVectorSize> ArgNames;
0078   SmallVector<QualType, SmallVectorSize> ParamTypes;
0079   SmallVector<StringRef, SmallVectorSize> ParamNames;
0080 
0081   void setParamNamesAndTypes(const FunctionDecl *CalleeFuncDecl);
0082 
0083   void setArgNamesAndTypes(const CallExpr *MatchedCallExpr,
0084                            std::size_t InitialArgIndex);
0085 
0086   bool areParamAndArgComparable(std::size_t Position1, std::size_t Position2,
0087                                 const ASTContext &Ctx) const;
0088 
0089   bool areArgsSwapped(std::size_t Position1, std::size_t Position2) const;
0090 
0091   bool areNamesSimilar(StringRef Arg, StringRef Param, Heuristic H,
0092                        BoundKind BK) const;
0093 };
0094 
0095 } // namespace clang::tidy::readability
0096 
0097 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SUSPICIOUSCALLARGUMENTCHECK_H