xref: /freebsd/contrib/llvm-project/clang/include/clang/Sema/AnalysisBasedWarnings.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //=- AnalysisBasedWarnings.h - Sema warnings based on libAnalysis -*- 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 AnalysisBasedWarnings, a worker object used by Sema
10 // that issues warnings based on dataflow-analysis.
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_SEMA_ANALYSISBASEDWARNINGS_H
14 #define LLVM_CLANG_SEMA_ANALYSISBASEDWARNINGS_H
15 
16 #include "clang/AST/Decl.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include <memory>
19 
20 namespace clang {
21 
22 class Decl;
23 class FunctionDecl;
24 class QualType;
25 class Sema;
26 namespace sema {
27   class FunctionScopeInfo;
28   class SemaPPCallbacks;
29 }
30 
31 namespace sema {
32 
33 class AnalysisBasedWarnings {
34 public:
35   class Policy {
36     friend class AnalysisBasedWarnings;
37     friend class SemaPPCallbacks;
38     // The warnings to run.
39     LLVM_PREFERRED_TYPE(bool)
40     unsigned enableCheckFallThrough : 1;
41     LLVM_PREFERRED_TYPE(bool)
42     unsigned enableCheckUnreachable : 1;
43     LLVM_PREFERRED_TYPE(bool)
44     unsigned enableThreadSafetyAnalysis : 1;
45     LLVM_PREFERRED_TYPE(bool)
46     unsigned enableConsumedAnalysis : 1;
47   public:
48     Policy();
disableCheckFallThrough()49     void disableCheckFallThrough() { enableCheckFallThrough = 0; }
50   };
51 
52 private:
53   Sema &S;
54 
55   class InterProceduralData;
56   std::unique_ptr<InterProceduralData> IPData;
57 
58   enum VisitFlag { NotVisited = 0, Visited = 1, Pending = 2 };
59   llvm::DenseMap<const FunctionDecl*, VisitFlag> VisitedFD;
60 
61   Policy PolicyOverrides;
62   void clearOverrides();
63 
64   /// \name Statistics
65   /// @{
66 
67   /// Number of function CFGs built and analyzed.
68   unsigned NumFunctionsAnalyzed;
69 
70   /// Number of functions for which the CFG could not be successfully
71   /// built.
72   unsigned NumFunctionsWithBadCFGs;
73 
74   /// Total number of blocks across all CFGs.
75   unsigned NumCFGBlocks;
76 
77   /// Largest number of CFG blocks for a single function analyzed.
78   unsigned MaxCFGBlocksPerFunction;
79 
80   /// Total number of CFGs with variables analyzed for uninitialized
81   /// uses.
82   unsigned NumUninitAnalysisFunctions;
83 
84   /// Total number of variables analyzed for uninitialized uses.
85   unsigned NumUninitAnalysisVariables;
86 
87   /// Max number of variables analyzed for uninitialized uses in a single
88   /// function.
89   unsigned MaxUninitAnalysisVariablesPerFunction;
90 
91   /// Total number of block visits during uninitialized use analysis.
92   unsigned NumUninitAnalysisBlockVisits;
93 
94   /// Max number of block visits during uninitialized use analysis of
95   /// a single function.
96   unsigned MaxUninitAnalysisBlockVisitsPerFunction;
97 
98   /// @}
99 
100 public:
101   AnalysisBasedWarnings(Sema &s);
102   ~AnalysisBasedWarnings();
103 
104   void IssueWarnings(Policy P, FunctionScopeInfo *fscope,
105                      const Decl *D, QualType BlockType);
106 
107   // Issue warnings that require whole-translation-unit analysis.
108   void IssueWarnings(TranslationUnitDecl *D);
109 
110   // Gets the default policy which is in effect at the given source location.
111   Policy getPolicyInEffectAt(SourceLocation Loc);
112 
113   // Get the policies we may want to override due to things like #pragma clang
114   // diagnostic handling. If a caller sets any of these policies to true, that
115   // will override the policy used to issue warnings.
getPolicyOverrides()116   Policy &getPolicyOverrides() { return PolicyOverrides; }
117 
118   void PrintStats() const;
119 };
120 
121 } // namespace sema
122 } // namespace clang
123 
124 #endif
125