Source: reduce stringstreams usage

This commit is contained in:
AJIOB committed 2026-09-24 20:49:12 +00:00
1 parent 6c7dfb3afd
commit d1d6e6d69d
5 files changed
+90 -108

No files matched your search

+9 -14
View File
@@ -3,7 +3,6 @@
#include "cmCommonTargetGenerator.h"
#include <algorithm>
#include <sstream>
#include <utility>
#include <cm/filesystem>
@@ -417,9 +416,8 @@ void cmCommonTargetGenerator::AppendOSXVerFlag(std::string& flags,
minor, patch);
if (major > 0 || minor > 0 || patch > 0) {
// Append the flag since a non-zero version is specified.
std::ostringstream vflag;
vflag << *flag << major << "." << minor << "." << patch;
this->LocalCommonGenerator->AppendFlags(flags, vflag.str());
std::string vflag = cmStrCat(*flag, major, '.', minor, '.', patch);
this->LocalCommonGenerator->AppendFlags(flags, vflag);
}
}
@@ -714,8 +712,6 @@ void cmCommonTargetGenerator::ComputeRustFlagsForObjects(
std::string& linkCrates, std::string& nativeObjects,
std::vector<std::string> const& objects)
{
std::stringstream rlibsArgs;
std::stringstream objectsArgs;
auto const processObject = [&](std::string const& obj) {
cm::filesystem::path const objPath(obj);
if (objPath.extension() == ".rlib") {
@@ -725,18 +721,17 @@ void cmCommonTargetGenerator::ComputeRustFlagsForObjects(
// common usage in Rust.
std::string objStem = objPath.stem().string();
objStem = objStem.substr(3, objStem.length() - 6);
rlibsArgs << " --extern=" << objStem << "="
<< this->LocalCommonGenerator->ConvertToOutputFormat(
obj, cmOutputConverter::SHELL);
linkCrates = cmStrCat(std::move(linkCrates), " --extern=", objStem, "=",
this->LocalCommonGenerator->ConvertToOutputFormat(
obj, cmOutputConverter::SHELL));
} else {
objectsArgs << " -Clink-arg="
<< this->LocalCommonGenerator->ConvertToOutputFormat(
obj, cmOutputConverter::SHELL);
nativeObjects =
cmStrCat(std::move(nativeObjects), " -Clink-arg=",
this->LocalCommonGenerator->ConvertToOutputFormat(
obj, cmOutputConverter::SHELL));
}
};
for (std::string const& obj : objects) {
processObject(obj);
}
linkCrates += rlibsArgs.str();
nativeObjects += objectsArgs.str();
}
+22 -24
View File
@@ -4,12 +4,10 @@
#include <cstddef>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <cm/string_view>
#include <cmext/string_view>
#include "cmScanDepFormat.h"
@@ -131,8 +129,6 @@ std::string CxxModuleMapContentClang(CxxModuleLocations const& loc,
cmScanDepInfo const& obj,
CxxModuleUsage const& usages)
{
std::stringstream mm;
// Clang's command line only supports a single output. If more than one is
// expected, we cannot make a useful module map file.
if (obj.Provides.size() > 1) {
@@ -141,32 +137,32 @@ std::string CxxModuleMapContentClang(CxxModuleLocations const& loc,
// A series of flags which tell the compiler where to look for modules.
std::string mm;
for (auto const& p : obj.Provides) {
auto bmi_loc = loc.BmiGeneratorPathForModule(p.LogicalName);
if (bmi_loc.IsKnown()) {
// Force the TU to be considered a C++ module source file regardless of
// extension.
mm << "-x c++-module\n";
mm << "-fmodule-output=\"" << bmi_loc.Location() << "\"\n";
mm = cmStrCat(std::move(mm),
"-x c++-module\n"
"-fmodule-output=\"",
bmi_loc.Location(), "\"\n");
break;
}
}
auto all_usages = GetTransitiveUsages(loc, obj.Requires, usages);
for (auto const& usage : all_usages) {
mm << "-fmodule-file=\"" << usage.LogicalName << '=' << usage.Location
<< "\"\n";
mm = cmStrCat(std::move(mm), "-fmodule-file=\"", usage.LogicalName, '=',
usage.Location, "\"\n");
}
return mm.str();
return mm;
}
std::string CxxModuleMapContentGcc(CxxModuleLocations const& loc,
cmScanDepInfo const& obj)
{
std::stringstream mm;
// Documented in GCC's documentation. The format is a series of
// lines with a module name and the associated filename separated
// by spaces. The first line may use `$root` as the module name
@@ -175,30 +171,30 @@ std::string CxxModuleMapContentGcc(CxxModuleLocations const& loc,
// generate any).
// Write the root directory to use for module paths.
mm << "$root " << loc.RootDirectory << '\n';
std::string mm = cmStrCat("$root ", loc.RootDirectory, '\n');
for (auto const& p : obj.Provides) {
auto bmi_loc = loc.BmiGeneratorPathForModule(p.LogicalName);
if (bmi_loc.IsKnown()) {
mm << p.LogicalName << ' ' << bmi_loc.Location() << '\n';
mm =
cmStrCat(std::move(mm), p.LogicalName, ' ', bmi_loc.Location(), '\n');
}
}
for (auto const& r : obj.Requires) {
auto bmi_loc = loc.BmiGeneratorPathForModule(r.LogicalName);
if (bmi_loc.IsKnown()) {
mm << r.LogicalName << ' ' << bmi_loc.Location() << '\n';
mm =
cmStrCat(std::move(mm), r.LogicalName, ' ', bmi_loc.Location(), '\n');
}
}
return mm.str();
return mm;
}
std::string CxxModuleMapContentMsvc(CxxModuleLocations const& loc,
cmScanDepInfo const& obj,
CxxModuleUsage const& usages)
{
std::stringstream mm;
// A response file of `-reference NAME=PATH` arguments.
// MSVC's command line only supports a single output. If more than one is
@@ -220,16 +216,18 @@ std::string CxxModuleMapContentMsvc(CxxModuleLocations const& loc,
return ""_s;
};
std::string mm;
for (auto const& p : obj.Provides) {
if (p.IsInterface) {
mm << "-interface\n";
mm += "-interface\n";
} else {
mm << "-internalPartition\n";
mm += "-internalPartition\n";
}
auto bmi_loc = loc.BmiGeneratorPathForModule(p.LogicalName);
if (bmi_loc.IsKnown()) {
mm << "-ifcOutput \"" << bmi_loc.Location() << "\"\n";
mm =
cmStrCat(std::move(mm), "-ifcOutput \"", bmi_loc.Location(), "\"\n");
}
}
@@ -237,11 +235,11 @@ std::string CxxModuleMapContentMsvc(CxxModuleLocations const& loc,
for (auto const& usage : all_usages) {
auto flag = flag_for_method(usage.Method);
mm << flag << " \"" << usage.LogicalName << '=' << usage.Location
<< "\"\n";
mm = cmStrCat(std::move(mm), flag, " \"", usage.LogicalName, '=',
usage.Location, "\"\n");
}
return mm.str();
return mm;
}
}
+7 -11
View File
@@ -560,15 +560,11 @@ void cmNinjaNormalTargetGenerator::WriteLinkRule(
std::string targetVersionMajor;
std::string targetVersionMinor;
{
std::ostringstream majorStream;
std::ostringstream minorStream;
int major;
int minor;
this->GetGeneratorTarget()->GetTargetVersion(major, minor);
majorStream << major;
minorStream << minor;
targetVersionMajor = majorStream.str();
targetVersionMinor = minorStream.str();
targetVersionMajor = std::to_string(major);
targetVersionMinor = std::to_string(minor);
}
vars.TargetVersionMajor = targetVersionMajor.c_str();
vars.TargetVersionMinor = targetVersionMinor.c_str();
@@ -1254,7 +1250,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement(
vars["SWIFT_SOURCES"] = [this, config]() -> std::string {
std::vector<cmSourceFile const*> sourceFiles;
std::stringstream oss;
std::string result;
this->GetGeneratorTarget()->GetObjectSources(sourceFiles, config);
cmLocalGenerator const* LocalGen = this->GetLocalGenerator();
@@ -1262,11 +1258,11 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement(
std::string const sourcePath = source->GetLanguage() == "Swift"
? this->GetCompiledSourceNinjaPath(source)
: this->GetObjectFilePath(source, config);
oss << " "
<< LocalGen->ConvertToOutputFormat(sourcePath,
cmOutputConverter::SHELL);
result = cmStrCat(std::move(result), ' ',
LocalGen->ConvertToOutputFormat(
sourcePath, cmOutputConverter::SHELL));
}
return oss.str();
return result;
}();
// Since we do not perform object builds, compute the
+20 -23
View File
@@ -1829,13 +1829,13 @@ void cmVisualStudio10TargetGenerator::WriteCustomRule(
std::string script = lg->ConstructScript(ccg);
bool symbolic = false;
// input files for custom command
std::stringstream additional_inputs;
std::string additional_inputs;
{
char const* sep = "";
cm::string_view sep;
if (this->ProjectType == VsProjectType::csproj) {
// csproj files do not attach the command to a specific file
// so the primary input must be listed explicitly.
additional_inputs << source->GetFullPath();
additional_inputs = source->GetFullPath();
sep = ";";
}
@@ -1852,7 +1852,7 @@ void cmVisualStudio10TargetGenerator::WriteCustomRule(
continue;
}
ConvertToWindowsSlash(dep);
additional_inputs << sep << dep;
additional_inputs = cmStrCat(std::move(additional_inputs), sep, dep);
sep = ";";
if (!symbolic) {
if (cmSourceFile* sf = this->Makefile->GetSource(
@@ -1872,17 +1872,18 @@ void cmVisualStudio10TargetGenerator::WriteCustomRule(
}
}
if (this->ProjectType != VsProjectType::csproj) {
additional_inputs << sep << "%(AdditionalInputs)";
additional_inputs =
cmStrCat(std::move(additional_inputs), sep, "%(AdditionalInputs)");
}
}
// output files for custom command
std::stringstream outputs;
std::string outputs;
{
char const* sep = "";
cm::string_view sep;
for (std::string const& o : ccg.GetOutputs()) {
std::string out = o;
ConvertToWindowsSlash(out);
outputs << sep << out;
outputs = cmStrCat(std::move(outputs), sep, out);
sep = ";";
if (!symbolic) {
if (cmSourceFile* sf = this->Makefile->GetSource(
@@ -1897,8 +1898,8 @@ void cmVisualStudio10TargetGenerator::WriteCustomRule(
cmCryptoHash hasher(cmCryptoHash::AlgoMD5);
std::string name =
cmStrCat("CustomCommand_", c, '_', hasher.HashString(sourcePath));
this->WriteCustomRuleCSharp(e0, c, name, script, additional_inputs.str(),
outputs.str(), comment, ccg);
this->WriteCustomRuleCSharp(e0, c, name, script, additional_inputs,
outputs, comment, ccg);
} else {
BuildInParallel buildInParallel = BuildInParallel::No;
if (command.GetCMP0147Status() == cmPolicies::NEW &&
@@ -1908,9 +1909,8 @@ void cmVisualStudio10TargetGenerator::WriteCustomRule(
"VS_CUSTOM_COMMAND_DISABLE_PARALLEL_BUILD")) {
buildInParallel = BuildInParallel::Yes;
}
this->WriteCustomRuleCpp(*spe2, c, script, additional_inputs.str(),
outputs.str(), comment, ccg, symbolic,
buildInParallel);
this->WriteCustomRuleCpp(*spe2, c, script, additional_inputs, outputs,
comment, ccg, symbolic, buildInParallel);
}
}
}
@@ -2666,18 +2666,15 @@ void cmVisualStudio10TargetGenerator::WriteAllSources(Elem& e0)
Elem e2(e1, tool);
bool isCSharp = (si.Source->GetLanguage() == "CSharp"_s);
if (isCSharp && !exclude_configs.empty()) {
std::stringstream conditions;
bool firstConditionSet{ false };
std::string conditions;
cm::string_view sep;
for (auto const& ci : include_configs) {
if (firstConditionSet) {
conditions << " Or ";
}
conditions << "('$(Configuration)|$(Platform)'=='"
<< this->Configurations[ci] << '|' << this->Platform
<< "')";
firstConditionSet = true;
conditions = cmStrCat(
std::move(conditions), sep, "('$(Configuration)|$(Platform)'=='",
this->Configurations[ci], '|', this->Platform, "')");
sep = " Or ";
}
e2.Attribute("Condition", conditions.str());
e2.Attribute("Condition", conditions);
}
this->WriteSource(e2, si.Source);
+32 -36
View File
@@ -251,13 +251,12 @@ bool cmakeCheckStampFile(std::string const& stampName)
// The build system is up to date. The stamp file has been removed
// by the VS IDE due to a "rebuild" request. Restore it atomically.
std::ostringstream stampTempStream;
stampTempStream << stampName << ".tmp" << cmSystemTools::RandomNumber();
std::string stampTemp = stampTempStream.str();
std::string stampTemp =
cmStrCat(stampName, ".tmp", cmSystemTools::RandomNumber());
{
// TODO: Teach cmGeneratedFileStream to use a random temp file (with
// multiple tries in unlikely case of conflict) and use that here.
cmsys::ofstream stamp(stampTemp.c_str());
cmsys::ofstream stamp(stampTemp);
stamp << "# CMake generation timestamp file for this directory.\n";
}
std::string err;
@@ -2367,20 +2366,21 @@ int cmake::DoPreConfigureChecks()
std::string srcList =
cmStrCat(this->GetHomeDirectory(), '/', this->CMakeListName);
if (!cmSystemTools::FileExists(srcList)) {
std::ostringstream err;
std::string err;
if (cmSystemTools::FileIsDirectory(this->GetHomeDirectory())) {
err << "The source directory \"" << this->GetHomeDirectory()
<< "\" does not appear to contain " << this->CMakeListName << ".\n";
err =
cmStrCat("The source directory \"", this->GetHomeDirectory(),
"\" does not appear to contain ", this->CMakeListName, ".\n");
} else if (cmSystemTools::FileExists(this->GetHomeDirectory())) {
err << "The source directory \"" << this->GetHomeDirectory()
<< "\" is a file, not a directory.\n";
err = cmStrCat("The source directory \"", this->GetHomeDirectory(),
"\" is a file, not a directory.\n");
} else {
err << "The source directory \"" << this->GetHomeDirectory()
<< "\" does not exist.\n";
err = cmStrCat("The source directory \"", this->GetHomeDirectory(),
"\" does not exist.\n");
}
err << "Specify --help for usage, or press the help button on the CMake "
err += "Specify --help for usage, or press the help button on the CMake "
"GUI.";
cmSystemTools::Error(err.str());
cmSystemTools::Error(err);
return -2;
}
@@ -2419,16 +2419,15 @@ int cmake::HandleDeleteCacheVariables(
return 0;
}
std::vector<SaveCacheEntry> saved;
std::ostringstream warning;
warning
<< "You have changed variables that require your cache to be deleted.\n"
"Configure will be re-run and you may have to reset some variables.\n"
"The following variables have changed:\n";
std::string warning(
"You have changed variables that require your cache to be deleted.\n"
"Configure will be re-run and you may have to reset some variables.\n"
"The following variables have changed:\n");
for (auto const& var : vars) {
SaveCacheEntry save;
save.key = var.first;
save.value = var.second;
warning << save.key << "= " << save.value << '\n';
warning = cmStrCat(std::move(warning), save.key, "= ", save.value, '\n');
cmValue existingValue = this->State->GetCacheEntryValue(save.key);
if (existingValue) {
save.type = this->State->GetCacheEntryType(save.key);
@@ -2459,7 +2458,7 @@ int cmake::HandleDeleteCacheVariables(
for (SaveCacheEntry const& i : saved) {
this->AddCacheEntry(i.key, i.value, i.help, i.type);
}
cmSystemTools::Message(warning.str());
cmSystemTools::Message(warning);
// avoid reconfigure if there were errors
if (!cmSystemTools::GetErrorOccurredFlag()) {
// re-run configure
@@ -3660,10 +3659,9 @@ int cmake::CheckBuildSystem()
// If the file provided does not exist, we have to rerun.
if (!cmSystemTools::FileExists(this->CheckBuildSystemArgument)) {
if (verbose) {
std::ostringstream msg;
msg << "Re-run cmake missing file: " << this->CheckBuildSystemArgument
<< '\n';
cmSystemTools::Stdout(msg.str());
std::string msg = cmStrCat(
"Re-run cmake missing file: ", this->CheckBuildSystemArgument, '\n');
cmSystemTools::Stdout(msg);
}
return 1;
}
@@ -3678,10 +3676,9 @@ int cmake::CheckBuildSystem()
if (!mf.ReadListFile(this->CheckBuildSystemArgument) ||
cmSystemTools::GetErrorOccurredFlag()) {
if (verbose) {
std::ostringstream msg;
msg << "Re-run cmake error reading : " << this->CheckBuildSystemArgument
<< '\n';
cmSystemTools::Stdout(msg.str());
std::string msg = cmStrCat(
"Re-run cmake error reading : ", this->CheckBuildSystemArgument, '\n');
cmSystemTools::Stdout(msg);
}
// There was an error reading the file. Just rerun.
return 1;
@@ -3774,10 +3771,9 @@ int cmake::CheckBuildSystem()
if (!this->FileTimeCache->Compare(out_oldest, dep_newest, &result) ||
result < 0) {
if (verbose) {
std::ostringstream msg;
msg << "Re-run cmake file: " << out_oldest
<< " older than: " << dep_newest << '\n';
cmSystemTools::Stdout(msg.str());
std::string msg = cmStrCat("Re-run cmake file: ", out_oldest,
" older than: ", dep_newest, '\n');
cmSystemTools::Stdout(msg);
}
return 1;
}
@@ -4622,16 +4618,16 @@ void cmake::RunCheckForUnusedVariables()
this->CurrentSnapshot.GetDiagnostic(cmDiagnostics::CMD_UNUSED_CLI);
if (action != cmDiagnostics::Ignore) {
bool haveUnused = false;
std::ostringstream msg;
msg << "Manually-specified variables were not used by the project:";
std::string msg(
"Manually-specified variables were not used by the project:");
for (auto const& it : this->UsedCliVariables) {
if (!it.second) {
haveUnused = true;
msg << "\n " << it.first;
msg = cmStrCat(std::move(msg), "\n ", it.first);
}
}
if (haveUnused) {
this->IssueDiagnostic(cmDiagnostics::CMD_UNUSED_CLI, msg.str());
this->IssueDiagnostic(cmDiagnostics::CMD_UNUSED_CLI, msg);
}
}
#endif