Refactor: Avoid std::endl where it's not necessary (part 1)

The `std::endl` manupulator, except inserting `\n` character, also
performs `os.flush()`, which may leads to undesired effects (like
disk I/O in the middle of forming data strings). For the
`std::stringstream` it also has no meaning.
This commit is contained in:
Alex Turbov 2020-03-22 21:54:31 +08:00
parent ea54f8d441
commit 3fdd8db3aa
9 changed files with 101 additions and 111 deletions

View File

@ -77,7 +77,7 @@ bool cmCacheManager::LoadCache(const std::string& path, bool internal,
}
while (realbuffer[0] == '/' && realbuffer[1] == '/') {
if ((realbuffer[2] == '\\') && (realbuffer[3] == 'n')) {
helpString += "\n";
helpString += '\n';
helpString += &realbuffer[4];
} else {
helpString += &realbuffer[2];
@ -117,8 +117,8 @@ bool cmCacheManager::LoadCache(const std::string& path, bool internal,
}
} else {
std::ostringstream error;
error << "Parse error in cache file " << cacheFile;
error << " on line " << lineno << ". Offending entry: " << realbuffer;
error << "Parse error in cache file " << cacheFile << " on line "
<< lineno << ". Offending entry: " << realbuffer;
cmSystemTools::Error(error.str());
}
}
@ -217,7 +217,7 @@ void cmCacheManager::WritePropertyEntries(std::ostream& os, CacheIterator i,
cmCacheManager::OutputKey(os, key);
os << ":INTERNAL=";
cmCacheManager::OutputValue(os, *value);
os << "\n";
os << '\n';
cmCacheManager::OutputNewlineTruncationWarning(os, key, *value,
messenger);
}
@ -268,31 +268,29 @@ bool cmCacheManager::SaveCache(const std::string& path, cmMessenger* messenger)
/* clang-format off */
fout << "# This is the CMakeCache file.\n"
<< "# For build in directory: " << currentcwd << "\n"
<< "# It was generated by CMake: "
<< cmSystemTools::GetCMakeCommand() << std::endl;
"# For build in directory: " << currentcwd << "\n"
"# It was generated by CMake: "
<< cmSystemTools::GetCMakeCommand()
<< "\n"
"# You can edit this file to change values found and used by cmake."
"\n"
"# If you do not want to change any of the values, simply exit the "
"editor.\n"
"# If you do want to change a value, simply edit, save, and exit "
"the editor.\n"
"# The syntax for the file is as follows:\n"
"# KEY:TYPE=VALUE\n"
"# KEY is the name of a variable in the cache.\n"
"# TYPE is a hint to GUIs for the type of VALUE, DO NOT EDIT TYPE!."
"\n"
"# VALUE is the current value for the KEY.\n"
"\n"
"########################\n"
"# EXTERNAL cache entries\n"
"########################\n"
"\n";
/* clang-format on */
/* clang-format off */
fout << "# You can edit this file to change values found and used by cmake."
<< std::endl
<< "# If you do not want to change any of the values, simply exit the "
"editor." << std::endl
<< "# If you do want to change a value, simply edit, save, and exit "
"the editor." << std::endl
<< "# The syntax for the file is as follows:\n"
<< "# KEY:TYPE=VALUE\n"
<< "# KEY is the name of a variable in the cache.\n"
<< "# TYPE is a hint to GUIs for the type of VALUE, DO NOT EDIT "
"TYPE!." << std::endl
<< "# VALUE is the current value for the KEY.\n\n";
/* clang-format on */
fout << "########################\n";
fout << "# EXTERNAL cache entries\n";
fout << "########################\n";
fout << "\n";
for (auto const& i : this->Cache) {
CacheEntry const& ce = i.second;
cmStateEnums::CacheEntryType t = ce.Type;
@ -309,20 +307,20 @@ bool cmCacheManager::SaveCache(const std::string& path, cmMessenger* messenger)
cmCacheManager::OutputHelpString(fout, "Missing description");
}
cmCacheManager::OutputKey(fout, i.first);
fout << ":" << cmState::CacheEntryTypeToString(t) << "=";
fout << ':' << cmState::CacheEntryTypeToString(t) << '=';
cmCacheManager::OutputValue(fout, ce.Value);
fout << "\n";
fout << '\n';
cmCacheManager::OutputNewlineTruncationWarning(fout, i.first, ce.Value,
messenger);
fout << "\n";
fout << '\n';
}
}
fout << "\n";
fout << "########################\n";
fout << "# INTERNAL cache entries\n";
fout << "########################\n";
fout << "\n";
fout << "\n"
"########################\n"
"# INTERNAL cache entries\n"
"########################\n"
"\n";
for (cmCacheManager::CacheIterator i = this->NewIterator(); !i.IsAtEnd();
i.Next()) {
@ -338,14 +336,14 @@ bool cmCacheManager::SaveCache(const std::string& path, cmMessenger* messenger)
cmCacheManager::OutputHelpString(fout, *help);
}
cmCacheManager::OutputKey(fout, i.GetName());
fout << ":" << cmState::CacheEntryTypeToString(t) << "=";
fout << ':' << cmState::CacheEntryTypeToString(t) << '=';
cmCacheManager::OutputValue(fout, i.GetValue());
fout << "\n";
fout << '\n';
cmCacheManager::OutputNewlineTruncationWarning(fout, i.GetName(),
i.GetValue(), messenger);
}
}
fout << "\n";
fout << '\n';
fout.Close();
std::string checkCacheFile = cmStrCat(path, "/CMakeFiles");
cmSystemTools::MakeDirectory(checkCacheFile);
@ -428,7 +426,7 @@ void cmCacheManager::OutputHelpString(std::ostream& fout,
fout << "\\n";
}
oneLine = helpString.substr(pos, i - pos);
fout << oneLine << "\n";
fout << oneLine << '\n';
pos = i;
}
}
@ -450,7 +448,7 @@ void cmCacheManager::OutputWarningComment(std::ostream& fout,
fout << "\\n";
}
oneLine = message.substr(pos, i - pos);
fout << oneLine << "\n";
fout << oneLine << '\n';
pos = i;
}
}
@ -517,17 +515,17 @@ cmProp cmCacheManager::GetInitializedCacheValue(const std::string& key) const
void cmCacheManager::PrintCache(std::ostream& out) const
{
out << "=================================================" << std::endl;
out << "CMakeCache Contents:" << std::endl;
out << "=================================================\n"
"CMakeCache Contents:\n";
for (auto const& i : this->Cache) {
if (i.second.Type != cmStateEnums::INTERNAL) {
out << i.first << " = " << i.second.Value << std::endl;
out << i.first << " = " << i.second.Value << '\n';
}
}
out << "\n\n";
out << "To change values in the CMakeCache, " << std::endl
<< "edit CMakeCache.txt in your output directory.\n";
out << "=================================================" << std::endl;
out << "\n\n"
"To change values in the CMakeCache, \n"
"edit CMakeCache.txt in your output directory.\n"
"=================================================\n";
}
void cmCacheManager::AddCacheEntry(const std::string& key, const char* value,

View File

@ -43,8 +43,7 @@ static bool LogErrorsAsMessages;
if (LogErrorsAsMessages) { \
std::ostringstream _hresult_oss; \
_hresult_oss.flags(std::ios::hex); \
_hresult_oss << context << " failed HRESULT, hr = 0x" << hr \
<< std::endl; \
_hresult_oss << context << " failed HRESULT, hr = 0x" << hr << '\n'; \
_hresult_oss.flags(std::ios::dec); \
_hresult_oss << __FILE__ << "(" << __LINE__ << ")"; \
cmSystemTools::Message(_hresult_oss.str()); \
@ -98,32 +97,37 @@ HRESULT InstanceCallMacro(IDispatch* vsIDE, const std::string& macro,
DISPATCH_METHOD, &params, &result, &excep, &arg);
std::ostringstream oss;
oss << std::endl;
oss << "Invoke(ExecuteCommand)" << std::endl;
oss << " Macro: " << macro << std::endl;
oss << " Args: " << args << std::endl;
/* clang-format off */
oss << "\nInvoke(ExecuteCommand)\n"
" Macro: " << macro << "\n"
" Args: " << args << '\n';
/* clang-format on */
if (DISP_E_EXCEPTION == hr) {
oss << "DISP_E_EXCEPTION EXCEPINFO:" << excep.wCode << std::endl;
oss << " wCode: " << excep.wCode << std::endl;
oss << " wReserved: " << excep.wReserved << std::endl;
/* clang-format off */
oss << "DISP_E_EXCEPTION EXCEPINFO:" << excep.wCode << "\n"
" wCode: " << excep.wCode << "\n"
" wReserved: " << excep.wReserved << '\n';
/* clang-format on */
if (excep.bstrSource) {
oss << " bstrSource: " << (const char*)(_bstr_t)excep.bstrSource
<< std::endl;
<< '\n';
}
if (excep.bstrDescription) {
oss << " bstrDescription: "
<< (const char*)(_bstr_t)excep.bstrDescription << std::endl;
<< (const char*)(_bstr_t)excep.bstrDescription << '\n';
}
if (excep.bstrHelpFile) {
oss << " bstrHelpFile: " << (const char*)(_bstr_t)excep.bstrHelpFile
<< std::endl;
<< '\n';
}
oss << " dwHelpContext: " << excep.dwHelpContext << std::endl;
oss << " pvReserved: " << excep.pvReserved << std::endl;
oss << " pfnDeferredFillIn: "
<< reinterpret_cast<void*>(excep.pfnDeferredFillIn) << std::endl;
oss << " scode: " << excep.scode << std::endl;
/* clang-format off */
oss << " dwHelpContext: " << excep.dwHelpContext << "\n"
" pvReserved: " << excep.pvReserved << "\n"
" pfnDeferredFillIn: "
<< reinterpret_cast<void*>(excep.pfnDeferredFillIn) << "\n"
" scode: " << excep.scode << '\n';
/* clang-format on */
}
std::string exstr(oss.str());

View File

@ -276,8 +276,7 @@ static bool checkInterfaceDirs(const std::string& prepro,
<< "\"\nhowever it is also "
"a subdirectory of the "
<< (inBinary ? "build" : "source") << " tree:\n \""
<< (inBinary ? topBinaryDir : topSourceDir) << "\""
<< std::endl;
<< (inBinary ? topBinaryDir : topSourceDir) << "\"\n";
target->GetLocalGenerator()->IssueMessage(
MessageType::AUTHOR_WARNING, s.str());
CM_FALLTHROUGH;

View File

@ -1011,7 +1011,7 @@ void cmGlobalNinjaGenerator::AddCXXCompileCommand(
cm::make_unique<cmGeneratedFileStream>(buildFilePath);
*this->CompileCommandsStream << "[";
} else {
*this->CompileCommandsStream << "," << std::endl;
*this->CompileCommandsStream << ",\n";
}
std::string sourceFileName = sourceFile;

View File

@ -159,7 +159,7 @@ void cmGlobalUnixMakefileGenerator3::Generate()
this->WriteMainCMakefile();
if (this->CommandDatabase) {
*this->CommandDatabase << std::endl << "]";
*this->CommandDatabase << "\n]";
this->CommandDatabase.reset();
}
}
@ -174,21 +174,20 @@ void cmGlobalUnixMakefileGenerator3::AddCXXCompileCommand(
"/compile_commands.json";
this->CommandDatabase =
cm::make_unique<cmGeneratedFileStream>(commandDatabaseName);
*this->CommandDatabase << "[" << std::endl;
*this->CommandDatabase << "[\n";
} else {
*this->CommandDatabase << "," << std::endl;
*this->CommandDatabase << ",\n";
}
*this->CommandDatabase << "{" << std::endl
*this->CommandDatabase << "{\n"
<< R"( "directory": ")"
<< cmGlobalGenerator::EscapeJSON(workingDirectory)
<< "\"," << std::endl
<< "\",\n"
<< R"( "command": ")"
<< cmGlobalGenerator::EscapeJSON(compileCommand)
<< "\"," << std::endl
<< "\",\n"
<< R"( "file": ")"
<< cmGlobalGenerator::EscapeJSON(sourceFile) << "\""
<< std::endl
<< "}";
<< cmGlobalGenerator::EscapeJSON(sourceFile)
<< "\"\n}";
}
void cmGlobalUnixMakefileGenerator3::WriteMainMakefile2()

