[analyzer][NFC] Fix inconsistencies in AnalyzerOptions

I'm in the process of refactoring AnalyzerOptions. The main motivation behind
here is to emit warnings if an invalid -analyzer-config option is given from the
command line, and be able to list them all.

This first NFC patch contains small modifications to make AnalyzerOptions.cpp a
little more consistent.

Differential Revision: https://reviews.llvm.org/D53274

llvm-svn: 344870
This commit is contained in:
Kristof Umann 2018-10-21 18:19:32 +00:00
parent d9e2e383a9
commit ca8a05ac34
3 changed files with 65 additions and 62 deletions

View File

@ -85,7 +85,7 @@ enum CXXInlineableMemberKind {
// Uninitialized = 0,
/// A dummy mode in which no C++ inlining is enabled.
CIMK_None = 1,
CIMK_None,
/// Refers to regular member function and operator calls.
CIMK_MemberFunctions,
@ -102,8 +102,6 @@ enum CXXInlineableMemberKind {
/// Describes the different modes of inter-procedural analysis.
enum IPAKind {
IPAK_NotSet = 0,
/// Perform only intra-procedural analysis.
IPAK_None = 1,
@ -188,16 +186,11 @@ public:
UnexploredFirstQueue,
UnexploredFirstLocationQueue,
BFSBlockDFSContents,
NotSet
};
private:
ExplorationStrategyKind ExplorationStrategy = ExplorationStrategyKind::NotSet;
/// Describes the kinds for high-level analyzer mode.
enum UserModeKind {
UMK_NotSet = 0,
/// Perform shallow but fast analyzes.
UMK_Shallow = 1,
@ -205,16 +198,18 @@ private:
UMK_Deep = 2
};
llvm::Optional<ExplorationStrategyKind> ExplorationStrategy;
/// Controls the high-level analyzer mode, which influences the default
/// settings for some of the lower-level config options (such as IPAMode).
/// \sa getUserMode
UserModeKind UserMode = UMK_NotSet;
llvm::Optional<UserModeKind> UserMode;
/// Controls the mode of inter-procedural analysis.
IPAKind IPAMode = IPAK_NotSet;
llvm::Optional<IPAKind> IPAMode;
/// Controls which C++ member functions will be considered for inlining.
CXXInlineableMemberKind CXXMemberInliningMode;
llvm::Optional<CXXInlineableMemberKind> CXXMemberInliningMode;
/// \sa includeImplicitDtorsInCFG
Optional<bool> IncludeImplicitDtorsInCFG;
@ -420,6 +415,12 @@ public:
const ento::CheckerBase *C = nullptr,
bool SearchInParents = false);
unsigned getOptionAsUInt(Optional<unsigned> &V, StringRef Name,
unsigned DefaultVal,
const ento::CheckerBase *C = nullptr,
bool SearchInParents = false);
/// Query an option's string value.
///
/// If an option value is not provided, returns the given \p DefaultVal.
@ -437,6 +438,11 @@ public:
const ento::CheckerBase *C = nullptr,
bool SearchInParents = false);
StringRef getOptionAsString(Optional<StringRef> &V, StringRef Name,
StringRef DefaultVal,
const ento::CheckerBase *C = nullptr,
bool SearchInParents = false);
/// Retrieves and sets the UserMode. This is a high-level option,
/// which is used to set other low-level options. It is not accessible
/// outside of AnalyzerOptions.

View File

