Szelethus created this revision.
Szelethus added reviewers: NoQ, george.karpenkov, xazax.hun, MTC, rnkovacs.
Herald added subscribers: cfe-commits, donat.nagy, mikhail.ramalho, a.sidorin, 
szepet, whisperity.

I was a little unsure about the title, but it is what it is.

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.


Repository:
  rC Clang

https://reviews.llvm.org/D53274

Files:
  include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
  lib/StaticAnalyzer/Core/AnalyzerOptions.cpp
  lib/StaticAnalyzer/Core/CoreEngine.cpp

Index: lib/StaticAnalyzer/Core/CoreEngine.cpp
===================================================================
--- lib/StaticAnalyzer/Core/CoreEngine.cpp
+++ lib/StaticAnalyzer/Core/CoreEngine.cpp
@@ -68,8 +68,6 @@
       return WorkList::makeUnexploredFirstPriorityQueue();
     case AnalyzerOptions::ExplorationStrategyKind::UnexploredFirstLocationQueue:
       return WorkList::makeUnexploredFirstPriorityLocationQueue();
-    default:
-      llvm_unreachable("Unexpected case");
   }
 }
 
Index: lib/StaticAnalyzer/Core/AnalyzerOptions.cpp
===================================================================
--- lib/StaticAnalyzer/Core/AnalyzerOptions.cpp
+++ 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<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 @@
                   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,53 +99,40 @@
     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
 AnalyzerOptions::mayInlineCXXMemberFunction(CXXInlineableMemberKind K) {
   if (getIPAMode() < IPAK_Inlining)
     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<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());
+      .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 +167,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<bool>(V)
       .Case("true", true)
       .Case("false", false)
@@ -338,16 +322,24 @@
 
   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);
   assert(!b && "analyzer-config option should be numeric");
   (void)b;
   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 +350,16 @@
                  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 +371,6 @@
     int DefaultValue = 0;
     UserModeKind HighLevelMode = getUserMode();
     switch (HighLevelMode) {
-      default:
-        llvm_unreachable("Invalid mode.");
       case UMK_Shallow:
         DefaultValue = 4;
         break;
@@ -414,8 +414,6 @@
     int DefaultValue = 0;
     UserModeKind HighLevelMode = getUserMode();
     switch (HighLevelMode) {
-      default:
-        llvm_unreachable("Invalid mode.");
       case UMK_Shallow:
         DefaultValue = 75000;
         break;
@@ -458,6 +456,7 @@
   return UnrollLoops.getValue();
 }
 
+
 bool AnalyzerOptions::shouldDisplayNotesAsEvents() {
   if (!DisplayNotesAsEvents.hasValue())
     DisplayNotesAsEvents =
Index: include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
===================================================================
--- include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
+++ include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
@@ -84,9 +84,6 @@
 enum CXXInlineableMemberKind {
   // Uninitialized = 0,
 
-  /// A dummy mode in which no C++ inlining is enabled.
-  CIMK_None = 1,
-
   /// Refers to regular member function and operator calls.
   CIMK_MemberFunctions,
 
@@ -102,8 +99,6 @@
 
 /// Describes the different modes of inter-procedural analysis.
 enum IPAKind {
-  IPAK_NotSet = 0,
-
   /// Perform only intra-procedural analysis.
   IPAK_None = 1,
 
@@ -188,33 +183,30 @@
     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,
 
     /// Perform deep analyzes.
     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 +412,12 @@
                          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 +435,11 @@
                               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.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to