Source sweep: Replace std::ostringstream when used with a single append

This replaces `std::ostringstream`, when it is written to only once.
If the single written argument was numeric, `std::to_string` is used instead.
Otherwise, the single written argument is used directly instead of the
`std::ostringstream::str()` invocation.
This commit is contained in:
Sebastian Holtermann 2019-08-23 12:21:46 +02:00
parent 19612dffd2
commit 3b2b02825d
24 changed files with 92 additions and 153 deletions

View File

@ -89,11 +89,10 @@ int cmCPackBundleGenerator::ConstructBundle()
// The staging directory contains everything that will end-up inside the
// final disk image ...
std::ostringstream staging;
staging << toplevel;
std::string const staging = toplevel;
std::ostringstream contents;
contents << staging.str() << "/" << cpack_bundle_name << ".app/"
contents << staging << "/" << cpack_bundle_name << ".app/"
<< "Contents";
std::ostringstream application;

View File

@ -154,16 +154,15 @@ bool cmCTestBuildCommand::InitialPass(std::vector<std::string> const& args,
{
bool ret = cmCTestHandlerCommand::InitialPass(args, status);
if (this->Values[ctb_NUMBER_ERRORS] && *this->Values[ctb_NUMBER_ERRORS]) {
std::ostringstream str;
str << this->Handler->GetTotalErrors();
this->Makefile->AddDefinition(this->Values[ctb_NUMBER_ERRORS], str.str());
this->Makefile->AddDefinition(
this->Values[ctb_NUMBER_ERRORS],
std::to_string(this->Handler->GetTotalErrors()));
}
if (this->Values[ctb_NUMBER_WARNINGS] &&
*this->Values[ctb_NUMBER_WARNINGS]) {
std::ostringstream str;
str << this->Handler->GetTotalWarnings();
this->Makefile->AddDefinition(this->Values[ctb_NUMBER_WARNINGS],
str.str());
this->Makefile->AddDefinition(
this->Values[ctb_NUMBER_WARNINGS],
std::to_string(this->Handler->GetTotalWarnings()));
}
return ret;
}

View File

@ -228,9 +228,8 @@ bool cmCTestHandlerCommand::InitialPass(std::vector<std::string> const& args,
int res = handler->ProcessHandler();
if (this->Values[ct_RETURN_VALUE] && *this->Values[ct_RETURN_VALUE]) {
std::ostringstream str;
str << res;
this->Makefile->AddDefinition(this->Values[ct_RETURN_VALUE], str.str());
this->Makefile->AddDefinition(this->Values[ct_RETURN_VALUE],
std::to_string(res));
}
this->ProcessAdditionalValues(handler);
// log the error message if there was an error

View File

@ -2,7 +2,7 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCTestMemCheckCommand.h"
#include <sstream>
#include <string>
#include <vector>
#include "cmCTest.h"
@ -44,8 +44,9 @@ void cmCTestMemCheckCommand::ProcessAdditionalValues(
cmCTestGenericHandler* handler)
{
if (this->Values[ctm_DEFECT_COUNT] && *this->Values[ctm_DEFECT_COUNT]) {
std::ostringstream str;
str << static_cast<cmCTestMemCheckHandler*>(handler)->GetDefectCount();
this->Makefile->AddDefinition(this->Values[ctm_DEFECT_COUNT], str.str());
this->Makefile->AddDefinition(
this->Values[ctm_DEFECT_COUNT],
std::to_string(
static_cast<cmCTestMemCheckHandler*>(handler)->GetDefectCount()));
}
}

View File

@ -1074,10 +1074,7 @@ void cmCTestMemCheckHandler::AppendMemTesterOutput(cmCTestTestResult& res,
void cmCTestMemCheckHandler::TestOutputFileNames(
int test, std::vector<std::string>& files)
{
std::string index;
std::ostringstream stream;
stream << test;
index = stream.str();
std::string index = std::to_string(test);
std::string ofile = this->MemoryTesterOutputFile;
std::string::size_type pos = ofile.find("??");
ofile.replace(pos, 2, index);

View File

@ -5,8 +5,6 @@
#include "cmCTestScriptHandler.h"
#include "cmMakefile.h"
#include <sstream>
class cmExecutionStatus;
bool cmCTestRunScriptCommand::InitialPass(std::vector<std::string> const& args,
@ -41,9 +39,7 @@ bool cmCTestRunScriptCommand::InitialPass(std::vector<std::string> const& args,
int ret;
cmCTestScriptHandler::RunScript(this->CTest, this->Makefile,
args[i].c_str(), !np, &ret);
std::ostringstream str;
str << ret;
this->Makefile->AddDefinition(returnVariable, str.str());
this->Makefile->AddDefinition(returnVariable, std::to_string(ret));
}
}
return true;

View File

@ -93,9 +93,7 @@ bool cmAddExecutableCommand(std::vector<std::string> const& args,
return false;
}
if (args.size() != 3) {
std::ostringstream e;
e << "ALIAS requires exactly one target argument.";
status.SetError(e.str());
status.SetError("ALIAS requires exactly one target argument.");
return false;
}

View File

@ -49,9 +49,8 @@ bool cmAddLibraryCommand(std::vector<std::string> const& args,
std::string libType = *s;
if (libType == "STATIC") {
if (type == cmStateEnums::INTERFACE_LIBRARY) {
std::ostringstream e;
e << "INTERFACE library specified with conflicting STATIC type.";
status.SetError(e.str());
status.SetError(
"INTERFACE library specified with conflicting STATIC type.");
return false;
}
++s;
@ -59,9 +58,8 @@ bool cmAddLibraryCommand(std::vector<std::string> const& args,
haveSpecifiedType = true;
} else if (libType == "SHARED") {
if (type == cmStateEnums::INTERFACE_LIBRARY) {
std::ostringstream e;
e << "INTERFACE library specified with conflicting SHARED type.";
status.SetError(e.str());
status.SetError(
"INTERFACE library specified with conflicting SHARED type.");
return false;
}
++s;
@ -69,9 +67,8 @@ bool cmAddLibraryCommand(std::vector<std::string> const& args,
haveSpecifiedType = true;
} else if (libType == "MODULE") {
if (type == cmStateEnums::INTERFACE_LIBRARY) {
std::ostringstream e;
e << "INTERFACE library specified with conflicting MODULE type.";
status.SetError(e.str());
status.SetError(
"INTERFACE library specified with conflicting MODULE type.");
return false;
}
++s;
@ -79,9 +76,8 @@ bool cmAddLibraryCommand(std::vector<std::string> const& args,
haveSpecifiedType = true;
} else if (libType == "OBJECT") {
if (type == cmStateEnums::INTERFACE_LIBRARY) {
std::ostringstream e;
e << "INTERFACE library specified with conflicting OBJECT type.";
status.SetError(e.str());
status.SetError(
"INTERFACE library specified with conflicting OBJECT type.");
return false;
}
++s;
@ -89,9 +85,8 @@ bool cmAddLibraryCommand(std::vector<std::string> const& args,
haveSpecifiedType = true;
} else if (libType == "UNKNOWN") {
if (type == cmStateEnums::INTERFACE_LIBRARY) {
std::ostringstream e;
e << "INTERFACE library specified with conflicting UNKNOWN type.";
status.SetError(e.str());
status.SetError(
"INTERFACE library specified with conflicting UNKNOWN type.");
return false;
}
++s;
@ -99,30 +94,26 @@ bool cmAddLibraryCommand(std::vector<std::string> const& args,
haveSpecifiedType = true;
} else if (libType == "ALIAS") {
if (type == cmStateEnums::INTERFACE_LIBRARY) {
std::ostringstream e;
e << "INTERFACE library specified with conflicting ALIAS type.";
status.SetError(e.str());
status.SetError(
"INTERFACE library specified with conflicting ALIAS type.");
return false;
}
++s;
isAlias = true;
} else if (libType == "INTERFACE") {
if (haveSpecifiedType) {
std::ostringstream e;
e << "INTERFACE library specified with conflicting/multiple types.";
status.SetError(e.str());
status.SetError(
"INTERFACE library specified with conflicting/multiple types.");
return false;
}
if (isAlias) {
std::ostringstream e;
e << "INTERFACE library specified with conflicting ALIAS type.";
status.SetError(e.str());
status.SetError(
"INTERFACE library specified with conflicting ALIAS type.");
return false;
}
if (excludeFromAll) {
std::ostringstream e;
e << "INTERFACE library may not be used with EXCLUDE_FROM_ALL.";
status.SetError(e.str());
status.SetError(
"INTERFACE library may not be used with EXCLUDE_FROM_ALL.");
return false;
}
++s;
@ -130,9 +121,8 @@ bool cmAddLibraryCommand(std::vector<std::string> const& args,
haveSpecifiedType = true;
} else if (*s == "EXCLUDE_FROM_ALL") {
if (type == cmStateEnums::INTERFACE_LIBRARY) {
std::ostringstream e;
e << "INTERFACE library may not be used with EXCLUDE_FROM_ALL.";
status.SetError(e.str());
status.SetError(
"INTERFACE library may not be used with EXCLUDE_FROM_ALL.");
return false;
}
++s;
@ -144,9 +134,8 @@ bool cmAddLibraryCommand(std::vector<std::string> const& args,
++s;
importGlobal = true;
} else if (type == cmStateEnums::INTERFACE_LIBRARY && *s == "GLOBAL") {
std::ostringstream e;
e << "GLOBAL option may only be used with IMPORTED libraries.";
status.SetError(e.str());
status.SetError(
"GLOBAL option may only be used with IMPORTED libraries.");
return false;
} else {
break;
@ -155,15 +144,12 @@ bool cmAddLibraryCommand(std::vector<std::string> const& args,
if (type == cmStateEnums::INTERFACE_LIBRARY) {
if (s != args.end()) {
std::ostringstream e;
e << "INTERFACE library requires no source arguments.";
status.SetError(e.str());
status.SetError("INTERFACE library requires no source arguments.");
return false;
}
if (importGlobal && !importTarget) {
std::ostringstream e;
e << "INTERFACE library specified as GLOBAL, but not as IMPORTED.";
status.SetError(e.str());
status.SetError(
"INTERFACE library specified as GLOBAL, but not as IMPORTED.");
return false;
}
}
@ -192,9 +178,7 @@ bool cmAddLibraryCommand(std::vector<std::string> const& args,
return false;
}
if (args.size() != 3) {
std::ostringstream e;
e << "ALIAS requires exactly one target argument.";
status.SetError(e.str());
status.SetError("ALIAS requires exactly one target argument.");
return false;
}

View File

@ -2,7 +2,6 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCMakeHostSystemInformationCommand.h"
#include <sstream>
#include <stddef.h>
#include "cmExecutionStatus.h"
@ -183,9 +182,7 @@ bool GetValue(cmExecutionStatus& status, cmsys::SystemInformation& info,
std::string ValueToString(size_t value)
{
std::ostringstream tmp;
tmp << value;
return tmp.str();
return std::to_string(value);
}
std::string ValueToString(const char* value)

View File

@ -1218,9 +1218,7 @@ int cmCTest::RunTest(std::vector<const char*> argv, std::string* output,
timeout != cmCTest::MaxDuration() &&
timeout > cmDuration::zero()) {
args.emplace_back("--test-timeout");
std::ostringstream msg;
msg << cmDurationTo<unsigned int>(timeout);
args.push_back(msg.str());
args.push_back(std::to_string(cmDurationTo<unsigned int>(timeout)));
}
args.emplace_back(i);
}

View File

@ -88,9 +88,7 @@ const char* cmCommandArgumentParserHelper::ExpandVariable(const char* var)
return nullptr;
}
if (this->FileLine >= 0 && strcmp(var, "CMAKE_CURRENT_LIST_LINE") == 0) {
std::ostringstream ostr;
ostr << this->FileLine;
return this->AddString(ostr.str());
return this->AddString(std::to_string(this->FileLine));
}
const char* value = this->Makefile->GetDefinition(var);
if (!value) {

View File

@ -843,9 +843,7 @@ void cmExportFileGenerator::SetImportDetailProperties(
if (iface->Multiplicity > 0) {
std::string prop =
cmStrCat("IMPORTED_LINK_INTERFACE_MULTIPLICITY", suffix);
std::ostringstream m;
m << iface->Multiplicity;
properties[prop] = m.str();
properties[prop] = std::to_string(iface->Multiplicity);
}
}

View File

@ -446,9 +446,8 @@ bool cmFileCopier::Install(const std::string& fromFile,
const std::string& toFile)
{
if (fromFile.empty()) {
std::ostringstream e;
e << "INSTALL encountered an empty string input file name.";
this->Status.SetError(e.str());
this->Status.SetError(
"INSTALL encountered an empty string input file name.");
return false;
}

View File

@ -62,9 +62,7 @@ bool cmFunctionHelperCommand::operator()(
this->Policies);
// set the value of argc
std::ostringstream strStream;
strStream << expandedArgs.size();
makefile.AddDefinition("ARGC", strStream.str());
makefile.AddDefinition("ARGC", std::to_string(expandedArgs.size()));
makefile.MarkVariableAsUsed("ARGC");
// set the values for ARGV0 ARGV1 ...

View File

@ -684,10 +684,10 @@ struct CompilerIdNode : public cmGeneratorExpressionNode
if (cmsysString_strcasecmp(param.c_str(), compilerId.c_str()) == 0) {
switch (context->LG->GetPolicyStatus(cmPolicies::CMP0044)) {
case cmPolicies::WARN: {
std::ostringstream e;
e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0044);
context->LG->GetCMakeInstance()->IssueMessage(
MessageType::AUTHOR_WARNING, e.str(), context->Backtrace);
MessageType::AUTHOR_WARNING,
cmPolicies::GetPolicyWarning(cmPolicies::CMP0044),
context->Backtrace);
CM_FALLTHROUGH;
}
case cmPolicies::OLD:

View File

@ -3289,10 +3289,9 @@ std::vector<BT<std::string>> cmGeneratorTarget::GetCompileDefinitions(
if (configProp) {
switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0043)) {
case cmPolicies::WARN: {
std::ostringstream e;
e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0043);
this->LocalGenerator->IssueMessage(MessageType::AUTHOR_WARNING,
e.str());
this->LocalGenerator->IssueMessage(
MessageType::AUTHOR_WARNING,
cmPolicies::GetPolicyWarning(cmPolicies::CMP0043));
CM_FALLTHROUGH;
}
case cmPolicies::OLD: {

View File

@ -636,9 +636,7 @@ bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
// follow the expected naming scheme. Expected naming scheme is that
// the subkeys of OtherProjects7 is 0 to n-1, so it's ok to use "n"
// as the name of the next subkey.
std::ostringstream ossNext;
ossNext << index;
nextAvailableSubKeyName = ossNext.str();
nextAvailableSubKeyName = std::to_string(index);
keyname = regKeyBase + "\\RecordingProject7";
hkey = NULL;

View File

@ -368,10 +368,9 @@ bool cmInstallCommand::HandleTargetsMode(std::vector<std::string> const& args)
!objectArgs.GetType().empty() || !frameworkArgs.GetType().empty() ||
!bundleArgs.GetType().empty() || !privateHeaderArgs.GetType().empty() ||
!publicHeaderArgs.GetType().empty() || !resourceArgs.GetType().empty()) {
std::ostringstream e;
e << "TARGETS given TYPE option. The TYPE option may only be specified in "
" install(FILES) and install(DIRECTORIES).";
this->SetError(e.str());
this->SetError(
"TARGETS given TYPE option. The TYPE option may only be specified in "
" install(FILES) and install(DIRECTORIES).");
return false;
}

View File

@ -440,9 +440,9 @@ bool HandleFindCommand(std::vector<std::string> const& args,
std::vector<std::string>::iterator it =
std::find(varArgsExpanded.begin(), varArgsExpanded.end(), args[2]);
if (it != varArgsExpanded.end()) {
std::ostringstream indexStream;
indexStream << std::distance(varArgsExpanded.begin(), it);
status.GetMakefile().AddDefinition(variableName, indexStream.str());
status.GetMakefile().AddDefinition(
variableName,
std::to_string(std::distance(varArgsExpanded.begin(), it)));
return true;
}

View File

@ -2,7 +2,6 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmMacroCommand.h"
#include <sstream>
#include <stdio.h>
#include <utility>
@ -64,9 +63,7 @@ bool cmMacroHelperCommand::operator()(
this->Policies);
// set the value of argc
std::ostringstream argcDefStream;
argcDefStream << expandedArgs.size();
std::string argcDef = argcDefStream.str();
std::string argcDef = std::to_string(expandedArgs.size());
std::vector<std::string>::const_iterator eit =
expandedArgs.begin() + (this->Args.size() - 1);

View File

@ -2813,9 +2813,7 @@ MessageType cmMakefile::ExpandVariablesInStringNew(
switch (var.domain) {
case NORMAL:
if (filename && lookup == lineVar) {
std::ostringstream ostr;
ostr << line;
varresult = ostr.str();
varresult = std::to_string(line);
} else {
value = this->GetDefinition(lookup);
}
@ -3248,9 +3246,7 @@ void cmMakefile::SetScriptModeFile(std::string const& scriptfile)
void cmMakefile::SetArgcArgv(const std::vector<std::string>& args)
{
std::ostringstream strStream;
strStream << args.size();
this->AddDefinition("CMAKE_ARGC", strStream.str());
this->AddDefinition("CMAKE_ARGC", std::to_string(args.size()));
// this->MarkVariableAsUsed("CMAKE_ARGC");
for (unsigned int t = 0; t < args.size(); ++t) {
@ -4498,10 +4494,10 @@ bool cmMakefile::HaveCStandardAvailable(cmTarget const* target,
const char* defaultCStandard =
this->GetDefinition("CMAKE_C_STANDARD_DEFAULT");
if (!defaultCStandard) {
std::ostringstream e;
e << "CMAKE_C_STANDARD_DEFAULT is not set. COMPILE_FEATURES support "
"not fully configured for this compiler.";
this->IssueMessage(MessageType::INTERNAL_ERROR, e.str());
this->IssueMessage(
MessageType::INTERNAL_ERROR,
"CMAKE_C_STANDARD_DEFAULT is not set. COMPILE_FEATURES support "
"not fully configured for this compiler.");
// Return true so the caller does not try to lookup the default standard.
return true;
}
@ -4582,10 +4578,10 @@ bool cmMakefile::HaveCxxStandardAvailable(cmTarget const* target,
const char* defaultCxxStandard =
this->GetDefinition("CMAKE_CXX_STANDARD_DEFAULT");
if (!defaultCxxStandard) {
std::ostringstream e;
e << "CMAKE_CXX_STANDARD_DEFAULT is not set. COMPILE_FEATURES support "
"not fully configured for this compiler.";
this->IssueMessage(MessageType::INTERNAL_ERROR, e.str());
this->IssueMessage(
MessageType::INTERNAL_ERROR,
"CMAKE_CXX_STANDARD_DEFAULT is not set. COMPILE_FEATURES support "
"not fully configured for this compiler.");
// Return true so the caller does not try to lookup the default standard.
return true;
}

View File

@ -1084,21 +1084,19 @@ void cmTarget::SetProperty(const std::string& prop, const char* value)
MAKE_STATIC_PROP(TYPE);
#undef MAKE_STATIC_PROP
if (prop == propMANUALLY_ADDED_DEPENDENCIES) {
std::ostringstream e;
e << "MANUALLY_ADDED_DEPENDENCIES property is read-only\n";
impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
impl->Makefile->IssueMessage(
MessageType::FATAL_ERROR,
"MANUALLY_ADDED_DEPENDENCIES property is read-only\n");
return;
}
if (prop == propNAME) {
std::ostringstream e;
e << "NAME property is read-only\n";
impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
"NAME property is read-only\n");
return;
}
if (prop == propTYPE) {
std::ostringstream e;
e << "TYPE property is read-only\n";
impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
"TYPE property is read-only\n");
return;
}
if (prop == propEXPORT_NAME && this->IsImported()) {
@ -1225,9 +1223,8 @@ void cmTarget::AppendProperty(const std::string& prop, const char* value,
return;
}
if (prop == "NAME") {
std::ostringstream e;
e << "NAME property is read-only\n";
impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
impl->Makefile->IssueMessage(MessageType::FATAL_ERROR,
"NAME property is read-only\n");
return;
}
if (prop == "EXPORT_NAME" && this->IsImported()) {

View File

@ -168,9 +168,7 @@ std::string cmTimestamp::AddTimestampComponent(char flag,
return std::string();
}
std::ostringstream ss;
ss << static_cast<long int>(difftime(timeT, unixEpoch));
return ss.str();
return std::to_string(static_cast<long int>(difftime(timeT, unixEpoch)));
}
default: {
return formatString;

View File

@ -2106,9 +2106,7 @@ int cmake::CheckBuildSystem()
// If no file is provided for the check, we have to rerun.
if (this->CheckBuildSystemArgument.empty()) {
if (verbose) {
std::ostringstream msg;
msg << "Re-run cmake no build system arguments\n";
cmSystemTools::Stdout(msg.str());
cmSystemTools::Stdout("Re-run cmake no build system arguments\n");
}
return 1;
}
@ -2191,10 +2189,8 @@ int cmake::CheckBuildSystem()
if (depends.empty() || outputs.empty()) {
// Not enough information was provided to do the test. Just rerun.
if (verbose) {
std::ostringstream msg;
msg << "Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
"or CMAKE_MAKEFILE_OUTPUTS :\n";
cmSystemTools::Stdout(msg.str());
cmSystemTools::Stdout("Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
"or CMAKE_MAKEFILE_OUTPUTS :\n");
}
return 1;
}
@ -2210,9 +2206,8 @@ int cmake::CheckBuildSystem()
}
} else {
if (verbose) {
std::ostringstream msg;
msg << "Re-run cmake: build system dependency is missing\n";
cmSystemTools::Stdout(msg.str());
cmSystemTools::Stdout(
"Re-run cmake: build system dependency is missing\n");
}
return 1;
}
@ -2229,9 +2224,8 @@ int cmake::CheckBuildSystem()
}
} else {
if (verbose) {
std::ostringstream msg;
msg << "Re-run cmake: build system output is missing\n";
cmSystemTools::Stdout(msg.str());
cmSystemTools::Stdout(
"Re-run cmake: build system output is missing\n");
}
return 1;
}