@ -50,27 +50,24 @@ AnalyzerOptions::getRegisteredCheckers(bool IncludeExperimental /* = false */) {
}
AnalyzerOptions::UserModeKind AnalyzerOptions::getUserMode() {
if (UserMode == UMK_NotSet) {
StringRef ModeStr =
Config.insert(std::make_pair("mode", "deep")).first->second;
UserMode = llvm::StringSwitch<UserModeKind>(ModeStr)
if (!UserMode.hasValue()) {
StringRef ModeStr = getOptionAsString("mode", "deep");
UserMode = llvm::StringSwitch<llvm::Optional<UserModeKind>>(ModeStr)
.Case("shallow", UMK_Shallow)
.Case("deep", UMK_Deep)
.Default(UMK_NotSet);
assert(UserMode != UMK_NotSet && "User mode is invalid.");
.Default(None);
assert(UserMode.getValue() && "User mode is invalid.");
}
return UserMode;
return UserMode.getValue();
}
AnalyzerOptions::ExplorationStrategyKind
AnalyzerOptions::getExplorationStrategy() {
if (ExplorationStrategy == ExplorationStrategyKind::NotSet) {
StringRef StratStr =
Config
.insert(std::make_pair("exploration_strategy", "unexplored_first_queue"))
.first->second;
if (!ExplorationStrategy.hasValue()) {
StringRef StratStr = getOptionAsString("exploration_strategy",
"unexplored_first_queue");
ExplorationStrategy =
llvm::StringSwitch<ExplorationStrategyKind>(StratStr)
llvm::StringSwitch<llvm::Optional<ExplorationStrategyKind>>(StratStr)
.Case("dfs", ExplorationStrategyKind::DFS)
.Case("bfs", ExplorationStrategyKind::BFS)
.Case("unexplored_first",
@ -81,15 +78,15 @@ AnalyzerOptions::getExplorationStrategy() {
ExplorationStrategyKind::UnexploredFirstLocationQueue)
.Case("bfs_block_dfs_contents",
ExplorationStrategyKind::BFSBlockDFSContents)
.Default(ExplorationStrategyKind::NotSet);
assert(ExplorationStrategy != ExplorationStrategyKind::NotSet &&
.Default(None);
assert(ExplorationStrategy.hasValue() &&
"User mode is invalid.");
}
return ExplorationStrategy;
return ExplorationStrategy.getValue();
}
IPAKind AnalyzerOptions::getIPAMode() {
if (IPAMode == IPAK_NotSet) {
if (!IPAMode.hasValue()) {
// Use the User Mode to set the default IPA value.
// Note, we have to add the string to the Config map for the ConfigDumper
// checker to function properly.
@ -102,22 +99,18 @@ IPAKind AnalyzerOptions::getIPAMode() {
assert(DefaultIPA);
// Lookup the ipa configuration option, use the default from User Mode.
StringRef ModeStr =
Config.insert(std::make_pair("ipa", DefaultIPA)).first->second;
IPAKind IPAConfig = llvm::StringSwitch<IPAKind>(ModeStr)
StringRef ModeStr = getOptionAsString("ipa", DefaultIPA);
IPAMode = llvm::StringSwitch<llvm::Optional<IPAKind>>(ModeStr)
.Case("none", IPAK_None)
.Case("basic-inlining", IPAK_BasicInlining)
.Case("inlining", IPAK_Inlining)
.Case("dynamic", IPAK_DynamicDispatch)
.Case("dynamic-bifurcate", IPAK_DynamicDispatchBifurcate)
.Default(IPAK_NotSet);
assert(IPAConfig != IPAK_NotSet && "IPA Mode is invalid.");
// Set the member variable.
IPAMode = IPAConfig;
.Default(None);
assert(IPAMode.hasValue() && "IPA Mode is invalid.");
}
return IPAMode;
return IPAMode.getValue();
}
bool
@ -126,29 +119,21 @@ AnalyzerOptions::mayInlineCXXMemberFunction(CXXInlineableMemberKind K) {
return false;
if (!CXXMemberInliningMode) {
static const char *ModeKey = "c++-inlining";
StringRef ModeStr = getOptionAsString("c++-inlining", "destructors");
StringRef ModeStr =
Config.insert(std::make_pair(ModeKey, "destructors")).first->second;
CXXInlineableMemberKind &MutableMode =
const_cast<CXXInlineableMemberKind &>(CXXMemberInliningMode);
MutableMode = llvm::StringSwitch<CXXInlineableMemberKind>(ModeStr)
CXXMemberInliningMode =
llvm::StringSwitch<llvm::Optional<CXXInlineableMemberKind>>(ModeStr)
.Case("constructors", CIMK_Constructors)
.Case("destructors", CIMK_Destructors)
.Case("none", CIMK_None)
.Case("methods", CIMK_MemberFunctions)
.Default(CXXInlineableMemberKind());
.Case("none", CIMK_None)
.Default(None);
if (!MutableMode) {
// FIXME: We should emit a warning here about an unknown inlining kind,
// but the AnalyzerOptions doesn't have access to a diagnostic engine.
MutableMode = CIMK_None;
}
assert(CXXMemberInliningMode.hasValue() &&
"Invalid c++ member function inlining mode.");
}
return CXXMemberInliningMode >= K;
return *CXXMemberInliningMode >= K;
}
static StringRef toString(bool b) { return b ? "true" : "false"; }
@ -183,7 +168,7 @@ bool AnalyzerOptions::getBooleanOption(StringRef Name, bool DefaultVal,
StringRef V =
C ? getCheckerOption(C->getTagDescription(), Name, Default,
SearchInParents)
: StringRef(Config.insert(std::make_pair(Name, Default)).first->second);
: getOptionAsString(Name, Default);
return llvm::StringSwitch<bool>(V)
.Case("true", true)
.Case("false", false)
@ -338,8 +323,7 @@ int AnalyzerOptions::getOptionAsInteger(StringRef Name, int DefaultVal,
StringRef V = C ? getCheckerOption(C->getTagDescription(), Name, OS.str(),
SearchInParents)
: StringRef(Config.insert(std::make_pair(Name, OS.str()))
.first->second);
: getOptionAsString(Name, OS.str());
int Res = DefaultVal;
bool b = V.getAsInteger(10, Res);
@ -348,6 +332,15 @@ int AnalyzerOptions::getOptionAsInteger(StringRef Name, int DefaultVal,
return Res;
}
unsigned AnalyzerOptions::getOptionAsUInt(Optional<unsigned> &V, StringRef Name,
unsigned DefaultVal,
const CheckerBase *C,
bool SearchInParents) {
if (!V.hasValue())
V = getOptionAsInteger(Name, DefaultVal, C, SearchInParents);
return V.getValue();
}
StringRef AnalyzerOptions::getOptionAsString(StringRef Name,
StringRef DefaultVal,
const CheckerBase *C,
@ -358,6 +351,16 @@ StringRef AnalyzerOptions::getOptionAsString(StringRef Name,
Config.insert(std::make_pair(Name, DefaultVal)).first->second);
}
StringRef AnalyzerOptions::getOptionAsString(Optional<StringRef> &V,
StringRef Name,
StringRef DefaultVal,
const ento::CheckerBase *C,
bool SearchInParents) {
if (!V.hasValue())
V = getOptionAsString(Name, DefaultVal, C, SearchInParents);
return V.getValue();
}
unsigned AnalyzerOptions::getAlwaysInlineSize() {
if (!AlwaysInlineSize.hasValue())
AlwaysInlineSize = getOptionAsInteger("ipa-always-inline-size", 3);
@ -369,8 +372,6 @@ unsigned AnalyzerOptions::getMaxInlinableSize() {
int DefaultValue = 0;
UserModeKind HighLevelMode = getUserMode();
switch (HighLevelMode) {
default:
llvm_unreachable("Invalid mode.");
case UMK_Shallow:
DefaultValue = 4;
break;
@ -414,8 +415,6 @@ unsigned AnalyzerOptions::getMaxNodesPerTopLevelFunction() {
int DefaultValue = 0;
UserModeKind HighLevelMode = getUserMode();
switch (HighLevelMode) {
default:
llvm_unreachable("Invalid mode.");
case UMK_Shallow:
DefaultValue = 75000;
break;

View File

@ -68,8 +68,6 @@ static std::unique_ptr<WorkList> generateWorkList(AnalyzerOptions &Opts,
return WorkList::makeUnexploredFirstPriorityQueue();
case AnalyzerOptions::ExplorationStrategyKind::UnexploredFirstLocationQueue:
return WorkList::makeUnexploredFirstPriorityLocationQueue();
default:
llvm_unreachable("Unexpected case");
}
}