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
17 #include "clang/Analysis/PathDiagnostic.h"
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
27 namespace clang {
28
29 namespace ento {
30
31 class CheckerBase;
32
33 } // namespace ento
34
35 /// AnalysisConstraints - Set of available constraint models.
36 enum AnalysisConstraints {
37 #define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN) NAME##Model,
38 #include "clang/StaticAnalyzer/Core/Analyses.def"
39 NumConstraints
40 };
41
42 /// AnalysisDiagClients - Set of available diagnostic clients for rendering
43 /// analysis results.
44 enum AnalysisDiagClients {
45 #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN) PD_##NAME,
46 #include "clang/StaticAnalyzer/Core/Analyses.def"
47 PD_NONE,
48 NUM_ANALYSIS_DIAG_CLIENTS
49 };
50
51 /// AnalysisPurgeModes - Set of available strategies for dead symbol removal.
52 enum AnalysisPurgeMode {
53 #define ANALYSIS_PURGE(NAME, CMDFLAG, DESC) NAME,
54 #include "clang/StaticAnalyzer/Core/Analyses.def"
55 NumPurgeModes
56 };
57
58 /// AnalysisInlineFunctionSelection - Set of inlining function selection heuristics.
59 enum AnalysisInliningMode {
60 #define ANALYSIS_INLINING_MODE(NAME, CMDFLAG, DESC) NAME,
61 #include "clang/StaticAnalyzer/Core/Analyses.def"
62 NumInliningModes
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.
70 enum CXXInlineableMemberKind {
71 // Uninitialized = 0,
72
73 /// A dummy mode in which no C++ inlining is enabled.
74 CIMK_None,
75
76 /// Refers to regular member function and operator calls.
77 CIMK_MemberFunctions,
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.
83 CIMK_Constructors,
84
85 /// Refers to destructors (implicit or explicit).
86 CIMK_Destructors
87 };
88
89 /// Describes the different modes of inter-procedural analysis.
90 enum IPAKind {
91 /// Perform only intra-procedural analysis.
92 IPAK_None = 1,
93
94 /// Inline C functions and blocks when their definitions are available.
95 IPAK_BasicInlining = 2,
96
97 /// Inline callees(C, C++, ObjC) when their definitions are available.
98 IPAK_Inlining = 3,
99
100 /// Enable inlining of dynamically dispatched methods.
101 IPAK_DynamicDispatch = 4,
102
103 /// Enable inlining of dynamically dispatched methods, bifurcate paths when
104 /// exact type info is unavailable.
105 IPAK_DynamicDispatchBifurcate = 5
106 };
107
108 enum class ExplorationStrategyKind {
109 DFS,
110 BFS,
111 UnexploredFirst,
112 UnexploredFirstQueue,
113 UnexploredFirstLocationQueue,
114 BFSBlockDFSContents,
115 };
116
117 /// Describes the kinds for high-level analyzer mode.
118 enum UserModeKind {
119 /// Perform shallow but fast analyzes.
120 UMK_Shallow = 1,
121
122 /// Perform deep analyzes.
123 UMK_Deep = 2
124 };
125
126 enum class CTUPhase1InliningKind { None, Small, All };
127
128 class PositiveAnalyzerOption {
129 public:
130 constexpr PositiveAnalyzerOption() = default;
PositiveAnalyzerOption(unsigned Value)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 &
136 operator=(const PositiveAnalyzerOption &Other) {
137 Value = Other.Value;
138 return *this;
139 }
140
create(unsigned Val)141 static constexpr std::optional<PositiveAnalyzerOption> create(unsigned Val) {
142 if (Val == 0)
143 return std::nullopt;
144 return PositiveAnalyzerOption{Val};
145 }
create(StringRef Str)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
154 private:
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.
180 class AnalyzerOptions {
181 public:
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.
224 ConfigTable Config;
225 AnalysisConstraints AnalysisConstraintsOpt = RangeConstraintsModel;
226 AnalysisDiagClients AnalysisDiagOpt = PD_HTML;
227 AnalysisPurgeMode AnalysisPurgeOpt = PurgeStmt;
228
229 std::string AnalyzeSpecificFunction;
230
231 /// File path to which the exploded graph should be dumped.
232 std::string DumpExplodedGraphTo;
233
234 /// Store full compiler invocation for reproducible instructions in the
235 /// generated report.
236 std::string FullCompilerInvocation;
237
238 /// The maximum number of times the analyzer visits a block.
239 unsigned maxBlockVisitOnPath;
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;
249 unsigned ShowCheckerHelpAlpha : 1;
250 unsigned ShowCheckerHelpDeveloper : 1;
251
252 unsigned ShowCheckerOptionList : 1;
253 unsigned ShowCheckerOptionAlphaList : 1;
254 unsigned ShowCheckerOptionDeveloperList : 1;
255
256 unsigned ShowEnabledCheckerList : 1;
257 unsigned ShowConfigOptionsList : 1;
258 unsigned ShouldEmitErrorsOnInvalidConfigValue : 1;
259 unsigned AnalyzeAll : 1;
260 unsigned AnalyzerDisplayProgress : 1;
261 unsigned AnalyzerNoteAnalysisEntryPoints : 1;
262
263 unsigned TrimGraph : 1;
264 unsigned visualizeExplodedGraphWithGraphViz : 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)
274 unsigned AnalyzerWerror : 1;
275
276 /// The inlining stack depth limit.
277 unsigned InlineMaxStackDepth;
278
279 /// The mode of function selection used during inlining.
280 AnalysisInliningMode InliningMode = NoRedundancy;
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
isUnknownAnalyzerConfig(llvm::StringRef Name)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
AnalyzerOptions()317 AnalyzerOptions()
318 : DisableAllCheckers(false), ShowCheckerHelp(false),
319 ShowCheckerHelpAlpha(false), ShowCheckerHelpDeveloper(false),
320 ShowCheckerOptionList(false), ShowCheckerOptionAlphaList(false),
321 ShowCheckerOptionDeveloperList(false), ShowEnabledCheckerList(false),
322 ShowConfigOptionsList(false),
323 ShouldEmitErrorsOnInvalidConfigValue(false), AnalyzeAll(false),
324 AnalyzerDisplayProgress(false), AnalyzerNoteAnalysisEntryPoints(false),
325 TrimGraph(false), visualizeExplodedGraphWithGraphViz(false),
326 UnoptimizedCFG(false), PrintStats(false), NoRetryExhausted(false),
327 AnalyzerWerror(false) {}
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
387 StringRef getCheckerStringOption(const ento::CheckerBase *C,
388 StringRef OptionName,
389 bool SearchInParents = false) const;
390
391 ExplorationStrategyKind getExplorationStrategy() const;
392 CTUPhase1InliningKind getCTUPhase1Inlining() const;
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
403 bool mayInlineCXXMemberFunction(CXXInlineableMemberKind K) const;
404
getDiagOpts()405 ento::PathDiagnosticConsumerOptions getDiagOpts() const {
406 return {FullCompilerInvocation,
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
428 inline std::vector<StringRef>
getRegisteredCheckers(bool IncludeExperimental)429 AnalyzerOptions::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
447 inline std::vector<StringRef>
getRegisteredPackages(bool IncludeExperimental)448 AnalyzerOptions::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
468