clang-tools  14.0.0git
NonConstParameterCheck.h
Go to the documentation of this file.
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
23 public:
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  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
clang::tidy::readability::NonConstParameterCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: NonConstParameterCheck.cpp:19
clang::tidy::readability::NonConstParameterCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: NonConstParameterCheck.cpp:39
E
const Expr * E
Definition: AvoidBindCheck.cpp:88
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::tidy::readability::NonConstParameterCheck
Warn when a pointer function parameter can be const.
Definition: NonConstParameterCheck.h:22
clang::tidy::readability::NonConstParameterCheck::getCheckTraversalKind
llvm::Optional< TraversalKind > getCheckTraversalKind() const override
Definition: NonConstParameterCheck.h:29
clang::tidy::readability::NonConstParameterCheck::onEndOfTranslationUnit
void onEndOfTranslationUnit() override
Definition: NonConstParameterCheck.cpp:122
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:72
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::readability::NonConstParameterCheck::NonConstParameterCheck
NonConstParameterCheck(StringRef Name, ClangTidyContext *Context)
Definition: NonConstParameterCheck.h:24
Parm
Params Parm
Definition: ConfigCompileTests.cpp:41
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27