Refactor: reduce cmToCStr usage

This commit is contained in:
Marc Chevrier 2021-08-13 15:57:23 +02:00
parent 2984df9100
commit 5a2a275bb4
17 changed files with 62 additions and 69 deletions

View File

@ -278,13 +278,11 @@ cmComputeLinkInformation::cmComputeLinkInformation(
// On platforms without import libraries there may be a special flag // On platforms without import libraries there may be a special flag
// to use when creating a plugin (module) that obtains symbols from // to use when creating a plugin (module) that obtains symbols from
// the program that will load it. // the program that will load it.
this->LoaderFlag = nullptr;
if (!this->Target->IsDLLPlatform() && if (!this->Target->IsDLLPlatform() &&
this->Target->GetType() == cmStateEnums::MODULE_LIBRARY) { this->Target->GetType() == cmStateEnums::MODULE_LIBRARY) {
std::string loader_flag_var = std::string loader_flag_var =
cmStrCat("CMAKE_SHARED_MODULE_LOADER_", this->LinkLanguage, "_FLAG"); cmStrCat("CMAKE_SHARED_MODULE_LOADER_", this->LinkLanguage, "_FLAG");
this->LoaderFlag = this->LoaderFlag = this->Makefile->GetDefinition(loader_flag_var);
cmToCStr(this->Makefile->GetDefinition(loader_flag_var));
} }
// Get options needed to link libraries. // Get options needed to link libraries.
@ -660,8 +658,7 @@ void cmComputeLinkInformation::AddItem(BT<std::string> const& item,
// This link item is an executable that may provide symbols // This link item is an executable that may provide symbols
// used by this target. A special flag is needed on this // used by this target. A special flag is needed on this
// platform. Add it now. // platform. Add it now.
std::string linkItem; std::string linkItem = this->LoaderFlag;
linkItem = this->LoaderFlag;
cmStateEnums::ArtifactType artifact = tgt->HasImportLibrary(config) cmStateEnums::ArtifactType artifact = tgt->HasImportLibrary(config)
? cmStateEnums::ImportLibraryArtifact ? cmStateEnums::ImportLibraryArtifact
: cmStateEnums::RuntimeBinaryArtifact; : cmStateEnums::RuntimeBinaryArtifact;

View File

@ -14,6 +14,7 @@
#include "cmsys/RegularExpression.hxx" #include "cmsys/RegularExpression.hxx"
#include "cmListFileCache.h" #include "cmListFileCache.h"
#include "cmProperty.h"
class cmGeneratorTarget; class cmGeneratorTarget;
class cmGlobalGenerator; class cmGlobalGenerator;
@ -137,7 +138,7 @@ private:
SharedDepModeLink // List file on link line SharedDepModeLink // List file on link line
}; };
const char* LoaderFlag; cmProp LoaderFlag;
std::string LibLinkFlag; std::string LibLinkFlag;
std::string LibLinkFileFlag; std::string LibLinkFileFlag;
std::string ObjLinkFileFlag; std::string ObjLinkFileFlag;

View File

