Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===--- NonConstParameterCheck.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_NON_CONST_PARAMETER_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_NON_CONST_PARAMETER_H
0011 
0012 #include "../ClangTidyCheck.h"
0013 
0014 namespace clang::tidy::readability {
0015 
0016 /// Warn when a pointer function parameter can be const.
0017 ///
0018 /// For the user-facing documentation see:
0019 /// http://clang.llvm.org/extra/clang-tidy/checks/readability/non-const-parameter.html
0020 class NonConstParameterCheck : public ClangTidyCheck {
0021 public:
0022   NonConstParameterCheck(StringRef Name, ClangTidyContext *Context)
0023       : ClangTidyCheck(Name, Context) {}
0024   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
0025   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
0026   void onEndOfTranslationUnit() override;
0027   std::optional<TraversalKind> getCheckTraversalKind() const override {
0028     return TK_IgnoreUnlessSpelledInSource;
0029   }
0030 
0031 private:
0032   /// Parameter info.
0033   struct ParmInfo {
0034     /// Is function parameter referenced?
0035     bool IsReferenced;
0036 
0037     /// Can function parameter be const?
0038     bool CanBeConst;
0039   };
0040 
0041   /// Track all nonconst integer/float parameters.
0042   std::map<const ParmVarDecl *, ParmInfo> Parameters;
0043 
0044   /// Add function parameter.
0045   void addParm(const ParmVarDecl *Parm);
0046 
0047   /// Set IsReferenced.
0048   void setReferenced(const DeclRefExpr *Ref);
0049 
0050   /// Set CanNotBeConst.
0051   /// Visits sub expressions recursively. If a DeclRefExpr is found
0052   /// and CanNotBeConst is true the Parameter is marked as not-const.
0053   /// The CanNotBeConst is updated as sub expressions are visited.
0054   void markCanNotBeConst(const Expr *E, bool CanNotBeConst);
0055 
0056   /// Diagnose non const parameters.
0057   void diagnoseNonConstParameters();
0058 };
0059 
0060 } // namespace clang::tidy::readability
0061 
0062 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_NON_CONST_PARAMETER_H