NonConstParameterCheck.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //===--- NonConstParameterCheck.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_NON_CONST_PARAMETER_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_NON_CONST_PARAMETER_H
  10. #include "../ClangTidyCheck.h"
  11. namespace clang::tidy::readability {
  12. /// Warn when a pointer function parameter can be const.
  13. ///
  14. /// For the user-facing documentation see:
  15. /// http://clang.llvm.org/extra/clang-tidy/checks/readability/non-const-parameter.html
  16. class NonConstParameterCheck : public ClangTidyCheck {
  17. public:
  18. NonConstParameterCheck(StringRef Name, ClangTidyContext *Context)
  19. : ClangTidyCheck(Name, Context) {}
  20. void registerMatchers(ast_matchers::MatchFinder *Finder) override;
  21. void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
  22. void onEndOfTranslationUnit() override;
  23. std::optional<TraversalKind> getCheckTraversalKind() const override {
  24. return TK_IgnoreUnlessSpelledInSource;
  25. }
  26. private:
  27. /// Parameter info.
  28. struct ParmInfo {
  29. /// Is function parameter referenced?
  30. bool IsReferenced;
  31. /// Can function parameter be const?
  32. bool CanBeConst;
  33. };
  34. /// Track all nonconst integer/float parameters.
  35. std::map<const ParmVarDecl *, ParmInfo> Parameters;
  36. /// Add function parameter.
  37. void addParm(const ParmVarDecl *Parm);
  38. /// Set IsReferenced.
  39. void setReferenced(const DeclRefExpr *Ref);
  40. /// Set CanNotBeConst.
  41. /// Visits sub expressions recursively. If a DeclRefExpr is found
  42. /// and CanNotBeConst is true the Parameter is marked as not-const.
  43. /// The CanNotBeConst is updated as sub expressions are visited.
  44. void markCanNotBeConst(const Expr *E, bool CanNotBeConst);
  45. /// Diagnose non const parameters.
  46. void diagnoseNonConstParameters();
  47. };
  48. } // namespace clang::tidy::readability
  49. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_NON_CONST_PARAMETER_H