@ -4962,11 +4962,11 @@ cmGeneratorTarget::Names cmGeneratorTarget::GetLibraryNames(
// The library's soname. // The library's soname.
this->ComputeVersionedName(targetNames.SharedObject, prefix, this->ComputeVersionedName(targetNames.SharedObject, prefix,
targetNames.Base, suffix, targetNames.Output, targetNames.Base, suffix, targetNames.Output,
cmToCStr(soversion)); soversion);
// The library's real name on disk. // The library's real name on disk.
this->ComputeVersionedName(targetNames.Real, prefix, targetNames.Base, this->ComputeVersionedName(targetNames.Real, prefix, targetNames.Base,
suffix, targetNames.Output, cmToCStr(version)); suffix, targetNames.Output, version);
} }
// The import library name. // The import library name.
@ -4999,10 +4999,10 @@ cmGeneratorTarget::Names cmGeneratorTarget::GetExecutableNames(
// This versioning is supported only for executables and then only // This versioning is supported only for executables and then only
// when the platform supports symbolic links. // when the platform supports symbolic links.
#if defined(_WIN32) && !defined(__CYGWIN__) #if defined(_WIN32) && !defined(__CYGWIN__)
const char* version = nullptr; cmProp version;
#else #else
// Check for executable version properties. // Check for executable version properties.
const char* version = cmToCStr(this->GetProperty("VERSION")); cmProp version = this->GetProperty("VERSION");
if (this->GetType() != cmStateEnums::EXECUTABLE || if (this->GetType() != cmStateEnums::EXECUTABLE ||
this->Makefile->IsOn("XCODE")) { this->Makefile->IsOn("XCODE")) {
version = nullptr; version = nullptr;
@ -5026,7 +5026,7 @@ cmGeneratorTarget::Names cmGeneratorTarget::GetExecutableNames(
#endif #endif
if (version) { if (version) {
targetNames.Real += "-"; targetNames.Real += "-";
targetNames.Real += version; targetNames.Real += *version;
} }
#if defined(__CYGWIN__) #if defined(__CYGWIN__)
targetNames.Real += suffix; targetNames.Real += suffix;
@ -6290,17 +6290,14 @@ std::string cmGeneratorTarget::GetFrameworkVersion() const
return "A"; return "A";
} }
void cmGeneratorTarget::ComputeVersionedName(std::string& vName, void cmGeneratorTarget::ComputeVersionedName(
std::string const& prefix, std::string& vName, std::string const& prefix, std::string const& base,
std::string const& base, std::string const& suffix, std::string const& name, cmProp version) const
std::string const& suffix,
std::string const& name,
const char* version) const
{ {
vName = this->Makefile->IsOn("APPLE") ? (prefix + base) : name; vName = this->Makefile->IsOn("APPLE") ? (prefix + base) : name;
if (version) { if (version) {
vName += "."; vName += ".";
vName += version; vName += *version;
} }
vName += this->Makefile->IsOn("APPLE") ? suffix : std::string(); vName += this->Makefile->IsOn("APPLE") ? suffix : std::string();
} }

View File

@ -905,8 +905,7 @@ private:
void ComputeVersionedName(std::string& vName, std::string const& prefix, void ComputeVersionedName(std::string& vName, std::string const& prefix,
std::string const& base, std::string const& suffix, std::string const& base, std::string const& suffix,
std::string const& name, std::string const& name, cmProp version) const;
const char* version) const;
struct CompatibleInterfacesBase struct CompatibleInterfacesBase
{ {

View File

@ -13,6 +13,8 @@
namespace { namespace {
void StoreResult(cmMakefile& makefile, std::string const& variable, void StoreResult(cmMakefile& makefile, std::string const& variable,
const char* prop); const char* prop);
void StoreResult(cmMakefile& makefile, std::string const& variable,
cmProp prop);
} }
// cmGetDirectoryPropertyCommand // cmGetDirectoryPropertyCommand
@ -76,7 +78,6 @@ bool cmGetDirectoryPropertyCommand(std::vector<std::string> const& args,
return false; return false;
} }
const char* prop = nullptr;
if (*i == "DEFINITIONS") { if (*i == "DEFINITIONS") {
switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0059)) { switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0059)) {
case cmPolicies::WARN: case cmPolicies::WARN:
@ -94,8 +95,7 @@ bool cmGetDirectoryPropertyCommand(std::vector<std::string> const& args,
break; break;
} }
} }
prop = cmToCStr(dir->GetProperty(*i)); StoreResult(status.GetMakefile(), variable, dir->GetProperty(*i));
StoreResult(status.GetMakefile(), variable, prop);
return true; return true;
} }
@ -105,4 +105,9 @@ void StoreResult(cmMakefile& makefile, std::string const& variable,
{ {
makefile.AddDefinition(variable, prop ? prop : ""); makefile.AddDefinition(variable, prop ? prop : "");
} }
void StoreResult(cmMakefile& makefile, std::string const& variable,
cmProp prop)
{
makefile.AddDefinition(variable, prop);
}
} }

View File

