Browse Source

SetProperty: suppress raw pointer usage

Marc Chevrier 2 years ago
parent
commit
aa5fed5052

+ 1 - 1
Source/cmCMakeLanguageCommand.cxx

@@ -303,7 +303,7 @@ bool cmCMakeLanguageCommandSET_DEPENDENCY_PROVIDER(
   state->SetDependencyProvider({ parsedArgs.Command, methods });
   state->SetGlobalProperty(
     fcmasProperty,
-    supportsFetchContentMakeAvailableSerial ? parsedArgs.Command.c_str() : "");
+    supportsFetchContentMakeAvailableSerial ? parsedArgs.Command : "");
 
   return true;
 }

+ 5 - 1
Source/cmCPluginAPI.cxx

@@ -615,7 +615,11 @@ static void CCONV cmSourceFileSetProperty(void* arg, const char* prop,
 {
   cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
   if (cmSourceFile* rsf = sf->RealSourceFile) {
-    rsf->SetProperty(prop, value);
+    if (value == nullptr) {
+      rsf->SetProperty(prop, nullptr);
+    } else {
+      rsf->SetProperty(prop, value);
+    }
   } else if (prop) {
     if (!value) {
       value = "NOTFOUND";

+ 25 - 12
Source/cmCacheManager.cxx

@@ -84,7 +84,7 @@ bool cmCacheManager::LoadCache(const std::string& path, bool internal,
         continue;
       }
     }
-    e.SetProperty("HELPSTRING", helpString.c_str());
+    e.SetProperty("HELPSTRING", helpString);
     if (cmState::ParseCacheEntry(realbuffer, entryKey, e.Value, e.Type)) {
       if (excludes.find(entryKey) == excludes.end()) {
         // Load internal values if internal is set.
@@ -102,7 +102,7 @@ bool cmCacheManager::LoadCache(const std::string& path, bool internal,
                                   " loaded from external file.  "
                                   "To change this value edit this file: ",
                                   path, "/CMakeCache.txt");
-            e.SetProperty("HELPSTRING", helpString.c_str());
+            e.SetProperty("HELPSTRING", helpString);
           }
           if (!this->ReadPropertyEntry(entryKey, e)) {
             e.Initialized = true;
@@ -186,11 +186,11 @@ bool cmCacheManager::ReadPropertyEntry(const std::string& entryKey,
       std::string key = entryKey.substr(0, entryKey.size() - plen);
       if (auto* entry = this->GetCacheEntry(key)) {
         // Store this property on its entry.
-        entry->SetProperty(p, e.Value.c_str());
+        entry->SetProperty(p, e.Value);
       } else {
         // Create an entry and store the property.
         CacheEntry& ne = this->Cache[key];
-        ne.SetProperty(p, e.Value.c_str());
+        ne.SetProperty(p, e.Value);
       }
       return true;
     }
@@ -541,10 +541,11 @@ void cmCacheManager::AddCacheEntry(const std::string& key, cmValue value,
       cmSystemTools::ConvertToUnixSlashes(e.Value);
     }
   }
-  e.SetProperty("HELPSTRING",
-                helpString
-                  ? helpString
-                  : "(This variable does not exist and should not be used)");
+  e.SetProperty(
+    "HELPSTRING",
+    helpString ? std::string{ helpString }
+               : std::string{
+                   "(This variable does not exist and should not be used)" });
 }
 
 void cmCacheManager::CacheEntry::SetValue(cmValue value)
@@ -580,12 +581,12 @@ bool cmCacheManager::CacheEntry::GetPropertyAsBool(
 }
 
 void cmCacheManager::CacheEntry::SetProperty(const std::string& prop,
-                                             const char* value)
+                                             const std::string& value)
 {
   if (prop == "TYPE") {
-    this->Type = cmState::StringToCacheEntryType(value ? value : "STRING");
+    this->Type = cmState::StringToCacheEntryType(value);
   } else if (prop == "VALUE") {
-    this->Value = value ? value : "";
+    this->Value = value;
   } else {
     this->Properties.SetProperty(prop, value);
   }
@@ -593,7 +594,19 @@ void cmCacheManager::CacheEntry::SetProperty(const std::string& prop,
 
 void cmCacheManager::CacheEntry::SetProperty(const std::string& p, bool v)
 {
-  this->SetProperty(p, v ? "ON" : "OFF");
+  this->SetProperty(p, v ? std::string{ "ON" } : std::string{ "OFF" });
+}
+
+void cmCacheManager::CacheEntry::SetProperty(const std::string& prop,
+                                             std::nullptr_t)
+{
+  if (prop == "TYPE") {
+    this->Type = cmState::StringToCacheEntryType("STRING");
+  } else if (prop == "VALUE") {
+    this->Value = "";
+  } else {
+    this->Properties.SetProperty(prop, cmValue{ nullptr });
+  }
 }
 
 void cmCacheManager::CacheEntry::AppendProperty(const std::string& prop,

+ 3 - 2
Source/cmCacheManager.h

@@ -39,8 +39,9 @@ class cmCacheManager
     std::vector<std::string> GetPropertyList() const;
     cmValue GetProperty(const std::string& property) const;
     bool GetPropertyAsBool(const std::string& property) const;
-    void SetProperty(const std::string& property, const char* value);
+    void SetProperty(const std::string& property, const std::string& value);
     void SetProperty(const std::string& property, bool value);
+    void SetProperty(const std::string& property, std::nullptr_t);
     void AppendProperty(const std::string& property, const std::string& value,
                         bool asString = false);
 
@@ -127,7 +128,7 @@ public:
                              std::string const& value)
   {
     if (auto* entry = this->GetCacheEntry(key)) {
-      entry->SetProperty(propName, value.c_str());
+      entry->SetProperty(propName, value);
     }
   }
 

+ 5 - 5
Source/cmFindPackageCommand.cxx

@@ -1804,11 +1804,11 @@ void cmFindPackageCommand::AppendToFoundProperty(const bool found)
     notFoundContents.push_back(this->Name);
   }
 
-  this->Makefile->GetState()->SetGlobalProperty(
-    "PACKAGES_FOUND", foundContents.to_string().c_str());
+  this->Makefile->GetState()->SetGlobalProperty("PACKAGES_FOUND",
+                                                foundContents.to_string());
 
-  this->Makefile->GetState()->SetGlobalProperty(
-    "PACKAGES_NOT_FOUND", notFoundContents.to_string().c_str());
+  this->Makefile->GetState()->SetGlobalProperty("PACKAGES_NOT_FOUND",
+                                                notFoundContents.to_string());
 }
 
 void cmFindPackageCommand::AppendSuccessInformation()
@@ -1845,7 +1845,7 @@ void cmFindPackageCommand::AppendSuccessInformation()
       cmStrCat(this->VersionExact ? "==" : ">=", ' ', this->Version);
   }
   this->Makefile->GetState()->SetGlobalProperty(versionInfoPropName,
-                                                versionInfo.c_str());
+                                                versionInfo);
   if (this->Required) {
     std::string const requiredInfoPropName =
       cmStrCat("_CMAKE_", this->Name, "_TYPE");

+ 1 - 1
Source/cmGlobalGenerator.cxx

@@ -268,7 +268,7 @@ void cmGlobalGenerator::ResolveLanguageCompiler(const std::string& lang,
       changeVars += ";";
       changeVars += *cname;
       this->GetCMakeInstance()->GetState()->SetGlobalProperty(
-        "__CMAKE_DELETE_CACHE_CHANGE_VARS_", changeVars.c_str());
+        "__CMAKE_DELETE_CACHE_CHANGE_VARS_", changeVars);
     }
   }
 }

