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