Source: Reduce string allocations, part 3

This commit is contained in:
AJIOB
2026-09-22 11:37:41 +03:00
parent cb20c5bd89
commit 1d2f0120be
27 changed files with 82 additions and 73 deletions
@@ -500,7 +500,7 @@ cm::optional<std::string> GetDistribValue(
std::string vars;
for (auto const& kv : *os_release) {
auto cmake_var_name = cmStrCat(variable, '_', kv.first);
vars += DELIM[!vars.empty()] + cmake_var_name;
vars = cmStrCat(vars, DELIM[!vars.empty()], cmake_var_name);
makefile.AddDefinition(cmake_var_name, kv.second);
}
return cm::optional<std::string>(std::move(vars));
+1 -1
View File
@@ -184,7 +184,7 @@ std::vector<std::string> GetPkgConfSysCflags(cmMakefile& mf)
std::string tmp;
cmSystemTools::GetEnv(var, tmp);
if (!tmp.empty()) {
paths += ";" + tmp;
paths = cmStrCat(paths, ';', tmp);
}
}
};
+1 -1
View File
@@ -707,7 +707,7 @@ bool cmCTest::OpenOutputFile(std::string const& path, std::string const& name,
{
std::string testingDir = this->Impl->BinaryDir + "/Testing";
if (!path.empty()) {
testingDir += "/" + path;
testingDir = cmStrCat(testingDir, '/', path);
}
if (cmSystemTools::FileExists(testingDir)) {
if (!cmSystemTools::FileIsDirectory(testingDir)) {
+2 -2
View File
@@ -1082,8 +1082,8 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
if (testLangs.find(LinkerLanguage) == testLangs.end()) {
this->Makefile->IssueMessage(
MessageType::FATAL_ERROR,
"Linker language '" + LinkerLanguage +
"' must be enabled in project(LANGUAGES).");
cmStrCat("Linker language '", LinkerLanguage,
"' must be enabled in project(LANGUAGES)."));
}
fprintf(fout, "set_property(TARGET %s PROPERTY LINKER_LANGUAGE %s)\n",
+1 -1
View File
@@ -37,7 +37,7 @@ static void cmFortranModuleAppendUpperLower(std::string const& mod,
}
std::string const& name = mod.substr(0, mod.size() - ext_len);
std::string const& ext = mod.substr(mod.size() - ext_len);
mod_upper += cmSystemTools::UpperCase(name) + ext;
mod_upper = cmStrCat(mod_upper, cmSystemTools::UpperCase(name), ext);
mod_lower += mod;
}
+3 -1
View File
@@ -18,6 +18,7 @@
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
#include "cmScriptGenerator.h"
#include "cmStringAlgorithms.h"
#include "cmTestDiscovery.h"
#include "cmTestGenerator.h"
@@ -106,7 +107,8 @@ bool cmDiscoverTestsCommand(std::vector<std::string> const& args,
}
if (!unparsed.empty()) {
status.SetError(" given unknown argument \"" + unparsed.front() + "\".");
status.SetError(
cmStrCat(" given unknown argument \"", unparsed.front(), "\"."));
return false;
}
+4 -4
View File
@@ -135,8 +135,8 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
return true;
}
if (!unparsedArguments.empty()) {
status.SetError(" given unknown argument \"" + unparsedArguments.front() +
"\".");
status.SetError(cmStrCat(" given unknown argument \"",
unparsedArguments.front(), "\"."));
return false;
}
@@ -159,8 +159,8 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
}
if (!status.GetMakefile().CanIWriteThisFile(outputFilename)) {
status.SetError("attempted to output into a file: " + outputFilename +
" into a source directory.");
status.SetError(cmStrCat("attempted to output into a file: ",
outputFilename, " into a source directory."));
cmSystemTools::SetFatalErrorOccurred();
return false;
}
+2 -1
View File
@@ -41,7 +41,8 @@ bool cmExportBuildCMakeConfigGenerator::GenerateMainFile(std::ostream& os)
std::string sep;
bool generatedInterfaceRequired = false;
auto visitor = [&](cmGeneratorTarget const* te) {
expectedTargets += sep + this->Namespace + te->GetExportName();
expectedTargets =
cmStrCat(expectedTargets, sep, this->Namespace, te->GetExportName());
sep = " ";
generatedInterfaceRequired |=
+2 -2
View File
@@ -507,8 +507,8 @@ static bool HandleSetupMode(std::vector<std::string> const& args,
SetupArguments arguments = parser.Parse(args, &unknownArgs);
if (!unknownArgs.empty()) {
status.SetError("SETUP given unknown argument: \"" + unknownArgs.front() +
"\".");
status.SetError(cmStrCat("SETUP given unknown argument: \"",
unknownArgs.front(), "\"."));
return false;
}
+1 -1
View File
@@ -462,7 +462,7 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpressions(
} else {
this->ResolveTargetsInGeneratorExpression(li, target, lg);
}
input += sep + li;
input = cmStrCat(input, sep, li);
sep = ";";
}
}
@@ -49,7 +49,8 @@ bool cmExportInstallCMakeConfigGenerator::GenerateMainFile(std::ostream& os)
std::string sep;
auto visitor = [&](cmTargetExport const* te) {
allTargets.push_back(te);
expectedTargets += sep + this->Namespace + te->Target->GetExportName();
expectedTargets = cmStrCat(expectedTargets, sep, this->Namespace,
te->Target->GetExportName());
sep = " ";
};
+1 -1
View File
@@ -605,7 +605,7 @@ void cmExportInstallFileGenerator::PopulateIncludeDirectoriesInterface(
std::string includes = (input ? *input : "");
char const* const sep = input ? ";" : "";
includes += sep + exportDirs;
includes = cmStrCat(includes, sep, exportDirs);
std::string prepro = cmGeneratorExpression::Preprocess(
includes, preprocessRule, this->GetImportPrefixWithSlash());
if (!prepro.empty()) {
+7 -6
View File
@@ -573,8 +573,8 @@ void cmFastbuildNormalTargetGenerator::ComputePCH(
// Reuse compiler options for PCH options.
node.PCHOptions += origCompileOptions;
if (this->Makefile->GetSafeDefinition("CMAKE_" + language +
"_COMPILER_ID") == "MSVC") {
if (this->Makefile->GetSafeDefinition(
cmStrCat("CMAKE_", language, "_COMPILER_ID")) == "MSVC") {
cmSystemTools::ReplaceString(node.PCHOptions,
FASTBUILD_2_INPUT_PLACEHOLDER,
FASTBUILD_3_INPUT_PLACEHOLDER);
@@ -1801,8 +1801,9 @@ void cmFastbuildNormalTargetGenerator::AppendExternalObject(
else if (target) {
if (!linkedDeps.emplace(objLibName + FASTBUILD_OBJECTS_ALIAS_POSTFIX)
.second) {
LogMessage("Object Target: " + objLibName +
FASTBUILD_OBJECTS_ALIAS_POSTFIX " already linked");
LogMessage(cmStrCat("Object Target: ", objLibName,
FASTBUILD_OBJECTS_ALIAS_POSTFIX
" already linked"));
continue;
}
linkerNode.LibrarianAdditionalInputs.emplace_back(
@@ -1890,8 +1891,8 @@ void cmFastbuildNormalTargetGenerator::AppendTargetDep(
// Skip exported objects.
// Tested in "ExportImport" test.
if (depType == cm::TargetType::OBJECT_LIBRARY) {
LogMessage("target : " + item.Target->GetName() +
" already linked... Skipping");
LogMessage(cmStrCat("target : ", item.Target->GetName(),
" already linked... Skipping"));
return;
}
// Tested in "ExportImport" test.
+4 -3
View File
@@ -157,7 +157,8 @@ std::string cmFastbuildTargetGenerator::GetCustomCommandTargetName(
extras += std::to_string(static_cast<int>(step));
cmCryptoHash hash(cmCryptoHash::AlgoSHA256);
targetName += "-" + hash.HashString(extras).substr(0, 14);
targetName =
cmStrCat(targetName, '-', hash.HashString(extras).substr(0, 14));
return targetName;
}
@@ -674,8 +675,8 @@ FastbuildExecNodes cmFastbuildTargetGenerator::GenerateCommands(
execNode.PreBuildDependencies);
for (auto const& util : ccg.GetUtilities()) {
auto const& utilTargetName = util.Value.first;
LogMessage("Util: " + utilTargetName +
", cross: " + std::to_string(util.Value.second));
LogMessage(cmStrCat("Util: ", utilTargetName,
", cross: ", std::to_string(util.Value.second)));
auto* const target = this->Makefile->FindTargetToUse(utilTargetName);
if (target && target->IsImported()) {
+4 -4
View File
@@ -75,8 +75,8 @@ void cmFastbuildUtilityTargetGenerator::Generate()
if (target && target->GetType() == cm::TargetType::INTERFACE_LIBRARY) {
for (auto const& dep : target->GetUtilities()) {
auto const& depName = this->ConvertToFastbuildPath(dep.Value.first);
LogMessage("Transitively propagating iface dep: " + depName +
", is cross: " + std::to_string(dep.Value.second));
LogMessage(cmStrCat("Transitively propagating iface dep: ", depName,
", is cross: ", std::to_string(dep.Value.second)));
nonImportedUtils.emplace_back(depName);
addUtilDepToTarget(this->ConvertToFastbuildPath(depName));
}
@@ -117,8 +117,8 @@ void cmFastbuildUtilityTargetGenerator::Generate()
for (auto& exec : GenerateCommands(FastbuildBuildStep::REST).Nodes) {
addUtilDepToTarget(exec.Name);
for (auto const& dep : TargetDirectDependencies) {
LogMessage("Direct dep " + dep->GetName() +
"-all propagating to CC: " + exec.Name);
LogMessage(cmStrCat("Direct dep ", dep->GetName(),
"-all propagating to CC: ", exec.Name));
// All custom commands from within the target must be executed AFTER all
// the target's deps.
exec.PreBuildDependencies.emplace(dep->GetName());
+3 -3
View File
@@ -316,7 +316,7 @@ std::string TargetId(cmGeneratorTarget const* gt, std::string const& topBuild)
topBuild, gt->GetLocalGenerator()->GetCurrentBinaryDirectory());
std::string hash = hasher.HashString(path);
hash.resize(20, '0');
return gt->GetName() + CMAKE_DIRECTORY_ID_SEP + hash;
return cmStrCat(gt->GetName(), CMAKE_DIRECTORY_ID_SEP, hash);
}
struct CompileData
@@ -755,7 +755,7 @@ Json::Value CodemodelConfig::DumpTarget(cmGeneratorTarget* gt,
std::replace(safeTargetName.begin(), safeTargetName.end(), ':', '_');
std::string prefix = "target-" + safeTargetName;
if (!this->Config.empty()) {
prefix += "-" + this->Config;
prefix = cmStrCat(prefix, '-', this->Config);
}
Json::Value target = this->FileAPI.MaybeJsonFile(t.Dump(), prefix);
target["name"] = gt->GetName();
@@ -856,7 +856,7 @@ Json::Value CodemodelConfig::DumpDirectoryObject(Directory& d)
}
}
if (!this->Config.empty()) {
prefix += "-" + this->Config;
prefix = cmStrCat(prefix, '-', this->Config);
}
DirectoryObject dir(d.LocalGenerator, this->VersionMajor, this->VersionMinor,
+11 -11
View File
@@ -755,7 +755,7 @@ bool HandleGlobImpl(std::vector<std::string> const& args, bool recurse,
expr = status.GetMakefile().GetCurrentSourceDirectory();
// Handle script mode
if (!expr.empty()) {
expr += "/" + *i;
expr = cmStrCat(expr, '/', *i);
} else {
expr = *i;
}
@@ -2126,10 +2126,10 @@ bool HandleDownloadCommand(std::vector<std::string> const& args,
std::string dir = cmSystemTools::GetFilenamePath(file);
if (!dir.empty() && !cmSystemTools::FileExists(dir) &&
!cmSystemTools::MakeDirectory(dir)) {
std::string errstring = "DOWNLOAD error: cannot create directory '" +
dir +
"' - Specify file by full path name and verify that you "
"have directory creation and file write privileges.";
std::string errstring =
cmStrCat("DOWNLOAD error: cannot create directory '", dir,
"' - Specify file by full path name and verify that you "
"have directory creation and file write privileges.");
status.SetError(errstring);
return false;
}
@@ -2358,10 +2358,10 @@ bool HandleDownloadCommand(std::vector<std::string> const& args,
if (expectedHash != actualHash) {
if (!statusVar.empty() && res == 0) {
status.GetMakefile().AddDefinition(statusVar,
"1;HASH mismatch: "
"expected: " +
expectedHash +
" actual: " + actualHash);
cmStrCat("1;HASH mismatch: "
"expected: ",
expectedHash,
" actual: ", actualHash));
}
status.SetError(cmStrCat("DOWNLOAD HASH mismatch\n"
@@ -3644,8 +3644,8 @@ bool HandleConfigureCommand(std::vector<std::string> const& args,
cmMakefile& makeFile = status.GetMakefile();
if (!makeFile.CanIWriteThisFile(outputFile)) {
cmSystemTools::Error("Attempt to write file: " + outputFile +
" into a source directory.");
cmSystemTools::Error(cmStrCat("Attempt to write file: ", outputFile,
" into a source directory."));
return false;
}
+3 -2
View File
@@ -447,8 +447,9 @@ bool cmFileInstaller::HandleInstallDestination()
if (this->InstallType != cmInstallType_DIRECTORY) {
if (!cmSystemTools::FileExists(destination)) {
if (!cmSystemTools::MakeDirectory(destination, default_dir_mode)) {
std::string errstring = "cannot create directory: " + destination +
". Maybe need administrative privileges.";
std::string errstring =
cmStrCat("cannot create directory: ", destination,
". Maybe need administrative privileges.");
this->Status.SetError(errstring);
return false;
}
+1 -1
View File
@@ -82,7 +82,7 @@ std::string cmFortranParser_s::SModName(std::string const& mod_name,
if (this->Compiler.SModSep.empty()) {
return sub_name + SModExt;
}
return mod_name + this->Compiler.SModSep + sub_name + SModExt;
return cmStrCat(mod_name, this->Compiler.SModSep, sub_name, SModExt);
}
bool cmFortranParser_FilePush(cmFortranParser* parser, char const* fname)
@@ -167,7 +167,7 @@ void cmGeneratorExpressionEvaluationFile::Generate(cmLocalGenerator* lg)
std::string line;
std::string sep;
while (cmSystemTools::GetLineFromStream(fin, line)) {
inputContent += sep + line;
inputContent = cmStrCat(inputContent, sep, line);
sep = "\n";
}
inputContent += sep;
+2 -1
View File
@@ -4834,7 +4834,8 @@ static const struct TargetPropertyNode : public cmGeneratorExpressionNode
std::string linkedTargetsContent = getLinkedTargetsContent(
target, interfacePropertyName, eval, &dagChecker, usage);
if (!linkedTargetsContent.empty()) {
result += (result.empty() ? "" : ";") + linkedTargetsContent;
result =
cmStrCat(result, (result.empty() ? "" : ";"), linkedTargetsContent);
}
}
return result;
+14 -14
View File
@@ -1326,7 +1326,7 @@ std::string cmGeneratorTarget::GetCompilePDBName(
*config_name, this->LocalGenerator, config, this);
NameComponents const& components = GetFullNameInternalComponents(
config, cmStateEnums::RuntimeBinaryArtifact);
return components.prefix + pdbName + ".pdb";
return cmStrCat(components.prefix, pdbName, ".pdb");
}
cmValue name = this->GetProperty("COMPILE_PDB_NAME");
@@ -1335,7 +1335,7 @@ std::string cmGeneratorTarget::GetCompilePDBName(
*name, this->LocalGenerator, config, this);
NameComponents const& components = GetFullNameInternalComponents(
config, cmStateEnums::RuntimeBinaryArtifact);
return components.prefix + pdbName + ".pdb";
return cmStrCat(components.prefix, pdbName, ".pdb");
}
// If the target is PCH-reused or PCH-reuses, we need a stable name for the
@@ -3211,8 +3211,8 @@ std::string cmGeneratorTarget::GetPchHeader(std::string const& config,
generatorTarget = reuseTarget;
}
auto const inserted =
this->PchHeaders.insert(std::make_pair(language + config + arch, ""));
auto const inserted = this->PchHeaders.insert(
std::make_pair(cmStrCat(language, config, arch), ""));
if (inserted.second) {
std::vector<BT<std::string>> const headers =
this->GetPrecompileHeaders(config, language);
@@ -3305,8 +3305,8 @@ std::string cmGeneratorTarget::GetPchSource(std::string const& config,
language != "OBJCXX") {
return std::string();
}
auto const inserted =
this->PchSources.insert(std::make_pair(language + config + arch, ""));
auto const inserted = this->PchSources.insert(
std::make_pair(cmStrCat(language, config, arch), ""));
if (inserted.second) {
std::string const pchHeader = this->GetPchHeader(config, language, arch);
if (pchHeader.empty()) {
@@ -3365,8 +3365,8 @@ std::string cmGeneratorTarget::GetPchFileObject(std::string const& config,
language != "OBJCXX") {
return std::string();
}
auto const inserted =
this->PchObjectFiles.insert(std::make_pair(language + config + arch, ""));
auto const inserted = this->PchObjectFiles.insert(
std::make_pair(cmStrCat(language, config, arch), ""));
if (inserted.second) {
std::string const pchSource = this->GetPchSource(config, language, arch);
if (pchSource.empty()) {
@@ -3388,8 +3388,8 @@ std::string cmGeneratorTarget::GetPchFile(std::string const& config,
std::string const& language,
std::string const& arch)
{
auto const inserted =
this->PchFiles.insert(std::make_pair(language + config + arch, ""));
auto const inserted = this->PchFiles.insert(
std::make_pair(cmStrCat(language, config, arch), ""));
if (inserted.second) {
std::string& pchFile = inserted.first->second;
@@ -3448,7 +3448,7 @@ std::string cmGeneratorTarget::GetPchCreateCompileOptions(
std::string const& arch)
{
auto const inserted = this->PchCreateCompileOptions.insert(
std::make_pair(language + config + arch, ""));
std::make_pair(cmStrCat(language, config, arch), ""));
if (inserted.second) {
std::string& createOptionList = inserted.first->second;
@@ -3496,7 +3496,7 @@ std::string cmGeneratorTarget::GetPchUseCompileOptions(
std::string const& arch)
{
auto const inserted = this->PchUseCompileOptions.insert(
std::make_pair(language + config + arch, ""));
std::make_pair(cmStrCat(language, config, arch), ""));
if (inserted.second) {
std::string& useOptionList = inserted.first->second;
@@ -3944,7 +3944,7 @@ cmGeneratorTarget::Names cmGeneratorTarget::GetExecutableNames(
targetNames.Output = components.prefix + targetNames.Base;
} else {
targetNames.Output =
components.prefix + targetNames.Base + components.suffix;
cmStrCat(components.prefix, targetNames.Base, components.suffix);
}
// The executable's real name on disk.
@@ -3977,7 +3977,7 @@ std::string cmGeneratorTarget::GetFullNameInternal(
{
NameComponents const& components =
this->GetFullNameInternalComponents(config, artifact);
return components.prefix + components.base + components.suffix;
return cmStrCat(components.prefix, components.base, components.suffix);
}
std::string cmGeneratorTarget::ImportedGetLocation(
+3 -3
View File
@@ -567,7 +567,7 @@ std::string cmGraphVizWriter::ItemNameWithAliases(
auto nameWithAliases = itemName;
for(auto const& item : items) {
nameWithAliases += "\\n(" + item + ")";
nameWithAliases = cmStrCat(nameWithAliases, "\\n(" , item , ')');
}
return nameWithAliases;
@@ -578,10 +578,10 @@ std::string cmGraphVizWriter::GetEdgeStyle(DependencyType dt)
std::string style;
switch (dt) {
case DependencyType::LinkPrivate:
style = "[ style = " + std::string(GRAPHVIZ_EDGE_STYLE_PRIVATE) + " ]";
style = cmStrCat("[ style = ", GRAPHVIZ_EDGE_STYLE_PRIVATE, " ]");
break;
case DependencyType::LinkInterface:
style = "[ style = " + std::string(GRAPHVIZ_EDGE_STYLE_INTERFACE) + " ]";
style = cmStrCat("[ style = ", GRAPHVIZ_EDGE_STYLE_INTERFACE, " ]");
break;
default:
break;
+3 -2
View File
@@ -82,8 +82,9 @@ static void FinalAction(cmMakefile& makefile, std::string const& dest,
// replace any variables
std::string const& temps = *s;
if (!cmSystemTools::GetFilenamePath(temps).empty()) {
testf = cmSystemTools::GetFilenamePath(temps) + "/" +
cmSystemTools::GetFilenameWithoutLastExtension(temps) + ext;
testf =
cmStrCat(cmSystemTools::GetFilenamePath(temps), '/',
cmSystemTools::GetFilenameWithoutLastExtension(temps), ext);
} else {
testf = cmSystemTools::GetFilenameWithoutLastExtension(temps) + ext;
}
+2 -2
View File
@@ -146,8 +146,8 @@ bool cmInstrumentationCommand(std::vector<std::string> const& args,
return true;
}
if (!unparsedArguments.empty()) {
status.SetError("given unknown argument \"" + unparsedArguments.front() +
"\".");
status.SetError(
cmStrCat("given unknown argument \"", unparsedArguments.front(), "\"."));
return false;
}
int apiVersion;
+1 -1
View File
@@ -974,7 +974,7 @@ bool cmake::FindPackage(std::vector<std::string> const& args)
lg->GetStateSnapshot().GetDirectory());
lg->GetTargetFlags(&linkLineComputer, buildType, linkLibs, flags,
linkFlags, frameworkPath, linkPath, gtgt);
linkLibs = frameworkPath + linkPath + linkLibs;
linkLibs = cmStrCat(frameworkPath, linkPath, linkLibs);
printf("%s\n", linkLibs.c_str());
+2 -2
View File
@@ -3400,8 +3400,8 @@ int cmVSLink::LinkNonIncremental()
}
// Run the manifest tool to embed the final manifest in the binary.
std::string mtOut = "/outputresource:" + this->TargetFile +
(this->Type == 1 ? ";#1" : ";#2");
std::string mtOut = cmStrCat("/outputresource:", this->TargetFile,
(this->Type == 1 ? ";#1" : ";#2"));
return this->RunMT(mtOut, false);
}