clang 22.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 "llvm/Support/Compiler.h"
23#include <string>
24#include <utility>
25#include <vector>
26
27namespace clang {
28
29namespace ento {
30
31class CheckerBase;
32
33} // namespace ento
34
35/// AnalysisConstraints - Set of available constraint models.
37#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN) NAME##Model,
38#include "clang/StaticAnalyzer/Core/Analyses.def"
40};
41
42/// AnalysisDiagClients - Set of available diagnostic clients for rendering
43/// analysis results.
45#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN) PD_##NAME,
46#include "clang/StaticAnalyzer/Core/Analyses.def"
49};
50
51/// AnalysisPurgeModes - Set of available strategies for dead symbol removal.
53#define ANALYSIS_PURGE(NAME, CMDFLAG, DESC) NAME,
54#include "clang/StaticAnalyzer/Core/Analyses.def"
56};
57
58/// AnalysisInlineFunctionSelection - Set of inlining function selection heuristics.
60#define ANALYSIS_INLINING_MODE(NAME, CMDFLAG, DESC) NAME,
61#include "clang/StaticAnalyzer/Core/Analyses.def"
63};
64
65/// Describes the different kinds of C++ member functions which can be
66/// considered for inlining by the analyzer.
67///
68/// These options are cumulative; enabling one kind of member function will
69/// enable all kinds with lower enum values.
71 // Uninitialized = 0,
72
73 /// A dummy mode in which no C++ inlining is enabled.
75
76 /// Refers to regular member function and operator calls.
78
79 /// Refers to constructors (implicit or explicit).
80 ///
81 /// Note that a constructor will not be inlined if the corresponding
82 /// destructor is non-trivial.
84
85 /// Refers to destructors (implicit or explicit).
87};
88
89/// Describes the different modes of inter-procedural analysis.
90enum IPAKind {
91 /// Perform only intra-procedural analysis.
93
94 /// Inline C functions and blocks when their definitions are available.
96
97 /// Inline callees(C, C++, ObjC) when their definitions are available.
99
100 /// Enable inlining of dynamically dispatched methods.
102
103 /// Enable inlining of dynamically dispatched methods, bifurcate paths when
104 /// exact type info is unavailable.
107
109 DFS,
110 BFS,
115};
116
117/// Describes the kinds for high-level analyzer mode.
119 /// Perform shallow but fast analyzes.
121
122 /// Perform deep analyzes.
123 UMK_Deep = 2
125
127
129public:
130 constexpr PositiveAnalyzerOption() = default;
131 constexpr PositiveAnalyzerOption(unsigned Value) : Value(Value) {
132 assert(Value > 0 && "only positive values are accepted");
133 }
134 constexpr PositiveAnalyzerOption(const PositiveAnalyzerOption &) = default;
135 constexpr PositiveAnalyzerOption &
137 Value = Other.Value;
138 return *this;
139 }
140
141 static constexpr std::optional<PositiveAnalyzerOption> create(unsigned Val) {
142 if (Val == 0)
143 return std::nullopt;
144 return PositiveAnalyzerOption{Val};
145 }
146 static std::optional<PositiveAnalyzerOption> create(StringRef Str) {
147 unsigned Parsed = 0;
148 if (Str.getAsInteger(0, Parsed))
149 return std::nullopt;
150 return PositiveAnalyzerOption::create(Parsed);
151 }
152 constexpr operator unsigned() const { return Value; }
153
154private:
155 unsigned Value = 1;
156};
157
158/// Stores options for the analyzer from the command line.
159///
160/// Some options are frontend flags (e.g.: -analyzer-output), but some are
161/// analyzer configuration options, which are preceded by -analyzer-config
162/// (e.g.: -analyzer-config notes-as-events=true).
163///
164/// If you'd like to add a new frontend flag, add it to
165/// include/clang/Driver/CC1Options.td, add a new field to store the value of
166/// that flag in this class, and initialize it in
167/// lib/Frontend/CompilerInvocation.cpp.
168///
169/// If you'd like to add a new non-checker configuration, register it in
170/// include/clang/StaticAnalyzer/Core/AnalyzerOptions.def, and refer to the
171/// top of the file for documentation.
172///
173/// If you'd like to add a new checker option, call getChecker*Option()
174/// whenever.
175///
176/// Some of the options are controlled by raw frontend flags for no good reason,
177/// and should be eventually converted into -analyzer-config flags. New analyzer
178/// options should not be implemented as frontend flags. Frontend flags still
179/// make sense for things that do not affect the actual analysis.
181public:
182 using ConfigTable = llvm::StringMap<std::string>;
183
184 /// Retrieves the list of checkers generated from Checkers.td. This doesn't
185 /// contain statically linked but non-generated checkers and plugin checkers!
186 static std::vector<StringRef>
187 getRegisteredCheckers(bool IncludeExperimental = false);
188
189 /// Retrieves the list of packages generated from Checkers.td. This doesn't
190 /// contain statically linked but non-generated packages and plugin packages!
191 static std::vector<StringRef>
192 getRegisteredPackages(bool IncludeExperimental = false);
193
194 /// Convenience function for printing options or checkers and their
195 /// description in a formatted manner. If \p MinLineWidth is set to 0, no line
196 /// breaks are introduced for the description.
197 ///
198 /// Format, depending whether the option name's length is less than
199 /// \p EntryWidth:
200 ///
201 /// <padding>EntryName<padding>Description
202 /// <---------padding--------->Description
203 /// <---------padding--------->Description
204 ///
205 /// <padding>VeryVeryLongEntryName
206 /// <---------padding--------->Description
207 /// <---------padding--------->Description
208 /// ^~~~~~~~~InitialPad
209 /// ^~~~~~~~~~~~~~~~~~EntryWidth
210 /// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MinLineWidth
211 static void printFormattedEntry(llvm::raw_ostream &Out,
212 std::pair<StringRef, StringRef> EntryDescPair,
213 size_t InitialPad, size_t EntryWidth,
214 size_t MinLineWidth = 0);
215
216 /// Pairs of checker/package name and enable/disable.
217 std::vector<std::pair<std::string, bool>> CheckersAndPackages;
218
219 /// Vector of checker/package names which will not emit warnings.
220 std::vector<std::string> SilencedCheckersAndPackages;
221
222 /// A key-value table of use-specified configuration values.
223 // TODO: This shouldn't be public.
228
230
231 /// File path to which the exploded graph should be dumped.
233
234 /// Store full compiler invocation for reproducible instructions in the
235 /// generated report.
237
238 /// The maximum number of times the analyzer visits a block.
240
241 /// Disable all analyzer checkers.
242 ///
243 /// This flag allows one to disable analyzer checkers on the code processed by
244 /// the given analysis consumer. Note, the code will get parsed and the
245 /// command-line options will get checked.
246 unsigned DisableAllCheckers : 1;
247
248 unsigned ShowCheckerHelp : 1;
251
255
259 unsigned AnalyzeAll : 1;
262
263 unsigned TrimGraph : 1;
265 unsigned UnoptimizedCFG : 1;
266 unsigned PrintStats : 1;
267
268 /// Do not re-analyze paths leading to exhausted nodes with a different
269 /// strategy. We get better code coverage when retry is enabled.
270 unsigned NoRetryExhausted : 1;
271
272 /// Emit analyzer warnings as errors.
273 LLVM_PREFERRED_TYPE(bool)
275
276 /// The inlining stack depth limit.
278
279 /// The mode of function selection used during inlining.
281
282 // Create a field for each -analyzer-config option.
283#define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
284 SHALLOW_VAL, DEEP_VAL) \
285 ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, SHALLOW_VAL)
286
287#define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \
288 TYPE NAME;
289
290#include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
291#undef ANALYZER_OPTION
292#undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
293
294 bool isUnknownAnalyzerConfig(llvm::StringRef Name) {
295 static std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = []() {
296 // Create an array of all -analyzer-config command line options.
297 std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = {
298#define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
299 SHALLOW_VAL, DEEP_VAL) \
300 ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, SHALLOW_VAL)
301
302#define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \
303 llvm::StringLiteral(CMDFLAG),
304
305#include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
306#undef ANALYZER_OPTION
307#undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
308 };
309 // FIXME: Sort this at compile-time when we get constexpr sort (C++20).
310 llvm::sort(AnalyzerConfigCmdFlags);
311 return AnalyzerConfigCmdFlags;
312 }();
313
314 return !llvm::binary_search(AnalyzerConfigCmdFlags, Name);
315 }
316
328
329 /// Interprets an option's string value as a boolean. The "true" string is
330 /// interpreted as true and the "false" string is interpreted as false.
331 ///
332 /// If an option value is not provided, returns the given \p DefaultVal.
333 /// @param [in] CheckerName The *full name* of the checker. One may retrieve
334 /// this from the checker object's field \c Name, or through \c
335 /// CheckerManager::getCurrentCheckerName within the checker's registry
336 /// function.
337 /// Checker options are retrieved in the following format:
338 /// `-analyzer-config CheckerName:OptionName=Value.
339 /// @param [in] OptionName Name for option to retrieve.
340 /// @param [in] SearchInParents If set to true and the searched option was not
341 /// specified for the given checker the options for the parent packages will
342 /// be searched as well. The inner packages take precedence over the outer
343 /// ones.
344 bool getCheckerBooleanOption(StringRef CheckerName, StringRef OptionName,
345 bool SearchInParents = false) const;
346
347 bool getCheckerBooleanOption(const ento::CheckerBase *C, StringRef OptionName,
348 bool SearchInParents = false) const;
349
350 /// Interprets an option's string value as an integer value.
351 ///
352 /// If an option value is not provided, returns the given \p DefaultVal.
353 /// @param [in] CheckerName The *full name* of the checker. One may retrieve
354 /// this from the checker object's field \c Name, or through \c
355 /// CheckerManager::getCurrentCheckerName within the checker's registry
356 /// function.
357 /// Checker options are retrieved in the following format:
358 /// `-analyzer-config CheckerName:OptionName=Value.
359 /// @param [in] OptionName Name for option to retrieve.
360 /// @param [in] SearchInParents If set to true and the searched option was not
361 /// specified for the given checker the options for the parent packages will
362 /// be searched as well. The inner packages take precedence over the outer
363 /// ones.
364 int getCheckerIntegerOption(StringRef CheckerName, StringRef OptionName,
365 bool SearchInParents = false) const;
366
367 int getCheckerIntegerOption(const ento::CheckerBase *C, StringRef OptionName,
368 bool SearchInParents = false) const;
369
370 /// Query an option's string value.
371 ///
372 /// If an option value is not provided, returns the given \p DefaultVal.
373 /// @param [in] CheckerName The *full name* of the checker. One may retrieve
374 /// this from the checker object's field \c Name, or through \c
375 /// CheckerManager::getCurrentCheckerName within the checker's registry
376 /// function.
377 /// Checker options are retrieved in the following format:
378 /// `-analyzer-config CheckerName:OptionName=Value.
379 /// @param [in] OptionName Name for option to retrieve.
380 /// @param [in] SearchInParents If set to true and the searched option was not
381 /// specified for the given checker the options for the parent packages will
382 /// be searched as well. The inner packages take precedence over the outer
383 /// ones.
384 StringRef getCheckerStringOption(StringRef CheckerName, StringRef OptionName,
385 bool SearchInParents = false) const;
386
388 StringRef OptionName,
389 bool SearchInParents = false) const;
390
393
394 /// Returns the inter-procedural analysis mode.
395 IPAKind getIPAMode() const;
396
397 /// Returns the option controlling which C++ member functions will be
398 /// considered for inlining.
399 ///
400 /// This is controlled by the 'c++-inlining' config option.
401 ///
402 /// \sa CXXMemberInliningMode
404
407 ShouldDisplayMacroExpansions,
408 ShouldSerializeStats,
409 // The stable report filename option is deprecated because
410 // file names are now always stable. Now the old option acts as
411 // an alias to the new verbose filename option because this
412 // closely mimics the behavior under the old option.
413 ShouldWriteStableReportFilename || ShouldWriteVerboseReportFilename,
414 static_cast<bool>(AnalyzerWerror),
415 ShouldApplyFixIts,
416 ShouldDisplayCheckerNameForText};
417 }
418};
419
420//===----------------------------------------------------------------------===//
421// We'll use AnalyzerOptions in the frontend, but we can't link the frontend
422// with clangStaticAnalyzerCore, because clangStaticAnalyzerCore depends on
423// clangFrontend.
424//
425// For this reason, implement some methods in this header file.
426//===----------------------------------------------------------------------===//
427
428inline std::vector<StringRef>
429AnalyzerOptions::getRegisteredCheckers(bool IncludeExperimental) {
430 static constexpr llvm::StringLiteral StaticAnalyzerCheckerNames[] = {
431#define GET_CHECKERS
432#define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \
433 llvm::StringLiteral(FULLNAME),
434#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
435#undef CHECKER
436#undef GET_CHECKERS
437 };
438 std::vector<StringRef> Checkers;
439 for (StringRef CheckerName : StaticAnalyzerCheckerNames) {
440 if (!CheckerName.starts_with("debug.") &&
441 (IncludeExperimental || !CheckerName.starts_with("alpha.")))
442 Checkers.push_back(CheckerName);
443 }
444 return Checkers;
445}
446
447inline std::vector<StringRef>
448AnalyzerOptions::getRegisteredPackages(bool IncludeExperimental) {
449 static constexpr llvm::StringLiteral StaticAnalyzerPackageNames[] = {
450#define GET_PACKAGES
451#define PACKAGE(FULLNAME) llvm::StringLiteral(FULLNAME),
452#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
453#undef PACKAGE
454#undef GET_PACKAGES
455 };
456 std::vector<StringRef> Packages;
457 for (StringRef PackageName : StaticAnalyzerPackageNames) {
458 if (PackageName != "debug" &&
459 (IncludeExperimental || PackageName != "alpha"))
460 Packages.push_back(PackageName);
461 }
462 return Packages;
463}
464
465} // namespace clang
466
467#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
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 AnalyzerWerror
Emit analyzer warnings as errors.
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.
static std::optional< PositiveAnalyzerOption > create(StringRef Str)
constexpr PositiveAnalyzerOption(const PositiveAnalyzerOption &)=default
constexpr PositiveAnalyzerOption(unsigned Value)
constexpr PositiveAnalyzerOption()=default
static constexpr std::optional< PositiveAnalyzerOption > create(unsigned Val)
constexpr PositiveAnalyzerOption & operator=(const PositiveAnalyzerOption &Other)
The non-templated common ancestor of all the simple Checker<...> classes.
Definition: Checker.h:541
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
@ Other
Other implicit parameter.
#define false
Definition: stdbool.h:26
These options tweak the behavior of path diangostic consumers.