AnalyzerOptions.cpp revision 94bb74cef72a33d77c5d6739abfc0840c781eb8e
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
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::includeTemporaryDtorsInCFG() {
68  if (!IncludeTemporaryDtorsInCFG.hasValue())
69    const_cast<llvm::Optional<bool> &>(IncludeTemporaryDtorsInCFG) =
70      getBooleanOption("cfg-temporary-dtors", /*Default=*/false);
71
72  return *IncludeTemporaryDtorsInCFG;
73}
74
75bool AnalyzerOptions::mayInlineCXXStandardLibrary() {
76  if (!InlineCXXStandardLibrary.hasValue())
77    const_cast<llvm::Optional<bool> &>(InlineCXXStandardLibrary) =
78      getBooleanOption("c++-stdlib-inlining", /*Default=*/true);
79
80  return *InlineCXXStandardLibrary;
81}
82
83bool AnalyzerOptions::mayInlineTemplateFunctions() {
84  if (!InlineTemplateFunctions.hasValue())
85    const_cast<llvm::Optional<bool> &>(InlineTemplateFunctions) =
86      getBooleanOption("c++-template-inlining", /*Default=*/true);
87
88  return *InlineTemplateFunctions;
89}
90
91bool AnalyzerOptions::mayInlineObjCMethod() {
92  if (!ObjCInliningMode.hasValue())
93    const_cast<llvm::Optional<bool> &>(ObjCInliningMode) =
94      getBooleanOption("objc-inlining", /*Default=*/true);
95
96  return *ObjCInliningMode;
97}
98
99bool AnalyzerOptions::shouldPruneNullReturnPaths() {
100  if (!PruneNullReturnPaths.hasValue())
101    const_cast<llvm::Optional<bool> &>(PruneNullReturnPaths) =
102      getBooleanOption("suppress-null-return-paths", /*Default=*/true);
103
104  return *PruneNullReturnPaths;
105}
106
107int AnalyzerOptions::getOptionAsInteger(StringRef Name, int DefaultVal) {
108  llvm::SmallString<10> StrBuf;
109  llvm::raw_svector_ostream OS(StrBuf);
110  OS << DefaultVal;
111
112  StringRef V(Config.GetOrCreateValue(Name, OS.str()).getValue());
113  int Res = DefaultVal;
114  bool b = V.getAsInteger(10, Res);
115  assert(!b && "analyzer-config option should be numeric");
116  return Res;
117}
118
119unsigned AnalyzerOptions::getAlwaysInlineSize() {
120  if (!AlwaysInlineSize.hasValue())
121    AlwaysInlineSize = getOptionAsInteger("ipa-always-inline-size", 3);
122  return AlwaysInlineSize.getValue();
123}
124
125bool AnalyzerOptions::shouldSynthesizeBodies() {
126  return getBooleanOption("faux-bodies", true);
127}
128