clang  13.0.0git
CalledOnceCheck.h
Go to the documentation of this file.
1 //===- CalledOnceCheck.h - Check 'called once' parameters -------*- 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 // This file defines a check for function-like parameters that should be
10 // called exactly one time.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_CALLEDONCECHECK_H
15 #define LLVM_CLANG_ANALYSIS_ANALYSES_CALLEDONCECHECK_H
16 
17 namespace clang {
18 
20 class CFG;
21 class Decl;
22 class DeclContext;
23 class Expr;
24 class ParmVarDecl;
25 class Stmt;
26 
27 /// Classification of situations when parameter is not called on every path.
28 /// \enum IfThen -- then branch of the if statement has no call.
29 /// \enum IfElse -- else branch of the if statement has no call.
30 /// \enum Switch -- one of the switch cases doesn't have a call.
31 /// \enum SwitchSkipped -- there is no call if none of the cases appies.
32 /// \enum LoopEntered -- no call when the loop is entered.
33 /// \enum LoopSkipped -- no call when the loop is not entered.
34 /// \enum FallbackReason -- fallback case when we were not able to figure out
35 /// the reason.
36 enum class NeverCalledReason {
37  IfThen,
38  IfElse,
39  Switch,
45 };
46 
48 public:
49  CalledOnceCheckHandler() = default;
50  virtual ~CalledOnceCheckHandler() = default;
51 
52  /// Called when parameter is called twice.
53  /// \param Parameter -- parameter that should be called once.
54  /// \param Call -- call to report the warning.
55  /// \param PrevCall -- previous call.
56  /// \param IsCompletionHandler -- true, if parameter is a completion handler.
57  /// \param Poised -- true, if the second call is guaranteed to happen after
58  /// the first call.
59  virtual void handleDoubleCall(const ParmVarDecl *Parameter, const Expr *Call,
60  const Expr *PrevCall, bool IsCompletionHandler,
61  bool Poised) {}
62 
63  /// Called when parameter is not called at all.
64  /// \param Parameter -- parameter that should be called once.
65  /// \param IsCompletionHandler -- true, if parameter is a completion handler.
66  virtual void handleNeverCalled(const ParmVarDecl *Parameter,
67  bool IsCompletionHandler) {}
68 
69  /// Called when captured parameter is not called at all.
70  /// \param Parameter -- parameter that should be called once.
71  /// \param Where -- declaration that captures \p Parameter
72  /// \param IsCompletionHandler -- true, if parameter is a completion handler.
73  virtual void handleCapturedNeverCalled(const ParmVarDecl *Parameter,
74  const Decl *Where,
75  bool IsCompletionHandler) {}
76 
77  /// Called when parameter is not called on one of the paths.
78  /// Usually we try to find a statement that is the least common ancestor of
79  /// the path containing the call and not containing the call. This helps us
80  /// to pinpoint a bad path for the user.
81  /// \param Parameter -- parameter that should be called once.
82  /// \param Where -- the least common ancestor statement.
83  /// \param Reason -- a reason describing the path without a call.
84  /// \param IsCalledDirectly -- true, if parameter actually gets called on
85  /// the other path. It is opposed to be used in some other way (added to some
86  /// collection, passed as a parameter, etc.).
87  /// \param IsCompletionHandler -- true, if parameter is a completion handler.
88  virtual void handleNeverCalled(const ParmVarDecl *Parameter,
89  const Stmt *Where, NeverCalledReason Reason,
90  bool IsCalledDirectly,
91  bool IsCompletionHandler) {}
92 };
93 
94 /// Check given CFG for 'called once' parameter violations.
95 ///
96 /// It traverses the function and tracks how such parameters are used.
97 /// It detects two main violations:
98 /// * parameter is called twice
99 /// * parameter is not called
100 ///
101 /// \param AC -- context.
102 /// \param Handler -- a handler for found violations.
103 /// \param CheckConventionalParameters -- true, if we want to check parameters
104 /// not explicitly marked as 'called once', but having the same requirements
105 /// according to conventions.
106 void checkCalledOnceParameters(AnalysisDeclContext &AC,
107  CalledOnceCheckHandler &Handler,
108  bool CheckConventionalParameters);
109 
110 } // end namespace clang
111 
112 #endif /* LLVM_CLANG_ANALYSIS_ANALYSES_CALLEDONCECHECK_H */
clang::CalledOnceCheckHandler::CalledOnceCheckHandler
CalledOnceCheckHandler()=default
clang::CalledOnceCheckHandler
Definition: CalledOnceCheck.h:47
clang::DeclContext
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1302
clang::CalledOnceCheckHandler::handleDoubleCall
virtual void handleDoubleCall(const ParmVarDecl *Parameter, const Expr *Call, const Expr *PrevCall, bool IsCompletionHandler, bool Poised)
Called when parameter is called twice.
Definition: CalledOnceCheck.h:59
clang::checkCalledOnceParameters
void checkCalledOnceParameters(AnalysisDeclContext &AC, CalledOnceCheckHandler &Handler, bool CheckConventionalParameters)
Check given CFG for 'called once' parameter violations.
Definition: CalledOnceCheck.cpp:1544
clang::AnalysisDeclContext
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Definition: AnalysisDeclContext.h:72
clang::ParmVarDecl
Represents a parameter to a function.
Definition: Decl.h:1620
clang::CalledOnceCheckHandler::handleCapturedNeverCalled
virtual void handleCapturedNeverCalled(const ParmVarDecl *Parameter, const Decl *Where, bool IsCompletionHandler)
Called when captured parameter is not called at all.
Definition: CalledOnceCheck.h:73
clang::NeverCalledReason::LoopSkipped
@ LoopSkipped
clang::CFG
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition: CFG.h:1225
clang::NeverCalledReason::LARGEST_VALUE
@ LARGEST_VALUE
clang::NeverCalledReason::IfThen
@ IfThen
clang::NeverCalledReason::LoopEntered
@ LoopEntered
clang::NeverCalledReason::SwitchSkipped
@ SwitchSkipped
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::NeverCalledReason
NeverCalledReason
Definition: CalledOnceCheck.h:36
clang::NeverCalledReason::FallbackReason
@ FallbackReason
clang::CalledOnceCheckHandler::handleNeverCalled
virtual void handleNeverCalled(const ParmVarDecl *Parameter, bool IsCompletionHandler)
Called when parameter is not called at all.
Definition: CalledOnceCheck.h:66
clang
Dataflow Directional Tag Classes.
Definition: CalledOnceCheck.h:17
clang::NeverCalledReason::Switch
@ Switch
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:68
clang::CalledOnceCheckHandler::handleNeverCalled
virtual void handleNeverCalled(const ParmVarDecl *Parameter, const Stmt *Where, NeverCalledReason Reason, bool IsCalledDirectly, bool IsCompletionHandler)
Called when parameter is not called on one of the paths.
Definition: CalledOnceCheck.h:88
clang::CalledOnceCheckHandler::~CalledOnceCheckHandler
virtual ~CalledOnceCheckHandler()=default
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::NeverCalledReason::IfElse
@ IfElse