SuspiciousCallArgumentCheck.h 3.7 KB

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