r344870 - [analyzer][NFC] Fix inconsistencies in AnalyzerOptions

Kristof Umann via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 21 11:19:33 PDT 2018


Author: szelethus
Date: Sun Oct 21 11:19:32 2018
New Revision: 344870

URL: http://llvm.org/viewvc/llvm-project?rev=344870&view=rev
Log:
[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

Modified:
    cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
    cfe/trunk/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp
    cfe/trunk/lib/StaticAnalyzer/Core/CoreEngine.cpp

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h?rev=344870&r1=344869&r2=344870&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h Sun Oct 21 11:19:32 2018
@@ -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.

Modified: cfe/trunk/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp?rev=344870&r1=344869&r2=344870&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp Sun Oct 21 11:19:32 2018
@@ -50,27 +50,24 @@ AnalyzerOptions::getRegisteredCheckers(b
 }
 
 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::mayInlineCXXMemberFunct
     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());
+      .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(S
   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 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(
   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::getOptionAsSt
                  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::getMaxInlinabl
     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::getMaxNodesPer
     int DefaultValue = 0;
     UserModeKind HighLevelMode = getUserMode();
     switch (HighLevelMode) {
-      default:
-        llvm_unreachable("Invalid mode.");
       case UMK_Shallow:
         DefaultValue = 75000;
         break;

Modified: cfe/trunk/lib/StaticAnalyzer/Core/CoreEngine.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/CoreEngine.cpp?rev=344870&r1=344869&r2=344870&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/CoreEngine.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/CoreEngine.cpp Sun Oct 21 11:19:32 2018
@@ -68,8 +68,6 @@ static std::unique_ptr<WorkList> generat
       return WorkList::makeUnexploredFirstPriorityQueue();
     case AnalyzerOptions::ExplorationStrategyKind::UnexploredFirstLocationQueue:
       return WorkList::makeUnexploredFirstPriorityLocationQueue();
-    default:
-      llvm_unreachable("Unexpected case");
   }
 }
 




More information about the cfe-commits mailing list