blob: 39959e619096017b8dfcc2900a2b43c9e511bec0 [file] [log] [blame]
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001//===--- 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
14namespace clang {
15namespace tidy {
16namespace 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
22class NonConstParameterCheck : public ClangTidyCheck {
23public:
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
30private:
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