Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- CalledOnceCheck.h - Check 'called once' parameters -------*- 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 //  This file defines a check for function-like parameters that should be
0010 //  called exactly one time.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_CALLEDONCECHECK_H
0015 #define LLVM_CLANG_ANALYSIS_ANALYSES_CALLEDONCECHECK_H
0016 
0017 namespace clang {
0018 
0019 class AnalysisDeclContext;
0020 class BlockDecl;
0021 class CFG;
0022 class Decl;
0023 class Expr;
0024 class ParmVarDecl;
0025 class Stmt;
0026 
0027 /// Classification of situations when parameter is not called on every path.
0028 /// \enum IfThen -- then branch of the if statement has no call.
0029 /// \enum IfElse -- else branch of the if statement has no call.
0030 /// \enum Switch -- one of the switch cases doesn't have a call.
0031 /// \enum SwitchSkipped -- there is no call if none of the cases applies.
0032 /// \enum LoopEntered -- no call when the loop is entered.
0033 /// \enum LoopSkipped -- no call when the loop is not entered.
0034 /// \enum FallbackReason -- fallback case when we were not able to figure out
0035 /// the reason.
0036 enum class NeverCalledReason {
0037   IfThen,
0038   IfElse,
0039   Switch,
0040   SwitchSkipped,
0041   LoopEntered,
0042   LoopSkipped,
0043   FallbackReason,
0044   LARGEST_VALUE = FallbackReason
0045 };
0046 
0047 class CalledOnceCheckHandler {
0048 public:
0049   CalledOnceCheckHandler() = default;
0050   virtual ~CalledOnceCheckHandler() = default;
0051 
0052   /// Called when parameter is called twice.
0053   /// \param Parameter -- parameter that should be called once.
0054   /// \param Call -- call to report the warning.
0055   /// \param PrevCall -- previous call.
0056   /// \param IsCompletionHandler -- true, if parameter is a completion handler.
0057   /// \param Poised -- true, if the second call is guaranteed to happen after
0058   /// the first call.
0059   virtual void handleDoubleCall(const ParmVarDecl *Parameter, const Expr *Call,
0060                                 const Expr *PrevCall, bool IsCompletionHandler,
0061                                 bool Poised) {}
0062 
0063   /// Called when parameter is not called at all.
0064   /// \param Parameter -- parameter that should be called once.
0065   /// \param IsCompletionHandler -- true, if parameter is a completion handler.
0066   virtual void handleNeverCalled(const ParmVarDecl *Parameter,
0067                                  bool IsCompletionHandler) {}
0068 
0069   /// Called when captured parameter is not called at all.
0070   /// \param Parameter -- parameter that should be called once.
0071   /// \param Where -- declaration that captures \p Parameter
0072   /// \param IsCompletionHandler -- true, if parameter is a completion handler.
0073   virtual void handleCapturedNeverCalled(const ParmVarDecl *Parameter,
0074                                          const Decl *Where,
0075                                          bool IsCompletionHandler) {}
0076 
0077   /// Called when parameter is not called on one of the paths.
0078   /// Usually we try to find a statement that is the least common ancestor of
0079   /// the path containing the call and not containing the call.  This helps us
0080   /// to pinpoint a bad path for the user.
0081   /// \param Parameter -- parameter that should be called once.
0082   /// \param Function -- function declaration where the problem occurred.
0083   /// \param Where -- the least common ancestor statement.
0084   /// \param Reason -- a reason describing the path without a call.
0085   /// \param IsCalledDirectly -- true, if parameter actually gets called on
0086   /// the other path.  It is opposed to be used in some other way (added to some
0087   /// collection, passed as a parameter, etc.).
0088   /// \param IsCompletionHandler -- true, if parameter is a completion handler.
0089   virtual void handleNeverCalled(const ParmVarDecl *Parameter,
0090                                  const Decl *Function, const Stmt *Where,
0091                                  NeverCalledReason Reason,
0092                                  bool IsCalledDirectly,
0093                                  bool IsCompletionHandler) {}
0094 
0095   /// Called when the block is guaranteed to be called exactly once.
0096   /// It means that we can be stricter with what we report on that block.
0097   /// \param Block -- block declaration that is known to be called exactly once.
0098   virtual void
0099   handleBlockThatIsGuaranteedToBeCalledOnce(const BlockDecl *Block) {}
0100 
0101   /// Called when the block has no guarantees about how many times it can get
0102   /// called.
0103   /// It means that we should be more lenient with reporting warnings in it.
0104   /// \param Block -- block declaration in question.
0105   virtual void handleBlockWithNoGuarantees(const BlockDecl *Block) {}
0106 };
0107 
0108 /// Check given CFG for 'called once' parameter violations.
0109 ///
0110 /// It traverses the function and tracks how such parameters are used.
0111 /// It detects two main violations:
0112 ///   * parameter is called twice
0113 ///   * parameter is not called
0114 ///
0115 /// \param AC -- context.
0116 /// \param Handler -- a handler for found violations.
0117 /// \param CheckConventionalParameters -- true, if we want to check parameters
0118 /// not explicitly marked as 'called once', but having the same requirements
0119 /// according to conventions.
0120 void checkCalledOnceParameters(AnalysisDeclContext &AC,
0121                                CalledOnceCheckHandler &Handler,
0122                                bool CheckConventionalParameters);
0123 
0124 } // end namespace clang
0125 
0126 #endif /* LLVM_CLANG_ANALYSIS_ANALYSES_CALLEDONCECHECK_H */