clang  9.0.0svn
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 
17 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/IntrusiveRefCntPtr.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/StringSwitch.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 /// Analysis - Set of available source code analyses.
36 enum Analyses {
37 #define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE) NAME,
38 #include "clang/StaticAnalyzer/Core/Analyses.def"
40 };
41 
42 /// AnalysisStores - Set of available analysis store models.
44 #define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN) NAME##Model,
45 #include "clang/StaticAnalyzer/Core/Analyses.def"
47 };
48 
49 /// AnalysisConstraints - Set of available constraint models.
51 #define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN) NAME##Model,
52 #include "clang/StaticAnalyzer/Core/Analyses.def"
54 };
55 
56 /// AnalysisDiagClients - Set of available diagnostic clients for rendering
57 /// analysis results.
59 #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN) PD_##NAME,
60 #include "clang/StaticAnalyzer/Core/Analyses.def"
63 };
64 
65 /// AnalysisPurgeModes - Set of available strategies for dead symbol removal.
67 #define ANALYSIS_PURGE(NAME, CMDFLAG, DESC) NAME,
68 #include "clang/StaticAnalyzer/Core/Analyses.def"
70 };
71 
72 /// AnalysisInlineFunctionSelection - Set of inlining function selection heuristics.
74 #define ANALYSIS_INLINING_MODE(NAME, CMDFLAG, DESC) NAME,
75 #include "clang/StaticAnalyzer/Core/Analyses.def"
77 };
78 
79 /// Describes the different kinds of C++ member functions which can be
80 /// considered for inlining by the analyzer.
81 ///
82 /// These options are cumulative; enabling one kind of member function will
83 /// enable all kinds with lower enum values.
85  // Uninitialized = 0,
86 
87  /// A dummy mode in which no C++ inlining is enabled.
89 
90  /// Refers to regular member function and operator calls.
92 
93  /// Refers to constructors (implicit or explicit).
94  ///
95  /// Note that a constructor will not be inlined if the corresponding
96  /// destructor is non-trivial.
98 
99  /// Refers to destructors (implicit or explicit).
101 };
102 
103 /// Describes the different modes of inter-procedural analysis.
104 enum IPAKind {
105  /// Perform only intra-procedural analysis.
107 
108  /// Inline C functions and blocks when their definitions are available.
110 
111  /// Inline callees(C, C++, ObjC) when their definitions are available.
113 
114  /// Enable inlining of dynamically dispatched methods.
116 
117  /// Enable inlining of dynamically dispatched methods, bifurcate paths when
118  /// exact type info is unavailable.
120 };
121 
123  DFS,
124  BFS,
129 };
130 
131 /// Describes the kinds for high-level analyzer mode.
133  /// Perform shallow but fast analyzes.
135 
136  /// Perform deep analyzes.
138 };
139 
140 /// Stores options for the analyzer from the command line.
141 ///
142 /// Some options are frontend flags (e.g.: -analyzer-output), but some are
143 /// analyzer configuration options, which are preceded by -analyzer-config
144 /// (e.g.: -analyzer-config notes-as-events=true).
145 ///
146 /// If you'd like to add a new frontend flag, add it to
147 /// include/clang/Driver/CC1Options.td, add a new field to store the value of
148 /// that flag in this class, and initialize it in
149 /// lib/Frontend/CompilerInvocation.cpp.
150 ///
151 /// If you'd like to add a new non-checker configuration, register it in
152 /// include/clang/StaticAnalyzer/Core/AnalyzerOptions.def, and refer to the
153 /// top of the file for documentation.
154 ///
155 /// If you'd like to add a new checker option, call getChecker*Option()
156 /// whenever.
157 ///
158 /// Some of the options are controlled by raw frontend flags for no good reason,
159 /// and should be eventually converted into -analyzer-config flags. New analyzer
160 /// options should not be implemented as frontend flags. Frontend flags still
161 /// make sense for things that do not affect the actual analysis.
162 class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
163 public:
164  using ConfigTable = llvm::StringMap<std::string>;
165 
166  static std::vector<StringRef>
167  getRegisteredCheckers(bool IncludeExperimental = false);
168 
169  /// Convenience function for printing options or checkers and their
170  /// description in a formatted manner. If \p MinLineWidth is set to 0, no line
171  /// breaks are introduced for the description.
172  ///
173  /// Format, depending whether the option name's length is less then
174  /// \p OptionWidth:
175  ///
176  /// <padding>EntryName<padding>Description
177  /// <---------padding--------->Description
178  /// <---------padding--------->Description
179  ///
180  /// <padding>VeryVeryLongOptionName
181  /// <---------padding--------->Description
182  /// <---------padding--------->Description
183  /// ^~~~~~~~ InitialPad
184  /// ^~~~~~~~~~~~~~~~~~~~~~~~~~ EntryWidth
185  /// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MinLineWidth
186  static void printFormattedEntry(
187  llvm::raw_ostream &Out,
188  std::pair<StringRef, StringRef> EntryDescPair,
189  size_t EntryWidth, size_t InitialPad, size_t MinLineWidth = 0);
190 
191 
192  /// Pair of checker name and enable/disable.
193  std::vector<std::pair<std::string, bool>> CheckersControlList;
194 
195  /// A key-value table of use-specified configuration values.
196  // TODO: This shouldn't be public.
198  AnalysisStores AnalysisStoreOpt = RegionStoreModel;
199  AnalysisConstraints AnalysisConstraintsOpt = RangeConstraintsModel;
200  AnalysisDiagClients AnalysisDiagOpt = PD_HTML;
201  AnalysisPurgeMode AnalysisPurgeOpt = PurgeStmt;
202 
204 
205  /// File path to which the exploded graph should be dumped.
206  std::string DumpExplodedGraphTo;
207 
208  /// Store full compiler invocation for reproducible instructions in the
209  /// generated report.
211 
212  /// The maximum number of times the analyzer visits a block.
214 
215  /// Disable all analyzer checks.
216  ///
217  /// This flag allows one to disable analyzer checks on the code processed by
218  /// the given analysis consumer. Note, the code will get parsed and the
219  /// command-line options will get checked.
220  unsigned DisableAllChecks : 1;
221 
222  unsigned ShowCheckerHelp : 1;
223  unsigned ShowCheckerHelpAlpha : 1;
225 
226  unsigned ShowCheckerOptionList : 1;
229 
231  unsigned ShowConfigOptionsList : 1;
233  unsigned AnalyzeAll : 1;
235  unsigned AnalyzeNestedBlocks : 1;
236 
238 
239  unsigned TrimGraph : 1;
241  unsigned UnoptimizedCFG : 1;
242  unsigned PrintStats : 1;
243 
244  /// Do not re-analyze paths leading to exhausted nodes with a different
245  /// strategy. We get better code coverage when retry is enabled.
246  unsigned NoRetryExhausted : 1;
247 
248  /// The inlining stack depth limit.
249  // Cap the stack depth at 4 calls (5 stack frames, base + 4 calls).
250  unsigned InlineMaxStackDepth = 5;
251 
252  /// The mode of function selection used during inlining.
253  AnalysisInliningMode InliningMode = NoRedundancy;
254 
255  // Create a field for each -analyzer-config option.
256 #define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
257  SHALLOW_VAL, DEEP_VAL) \
258  ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, SHALLOW_VAL)
259 
260 #define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \
261  TYPE NAME;
262 
263 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
264 #undef ANALYZER_OPTION
265 #undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
266 
267  // Create an array of all -analyzer-config command line options. Sort it in
268  // the constructor.
269  std::vector<StringRef> AnalyzerConfigCmdFlags = {
270 #define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
271  SHALLOW_VAL, DEEP_VAL) \
272  ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, SHALLOW_VAL)
273 
274 #define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \
275  CMDFLAG,
276 
277 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
278 #undef ANALYZER_OPTION
279 #undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
280  };
281 
282  bool isUnknownAnalyzerConfig(StringRef Name) const {
283 
284  assert(std::is_sorted(AnalyzerConfigCmdFlags.begin(),
285  AnalyzerConfigCmdFlags.end()));
286 
287  return !std::binary_search(AnalyzerConfigCmdFlags.begin(),
288  AnalyzerConfigCmdFlags.end(), Name);
289  }
290 
292  : DisableAllChecks(false), ShowCheckerHelp(false),
293  ShowCheckerHelpAlpha(false), ShowCheckerHelpDeveloper(false),
294  ShowCheckerOptionList(false), ShowCheckerOptionAlphaList(false),
295  ShowCheckerOptionDeveloperList(false), ShowEnabledCheckerList(false),
296  ShowConfigOptionsList(false), AnalyzeAll(false),
297  AnalyzerDisplayProgress(false), AnalyzeNestedBlocks(false),
298  eagerlyAssumeBinOpBifurcation(false), TrimGraph(false),
299  visualizeExplodedGraphWithGraphViz(false), UnoptimizedCFG(false),
300  PrintStats(false), NoRetryExhausted(false) {
301  llvm::sort(AnalyzerConfigCmdFlags);
302  }
303 
304  /// Interprets an option's string value as a boolean. The "true" string is
305  /// interpreted as true and the "false" string is interpreted as false.
306  ///
307  /// If an option value is not provided, returns the given \p DefaultVal.
308  /// @param [in] CheckerName The *full name* of the checker. One may retrieve
309  /// this from the checker object's field \c Name, or through \c
310  /// CheckerManager::getCurrentCheckName within the checker's registry
311  /// function.
312  /// Checker options are retrieved in the following format:
313  /// `-analyzer-config CheckerName:OptionName=Value.
314  /// @param [in] OptionName Name for option to retrieve.
315  /// @param [in] SearchInParents If set to true and the searched option was not
316  /// specified for the given checker the options for the parent packages will
317  /// be searched as well. The inner packages take precedence over the outer
318  /// ones.
319  bool getCheckerBooleanOption(StringRef CheckerName, StringRef OptionName,
320  bool SearchInParents = false) const;
321 
322  bool getCheckerBooleanOption(const ento::CheckerBase *C, StringRef OptionName,
323  bool SearchInParents = false) const;
324 
325  /// Interprets an option's string value as an integer value.
326  ///
327  /// If an option value is not provided, returns the given \p DefaultVal.
328  /// @param [in] CheckerName The *full name* of the checker. One may retrieve
329  /// this from the checker object's field \c Name, or through \c
330  /// CheckerManager::getCurrentCheckName within the checker's registry
331  /// function.
332  /// Checker options are retrieved in the following format:
333  /// `-analyzer-config CheckerName:OptionName=Value.
334  /// @param [in] OptionName Name for option to retrieve.
335  /// @param [in] SearchInParents If set to true and the searched option was not
336  /// specified for the given checker the options for the parent packages will
337  /// be searched as well. The inner packages take precedence over the outer
338  /// ones.
339  int getCheckerIntegerOption(StringRef CheckerName, StringRef OptionName,
340  bool SearchInParents = false) const;
341 
342  int getCheckerIntegerOption(const ento::CheckerBase *C, StringRef OptionName,
343  bool SearchInParents = false) const;
344 
345  /// Query an option's string value.
346  ///
347  /// If an option value is not provided, returns the given \p DefaultVal.
348  /// @param [in] CheckerName The *full name* of the checker. One may retrieve
349  /// this from the checker object's field \c Name, or through \c
350  /// CheckerManager::getCurrentCheckName within the checker's registry
351  /// function.
352  /// Checker options are retrieved in the following format:
353  /// `-analyzer-config CheckerName:OptionName=Value.
354  /// @param [in] OptionName Name for option to retrieve.
355  /// @param [in] SearchInParents If set to true and the searched option was not
356  /// specified for the given checker the options for the parent packages will
357  /// be searched as well. The inner packages take precedence over the outer
358  /// ones.
359  StringRef getCheckerStringOption(StringRef CheckerName, StringRef OptionName,
360  bool SearchInParents = false) const;
361 
362  StringRef getCheckerStringOption(const ento::CheckerBase *C,
363  StringRef OptionName,
364  bool SearchInParents = false) const;
365 
366  /// Retrieves and sets the UserMode. This is a high-level option,
367  /// which is used to set other low-level options. It is not accessible
368  /// outside of AnalyzerOptions.
369  UserModeKind getUserMode() const;
370 
371  ExplorationStrategyKind getExplorationStrategy() const;
372 
373  /// Returns the inter-procedural analysis mode.
374  IPAKind getIPAMode() const;
375 
376  /// Returns the option controlling which C++ member functions will be
377  /// considered for inlining.
378  ///
379  /// This is controlled by the 'c++-inlining' config option.
380  ///
381  /// \sa CXXMemberInliningMode
382  bool mayInlineCXXMemberFunction(CXXInlineableMemberKind K) const;
383 };
384 
386 
387 //===----------------------------------------------------------------------===//
388 // We'll use AnalyzerOptions in the frontend, but we can't link the frontend
389 // with clangStaticAnalyzerCore, because clangStaticAnalyzerCore depends on
390 // clangFrontend.
391 //
392 // For this reason, implement some methods in this header file.
393 //===----------------------------------------------------------------------===//
394 
396  auto K = llvm::StringSwitch<llvm::Optional<UserModeKind>>(UserMode)
397  .Case("shallow", UMK_Shallow)
398  .Case("deep", UMK_Deep)
399  .Default(None);
400  assert(K.hasValue() && "User mode is invalid.");
401  return K.getValue();
402 }
403 
404 } // namespace clang
405 
406 #endif // LLVM_CLANG_STATICANALYZER_CORE_ANALYZEROPTIONS_H
Inline C functions and blocks when their definitions are available.
IPAKind
Describes the different modes of inter-procedural analysis.
unsigned visualizeExplodedGraphWithGraphViz
Perform only intra-procedural analysis.
UserModeKind getUserMode() const
Retrieves and sets the UserMode.
A dummy mode in which no C++ inlining is enabled.
Inline callees(C, C++, ObjC) when their definitions are available.
Perform deep analyzes.
std::string DumpExplodedGraphTo
File path to which the exploded graph should be dumped.
unsigned eagerlyAssumeBinOpBifurcation
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
std::string FullCompilerInvocation
Store full compiler invocation for reproducible instructions in the generated report.
std::vector< std::pair< std::string, bool > > CheckersControlList
Pair of checker name and enable/disable.
AnalysisStores
AnalysisStores - Set of available analysis store models.
ExplorationStrategyKind
Refers to regular member function and operator calls.
AnalysisInliningMode
AnalysisInlineFunctionSelection - Set of inlining function selection heuristics.
Refers to constructors (implicit or explicit).
Enable inlining of dynamically dispatched methods.
UserModeKind
Describes the kinds for high-level analyzer mode.
#define false
Definition: stdbool.h:17
AnalysisConstraints
AnalysisConstraints - Set of available constraint models.
llvm::StringMap< std::string > ConfigTable
ConfigTable Config
A key-value table of use-specified configuration values.
unsigned ShowCheckerOptionDeveloperList
Refers to destructors (implicit or explicit).
bool isUnknownAnalyzerConfig(StringRef Name) const
unsigned maxBlockVisitOnPath
The maximum number of times the analyzer visits a block.
unsigned DisableAllChecks
Disable all analyzer checks.
Dataflow Directional Tag Classes.
Perform shallow but fast analyzes.
std::string AnalyzeSpecificFunction
AnalysisPurgeMode
AnalysisPurgeModes - Set of available strategies for dead symbol removal.
Stores options for the analyzer from the command line.
unsigned NoRetryExhausted
Do not re-analyze paths leading to exhausted nodes with a different strategy.
Analyses
Analysis - Set of available source code analyses.
CXXInlineableMemberKind
Describes the different kinds of C++ member functions which can be considered for inlining by the ana...
unsigned ShouldEmitErrorsOnInvalidConfigValue
Enable inlining of dynamically dispatched methods, bifurcate paths when exact type info is unavailabl...
AnalysisDiagClients
AnalysisDiagClients - Set of available diagnostic clients for rendering analysis results.