Index: cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h =================================================================== --- cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h +++ cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h @@ -85,7 +85,7 @@ // 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 @@ /// 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 @@ 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 @@ UMK_Deep = 2 }; + llvm::Optional 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 UserMode; /// Controls the mode of inter-procedural analysis. - IPAKind IPAMode = IPAK_NotSet; + llvm::Optional IPAMode; /// Controls which C++ member functions will be considered for inlining. - CXXInlineableMemberKind CXXMemberInliningMode; + llvm::Optional CXXMemberInliningMode; /// \sa includeImplicitDtorsInCFG Optional IncludeImplicitDtorsInCFG; @@ -420,6 +415,12 @@ const ento::CheckerBase *C = nullptr, bool SearchInParents = false); + + unsigned getOptionAsUInt(Optional &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 @@ const ento::CheckerBase *C = nullptr, bool SearchInParents = false); + StringRef getOptionAsString(Optional &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. Index: cfe/trunk/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp =================================================================== --- cfe/trunk/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp +++ cfe/trunk/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp @@ -50,27 +50,24 @@ } AnalyzerOptions::UserModeKind AnalyzerOptions::getUserMode() { - if (UserMode == UMK_NotSet) { - StringRef ModeStr = - Config.insert(std::make_pair("mode", "deep")).first->second; - UserMode = llvm::StringSwitch(ModeStr) + if (!UserMode.hasValue()) { + StringRef ModeStr = getOptionAsString("mode", "deep"); + UserMode = llvm::StringSwitch>(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(StratStr) + llvm::StringSwitch>(StratStr) .Case("dfs", ExplorationStrategyKind::DFS) .Case("bfs", ExplorationStrategyKind::BFS) .Case("unexplored_first", @@ -81,15 +78,15 @@ 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 @@ 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(ModeStr) + StringRef ModeStr = getOptionAsString("ipa", DefaultIPA); + IPAMode = llvm::StringSwitch>(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 @@ return false; if (!CXXMemberInliningMode) { - static const char *ModeKey = "c++-inlining"; - - StringRef ModeStr = - Config.insert(std::make_pair(ModeKey, "destructors")).first->second; + StringRef ModeStr = getOptionAsString("c++-inlining", "destructors"); - CXXInlineableMemberKind &MutableMode = - const_cast(CXXMemberInliningMode); - - MutableMode = llvm::StringSwitch(ModeStr) + CXXMemberInliningMode = + llvm::StringSwitch>(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 @@ 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(V) .Case("true", true) .Case("false", false) @@ -338,8 +323,7 @@ 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 @@ return Res; } +unsigned AnalyzerOptions::getOptionAsUInt(Optional &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 @@ Config.insert(std::make_pair(Name, DefaultVal)).first->second); } +StringRef AnalyzerOptions::getOptionAsString(Optional &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 @@ int DefaultValue = 0; UserModeKind HighLevelMode = getUserMode(); switch (HighLevelMode) { - default: - llvm_unreachable("Invalid mode."); case UMK_Shallow: DefaultValue = 4; break; @@ -414,8 +415,6 @@ int DefaultValue = 0; UserModeKind HighLevelMode = getUserMode(); switch (HighLevelMode) { - default: - llvm_unreachable("Invalid mode."); case UMK_Shallow: DefaultValue = 75000; break; Index: cfe/trunk/lib/StaticAnalyzer/Core/CoreEngine.cpp =================================================================== --- cfe/trunk/lib/StaticAnalyzer/Core/CoreEngine.cpp +++ cfe/trunk/lib/StaticAnalyzer/Core/CoreEngine.cpp @@ -68,8 +68,6 @@ return WorkList::makeUnexploredFirstPriorityQueue(); case AnalyzerOptions::ExplorationStrategyKind::UnexploredFirstLocationQueue: return WorkList::makeUnexploredFirstPriorityLocationQueue(); - default: - llvm_unreachable("Unexpected case"); } }