AnalyzerOptions.cpp revision 7229d0011766c174beffe6a846d78f448f845b39
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
18using namespace clang;
19using namespace llvm;
20
21bool
22AnalyzerOptions::mayInlineCXXMemberFunction(CXXInlineableMemberKind K) const {
23  if (IPAMode < Inlining)
24    return false;
25
26  if (!CXXMemberInliningMode) {
27    static const char *ModeKey = "c++-inlining";
28    std::string ModeStr = Config.lookup(ModeKey);
29
30    CXXInlineableMemberKind &MutableMode =
31      const_cast<CXXInlineableMemberKind &>(CXXMemberInliningMode);
32
33    MutableMode = llvm::StringSwitch<CXXInlineableMemberKind>(ModeStr)
34      .Case("", CIMK_MemberFunctions)
35      .Case("constructors", CIMK_Constructors)
36      .Case("destructors", CIMK_Destructors)
37      .Case("none", CIMK_None)
38      .Case("methods", CIMK_MemberFunctions)
39      .Default(CXXInlineableMemberKind());
40
41    if (!MutableMode) {
42      // FIXME: We should emit a warning here about an unknown inlining kind,
43      // but the AnalyzerOptions doesn't have access to a diagnostic engine.
44      MutableMode = CIMK_None;
45    }
46  }
47
48  return CXXMemberInliningMode >= K;
49}
50
51bool AnalyzerOptions::getBooleanOption(StringRef Name, bool DefaultVal) const {
52  // FIXME: We should emit a warning here if the value is something other than
53  // "true", "false", or the empty string (meaning the default value),
54  // but the AnalyzerOptions doesn't have access to a diagnostic engine.
55  return llvm::StringSwitch<bool>(Config.lookup(Name))
56    .Case("true", true)
57    .Case("false", false)
58    .Default(DefaultVal);
59}
60
61bool AnalyzerOptions::includeTemporaryDtorsInCFG() const {
62  if (!IncludeTemporaryDtorsInCFG.hasValue())
63    const_cast<llvm::Optional<bool> &>(IncludeTemporaryDtorsInCFG) =
64      getBooleanOption("cfg-temporary-dtors", /*Default=*/false);
65
66  return *IncludeTemporaryDtorsInCFG;
67}
68
69bool AnalyzerOptions::mayInlineCXXStandardLibrary() const {
70  if (!InlineCXXStandardLibrary.hasValue())
71    const_cast<llvm::Optional<bool> &>(InlineCXXStandardLibrary) =
72      getBooleanOption("c++-stdlib-inlining", /*Default=*/false);
73
74  return *InlineCXXStandardLibrary;
75}
76
77bool AnalyzerOptions::mayInlineTemplateFunctions() const {
78  if (!InlineTemplateFunctions.hasValue())
79    const_cast<llvm::Optional<bool> &>(InlineTemplateFunctions) =
80      getBooleanOption("c++-template-inlining", /*Default=*/true);
81
82  return *InlineTemplateFunctions;
83}
84
85int AnalyzerOptions::getOptionAsInteger(StringRef Name, int DefaultVal) const {
86  std::string OptStr = Config.lookup(Name);
87  if (OptStr.empty())
88    return DefaultVal;
89
90  int Res = DefaultVal;
91  assert(StringRef(OptStr).getAsInteger(10, Res) == false &&
92         "analyzer-config option should be numeric.");
93
94  return Res;
95}
96
97unsigned AnalyzerOptions::getAlwaysInlineSize() const {
98  if (!AlwaysInlineSize.hasValue()) {
99    unsigned DefaultSize = 3;
100    Optional<unsigned> &MutableOption =
101      const_cast<Optional<unsigned> &>(AlwaysInlineSize);
102    MutableOption = getOptionAsInteger("ipa-always-inline-size", DefaultSize);
103  }
104
105  return AlwaysInlineSize.getValue();
106}
107