1 //===- AnalyzerOptions.h - Analysis Engine Options --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header defines various options for the static analyzer that are set
11 // by the frontend and are consulted throughout the analyzer.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_STATICANALYZER_CORE_ANALYZEROPTIONS_H
16 #define LLVM_CLANG_STATICANALYZER_CORE_ANALYZEROPTIONS_H
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/IntrusiveRefCntPtr.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ADT/StringSwitch.h"
36 /// Analysis - Set of available source code analyses.
38 #define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE) NAME,
39 #include "clang/StaticAnalyzer/Core/Analyses.def"
43 /// AnalysisStores - Set of available analysis store models.
45 #define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN) NAME##Model,
46 #include "clang/StaticAnalyzer/Core/Analyses.def"
50 /// AnalysisConstraints - Set of available constraint models.
51 enum AnalysisConstraints {
52 #define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN) NAME##Model,
53 #include "clang/StaticAnalyzer/Core/Analyses.def"
57 /// AnalysisDiagClients - Set of available diagnostic clients for rendering
59 enum AnalysisDiagClients {
60 #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN) PD_##NAME,
61 #include "clang/StaticAnalyzer/Core/Analyses.def"
63 NUM_ANALYSIS_DIAG_CLIENTS
66 /// AnalysisPurgeModes - Set of available strategies for dead symbol removal.
67 enum AnalysisPurgeMode {
68 #define ANALYSIS_PURGE(NAME, CMDFLAG, DESC) NAME,
69 #include "clang/StaticAnalyzer/Core/Analyses.def"
73 /// AnalysisInlineFunctionSelection - Set of inlining function selection heuristics.
74 enum AnalysisInliningMode {
75 #define ANALYSIS_INLINING_MODE(NAME, CMDFLAG, DESC) NAME,
76 #include "clang/StaticAnalyzer/Core/Analyses.def"
80 /// Describes the different kinds of C++ member functions which can be
81 /// considered for inlining by the analyzer.
83 /// These options are cumulative; enabling one kind of member function will
84 /// enable all kinds with lower enum values.
85 enum CXXInlineableMemberKind {
88 /// A dummy mode in which no C++ inlining is enabled.
91 /// Refers to regular member function and operator calls.
94 /// Refers to constructors (implicit or explicit).
96 /// Note that a constructor will not be inlined if the corresponding
97 /// destructor is non-trivial.
100 /// Refers to destructors (implicit or explicit).
104 /// Describes the different modes of inter-procedural analysis.
106 /// Perform only intra-procedural analysis.
109 /// Inline C functions and blocks when their definitions are available.
110 IPAK_BasicInlining = 2,
112 /// Inline callees(C, C++, ObjC) when their definitions are available.
115 /// Enable inlining of dynamically dispatched methods.
116 IPAK_DynamicDispatch = 4,
118 /// Enable inlining of dynamically dispatched methods, bifurcate paths when
119 /// exact type info is unavailable.
120 IPAK_DynamicDispatchBifurcate = 5
123 enum class ExplorationStrategyKind {
127 UnexploredFirstQueue,
128 UnexploredFirstLocationQueue,
132 /// Describes the kinds for high-level analyzer mode.
134 /// Perform shallow but fast analyzes.
137 /// Perform deep analyzes.
141 /// Stores options for the analyzer from the command line.
143 /// Some options are frontend flags (e.g.: -analyzer-output), but some are
144 /// analyzer configuration options, which are preceded by -analyzer-config
145 /// (e.g.: -analyzer-config notes-as-events=true).
147 /// If you'd like to add a new frontend flag, add it to
148 /// include/clang/Driver/CC1Options.td, add a new field to store the value of
149 /// that flag in this class, and initialize it in
150 /// lib/Frontend/CompilerInvocation.cpp.
152 /// If you'd like to add a new non-checker configuration, register it in
153 /// include/clang/StaticAnalyzer/Core/AnalyzerOptions.def, and refer to the
154 /// top of the file for documentation.
156 /// If you'd like to add a new checker option, call getChecker*Option()
159 /// Some of the options are controlled by raw frontend flags for no good reason,
160 /// and should be eventually converted into -analyzer-config flags. New analyzer
161 /// options should not be implemented as frontend flags. Frontend flags still
162 /// make sense for things that do not affect the actual analysis.
163 class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
165 using ConfigTable = llvm::StringMap<std::string>;
167 static std::vector<StringRef>
168 getRegisteredCheckers(bool IncludeExperimental = false);
170 /// Pair of checker name and enable/disable.
171 std::vector<std::pair<std::string, bool>> CheckersControlList;
173 /// A key-value table of use-specified configuration values.
174 // TODO: This shouldn't be public.
176 AnalysisStores AnalysisStoreOpt = RegionStoreModel;
177 AnalysisConstraints AnalysisConstraintsOpt = RangeConstraintsModel;
178 AnalysisDiagClients AnalysisDiagOpt = PD_HTML;
179 AnalysisPurgeMode AnalysisPurgeOpt = PurgeStmt;
181 std::string AnalyzeSpecificFunction;
183 /// File path to which the exploded graph should be dumped.
184 std::string DumpExplodedGraphTo;
186 /// Store full compiler invocation for reproducible instructions in the
187 /// generated report.
188 std::string FullCompilerInvocation;
190 /// The maximum number of times the analyzer visits a block.
191 unsigned maxBlockVisitOnPath;
193 /// Disable all analyzer checks.
195 /// This flag allows one to disable analyzer checks on the code processed by
196 /// the given analysis consumer. Note, the code will get parsed and the
197 /// command-line options will get checked.
198 unsigned DisableAllChecks : 1;
200 unsigned ShowCheckerHelp : 1;
201 unsigned ShowEnabledCheckerList : 1;
202 unsigned ShowConfigOptionsList : 1;
203 unsigned ShouldEmitErrorsOnInvalidConfigValue : 1;
204 unsigned AnalyzeAll : 1;
205 unsigned AnalyzerDisplayProgress : 1;
206 unsigned AnalyzeNestedBlocks : 1;
208 unsigned eagerlyAssumeBinOpBifurcation : 1;
210 unsigned TrimGraph : 1;
211 unsigned visualizeExplodedGraphWithGraphViz : 1;
212 unsigned UnoptimizedCFG : 1;
213 unsigned PrintStats : 1;
215 /// Do not re-analyze paths leading to exhausted nodes with a different
216 /// strategy. We get better code coverage when retry is enabled.
217 unsigned NoRetryExhausted : 1;
219 /// The inlining stack depth limit.
220 // Cap the stack depth at 4 calls (5 stack frames, base + 4 calls).
221 unsigned InlineMaxStackDepth = 5;
223 /// The mode of function selection used during inlining.
224 AnalysisInliningMode InliningMode = NoRedundancy;
226 // Create a field for each -analyzer-config option.
227 #define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
228 SHALLOW_VAL, DEEP_VAL) \
229 ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, SHALLOW_VAL)
231 #define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \
234 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
235 #undef ANALYZER_OPTION
236 #undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
238 // Create an array of all -analyzer-config command line options. Sort it in
240 std::vector<StringRef> AnalyzerConfigCmdFlags = {
241 #define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
242 SHALLOW_VAL, DEEP_VAL) \
243 ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, SHALLOW_VAL)
245 #define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \
248 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
249 #undef ANALYZER_OPTION
250 #undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
253 bool isUnknownAnalyzerConfig(StringRef Name) const {
255 assert(std::is_sorted(AnalyzerConfigCmdFlags.begin(),
256 AnalyzerConfigCmdFlags.end()));
258 return !std::binary_search(AnalyzerConfigCmdFlags.begin(),
259 AnalyzerConfigCmdFlags.end(), Name);
263 : DisableAllChecks(false), ShowCheckerHelp(false),
264 ShowEnabledCheckerList(false), ShowConfigOptionsList(false),
265 AnalyzeAll(false), AnalyzerDisplayProgress(false),
266 AnalyzeNestedBlocks(false), eagerlyAssumeBinOpBifurcation(false),
267 TrimGraph(false), visualizeExplodedGraphWithGraphViz(false),
268 UnoptimizedCFG(false), PrintStats(false), NoRetryExhausted(false) {
269 llvm::sort(AnalyzerConfigCmdFlags);
272 /// Interprets an option's string value as a boolean. The "true" string is
273 /// interpreted as true and the "false" string is interpreted as false.
275 /// If an option value is not provided, returns the given \p DefaultVal.
276 /// @param [in] Name Name for option to retrieve.
277 /// @param [in] DefaultVal Default value returned if no such option was
279 /// @param [in] C The checker object the option belongs to. Checker options
280 /// are retrieved in the following format:
281 /// `-analyzer-config <package and checker name>:OptionName=Value.
282 /// @param [in] SearchInParents If set to true and the searched option was not
283 /// specified for the given checker the options for the parent packages will
284 /// be searched as well. The inner packages take precedence over the outer
286 bool getCheckerBooleanOption(StringRef Name, bool DefaultVal,
287 const ento::CheckerBase *C,
288 bool SearchInParents = false) const;
291 /// Interprets an option's string value as an integer value.
293 /// If an option value is not provided, returns the given \p DefaultVal.
294 /// @param [in] Name Name for option to retrieve.
295 /// @param [in] DefaultVal Default value returned if no such option was
297 /// @param [in] C The checker object the option belongs to. Checker options
298 /// are retrieved in the following format:
299 /// `-analyzer-config <package and checker name>:OptionName=Value.
300 /// @param [in] SearchInParents If set to true and the searched option was not
301 /// specified for the given checker the options for the parent packages will
302 /// be searched as well. The inner packages take precedence over the outer
304 int getCheckerIntegerOption(StringRef Name, int DefaultVal,
305 const ento::CheckerBase *C,
306 bool SearchInParents = false) const;
308 /// Query an option's string value.
310 /// If an option value is not provided, returns the given \p DefaultVal.
311 /// @param [in] Name Name for option to retrieve.
312 /// @param [in] DefaultVal Default value returned if no such option was
314 /// @param [in] C The checker object the option belongs to. Checker options
315 /// are retrieved in the following format:
316 /// `-analyzer-config <package and checker name>:OptionName=Value.
317 /// @param [in] SearchInParents If set to true and the searched option was not
318 /// specified for the given checker the options for the parent packages will
319 /// be searched as well. The inner packages take precedence over the outer
321 StringRef getCheckerStringOption(StringRef Name, StringRef DefaultVal,
322 const ento::CheckerBase *C,
323 bool SearchInParents = false) const;
325 /// Retrieves and sets the UserMode. This is a high-level option,
326 /// which is used to set other low-level options. It is not accessible
327 /// outside of AnalyzerOptions.
328 UserModeKind getUserMode() const;
330 ExplorationStrategyKind getExplorationStrategy() const;
332 /// Returns the inter-procedural analysis mode.
333 IPAKind getIPAMode() const;
335 /// Returns the option controlling which C++ member functions will be
336 /// considered for inlining.
338 /// This is controlled by the 'c++-inlining' config option.
340 /// \sa CXXMemberInliningMode
341 bool mayInlineCXXMemberFunction(CXXInlineableMemberKind K) const;
344 using AnalyzerOptionsRef = IntrusiveRefCntPtr<AnalyzerOptions>;
346 //===----------------------------------------------------------------------===//
347 // We'll use AnalyzerOptions in the frontend, but we can't link the frontend
348 // with clangStaticAnalyzerCore, because clangStaticAnalyzerCore depends on
351 // For this reason, implement some methods in this header file.
352 //===----------------------------------------------------------------------===//
354 inline UserModeKind AnalyzerOptions::getUserMode() const {
355 auto K = llvm::StringSwitch<llvm::Optional<UserModeKind>>(UserMode)
356 .Case("shallow", UMK_Shallow)
357 .Case("deep", UMK_Deep)
359 assert(K.hasValue() && "User mode is invalid.");
365 #endif // LLVM_CLANG_STATICANALYZER_CORE_ANALYZEROPTIONS_H