@ -431,9 +431,8 @@ bool HandleVariableMode(cmExecutionStatus& status, const std::string& name,
return false; return false;
} }
return StoreResult( return StoreResult(infoType, status.GetMakefile(), variable,
infoType, status.GetMakefile(), variable, status.GetMakefile().GetDefinition(propertyName));
cmToCStr(status.GetMakefile().GetDefinition(propertyName)));
} }
bool HandleCacheMode(cmExecutionStatus& status, const std::string& name, bool HandleCacheMode(cmExecutionStatus& status, const std::string& name,

View File

@ -157,11 +157,11 @@ void cmGlobalVisualStudio71Generator::WriteProjectDepends(
// executables to the libraries it uses are also done here // executables to the libraries it uses are also done here
void cmGlobalVisualStudio71Generator::WriteExternalProject( void cmGlobalVisualStudio71Generator::WriteExternalProject(
std::ostream& fout, const std::string& name, const std::string& location, std::ostream& fout, const std::string& name, const std::string& location,
const char* typeGuid, cmProp typeGuid, const std::set<BT<std::pair<std::string, bool>>>& depends)
const std::set<BT<std::pair<std::string, bool>>>& depends)
{ {
fout << "Project(\"{" fout << "Project(\"{"
<< (typeGuid ? typeGuid : this->ExternalProjectType(location)) << (typeGuid ? typeGuid
: std::string(this->ExternalProjectType(location)))
<< "}\") = \"" << name << "\", \"" << "}\") = \"" << name << "\", \""
<< this->ConvertToSolutionPath(location) << "\", \"{" << this->ConvertToSolutionPath(location) << "\", \"{"
<< this->GetGUID(name) << "}\"\n"; << this->GetGUID(name) << "}\"\n";

View File

@ -33,7 +33,7 @@ protected:
const std::string& platformMapping = "") override; const std::string& platformMapping = "") override;
void WriteExternalProject( void WriteExternalProject(
std::ostream& fout, const std::string& name, const std::string& path, std::ostream& fout, const std::string& name, const std::string& path,
const char* typeGuid, cmProp typeGuid,
const std::set<BT<std::pair<std::string, bool>>>& depends) override; const std::set<BT<std::pair<std::string, bool>>>& depends) override;
// Folders are not supported by VS 7.1. // Folders are not supported by VS 7.1.

View File

@ -391,10 +391,9 @@ void cmGlobalVisualStudio7Generator::WriteTargetsToSolution(
std::string project = target->GetName(); std::string project = target->GetName();
std::string location = *expath; std::string location = *expath;
this->WriteExternalProject( this->WriteExternalProject(fout, project, location,
fout, project, location, target->GetProperty("VS_PROJECT_TYPE"),
cmToCStr(target->GetProperty("VS_PROJECT_TYPE")), target->GetUtilities());
target->GetUtilities());
written = true; written = true;
} else { } else {
cmProp vcprojName = target->GetProperty("GENERATOR_FILE_NAME"); cmProp vcprojName = target->GetProperty("GENERATOR_FILE_NAME");

View File

@ -6,6 +6,7 @@
#include "cmGlobalGeneratorFactory.h" #include "cmGlobalGeneratorFactory.h"
#include "cmGlobalVisualStudioGenerator.h" #include "cmGlobalVisualStudioGenerator.h"
#include "cmProperty.h"
class cmTarget; class cmTarget;
struct cmIDEFlagTable; struct cmIDEFlagTable;
@ -142,7 +143,7 @@ protected:
virtual void WriteExternalProject( virtual void WriteExternalProject(
std::ostream& fout, const std::string& name, const std::string& path, std::ostream& fout, const std::string& name, const std::string& path,
const char* typeGuid, cmProp typeGuid,
const std::set<BT<std::pair<std::string, bool>>>& dependencies) = 0; const std::set<BT<std::pair<std::string, bool>>>& dependencies) = 0;
std::string ConvertToSolutionPath(const std::string& path); std::string ConvertToSolutionPath(const std::string& path);

View File

@ -14,7 +14,7 @@ bool cmIncludeRegularExpressionCommand(std::vector<std::string> const& args,
} }
cmMakefile& mf = status.GetMakefile(); cmMakefile& mf = status.GetMakefile();
mf.SetIncludeRegularExpression(args[0].c_str()); mf.SetIncludeRegularExpression(args[0]);
if (args.size() > 1) { if (args.size() > 1) {
mf.SetComplainRegularExpression(args[1]); mf.SetComplainRegularExpression(args[1]);

View File

@ -425,27 +425,25 @@ void cmLocalGenerator::ProcessEvaluationFiles(
void cmLocalGenerator::GenerateInstallRules() void cmLocalGenerator::GenerateInstallRules()
{ {
// Compute the install prefix. // Compute the install prefix.
const char* prefix = cmProp installPrefix = this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
cmToCStr(this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX")); std::string prefix = installPrefix;
#if defined(_WIN32) && !defined(__CYGWIN__) #if defined(_WIN32) && !defined(__CYGWIN__)
std::string prefix_win32; if (!installPrefix) {
if (!prefix) { if (!cmSystemTools::GetEnv("SystemDrive", prefix)) {
if (!cmSystemTools::GetEnv("SystemDrive", prefix_win32)) { prefix = "C:";
prefix_win32 = "C:";
} }
cmProp project_name = this->Makefile->GetDefinition("PROJECT_NAME"); cmProp project_name = this->Makefile->GetDefinition("PROJECT_NAME");
if (cmNonempty(project_name)) { if (cmNonempty(project_name)) {
prefix_win32 += "/Program Files/"; prefix += "/Program Files/";
prefix_win32 += *project_name; prefix += *project_name;
} else { } else {
prefix_win32 += "/InstalledCMakeProject"; prefix += "/InstalledCMakeProject";
} }
prefix = prefix_win32.c_str();
} }
#elif defined(__HAIKU__) #elif defined(__HAIKU__)
char dir[B_PATH_NAME_LENGTH]; char dir[B_PATH_NAME_LENGTH];
if (!prefix) { if (!installPrefix) {
if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir)) == if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir)) ==
B_OK) { B_OK) {
prefix = dir; prefix = dir;
@ -454,13 +452,13 @@ void cmLocalGenerator::GenerateInstallRules()
} }
} }
#else #else
if (!prefix) { if (!installPrefix) {
prefix = "/usr/local"; prefix = "/usr/local";
} }
#endif #endif
if (cmProp stagingPrefix = if (cmProp stagingPrefix =
this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX")) { this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX")) {
prefix = stagingPrefix->c_str(); prefix = *stagingPrefix;
} }
// Compute the set of configurations. // Compute the set of configurations.
@ -1869,17 +1867,17 @@ std::string cmLocalGenerator::GetLinkLibsCMP0065(
} }
bool cmLocalGenerator::AllAppleArchSysrootsAreTheSame( bool cmLocalGenerator::AllAppleArchSysrootsAreTheSame(
const std::vector<std::string>& archs, const char* sysroot) const std::vector<std::string>& archs, cmProp sysroot)
{ {
if (!sysroot) { if (!sysroot) {
return false; return false;
} }
return std::all_of(archs.begin(), archs.end(), return std::all_of(archs.begin(), archs.end(),
[this, &sysroot](std::string const& arch) -> bool { [this, sysroot](std::string const& arch) -> bool {
std::string const& archSysroot = std::string const& archSysroot =
this->AppleArchSysroots[arch]; this->AppleArchSysroots[arch];
return cmIsOff(archSysroot) || archSysroot == sysroot; return cmIsOff(archSysroot) || sysroot == archSysroot;
}); });
} }
@ -1912,7 +1910,7 @@ void cmLocalGenerator::AddArchitectureFlags(std::string& flags,
cmProp sysrootFlag = this->Makefile->GetDefinition(sysrootFlagVar); cmProp sysrootFlag = this->Makefile->GetDefinition(sysrootFlagVar);
if (cmNonempty(sysrootFlag)) { if (cmNonempty(sysrootFlag)) {
if (!this->AppleArchSysroots.empty() && if (!this->AppleArchSysroots.empty() &&
!this->AllAppleArchSysrootsAreTheSame(archs, cmToCStr(sysroot))) { !this->AllAppleArchSysrootsAreTheSame(archs, sysroot)) {
for (std::string const& arch : archs) { for (std::string const& arch : archs) {
std::string const& archSysroot = this->AppleArchSysroots[arch]; std::string const& archSysroot = this->AppleArchSysroots[arch];
if (cmIsOff(archSysroot)) { if (cmIsOff(archSysroot)) {

View File

@ -651,7 +651,7 @@ private:
void ComputeObjectMaxPath(); void ComputeObjectMaxPath();
bool AllAppleArchSysrootsAreTheSame(const std::vector<std::string>& archs, bool AllAppleArchSysrootsAreTheSame(const std::vector<std::string>& archs,
const char* sysroot); cmProp sysroot);
void CopyPchCompilePdb(const std::string& config, cmGeneratorTarget* target, void CopyPchCompilePdb(const std::string& config, cmGeneratorTarget* target,
const std::string& ReuseFrom, const std::string& ReuseFrom,

View File

@ -399,13 +399,13 @@ public:
* Set a regular expression that include files must match * Set a regular expression that include files must match
* in order to be considered as part of the depend information. * in order to be considered as part of the depend information.
*/ */
void SetIncludeRegularExpression(const char* regex) void SetIncludeRegularExpression(const std::string& regex)
{ {
this->SetProperty("INCLUDE_REGULAR_EXPRESSION", regex); this->SetProperty("INCLUDE_REGULAR_EXPRESSION", regex.c_str());
} }
const char* GetIncludeRegularExpression() const const std::string& GetIncludeRegularExpression() const
{ {
return cmToCStr(this->GetProperty("INCLUDE_REGULAR_EXPRESSION")); return this->GetProperty("INCLUDE_REGULAR_EXPRESSION");
} }
/** /**

View File

@ -293,8 +293,7 @@ void cmMakefileTargetGenerator::WriteTargetBuildRules()
this->GetConfigName()); this->GetConfigName());
for (cmSourceFile const* sf : externalObjects) { for (cmSourceFile const* sf : externalObjects) {
auto const& objectFileName = sf->GetFullPath(); auto const& objectFileName = sf->GetFullPath();
if (!cmSystemTools::StringEndsWith(objectFileName, if (!cmHasSuffix(objectFileName, pchExtension)) {
cmToCStr(pchExtension))) {
this->ExternalObjects.push_back(objectFileName); this->ExternalObjects.push_back(objectFileName);
} }
} }
@ -1732,7 +1731,7 @@ void cmMakefileTargetGenerator::WriteObjectsVariable(
cmProp pchExtension = this->Makefile->GetDefinition("CMAKE_PCH_EXTENSION"); cmProp pchExtension = this->Makefile->GetDefinition("CMAKE_PCH_EXTENSION");
for (std::string const& obj : this->Objects) { for (std::string const& obj : this->Objects) {
if (cmSystemTools::StringEndsWith(obj, cmToCStr(pchExtension))) { if (cmHasSuffix(obj, pchExtension)) {
continue; continue;
} }
*this->BuildFileStream << " " << lineContinue; *this->BuildFileStream << " " << lineContinue;
@ -1822,7 +1821,7 @@ void cmMakefileTargetGenerator::WriteObjectsStrings(
objStrings, this->LocalGenerator, objStrings, this->LocalGenerator,
this->LocalGenerator->GetStateSnapshot().GetDirectory(), limit); this->LocalGenerator->GetStateSnapshot().GetDirectory(), limit);
for (std::string const& obj : this->Objects) { for (std::string const& obj : this->Objects) {
if (cmSystemTools::StringEndsWith(obj, cmToCStr(pchExtension))) { if (cmHasSuffix(obj, pchExtension)) {
continue; continue;
} }
helper.Feed(obj); helper.Feed(obj);

View File

@ -994,8 +994,7 @@ void cmNinjaTargetGenerator::WriteObjectBuildStatements(
for (cmSourceFile const* sf : externalObjects) { for (cmSourceFile const* sf : externalObjects) {
auto objectFileName = this->GetGlobalGenerator()->ExpandCFGIntDir( auto objectFileName = this->GetGlobalGenerator()->ExpandCFGIntDir(
this->ConvertToNinjaPath(sf->GetFullPath()), config); this->ConvertToNinjaPath(sf->GetFullPath()), config);
if (!cmSystemTools::StringEndsWith(objectFileName, if (!cmHasSuffix(objectFileName, pchExtension)) {
cmToCStr(pchExtension))) {
this->Configs[config].Objects.push_back(objectFileName); this->Configs[config].Objects.push_back(objectFileName);
} }
} }
@ -1260,8 +1259,7 @@ void cmNinjaTargetGenerator::WriteObjectBuildStatement(
if (firstForConfig) { if (firstForConfig) {
cmProp pchExtension = cmProp pchExtension =
this->GetMakefile()->GetDefinition("CMAKE_PCH_EXTENSION"); this->GetMakefile()->GetDefinition("CMAKE_PCH_EXTENSION");
if (!cmSystemTools::StringEndsWith(objectFileName, if (!cmHasSuffix(objectFileName, pchExtension)) {
cmToCStr(pchExtension))) {
// Add this object to the list of object files. // Add this object to the list of object files.
this->Configs[config].Objects.push_back(objectFileName); this->Configs[config].Objects.push_back(objectFileName);
} }

View File

@ -508,15 +508,15 @@ void cmVisualStudio10TargetGenerator::Generate()
p = this->GeneratorTarget->GetProperty( p = this->GeneratorTarget->GetProperty(
"DOTNET_TARGET_FRAMEWORK_VERSION"); "DOTNET_TARGET_FRAMEWORK_VERSION");
} }
const char* targetFrameworkVersion = cmToCStr(p); std::string targetFrameworkVersion = p;
if (!targetFrameworkVersion && this->ProjectType == csproj && if (targetFrameworkVersion.empty() && this->ProjectType == csproj &&
this->GlobalGenerator->TargetsWindowsCE() && this->GlobalGenerator->TargetsWindowsCE() &&
this->GlobalGenerator->GetVersion() == this->GlobalGenerator->GetVersion() ==
cmGlobalVisualStudioGenerator::VS12) { cmGlobalVisualStudioGenerator::VS12) {
// VS12 .NETCF default to .NET framework 3.9 // VS12 .NETCF default to .NET framework 3.9
targetFrameworkVersion = "v3.9"; targetFrameworkVersion = "v3.9";
} }
if (targetFrameworkVersion) { if (!targetFrameworkVersion.empty()) {
e1.Element("TargetFrameworkVersion", targetFrameworkVersion); e1.Element("TargetFrameworkVersion", targetFrameworkVersion);
} }
} }