clang 20.0.0git
AnalyzerOptions.h
Go to the documentation of this file.
1//===- AnalyzerOptions.h - Analysis Engine Options --------------*- 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 header defines various options for the static analyzer that are set
10// by the frontend and are consulted throughout the analyzer.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_STATICANALYZER_CORE_ANALYZEROPTIONS_H
15#define LLVM_CLANG_STATICANALYZER_CORE_ANALYZEROPTIONS_H
16
18#include "clang/Basic/LLVM.h"
19#include "llvm/ADT/IntrusiveRefCntPtr.h"
20#include "llvm/ADT/StringMap.h"
21#include "llvm/ADT/StringRef.h"
22#include <string>
23#include <utility>
24#include <vector>
25
26namespace clang {
27
28namespace ento {
29
30class CheckerBase;
31
32} // namespace ento
33
34/// AnalysisConstraints - Set of available constraint models.
36#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN) NAME##Model,
37#include "clang/StaticAnalyzer/Core/Analyses.def"
39};
40
41/// AnalysisDiagClients - Set of available diagnostic clients for rendering
42/// analysis results.
44#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN) PD_##NAME,
45#include "clang/StaticAnalyzer/Core/Analyses.def"
48};
49
50/// AnalysisPurgeModes - Set of available strategies for dead symbol removal.
52#define ANALYSIS_PURGE(NAME, CMDFLAG, DESC) NAME,
53#include "clang/StaticAnalyzer/Core/Analyses.def"
55};
56
57/// AnalysisInlineFunctionSelection - Set of inlining function selection heuristics.
59#define ANALYSIS_INLINING_MODE(NAME, CMDFLAG, DESC) NAME,
60#include "clang/StaticAnalyzer/Core/Analyses.def"
62};
63
64/// Describes the different kinds of C++ member functions which can be
65/// considered for inlining by the analyzer.
66///
67/// These options are cumulative; enabling one kind of member function will
68/// enable all kinds with lower enum values.
70 // Uninitialized = 0,
71
72 /// A dummy mode in which no C++ inlining is enabled.
74
75 /// Refers to regular member function and operator calls.
77
78 /// Refers to constructors (implicit or explicit).
79 ///
80 /// Note that a constructor will not be inlined if the corresponding
81 /// destructor is non-trivial.
83
84 /// Refers to destructors (implicit or explicit).
86};
87
88/// Describes the different modes of inter-procedural analysis.
89enum IPAKind {
90 /// Perform only intra-procedural analysis.
92
93 /// Inline C functions and blocks when their definitions are available.
95
96 /// Inline callees(C, C++, ObjC) when their definitions are available.
98
99 /// Enable inlining of dynamically dispatched methods.
101
102 /// Enable inlining of dynamically dispatched methods, bifurcate paths when
103 /// exact type info is unavailable.
106
108 DFS,
109 BFS,
114};
115
116/// Describes the kinds for high-level analyzer mode.
118 /// Perform shallow but fast analyzes.
120
121 /// Perform deep analyzes.
122 UMK_Deep = 2
124
126
127/// Stores options for the analyzer from the command line.
128///
129/// Some options are frontend flags (e.g.: -analyzer-output), but some are
130/// analyzer configuration options, which are preceded by -analyzer-config
131/// (e.g.: -analyzer-config notes-as-events=true).
132///
133/// If you'd like to add a new frontend flag, add it to
134/// include/clang/Driver/CC1Options.td, add a new field to store the value of
135/// that flag in this class, and initialize it in
136/// lib/Frontend/CompilerInvocation.cpp.
137///
138/// If you'd like to add a new non-checker configuration, register it in
139/// include/clang/StaticAnalyzer/Core/AnalyzerOptions.def, and refer to the
140/// top of the file for documentation.
141///
142/// If you'd like to add a new checker option, call getChecker*Option()
143/// whenever.
144///
145/// Some of the options are controlled by raw frontend flags for no good reason,
146/// and should be eventually converted into -analyzer-config flags. New analyzer
147/// options should not be implemented as frontend flags. Frontend flags still
148/// make sense for things that do not affect the actual analysis.
149class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
150public:
151 using ConfigTable = llvm::StringMap<std::string>;
152
153 /// Retrieves the list of checkers generated from Checkers.td. This doesn't
154 /// contain statically linked but non-generated checkers and plugin checkers!
155 static std::vector<StringRef>
156 getRegisteredCheckers(bool IncludeExperimental = false);
157
158 /// Retrieves the list of packages generated from Checkers.td. This doesn't
159 /// contain statically linked but non-generated packages and plugin packages!
160 static std::vector<StringRef>
161 getRegisteredPackages(bool IncludeExperimental = false);
162
163 /// Convenience function for printing options or checkers and their
164 /// description in a formatted manner. If \p MinLineWidth is set to 0, no line
165 /// breaks are introduced for the description.
166 ///
167 /// Format, depending whether the option name's length is less than
168 /// \p EntryWidth:
169 ///
170 /// <padding>EntryName<padding>Description
171 /// <---------padding--------->Description
172 /// <---------padding--------->Description
173 ///
174 /// <padding>VeryVeryLongEntryName
175 /// <---------padding--------->Description
176 /// <---------padding--------->Description
177 /// ^~~~~~~~~InitialPad
178 /// ^~~~~~~~~~~~~~~~~~EntryWidth
179 /// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MinLineWidth
180 static void printFormattedEntry(llvm::raw_ostream &Out,
181 std::pair<StringRef, StringRef> EntryDescPair,
182 size_t InitialPad, size_t EntryWidth,
183 size_t MinLineWidth = 0);
184
185 /// Pairs of checker/package name and enable/disable.
186 std::vector<std::pair<std::string, bool>> CheckersAndPackages;
187
188 /// Vector of checker/package names which will not emit warnings.
189 std::vector<std::string> SilencedCheckersAndPackages;
190
191 /// A key-value table of use-specified configuration values.
192 // TODO: This shouldn't be public.
197
199
200 /// File path to which the exploded graph should be dumped.
202
203 /// Store full compiler invocation for reproducible instructions in the
204 /// generated report.
206
207 /// The maximum number of times the analyzer visits a block.
209
210 /// Disable all analyzer checkers.
211 ///
212 /// This flag allows one to disable analyzer checkers on the code processed by
213 /// the given analysis consumer. Note, the code will get parsed and the
214 /// command-line options will get checked.
215 unsigned DisableAllCheckers : 1;
216
217 unsigned ShowCheckerHelp : 1;
220
224
228 unsigned AnalyzeAll : 1;
231
232 unsigned TrimGraph : 1;
234 unsigned UnoptimizedCFG : 1;
235 unsigned PrintStats : 1;
236
237 /// Do not re-analyze paths leading to exhausted nodes with a different
238 /// strategy. We get better code coverage when retry is enabled.
239 unsigned NoRetryExhausted : 1;
240
241 /// Emit analyzer warnings as errors.
243
244 /// The inlining stack depth limit.
246
247 /// The mode of function selection used during inlining.
249
250 // Create a field for each -analyzer-config option.
251#define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
252 SHALLOW_VAL, DEEP_VAL) \
253 ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, SHALLOW_VAL)
254
255#define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \
256 TYPE NAME;
257
258#include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
259#undef ANALYZER_OPTION
260#undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
261
262 bool isUnknownAnalyzerConfig(llvm::StringRef Name) {
263 static std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = []() {
264 // Create an array of all -analyzer-config command line options.
265 std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = {
266#define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
267 SHALLOW_VAL, DEEP_VAL) \
268 ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, SHALLOW_VAL)
269
270#define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \
271 llvm::StringLiteral(CMDFLAG),
272
273#include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
274#undef ANALYZER_OPTION
275#undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
276 };
277 // FIXME: Sort this at compile-time when we get constexpr sort (C++20).
278 llvm::sort(AnalyzerConfigCmdFlags);
279 return AnalyzerConfigCmdFlags;
280 }();
281
282 return !std::binary_search(AnalyzerConfigCmdFlags.begin(),
283 AnalyzerConfigCmdFlags.end(), Name);
284 }
285
297
298 /// Interprets an option's string value as a boolean. The "true" string is
299 /// interpreted as true and the "false" string is interpreted as false.
300 ///
301 /// If an option value is not provided, returns the given \p DefaultVal.
302 /// @param [in] CheckerName The *full name* of the checker. One may retrieve
303 /// this from the checker object's field \c Name, or through \c
304 /// CheckerManager::getCurrentCheckerName within the checker's registry
305 /// function.
306 /// Checker options are retrieved in the following format:
307 /// `-analyzer-config CheckerName:OptionName=Value.
308 /// @param [in] OptionName Name for option to retrieve.
309 /// @param [in] SearchInParents If set to true and the searched option was not
310 /// specified for the given checker the options for the parent packages will
311 /// be searched as well. The inner packages take precedence over the outer
312 /// ones.
313 bool getCheckerBooleanOption(StringRef CheckerName, StringRef OptionName,
314 bool SearchInParents = false) const;
315
316 bool getCheckerBooleanOption(const ento::CheckerBase *C, StringRef OptionName,
317 bool SearchInParents = false) const;
318
319 /// Interprets an option's string value as an integer value.
320 ///
321 /// If an option value is not provided, returns the given \p DefaultVal.
322 /// @param [in] CheckerName The *full name* of the checker. One may retrieve
323 /// this from the checker object's field \c Name, or through \c
324 /// CheckerManager::getCurrentCheckerName within the checker's registry
325 /// function.
326 /// Checker options are retrieved in the following format:
327 /// `-analyzer-config CheckerName:OptionName=Value.
328 /// @param [in] OptionName Name for option to retrieve.
329 /// @param [in] SearchInParents If set to true and the searched option was not
330 /// specified for the given checker the options for the parent packages will
331 /// be searched as well. The inner packages take precedence over the outer
332 /// ones.
333 int getCheckerIntegerOption(StringRef CheckerName, StringRef OptionName,
334 bool SearchInParents = false) const;
335
336 int getCheckerIntegerOption(const ento::CheckerBase *C, StringRef OptionName,
337 bool SearchInParents = false) const;
338
339 /// Query an option's string value.
340 ///
341 /// If an option value is not provided, returns the given \p DefaultVal.
342 /// @param [in] CheckerName The *full name* of the checker. One may retrieve
343 /// this from the checker object's field \c Name, or through \c
344 /// CheckerManager::getCurrentCheckerName within the checker's registry
345 /// function.
346 /// Checker options are retrieved in the following format:
347 /// `-analyzer-config CheckerName:OptionName=Value.
348 /// @param [in] OptionName Name for option to retrieve.
349 /// @param [in] SearchInParents If set to true and the searched option was not
350 /// specified for the given checker the options for the parent packages will
351 /// be searched as well. The inner packages take precedence over the outer
352 /// ones.
353 StringRef getCheckerStringOption(StringRef CheckerName, StringRef OptionName,
354 bool SearchInParents = false) const;
355
357 StringRef OptionName,
358 bool SearchInParents = false) const;
359
362
363 /// Returns the inter-procedural analysis mode.
364 IPAKind getIPAMode() const;
365
366 /// Returns the option controlling which C++ member functions will be
367 /// considered for inlining.
368 ///
369 /// This is controlled by the 'c++-inlining' config option.
370 ///
371 /// \sa CXXMemberInliningMode
373
376 ShouldDisplayMacroExpansions,
377 ShouldSerializeStats,
378 // The stable report filename option is deprecated because
379 // file names are now always stable. Now the old option acts as
380 // an alias to the new verbose filename option because this
381 // closely mimics the behavior under the old option.
382 ShouldWriteStableReportFilename || ShouldWriteVerboseReportFilename,
384 ShouldApplyFixIts,
385 ShouldDisplayCheckerNameForText};
386 }
387};
388
390
391//===----------------------------------------------------------------------===//
392// We'll use AnalyzerOptions in the frontend, but we can't link the frontend
393// with clangStaticAnalyzerCore, because clangStaticAnalyzerCore depends on
394// clangFrontend.
395//
396// For this reason, implement some methods in this header file.
397//===----------------------------------------------------------------------===//
398
399inline std::vector<StringRef>
400AnalyzerOptions::getRegisteredCheckers(bool IncludeExperimental) {
401 static constexpr llvm::StringLiteral StaticAnalyzerCheckerNames[] = {
402#define GET_CHECKERS
403#define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \
404 llvm::StringLiteral(FULLNAME),
405#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
406#undef CHECKER
407#undef GET_CHECKERS
408 };
409 std::vector<StringRef> Checkers;
410 for (StringRef CheckerName : StaticAnalyzerCheckerNames) {
411 if (!CheckerName.starts_with("debug.") &&
412 (IncludeExperimental || !CheckerName.starts_with("alpha.")))
413 Checkers.push_back(CheckerName);
414 }
415 return Checkers;
416}
417
418inline std::vector<StringRef>
419AnalyzerOptions::getRegisteredPackages(bool IncludeExperimental) {
420 static constexpr llvm::StringLiteral StaticAnalyzerPackageNames[] = {
421#define GET_PACKAGES
422#define PACKAGE(FULLNAME) llvm::StringLiteral(FULLNAME),
423#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
424#undef PACKAGE
425#undef GET_PACKAGES
426 };
427 std::vector<StringRef> Packages;
428 for (StringRef PackageName : StaticAnalyzerPackageNames) {
429 if (PackageName != "debug" &&
430 (IncludeExperimental || PackageName != "alpha"))
431 Packages.push_back(PackageName);
432 }
433 return Packages;
434}
435
436} // namespace clang
437
438#endif // LLVM_CLANG_STATICANALYZER_CORE_ANALYZEROPTIONS_H
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Stores options for the analyzer from the command line.
static std::vector< StringRef > getRegisteredPackages(bool IncludeExperimental=false)
Retrieves the list of packages generated from Checkers.td.
std::vector< std::pair< std::string, bool > > CheckersAndPackages
Pairs of checker/package name and enable/disable.
unsigned ShowCheckerOptionDeveloperList
unsigned DisableAllCheckers
Disable all analyzer checkers.
std::vector< std::string > SilencedCheckersAndPackages
Vector of checker/package names which will not emit warnings.
unsigned NoRetryExhausted
Do not re-analyze paths leading to exhausted nodes with a different strategy.
AnalysisDiagClients AnalysisDiagOpt
bool mayInlineCXXMemberFunction(CXXInlineableMemberKind K) const
Returns the option controlling which C++ member functions will be considered for inlining.
AnalysisConstraints AnalysisConstraintsOpt
unsigned visualizeExplodedGraphWithGraphViz
bool AnalyzerWerror
Emit analyzer warnings as errors.
unsigned AnalyzerNoteAnalysisEntryPoints
ConfigTable Config
A key-value table of use-specified configuration values.
bool getCheckerBooleanOption(StringRef CheckerName, StringRef OptionName, bool SearchInParents=false) const
Interprets an option's string value as a boolean.
unsigned maxBlockVisitOnPath
The maximum number of times the analyzer visits a block.
std::string AnalyzeSpecificFunction
IPAKind getIPAMode() const
Returns the inter-procedural analysis mode.
std::string DumpExplodedGraphTo
File path to which the exploded graph should be dumped.
unsigned ShouldEmitErrorsOnInvalidConfigValue
ento::PathDiagnosticConsumerOptions getDiagOpts() const
CTUPhase1InliningKind getCTUPhase1Inlining() const
AnalysisPurgeMode AnalysisPurgeOpt
bool isUnknownAnalyzerConfig(llvm::StringRef Name)
static std::vector< StringRef > getRegisteredCheckers(bool IncludeExperimental=false)
Retrieves the list of checkers generated from Checkers.td.
unsigned InlineMaxStackDepth
The inlining stack depth limit.
llvm::StringMap< std::string > ConfigTable
int getCheckerIntegerOption(StringRef CheckerName, StringRef OptionName, bool SearchInParents=false) const
Interprets an option's string value as an integer value.
static void printFormattedEntry(llvm::raw_ostream &Out, std::pair< StringRef, StringRef > EntryDescPair, size_t InitialPad, size_t EntryWidth, size_t MinLineWidth=0)
Convenience function for printing options or checkers and their description in a formatted manner.
ExplorationStrategyKind getExplorationStrategy() const
StringRef getCheckerStringOption(StringRef CheckerName, StringRef OptionName, bool SearchInParents=false) const
Query an option's string value.
std::string FullCompilerInvocation
Store full compiler invocation for reproducible instructions in the generated report.
AnalysisInliningMode InliningMode
The mode of function selection used during inlining.
The JSON file list parser is used to communicate input to InstallAPI.
UserModeKind
Describes the kinds for high-level analyzer mode.
@ UMK_Deep
Perform deep analyzes.
@ UMK_Shallow
Perform shallow but fast analyzes.
IPAKind
Describes the different modes of inter-procedural analysis.
@ IPAK_Inlining
Inline callees(C, C++, ObjC) when their definitions are available.
@ IPAK_BasicInlining
Inline C functions and blocks when their definitions are available.
@ IPAK_None
Perform only intra-procedural analysis.
@ IPAK_DynamicDispatch
Enable inlining of dynamically dispatched methods.
@ IPAK_DynamicDispatchBifurcate
Enable inlining of dynamically dispatched methods, bifurcate paths when exact type info is unavailabl...
AnalysisConstraints
AnalysisConstraints - Set of available constraint models.
@ NumConstraints
CXXInlineableMemberKind
Describes the different kinds of C++ member functions which can be considered for inlining by the ana...
@ CIMK_Destructors
Refers to destructors (implicit or explicit).
@ CIMK_MemberFunctions
Refers to regular member function and operator calls.
@ CIMK_Constructors
Refers to constructors (implicit or explicit).
@ CIMK_None
A dummy mode in which no C++ inlining is enabled.
AnalysisPurgeMode
AnalysisPurgeModes - Set of available strategies for dead symbol removal.
@ NumPurgeModes
CTUPhase1InliningKind
AnalysisDiagClients
AnalysisDiagClients - Set of available diagnostic clients for rendering analysis results.
@ NUM_ANALYSIS_DIAG_CLIENTS
AnalysisInliningMode
AnalysisInlineFunctionSelection - Set of inlining function selection heuristics.
@ NumInliningModes
@ None
The alignment was not explicit in code.
ExplorationStrategyKind
#define false
Definition: stdbool.h:26
These options tweak the behavior of path diangostic consumers.