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; 29 30 private: 31 /// Parameter info. 32 struct ParmInfo { 33 /// Is function parameter referenced? 34 bool IsReferenced; 35 36 /// Can function parameter be const? 37 bool CanBeConst; 38 }; 39 40 /// Track all nonconst integer/float parameters. 41 std::map<const ParmVarDecl *, ParmInfo> Parameters; 42 43 /// Add function parameter. 44 void addParm(const ParmVarDecl *Parm); 45 46 /// Set IsReferenced. 47 void setReferenced(const DeclRefExpr *Ref); 48 49 /// Set CanNotBeConst. 50 /// Visits sub expressions recursively. If a DeclRefExpr is found 51 /// and CanNotBeConst is true the Parameter is marked as not-const. 52 /// The CanNotBeConst is updated as sub expressions are visited. 53 void markCanNotBeConst(const Expr *E, bool CanNotBeConst); 54 55 /// Diagnose non const parameters. 56 void diagnoseNonConstParameters(); 57 }; 58 59 } // namespace readability 60 } // namespace tidy 61 } // namespace clang 62 63 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_NON_CONST_PARAMETER_H 64