浏览代码

cmProp: cm::string_view cast operator must be explicit

To avoid ambiguity on std::string assigment between the following two
cmProp cast operators:
* operator const std::string&() const noexcept
* operator cm::string_view() const noexcept
Marc Chevrier 4 年之前
父节点
当前提交
79362cf117

+ 2 - 2
Source/CPack/cmCPackGenerator.cxx

@@ -167,7 +167,7 @@ int cmCPackGenerator::PrepareNames()
   }
   cmProp algoSignature = this->GetOption("CPACK_PACKAGE_CHECKSUM");
   if (algoSignature) {
-    if (!cmCryptoHash::New(algoSignature)) {
+    if (!cmCryptoHash::New(*algoSignature)) {
       cmCPackLogger(cmCPackLog::LOG_ERROR,
                     "Cannot recognize algorithm: " << algoSignature
                                                    << std::endl);
@@ -1129,7 +1129,7 @@ int cmCPackGenerator::DoPackage()
 
   /* Prepare checksum algorithm*/
   cmProp algo = this->GetOption("CPACK_PACKAGE_CHECKSUM");
-  std::unique_ptr<cmCryptoHash> crypto = cmCryptoHash::New(algo);
+  std::unique_ptr<cmCryptoHash> crypto = cmCryptoHash::New(*algo);
 
   /*
    * Copy the generated packages to final destination

+ 3 - 3
Source/cmCoreTryCompile.cxx

@@ -606,7 +606,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
       std::string langFlags = "CMAKE_" + li + "_FLAGS";
       cmProp flags = this->Makefile->GetDefinition(langFlags);
       fprintf(fout, "set(CMAKE_%s_FLAGS %s)\n", li.c_str(),
-              cmOutputConverter::EscapeForCMake(flags).c_str());
+              cmOutputConverter::EscapeForCMake(*flags).c_str());
       fprintf(fout,
               "set(CMAKE_%s_FLAGS \"${CMAKE_%s_FLAGS}"
               " ${COMPILE_DEFINITIONS}\")\n",
@@ -645,7 +645,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
             cmStrCat("CMAKE_", li, "_FLAGS_", cfg);
           cmProp flagsCfg = this->Makefile->GetDefinition(langFlagsCfg);
           fprintf(fout, "set(%s %s)\n", langFlagsCfg.c_str(),
-                  cmOutputConverter::EscapeForCMake(flagsCfg).c_str());
+                  cmOutputConverter::EscapeForCMake(*flagsCfg).c_str());
         }
       } break;
     }
@@ -678,7 +678,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
           cmProp exeLinkFlags =
             this->Makefile->GetDefinition("CMAKE_EXE_LINKER_FLAGS");
           fprintf(fout, "set(CMAKE_EXE_LINKER_FLAGS %s)\n",
-                  cmOutputConverter::EscapeForCMake(exeLinkFlags).c_str());
+                  cmOutputConverter::EscapeForCMake(*exeLinkFlags).c_str());
         }
         break;
     }

+ 4 - 0
Source/cmMakefile.h

@@ -286,6 +286,10 @@ public:
    * can be used in CMake to refer to lists, directories, etc.
    */
   void AddDefinition(const std::string& name, cm::string_view value);
+  void AddDefinition(const std::string& name, cmProp value)
+  {
+    this->AddDefinition(name, *value);
+  }
   /**
    * Add bool variable definition to the build.
    */

+ 4 - 1
Source/cmProperty.h

@@ -66,7 +66,10 @@ public:
 
   explicit operator bool() const noexcept { return this->Value != nullptr; }
   operator const std::string&() const noexcept { return this->operator*(); }
-  operator cm::string_view() const noexcept { return this->operator*(); }
+  explicit operator cm::string_view() const noexcept
+  {
+    return this->operator*();
+  }
 
   /**
    * Does the value indicate a true or ON value?

+ 2 - 1
Source/cmSeparateArgumentsCommand.cxx

@@ -4,6 +4,7 @@
 
 #include <algorithm>
 
+#include <cm/string_view>
 #include <cmext/string_view>
 
 #include "cmArgumentParser.h"
@@ -81,7 +82,7 @@ bool cmSeparateArgumentsCommand(std::vector<std::string> const& args,
   }
 
   if (unparsedArguments.empty()) {
-    status.GetMakefile().AddDefinition(var, {});
+    status.GetMakefile().AddDefinition(var, cm::string_view{});
     return true;
   }
 

+ 24 - 2
Source/cmStringAlgorithms.h

@@ -94,6 +94,13 @@ std::vector<std::string> cmTokenize(cm::string_view str, cm::string_view sep);
  */
 void cmExpandList(cm::string_view arg, std::vector<std::string>& argsOut,
                   bool emptyArgs = false);
+inline void cmExpandList(cmProp arg, std::vector<std::string>& argsOut,
+                         bool emptyArgs = false)
+{
+  if (arg) {
+    cmExpandList(*arg, argsOut, emptyArgs);
+  }
+}
 
 /**
  * Expand out any arguments in the string range [@a first, @a last) that have
@@ -115,6 +122,14 @@ void cmExpandLists(InputIt first, InputIt last,
  */
 std::vector<std::string> cmExpandedList(cm::string_view arg,
                                         bool emptyArgs = false);
+inline std::vector<std::string> cmExpandedList(cmProp arg,
+                                               bool emptyArgs = false)
+{
+  if (!arg) {
+    return {};
+  }
+  return cmExpandedList(*arg, emptyArgs);
+}
 
 /**
  * Same as cmExpandList but a new vector is created containing the expanded
@@ -217,6 +232,13 @@ inline bool cmIsInternallyOn(const char* val)
   }
   return cmIsInternallyOn(cm::string_view(val));
 }
+inline bool cmIsInternallyOn(cmProp val)
+{
+  if (!val) {
+    return false;
+  }
+  return cmIsInternallyOn(*val);
+}
 
 /** Check for non-empty Property/Variable value.  */
 inline bool cmNonempty(cm::string_view val)
@@ -297,7 +319,7 @@ inline bool cmHasPrefix(cm::string_view str, cmProp prefix)
     return false;
   }
 
-  return str.compare(0, prefix->size(), prefix) == 0;
+  return str.compare(0, prefix->size(), *prefix) == 0;
 }
 
 /** Returns true if string @a str starts with string @a prefix.  */
@@ -328,7 +350,7 @@ inline bool cmHasSuffix(cm::string_view str, cmProp suffix)
   }
 
   return str.size() >= suffix->size() &&
-    str.compare(str.size() - suffix->size(), suffix->size(), suffix) == 0;
+    str.compare(str.size() - suffix->size(), suffix->size(), *suffix) == 0;
 }
 
 /** Returns true if string @a str ends with string @a suffix.  */