Browse Source

cmStandardLevelResolver: Factor out public representation of level

Brad King 2 years ago
parent
commit
99fa01d3fa
3 changed files with 37 additions and 17 deletions
  1. 1 0
      Source/CMakeLists.txt
  2. 21 0
      Source/cmStandardLevel.h
  3. 15 17
      Source/cmStandardLevelResolver.cxx

+ 1 - 0
Source/CMakeLists.txt

@@ -417,6 +417,7 @@ add_library(
   cmSourceFileLocationKind.h
   cmSourceGroup.cxx
   cmSourceGroup.h
+  cmStandardLevel.h
   cmStandardLevelResolver.cxx
   cmStandardLevelResolver.h
   cmState.cxx

+ 21 - 0
Source/cmStandardLevel.h

@@ -0,0 +1,21 @@
+/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
+   file Copyright.txt or https://cmake.org/licensing for details.  */
+#pragma once
+
+#include "cmConfigure.h" // IWYU pragma: keep
+
+class cmStandardLevel
+{
+  size_t index_;
+
+public:
+  cmStandardLevel(size_t index)
+    : index_(index)
+  {
+  }
+  size_t Index() const { return index_; }
+  friend bool operator<(cmStandardLevel const& l, cmStandardLevel const& r)
+  {
+    return l.index_ < r.index_;
+  }
+};

+ 15 - 17
Source/cmStandardLevelResolver.cxx

@@ -13,6 +13,7 @@
 #include <vector>
 
 #include <cm/iterator>
+#include <cm/optional>
 #include <cm/string_view>
 #include <cmext/algorithm>
 #include <cmext/string_view>
@@ -25,6 +26,7 @@
 #include "cmMakefile.h"
 #include "cmMessageType.h"
 #include "cmPolicies.h"
+#include "cmStandardLevel.h"
 #include "cmStringAlgorithms.h"
 #include "cmTarget.h"
 #include "cmValue.h"
@@ -46,12 +48,6 @@ const char* const HIP_FEATURES[] = { nullptr FOR_EACH_HIP_FEATURE(
   FEATURE_STRING) };
 #undef FEATURE_STRING
 
-struct StandardNeeded
-{
-  int index;
-  int value;
-};
-
 int ParseStd(std::string const& level)
 {
   try {
@@ -352,7 +348,8 @@ struct StandardLevelComputer
       newRequiredStandard.clear();
     }
 
-    auto needed = this->HighestStandardNeeded(makefile, feature);
+    cm::optional<cmStandardLevel> needed =
+      this->HighestStandardNeeded(makefile, feature);
 
     cmValue existingStandard = currentLangStandardValue;
     if (!existingStandard) {
@@ -382,12 +379,12 @@ struct StandardLevelComputer
       }
     }
 
-    if (needed.index != -1) {
+    if (needed) {
       // Ensure the C++ language level is high enough to support
       // the needed C++ features.
       if (existingLevelIter == cm::cend(this->Levels) ||
-          existingLevelIter < this->Levels.begin() + needed.index) {
-        newRequiredStandard = this->LevelsAsStrings[needed.index];
+          existingLevelIter < this->Levels.begin() + needed->Index()) {
+        newRequiredStandard = this->LevelsAsStrings[needed->Index()];
       }
     }
 
@@ -439,23 +436,24 @@ struct StandardLevelComputer
       return false;
     }
 
-    auto needed = this->HighestStandardNeeded(makefile, feature);
+    cm::optional<cmStandardLevel> needed =
+      this->HighestStandardNeeded(makefile, feature);
 
-    return (needed.index == -1) ||
-      (this->Levels.begin() + needed.index) <= existingLevelIter;
+    return !needed ||
+      (this->Levels.begin() + needed->Index()) <= existingLevelIter;
   }
 
-  StandardNeeded HighestStandardNeeded(cmMakefile* makefile,
-                                       std::string const& feature) const
+  cm::optional<cmStandardLevel> HighestStandardNeeded(
+    cmMakefile* makefile, std::string const& feature) const
   {
     std::string prefix = cmStrCat("CMAKE_", this->Language);
-    StandardNeeded maxLevel = { -1, -1 };
+    cm::optional<cmStandardLevel> maxLevel;
     for (size_t i = 0; i < this->Levels.size(); ++i) {
       if (cmValue prop = makefile->GetDefinition(
             cmStrCat(prefix, this->LevelsAsStrings[i], "_COMPILE_FEATURES"))) {
         cmList props{ *prop };
         if (cm::contains(props, feature)) {
-          maxLevel = { static_cast<int>(i), this->Levels[i] };
+          maxLevel = cmStandardLevel(i);
         }
       }
     }