clang 24.0.0git
AnalysisBasedWarnings.h
Go to the documentation of this file.
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"
19#include <memory>
20
21namespace clang {
22
24class Decl;
25class FunctionDecl;
26class QualType;
27class Sema;
28class VarDecl;
29namespace sema {
31 class SemaPPCallbacks;
32}
33
34namespace sema {
35
37public:
38 class Policy {
40 friend class SemaPPCallbacks;
41 // The warnings to run.
42 LLVM_PREFERRED_TYPE(bool)
43 unsigned enableCheckFallThrough : 1;
44 LLVM_PREFERRED_TYPE(bool)
45 unsigned enableCheckUnreachable : 1;
46 LLVM_PREFERRED_TYPE(bool)
47 unsigned enableThreadSafetyAnalysis : 1;
48 LLVM_PREFERRED_TYPE(bool)
49 unsigned enableConsumedAnalysis : 1;
50 public:
51 Policy();
52 void disableCheckFallThrough() { enableCheckFallThrough = 0; }
53 };
54
55private:
56 Sema &S;
57
58 class InterProceduralData;
59 std::unique_ptr<InterProceduralData> IPData;
60
61 enum VisitFlag { NotVisited = 0, Visited = 1, Pending = 2 };
62 llvm::DenseMap<const FunctionDecl*, VisitFlag> VisitedFD;
63 std::multimap<VarDecl *, PossiblyUnreachableDiag>
64 VarDeclPossiblyUnreachableDiags;
65
66 Policy PolicyOverrides;
67 void clearOverrides();
68
69 /// Caches results for getPolicyInEffectAt().
70 /// Flushed whenever a diagnostic pragma changes severities.
71 llvm::DenseMap<const void *, Policy>
72 PolicyCache[static_cast<unsigned>(DiagStateSystemClass::NUM_CLASSES)];
73
74 /// \name Statistics
75 /// @{
76
77 /// Number of function CFGs built and analyzed.
78 unsigned NumFunctionsAnalyzed;
79
80 /// Number of functions for which the CFG could not be successfully
81 /// built.
82 unsigned NumFunctionsWithBadCFGs;
83
84 /// Total number of blocks across all CFGs.
85 unsigned NumCFGBlocks;
86
87 /// Largest number of CFG blocks for a single function analyzed.
88 unsigned MaxCFGBlocksPerFunction;
89
90 /// Total number of CFGs with variables analyzed for uninitialized
91 /// uses.
92 unsigned NumUninitAnalysisFunctions;
93
94 /// Total number of variables analyzed for uninitialized uses.
95 unsigned NumUninitAnalysisVariables;
96
97 /// Max number of variables analyzed for uninitialized uses in a single
98 /// function.
99 unsigned MaxUninitAnalysisVariablesPerFunction;
100
101 /// Total number of block visits during uninitialized use analysis.
102 unsigned NumUninitAnalysisBlockVisits;
103
104 /// Max number of block visits during uninitialized use analysis of
105 /// a single function.
106 unsigned MaxUninitAnalysisBlockVisitsPerFunction;
107
108 /// Statistics collected during lifetime safety analysis.
109 /// These are accumulated across all analyzed functions and printed
110 /// when -print-stats is enabled.
112
113 /// @}
114
115public:
118
119 void IssueWarnings(Policy P, FunctionScopeInfo *fscope,
120 const Decl *D, QualType BlockType);
121
122 // Issue warnings that require whole-translation-unit analysis.
124
125 // Run analysis-based warnings on an implicitly-defined function body (e.g. a
126 // defaulted/implicit default constructor). Such functions never reach the
127 // normal IssueWarnings path.
129
131
133
134 // Gets the default policy which is in effect at the given source location.
136
137 // Get the policies we may want to override due to things like #pragma clang
138 // diagnostic handling. If a caller sets any of these policies to true, that
139 // will override the policy used to issue warnings.
140 Policy &getPolicyOverrides() { return PolicyOverrides; }
141
142 /// Drop cached getPolicyInEffectAt() results (diagnostic state changed).
143 void clearPolicyCache();
144
145 void PrintStats() const;
146};
147
148} // namespace sema
149} // namespace clang
150
151#endif
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
Represents a function declaration or definition.
Definition Decl.h:2059
A (possibly-)qualified type.
Definition TypeBase.h:938
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:863
Encodes a location in the source.
The top declaration context.
Definition Decl.h:106
Represents a variable declaration or definition.
Definition Decl.h:933
void clearPolicyCache()
Drop cached getPolicyInEffectAt() results (diagnostic state changed).
void IssueWarnings(Policy P, FunctionScopeInfo *fscope, const Decl *D, QualType BlockType)
void registerVarDeclWarning(VarDecl *VD, PossiblyUnreachableDiag PUD)
Policy getPolicyInEffectAt(SourceLocation Loc)
Retains information about a function, method, or block that is currently being parsed.
Definition ScopeInfo.h:104
Top level wrappers for InstallAPI frontend operations.
A structure to hold the statistics related to LifetimeAnalysis.