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/StringSwitch.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/Support/raw_ostream.h"
19 
20 using namespace clang;
21 using namespace llvm;
22 
23 bool
24 AnalyzerOptions::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 
54 static StringRef toString(bool b) { return b ? "true" : "false"; }
55 
56 bool 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 
67 bool 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 
75 bool AnalyzerOptions::includeTemporaryDtorsInCFG() {
76   return getBooleanOption(IncludeTemporaryDtorsInCFG,
77                           "cfg-temporary-dtors",
78                           /* Default = */ false);
79 }
80 
81 bool AnalyzerOptions::mayInlineCXXStandardLibrary() {
82   return getBooleanOption(InlineCXXStandardLibrary,
83                           "c++-stdlib-inlining",
84                           /*Default=*/true);
85 }
86 
87 bool AnalyzerOptions::mayInlineTemplateFunctions() {
88   return getBooleanOption(InlineTemplateFunctions,
89                           "c++-template-inlining",
90                           /*Default=*/true);
91 }
92 
93 bool AnalyzerOptions::mayInlineObjCMethod() {
94   return getBooleanOption(ObjCInliningMode,
95                           "objc-inlining",
96                           /* Default = */ true);
97 }
98 
99 bool AnalyzerOptions::shouldPruneNullReturnPaths() {
100   return getBooleanOption(PruneNullReturnPaths,
101                           "suppress-null-return-paths",
102                           /* Default = */ true);
103 }
104 
105 int AnalyzerOptions::getOptionAsInteger(StringRef Name, int DefaultVal) {
106   llvm::SmallString<10> StrBuf;
107   llvm::raw_svector_ostream OS(StrBuf);
108   OS << DefaultVal;
109 
110   StringRef V(Config.GetOrCreateValue(Name, OS.str()).getValue());
111   int Res = DefaultVal;
112   bool b = V.getAsInteger(10, Res);
113   assert(!b && "analyzer-config option should be numeric");
114   (void) b;
115   return Res;
116 }
117 
118 unsigned AnalyzerOptions::getAlwaysInlineSize() {
119   if (!AlwaysInlineSize.hasValue())
120     AlwaysInlineSize = getOptionAsInteger("ipa-always-inline-size", 3);
121   return AlwaysInlineSize.getValue();
122 }
123 
124 bool AnalyzerOptions::shouldSynthesizeBodies() {
125   return getBooleanOption("faux-bodies", true);
126 }
127