View File

@ -206,9 +206,8 @@ void cmLocalNinjaGenerator::WriteBuildFileTop()
void cmLocalNinjaGenerator::WriteProjectHeader(std::ostream& os)
{
cmGlobalNinjaGenerator::WriteDivider(os);
os << "# Project: " << this->GetProjectName() << std::endl
<< "# Configurations: " << cmJoin(this->GetConfigNames(), ", ")
<< std::endl;
os << "# Project: " << this->GetProjectName() << '\n'
<< "# Configurations: " << cmJoin(this->GetConfigNames(), ", ") << '\n';
cmGlobalNinjaGenerator::WriteDivider(os);
}
@ -235,8 +234,7 @@ void cmLocalNinjaGenerator::WriteNinjaRequiredVersion(std::ostream& os)
cmGlobalNinjaGenerator::WriteComment(
os, "Minimal version of Ninja required by this file");
os << "ninja_required_version = " << requiredVersion << std::endl
<< std::endl;
os << "ninja_required_version = " << requiredVersion << "\n\n";
}
void cmLocalNinjaGenerator::WriteNinjaConfigurationVariable(
@ -265,9 +263,8 @@ void cmLocalNinjaGenerator::WritePools(std::ostream& os)
unsigned int jobs;
if (eq != std::string::npos &&
sscanf(pool.c_str() + eq, "=%u", &jobs) == 1) {
os << "pool " << pool.substr(0, eq) << std::endl;
os << " depth = " << jobs << std::endl;
os << std::endl;
os << "pool " << pool.substr(0, eq) << "\n depth = " << jobs
<< "\n\n";
} else {
cmSystemTools::Error("Invalid pool defined by property 'JOB_POOLS': " +
pool);
@ -279,8 +276,7 @@ void cmLocalNinjaGenerator::WritePools(std::ostream& os)
void cmLocalNinjaGenerator::WriteNinjaFilesInclusionConfig(std::ostream& os)
{
cmGlobalNinjaGenerator::WriteDivider(os);
os << "# Include auxiliary files.\n"
<< "\n";
os << "# Include auxiliary files.\n\n";
cmGlobalNinjaGenerator* ng = this->GetGlobalNinjaGenerator();
std::string const ninjaCommonFile =
ng->NinjaOutputPath(cmGlobalNinjaMultiGenerator::NINJA_COMMON_FILE);
@ -293,8 +289,7 @@ void cmLocalNinjaGenerator::WriteNinjaFilesInclusionConfig(std::ostream& os)
void cmLocalNinjaGenerator::WriteNinjaFilesInclusionCommon(std::ostream& os)
{
cmGlobalNinjaGenerator::WriteDivider(os);
os << "# Include auxiliary files.\n"
<< "\n";
os << "# Include auxiliary files.\n\n";
cmGlobalNinjaGenerator* ng = this->GetGlobalNinjaGenerator();
std::string const ninjaRulesFile =
ng->NinjaOutputPath(cmGlobalNinjaGenerator::NINJA_RULES_FILE);
@ -307,14 +302,14 @@ void cmLocalNinjaGenerator::WriteNinjaFilesInclusionCommon(std::ostream& os)
void cmLocalNinjaGenerator::WriteProcessedMakefile(std::ostream& os)
{
cmGlobalNinjaGenerator::WriteDivider(os);
os << "# Write statements declared in CMakeLists.txt:" << std::endl
os << "# Write statements declared in CMakeLists.txt:\n"
<< "# " << this->Makefile->GetDefinition("CMAKE_CURRENT_LIST_FILE")
<< std::endl;
<< '\n';
if (this->IsRootMakefile()) {
os << "# Which is the root file." << std::endl;
os << "# Which is the root file.\n";
}
cmGlobalNinjaGenerator::WriteDivider(os);
os << std::endl;
os << '\n';
}
void cmLocalNinjaGenerator::AppendTargetOutputs(cmGeneratorTarget* target,

View File

@ -1328,10 +1328,9 @@ bool cmLocalUnixMakefileGenerator3::UpdateDependencies(
int result;
if (!ftc->Compare(internalDependFile, tgtInfo, &result) || result < 0) {
if (verbose) {
std::ostringstream msg;
msg << "Dependee \"" << tgtInfo << "\" is newer than depender \""
<< internalDependFile << "\"." << std::endl;
cmSystemTools::Stdout(msg.str());
cmSystemTools::Stdout(cmStrCat("Dependee \"", tgtInfo,
"\" is newer than depender \"",
internalDependFile, "\".\n"));
}
needRescanDependInfo = true;
}
@ -1348,10 +1347,9 @@ bool cmLocalUnixMakefileGenerator3::UpdateDependencies(
if (!ftc->Compare(internalDependFile, dirInfoFile, &result) ||
result < 0) {
if (verbose) {
std::ostringstream msg;
msg << "Dependee \"" << dirInfoFile << "\" is newer than depender \""
<< internalDependFile << "\"." << std::endl;
cmSystemTools::Stdout(msg.str());
cmSystemTools::Stdout(cmStrCat("Dependee \"", dirInfoFile,
"\" is newer than depender \"",
internalDependFile, "\".\n"));
}
needRescanDirInfo = true;
}
@ -1517,11 +1515,9 @@ void cmLocalUnixMakefileGenerator3::CheckMultipleOutputs(bool verbose)
if (cmSystemTools::FileExists(dependee) &&
!cmSystemTools::FileExists(depender)) {
if (verbose) {
std::ostringstream msg;
msg << "Deleting primary custom command output \"" << dependee
<< "\" because another output \"" << depender
<< "\" does not exist." << std::endl;
cmSystemTools::Stdout(msg.str());
cmSystemTools::Stdout(cmStrCat(
"Deleting primary custom command output \"", dependee,
"\" because another output \"", depender, "\" does not exist.\n"));
}
cmSystemTools::RemoveFile(dependee);
}
@ -1969,7 +1965,7 @@ std::string cmLocalUnixMakefileGenerator3::GetRecursiveMakeCall(
void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream& os)
{
os << "#======================================"
<< "=======================================\n";
"=======================================\n";
}
void cmLocalUnixMakefileGenerator3::WriteCMakeArgument(std::ostream& os,
@ -1977,7 +1973,7 @@ void cmLocalUnixMakefileGenerator3::WriteCMakeArgument(std::ostream& os,
{
// Write the given string to the stream with escaping to get it back
// into CMake through the lexical scanner.
os << "\"";
os << '"';
for (char c : s) {
if (c == '\\') {
os << "\\\\";
@ -1987,7 +1983,7 @@ void cmLocalUnixMakefileGenerator3::WriteCMakeArgument(std::ostream& os,
os << c;
}
}
os << "\"";
os << '"';
}
std::string cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(

View File

@ -300,8 +300,7 @@ void cmMakefileTargetGenerator::WriteCommonCodeRules()
dependFileNameFull, false, this->GlobalGenerator->GetMakefileEncoding());
depFileStream << "# Empty dependencies file for "
<< this->GeneratorTarget->GetName() << ".\n"
<< "# This may be replaced when dependencies are built."
<< std::endl;
<< "# This may be replaced when dependencies are built.\n";
}
// Open the flags file. This should be copy-if-different because the

View File

@ -72,7 +72,7 @@ bool cmWriteFileCommand(std::vector<std::string> const& args,
status.SetError(error);
return false;
}
file << message << std::endl;
file << message << '\n';
file.close();
if (mode && !writable) {
cmSystemTools::SetPermissions(fileName.c_str(), mode);