AnalyzerOptions.cpp revision 7959671d456c916706a5f61af609d8f1fc95decf
1//===-- AnalyzerOptions.cpp - Analysis Engine Options -----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains special accessors for analyzer configuration options
11// with string representations.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
16#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/StringSwitch.h"
18#include "llvm/Support/raw_ostream.h"
19
20using namespace clang;
21using namespace llvm;
22
23bool
24AnalyzerOptions::mayInlineCXXMemberFunction(CXXInlineableMemberKind K) {
25  if (IPAMode < Inlining)
26    return false;
27
28  if (!CXXMemberInliningMode) {
29    static const char *ModeKey = "c++-inlining";
30
31    StringRef ModeStr(Config.GetOrCreateValue(ModeKey,
32                                              "methods").getValue());
33
34    CXXInlineableMemberKind &MutableMode =
35      const_cast<CXXInlineableMemberKind &>(CXXMemberInliningMode);
36
37    MutableMode = llvm::StringSwitch<CXXInlineableMemberKind>(ModeStr)
38      .Case("constructors", CIMK_Constructors)
39      .Case("destructors", CIMK_Destructors)
40      .Case("none", CIMK_None)
41      .Case("methods", CIMK_MemberFunctions)
42      .Default(CXXInlineableMemberKind());
43
44    if (!MutableMode) {
45      // FIXME: We should emit a warning here about an unknown inlining kind,
46      // but the AnalyzerOptions doesn't have access to a diagnostic engine.
47      MutableMode = CIMK_None;
48    }
49  }
50
51  return CXXMemberInliningMode >= K;
52}
53
54static StringRef toString(bool b) { return b ? "true" : "false"; }
55
56bool AnalyzerOptions::getBooleanOption(StringRef Name, bool DefaultVal) {
57  // FIXME: We should emit a warning here if the value is something other than
58  // "true", "false", or the empty string (meaning the default value),
59  // but the AnalyzerOptions doesn't have access to a diagnostic engine.
60  StringRef V(Config.GetOrCreateValue(Name, toString(DefaultVal)).getValue());
61  return llvm::StringSwitch<bool>(V)
62      .Case("true", true)
63      .Case("false", false)
64      .Default(DefaultVal);
65}
66
67bool AnalyzerOptions::getBooleanOption(llvm::Optional<bool> &V,
68                                       StringRef Name,
69                                       bool DefaultVal) {
70  if (!V.hasValue())
71    V = getBooleanOption(Name, DefaultVal);
72  return V.getValue();
73}
74
75bool AnalyzerOptions::includeTemporaryDtorsInCFG() {
76  return getBooleanOption(IncludeTemporaryDtorsInCFG,
77                          "cfg-temporary-dtors",
78                          /* Default = */ false);
79}
80
81bool AnalyzerOptions::mayInlineCXXStandardLibrary() {
82  return getBooleanOption(InlineCXXStandardLibrary,
83                          "c++-stdlib-inlining",
84                          /*Default=*/true);
85}
86
87bool AnalyzerOptions::mayInlineTemplateFunctions() {
88  return getBooleanOption(InlineTemplateFunctions,
89                          "c++-template-inlining",
90                          /*Default=*/true);
91}
92
93bool AnalyzerOptions::mayInlineObjCMethod() {
94  return getBooleanOption(ObjCInliningMode,
95                          "objc-inlining",
96                          /* Default = */ true);
97}
98
99bool AnalyzerOptions::shouldPruneNullReturnPaths() {
100  return getBooleanOption(PruneNullReturnPaths,
101                          "suppress-null-return-paths",
102                          /* Default = */ true);
103}
104
105bool AnalyzerOptions::shouldAvoidSuppressingNullArgumentPaths() {
106  return getBooleanOption(AvoidSuppressingNullArgumentPaths,
107                          "avoid-suppressing-null-argument-paths",
108                          /* Default = */ false);
109}
110
111int AnalyzerOptions::getOptionAsInteger(StringRef Name, int DefaultVal) {
112  llvm::SmallString<10> StrBuf;
113  llvm::raw_svector_ostream OS(StrBuf);
114  OS << DefaultVal;
115
116  StringRef V(Config.GetOrCreateValue(Name, OS.str()).getValue());
117  int Res = DefaultVal;
118  bool b = V.getAsInteger(10, Res);
119  assert(!b && "analyzer-config option should be numeric");
120  (void) b;
121  return Res;
122}
123
124unsigned AnalyzerOptions::getAlwaysInlineSize() {
125  if (!AlwaysInlineSize.hasValue())
126    AlwaysInlineSize = getOptionAsInteger("ipa-always-inline-size", 3);
127  return AlwaysInlineSize.getValue();
128}
129
130unsigned AnalyzerOptions::getGraphTrimInterval() {
131  if (!GraphTrimInterval.hasValue())
132    GraphTrimInterval = getOptionAsInteger("graph-trim-interval", 1000);
133  return GraphTrimInterval.getValue();
134}
135
136unsigned AnalyzerOptions::getMaxTimesInlineLarge() {
137  if (!MaxTimesInlineLarge.hasValue())
138    MaxTimesInlineLarge = getOptionAsInteger("max-times-inline-large", 32);
139  return MaxTimesInlineLarge.getValue();
140}
141
142bool AnalyzerOptions::shouldSynthesizeBodies() {
143  return getBooleanOption("faux-bodies", true);
144}
145