xref: /freebsd/contrib/llvm-project/clang/include/clang/Sema/AnalysisBasedWarnings.h (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
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 }
29 
30 namespace sema {
31 
32 class AnalysisBasedWarnings {
33 public:
34   class Policy {
35     friend class AnalysisBasedWarnings;
36     // The warnings to run.
37     unsigned enableCheckFallThrough : 1;
38     unsigned enableCheckUnreachable : 1;
39     unsigned enableThreadSafetyAnalysis : 1;
40     unsigned enableConsumedAnalysis : 1;
41   public:
42     Policy();
43     void disableCheckFallThrough() { enableCheckFallThrough = 0; }
44   };
45 
46 private:
47   Sema &S;
48   Policy DefaultPolicy;
49 
50   class InterProceduralData;
51   std::unique_ptr<InterProceduralData> IPData;
52 
53   enum VisitFlag { NotVisited = 0, Visited = 1, Pending = 2 };
54   llvm::DenseMap<const FunctionDecl*, VisitFlag> VisitedFD;
55 
56   /// \name Statistics
57   /// @{
58 
59   /// Number of function CFGs built and analyzed.
60   unsigned NumFunctionsAnalyzed;
61 
62   /// Number of functions for which the CFG could not be successfully
63   /// built.
64   unsigned NumFunctionsWithBadCFGs;
65 
66   /// Total number of blocks across all CFGs.
67   unsigned NumCFGBlocks;
68 
69   /// Largest number of CFG blocks for a single function analyzed.
70   unsigned MaxCFGBlocksPerFunction;
71 
72   /// Total number of CFGs with variables analyzed for uninitialized
73   /// uses.
74   unsigned NumUninitAnalysisFunctions;
75 
76   /// Total number of variables analyzed for uninitialized uses.
77   unsigned NumUninitAnalysisVariables;
78 
79   /// Max number of variables analyzed for uninitialized uses in a single
80   /// function.
81   unsigned MaxUninitAnalysisVariablesPerFunction;
82 
83   /// Total number of block visits during uninitialized use analysis.
84   unsigned NumUninitAnalysisBlockVisits;
85 
86   /// Max number of block visits during uninitialized use analysis of
87   /// a single function.
88   unsigned MaxUninitAnalysisBlockVisitsPerFunction;
89 
90   /// @}
91 
92 public:
93   AnalysisBasedWarnings(Sema &s);
94   ~AnalysisBasedWarnings();
95 
96   void IssueWarnings(Policy P, FunctionScopeInfo *fscope,
97                      const Decl *D, QualType BlockType);
98 
99   // Issue warnings that require whole-translation-unit analysis.
100   void IssueWarnings(TranslationUnitDecl *D);
101 
102   Policy getDefaultPolicy() { return DefaultPolicy; }
103 
104   void PrintStats() const;
105 };
106 
107 } // namespace sema
108 } // namespace clang
109 
110 #endif
111