+ 0 - 4
Source/cmMakefile.cxx

@@ -4044,10 +4044,6 @@ int cmMakefile::ConfigureFile(const std::string& infile,
   return res;
 }
 
-void cmMakefile::SetProperty(const std::string& prop, const char* value)
-{
-  this->StateSnapshot.GetDirectory().SetProperty(prop, value, this->Backtrace);
-}
 void cmMakefile::SetProperty(const std::string& prop, cmValue value)
 {
   this->StateSnapshot.GetDirectory().SetProperty(prop, value, this->Backtrace);

+ 5 - 2
Source/cmMakefile.h

@@ -425,7 +425,7 @@ public:
    */
   void SetIncludeRegularExpression(const std::string& regex)
   {
-    this->SetProperty("INCLUDE_REGULAR_EXPRESSION", regex.c_str());
+    this->SetProperty("INCLUDE_REGULAR_EXPRESSION", regex);
   }
   const std::string& GetIncludeRegularExpression() const
   {
@@ -801,8 +801,11 @@ public:
                              std::string& debugBuffer) const;
 
   //! Set/Get a property of this directory
-  void SetProperty(const std::string& prop, const char* value);
   void SetProperty(const std::string& prop, cmValue value);
+  void SetProperty(const std::string& prop, std::nullptr_t)
+  {
+    this->SetProperty(prop, cmValue{ nullptr });
+  }
   void SetProperty(const std::string& prop, const std::string& value)
   {
     this->SetProperty(prop, cmValue(value));

+ 2 - 7
Source/cmPropertyMap.cxx

@@ -10,14 +10,9 @@ void cmPropertyMap::Clear()
   this->Map_.clear();
 }
 
-void cmPropertyMap::SetProperty(const std::string& name, const char* value)
+void cmPropertyMap::SetProperty(const std::string& name, std::nullptr_t)
 {
-  if (!value) {
-    this->Map_.erase(name);
-    return;
-  }
-
-  this->Map_[name] = value;
+  this->Map_.erase(name);
 }
 void cmPropertyMap::SetProperty(const std::string& name, cmValue value)
 {

+ 2 - 1
Source/cmPropertyMap.h

@@ -4,6 +4,7 @@
 
 #include "cmConfigure.h" // IWYU pragma: keep
 
+#include <cstddef>
 #include <string>
 #include <unordered_map>
 #include <utility>
@@ -25,7 +26,7 @@ public:
   // -- Properties
 
   //! Set the property value
-  void SetProperty(const std::string& name, const char* value);
+  void SetProperty(const std::string& name, std::nullptr_t);
   void SetProperty(const std::string& name, cmValue value);
   void SetProperty(const std::string& name, const std::string& value)
   {

+ 1 - 11
Source/cmSourceFile.cxx

@@ -278,8 +278,7 @@ bool cmSourceFile::Matches(cmSourceFileLocation const& loc)
   return this->Location.Matches(loc);
 }
 
-template <typename ValueType>
-void cmSourceFile::StoreProperty(const std::string& prop, ValueType value)
+void cmSourceFile::SetProperty(const std::string& prop, cmValue value)
 {
   if (prop == propINCLUDE_DIRECTORIES) {
     this->IncludeDirectories.clear();
@@ -304,15 +303,6 @@ void cmSourceFile::StoreProperty(const std::string& prop, ValueType value)
   }
 }
 
-void cmSourceFile::SetProperty(const std::string& prop, const char* value)
-{
-  this->StoreProperty(prop, value);
-}
-void cmSourceFile::SetProperty(const std::string& prop, cmValue value)
-{
-  this->StoreProperty(prop, value);
-}
-
 void cmSourceFile::AppendProperty(const std::string& prop,
                                   const std::string& value, bool asString)
 {

+ 5 - 1
Source/cmSourceFile.h

@@ -4,6 +4,7 @@
 
 #include "cmConfigure.h" // IWYU pragma: keep
 
+#include <cstddef>
 #include <memory>
 #include <string>
 #include <vector>
@@ -41,8 +42,11 @@ public:
   void SetCustomCommand(std::unique_ptr<cmCustomCommand> cc);
 
   //! Set/Get a property of this source file
-  void SetProperty(const std::string& prop, const char* value);
   void SetProperty(const std::string& prop, cmValue value);
+  void SetProperty(const std::string& prop, std::nullptr_t)
+  {
+    this->SetProperty(prop, cmValue{ nullptr });
+  }
   void SetProperty(const std::string& prop, const std::string& value)
   {
     this->SetProperty(prop, cmValue(value));

+ 6 - 5
Source/cmState.cxx

@@ -564,7 +564,8 @@ void cmState::RemoveUserDefinedCommands()
   this->ScriptedCommands.clear();
 }
 
-void cmState::SetGlobalProperty(const std::string& prop, const char* value)
+void cmState::SetGlobalProperty(const std::string& prop,
+                                const std::string& value)
 {
   this->GlobalProperties.SetProperty(prop, value);
 }
@@ -583,10 +584,10 @@ cmValue cmState::GetGlobalProperty(const std::string& prop)
 {
   if (prop == "CACHE_VARIABLES") {
     std::vector<std::string> cacheKeys = this->GetCacheEntryKeys();
-    this->SetGlobalProperty("CACHE_VARIABLES", cmJoin(cacheKeys, ";").c_str());
+    this->SetGlobalProperty("CACHE_VARIABLES", cmJoin(cacheKeys, ";"));
   } else if (prop == "COMMANDS") {
     std::vector<std::string> commands = this->GetCommandNames();
-    this->SetGlobalProperty("COMMANDS", cmJoin(commands, ";").c_str());
+    this->SetGlobalProperty("COMMANDS", cmJoin(commands, ";"));
   } else if (prop == "IN_TRY_COMPILE") {
     this->SetGlobalProperty(
       "IN_TRY_COMPILE",
@@ -597,10 +598,10 @@ cmValue cmState::GetGlobalProperty(const std::string& prop)
   } else if (prop == "ENABLED_LANGUAGES") {
     std::string langs;
     langs = cmJoin(this->EnabledLanguages, ";");
-    this->SetGlobalProperty("ENABLED_LANGUAGES", langs.c_str());
+    this->SetGlobalProperty("ENABLED_LANGUAGES", langs);
   } else if (prop == "CMAKE_ROLE") {
     std::string mode = this->GetModeString();
-    this->SetGlobalProperty("CMAKE_ROLE", mode.c_str());
+    this->SetGlobalProperty("CMAKE_ROLE", mode);
   }
 #define STRING_LIST_ELEMENT(F) ";" #F
   if (prop == "CMAKE_C_KNOWN_FEATURES") {

+ 1 - 1
Source/cmState.h

@@ -194,7 +194,7 @@ public:
   void RemoveUserDefinedCommands();
   std::vector<std::string> GetCommandNames() const;
 
-  void SetGlobalProperty(const std::string& prop, const char* value);
+  void SetGlobalProperty(const std::string& prop, const std::string& value);
   void SetGlobalProperty(const std::string& prop, cmValue value);
   void AppendGlobalProperty(const std::string& prop, const std::string& value,
                             bool asString = false);

+ 2 - 14
Source/cmStateDirectory.cxx

@@ -271,9 +271,8 @@ void cmStateDirectory::ClearLinkDirectories()
                this->Snapshot_.Position->LinkDirectoriesPosition);
 }
 
-template <typename ValueType>
-void cmStateDirectory::StoreProperty(const std::string& prop, ValueType value,
-                                     cmListFileBacktrace const& lfbt)
+void cmStateDirectory::SetProperty(const std::string& prop, cmValue value,
+                                   cmListFileBacktrace const& lfbt)
 {
   if (prop == "INCLUDE_DIRECTORIES") {
     if (!value) {
@@ -319,17 +318,6 @@ void cmStateDirectory::StoreProperty(const std::string& prop, ValueType value,
   this->DirectoryState->Properties.SetProperty(prop, value);
 }
 
-void cmStateDirectory::SetProperty(const std::string& prop, const char* value,
-                                   cmListFileBacktrace const& lfbt)
-{
-  this->StoreProperty(prop, value, lfbt);
-}
-void cmStateDirectory::SetProperty(const std::string& prop, cmValue value,
-                                   cmListFileBacktrace const& lfbt)
-{
-  this->StoreProperty(prop, value, lfbt);
-}
-
 void cmStateDirectory::AppendProperty(const std::string& prop,
                                       const std::string& value, bool asString,
                                       cmListFileBacktrace const& lfbt)

+ 6 - 2
Source/cmStateDirectory.h

@@ -5,6 +5,7 @@
 
 #include "cmConfigure.h" // IWYU pragma: keep
 
+#include <cstddef>
 #include <string>
 #include <vector>
 
@@ -57,10 +58,13 @@ public:
   void SetLinkDirectories(BT<std::string> const& vecs);
   void ClearLinkDirectories();
 
-  void SetProperty(const std::string& prop, const char* value,
-                   cmListFileBacktrace const& lfbt);
   void SetProperty(const std::string& prop, cmValue value,
                    cmListFileBacktrace const& lfbt);
+  void SetProperty(const std::string& prop, std::nullptr_t,
+                   cmListFileBacktrace const& lfbt)
+  {
+    this->SetProperty(prop, cmValue{ nullptr }, lfbt);
+  }
   void AppendProperty(const std::string& prop, const std::string& value,
                       bool asString, cmListFileBacktrace const& lfbt);
   cmValue GetProperty(const std::string& prop) const;

+ 2 - 30
Source/cmTarget.cxx

@@ -1810,26 +1810,7 @@ MAKE_PROP(INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE);
 #undef MAKE_PROP
 }
 
-namespace {
-// to workaround bug on GCC/AIX
-// Define a template to force conversion to std::string
-template <typename ValueType>
-std::string ConvertToString(ValueType value);
-
-template <>
-std::string ConvertToString<const char*>(const char* value)
-{
-  return std::string(value);
-}
-template <>
-std::string ConvertToString<cmValue>(cmValue value)
-{
-  return std::string(*value);
-}
-}
-
-template <typename ValueType>
-void cmTarget::StoreProperty(const std::string& prop, ValueType value)
+void cmTarget::SetProperty(const std::string& prop, cmValue value)
 {
   if (prop == propMANUALLY_ADDED_DEPENDENCIES) {
     this->impl->Makefile->IssueMessage(
@@ -1975,7 +1956,7 @@ void cmTarget::StoreProperty(const std::string& prop, ValueType value)
 
     std::string reusedFrom = reusedTarget->GetSafeProperty(prop);
     if (reusedFrom.empty()) {
-      reusedFrom = ConvertToString(value);
+      reusedFrom = *value;
     }
 
     this->impl->Properties.SetProperty(prop, reusedFrom);
@@ -2091,15 +2072,6 @@ void cmTarget::AppendProperty(const std::string& prop,
   }
 }
 
-void cmTarget::SetProperty(const std::string& prop, const char* value)
-{
-  this->StoreProperty(prop, value);
-}
-void cmTarget::SetProperty(const std::string& prop, cmValue value)
-{
-  this->StoreProperty(prop, value);
-}
-
 template <typename ValueType>
 void cmTargetInternals::AddDirectoryToFileSet(cmTarget* self,
                                               std::string const& fileSetName,

+ 4 - 1
Source/cmTarget.h

@@ -180,8 +180,11 @@ public:
   std::set<BT<std::pair<std::string, bool>>> const& GetUtilities() const;
 
   //! Set/Get a property of this target file
-  void SetProperty(const std::string& prop, const char* value);
   void SetProperty(const std::string& prop, cmValue value);
+  void SetProperty(const std::string& prop, std::nullptr_t)
+  {
+    this->SetProperty(prop, cmValue{ nullptr });
+  }
   void SetProperty(const std::string& prop, const std::string& value)
   {
     this->SetProperty(prop, cmValue(value));

+ 0 - 4
Source/cmTest.cxx

@@ -52,10 +52,6 @@ bool cmTest::GetPropertyAsBool(const std::string& prop) const
   return cmIsOn(this->GetProperty(prop));
 }
 
-void cmTest::SetProperty(const std::string& prop, const char* value)
-{
-  this->Properties.SetProperty(prop, value);
-}
 void cmTest::SetProperty(const std::string& prop, cmValue value)
 {
   this->Properties.SetProperty(prop, value);

+ 5 - 1
Source/cmTest.h

@@ -4,6 +4,7 @@
 
 #include "cmConfigure.h" // IWYU pragma: keep
 
+#include <cstddef>
 #include <string>
 #include <vector>
 
@@ -34,8 +35,11 @@ public:
   std::vector<std::string> const& GetCommand() const { return this->Command; }
 
   //! Set/Get a property of this source file
-  void SetProperty(const std::string& prop, const char* value);
   void SetProperty(const std::string& prop, cmValue value);
+  void SetProperty(const std::string& prop, std::nullptr_t)
+  {
+    this->SetProperty(prop, cmValue{ nullptr });
+  }
   void SetProperty(const std::string& prop, const std::string& value)
   {
     this->SetProperty(prop, cmValue(value));

+ 0 - 4
Source/cmake.cxx

@@ -3261,10 +3261,6 @@ void cmake::GenerateGraphViz(const std::string& fileName) const
 #endif
 }
 
-void cmake::SetProperty(const std::string& prop, const char* value)
-{
-  this->State->SetGlobalProperty(prop, value);
-}
 void cmake::SetProperty(const std::string& prop, cmValue value)
 {
   this->State->SetGlobalProperty(prop, value);

+ 4 - 1
Source/cmake.h

@@ -404,8 +404,11 @@ public:
   std::vector<cmDocumentationEntry> GetGeneratorsDocumentation();
 
   //! Set/Get a property of this target file
-  void SetProperty(const std::string& prop, const char* value);
   void SetProperty(const std::string& prop, cmValue value);
+  void SetProperty(const std::string& prop, std::nullptr_t)
+  {
+    this->SetProperty(prop, cmValue{ nullptr });
+  }
   void SetProperty(const std::string& prop, const std::string& value)
   {
     this->SetProperty(prop, cmValue(value));