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

The `std::endl` manipulator, except inserting `\n` character, also
performs `os.flush()`, which may lead to undesired effects (like
disk I/O in the middle of forming data strings). For the
`std::stringstream` it also has no meaning.

* replace multiple `operator<<` calls on a string literal w/
  the only call and the only (bigger) string literal;
* replace one character string literal used in `operator<<`
  w/ a char literal.
This commit is contained in:
Alex Turbov 2020-03-24 12:19:36 +08:00
parent 7099db5dd4
commit 8e3a65d963
No known key found for this signature in database
GPG Key ID: 8BEDB7D11F95D5E3
4 changed files with 121 additions and 124 deletions

View File

@ -165,13 +165,15 @@ void cmGhsMultiTargetGenerator::WriteTargetSpecifics(std::ostream& fout,
outpath = this->GeneratorTarget->GetDirectory(config);
outpath =
this->LocalGenerator->MaybeConvertToRelativePath(rootpath, outpath);
fout << " :binDirRelative=\"" << outpath << "\"" << std::endl;
fout << " -o \"" << this->TargetNameReal << "\"" << std::endl;
/* clang-format off */
fout << " :binDirRelative=\"" << outpath << "\"\n"
" -o \"" << this->TargetNameReal << "\"\n";
/* clang-format on */
}
// set target object file destination
outpath = this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
fout << " :outputDirRelative=\"" << outpath << "\"" << std::endl;
fout << " :outputDirRelative=\"" << outpath << "\"\n";
}
void cmGhsMultiTargetGenerator::SetCompilerFlags(std::string const& config,
@ -232,7 +234,7 @@ void cmGhsMultiTargetGenerator::WriteCompilerFlags(std::ostream& fout,
std::vector<std::string> ghsCompFlags =
cmSystemTools::ParseArguments(flagsByLangI->second);
for (const std::string& f : ghsCompFlags) {
fout << " " << f << std::endl;
fout << " " << f << '\n';
}
}
}
@ -245,7 +247,7 @@ void cmGhsMultiTargetGenerator::WriteCompilerDefinitions(
this->GeneratorTarget->GetCompileDefinitions(compileDefinitions, config,
language);
for (std::string const& compileDefinition : compileDefinitions) {
fout << " -D" << compileDefinition << std::endl;
fout << " -D" << compileDefinition << '\n';
}
}
@ -258,7 +260,7 @@ void cmGhsMultiTargetGenerator::WriteIncludes(std::ostream& fout,
language, config);
for (std::string const& include : includes) {
fout << " -I\"" << include << "\"" << std::endl;
fout << " -I\"" << include << "\"\n";
}
}
@ -287,14 +289,14 @@ void cmGhsMultiTargetGenerator::WriteTargetLinkLine(std::ostream& fout,
// write out link options
std::vector<std::string> lopts = cmSystemTools::ParseArguments(linkFlags);
for (const std::string& l : lopts) {
fout << " " << l << std::endl;
fout << " " << l << '\n';
}
// write out link search paths
// must be quoted for paths that contain spaces
std::vector<std::string> lpath = cmSystemTools::ParseArguments(linkPath);
for (const std::string& l : lpath) {
fout << " -L\"" << l << "\"" << std::endl;
fout << " -L\"" << l << "\"\n";
}
// write out link libs
@ -305,10 +307,10 @@ void cmGhsMultiTargetGenerator::WriteTargetLinkLine(std::ostream& fout,
cmSystemTools::ParseArguments(linkLibraries);
for (const std::string& l : llibs) {
if (l.compare(0, 2, "-l") == 0) {
fout << " \"" << l << "\"" << std::endl;
fout << " \"" << l << "\"\n";
} else {
std::string rl = cmSystemTools::CollapseFullPath(l, cbd);
fout << " -l\"" << rl << "\"" << std::endl;
fout << " -l\"" << rl << "\"\n";
}
}
}
@ -349,13 +351,12 @@ void cmGhsMultiTargetGenerator::WriteBuildEventsHelper(
this->WriteCustomCommandsHelper(f, ccg);
f.Close();
if (this->TagType != GhsMultiGpj::CUSTOM_TARGET) {
fout << " :" << cmd << "=\"" << fname << "\"" << std::endl;
fout << " :" << cmd << "=\"" << fname << "\"\n";
} else {
fout << fname << std::endl;
fout << " :outputName=\"" << fname << ".rule\"" << std::endl;
fout << fname << "\n :outputName=\"" << fname << ".rule\"\n";
}
for (auto& byp : ccg.GetByproducts()) {
fout << " :extraOutputFile=\"" << byp << "\"" << std::endl;
fout << " :extraOutputFile=\"" << byp << "\"\n";
}
}
}
@ -447,8 +448,7 @@ void cmGhsMultiTargetGenerator::WriteCustomCommandsHelper(
// push back the custom commands
for (auto const& c : cmdLines) {
fout << c << std::endl;
fout << check_error << std::endl;
fout << c << '\n' << check_error << '\n';
}
}
@ -460,7 +460,7 @@ void cmGhsMultiTargetGenerator::WriteSourceProperty(
if (prop) {
std::vector<std::string> list = cmExpandedList(prop);
for (const std::string& p : list) {
fout << " " << propFlag << p << std::endl;
fout << " " << propFlag << p << '\n';
}
}
}
@ -575,12 +575,12 @@ void cmGhsMultiTargetGenerator::WriteSources(std::ostream& fout_proj)
if (useProjectFile) {
if (sg.empty()) {
*fout << "{comment} Others" << std::endl;
*fout << "{comment} Others" << '\n';
} else {
*fout << "{comment} " << sg << std::endl;
*fout << "{comment} " << sg << '\n';
}
} else if (sg.empty()) {
*fout << "{comment} Others" << std::endl;
*fout << "{comment} Others\n";
}
if (sg != "CMake Rules") {
@ -608,7 +608,7 @@ void cmGhsMultiTargetGenerator::WriteSources(std::ostream& fout_proj)
compile = false;
}
*fout << comment << fname << std::endl;
*fout << comment << fname << '\n';
if (compile) {
if ("ld" != si->GetExtension() && "int" != si->GetExtension() &&
"bsp" != si->GetExtension()) {
@ -624,7 +624,7 @@ void cmGhsMultiTargetGenerator::WriteSources(std::ostream& fout_proj)
std::string objectName = this->GeneratorTarget->GetObjectName(si);
if (!objectName.empty() &&
this->GeneratorTarget->HasExplicitObjectName(si)) {
*fout << " -o " << objectName << std::endl;
*fout << " -o " << objectName << '\n';
}
}
}
@ -691,14 +691,14 @@ void cmGhsMultiTargetGenerator::WriteCustomCommandLine(
*/
bool specifyExtra = true;
for (auto& out : ccg.GetOutputs()) {
fout << fname << std::endl;
fout << " :outputName=\"" << out << "\"" << std::endl;
fout << fname << '\n';
fout << " :outputName=\"" << out << "\"\n";
if (specifyExtra) {
for (auto& byp : ccg.GetByproducts()) {
fout << " :extraOutputFile=\"" << byp << "\"" << std::endl;
fout << " :extraOutputFile=\"" << byp << "\"\n";
}
for (auto& dep : ccg.GetDepends()) {
fout << " :depends=\"" << dep << "\"" << std::endl;
fout << " :depends=\"" << dep << "\"\n";
}
specifyExtra = false;
}
@ -713,7 +713,7 @@ void cmGhsMultiTargetGenerator::WriteObjectLangOverride(
std::string sourceLangProp(rawLangProp);
std::string const& extension = sourceFile->GetExtension();
if ("CXX" == sourceLangProp && ("c" == extension || "C" == extension)) {
fout << " -dotciscxx" << std::endl;
fout << " -dotciscxx\n";
}
}
}

View File

@ -254,14 +254,15 @@ void cmGlobalGhsMultiGenerator::GetToolset(cmMakefile* mf, std::string& tsd,
void cmGlobalGhsMultiGenerator::WriteFileHeader(std::ostream& fout)
{
fout << "#!gbuild" << std::endl;
fout << "#" << std::endl
<< "# CMAKE generated file: DO NOT EDIT!" << std::endl
<< "# Generated by \"" << GetActualName() << "\""
<< " Generator, CMake Version " << cmVersion::GetMajorVersion() << "."
<< cmVersion::GetMinorVersion() << std::endl
<< "#" << std::endl
<< std::endl;
/* clang-format off */
fout << "#!gbuild\n"
"#\n"
"# CMAKE generated file: DO NOT EDIT!\n"
"# Generated by \"" << GetActualName() << "\""
" Generator, CMake Version " << cmVersion::GetMajorVersion() << '.'
<< cmVersion::GetMinorVersion() << "\n"
"#\n\n";
/* clang-format on */
}
void cmGlobalGhsMultiGenerator::WriteCustomRuleBOD(std::ostream& fout)
@ -269,36 +270,36 @@ void cmGlobalGhsMultiGenerator::WriteCustomRuleBOD(std::ostream& fout)
fout << "Commands {\n"
" Custom_Rule_Command {\n"
" name = \"Custom Rule Command\"\n"
" exec = \"";
" exec = \""
#ifdef _WIN32
fout << "cmd.exe";
"cmd.exe"
#else
fout << "/bin/sh";
"/bin/sh"
#endif
fout << "\"\n"
"\"\n"
" options = {\"SpecialOptions\"}\n"
" }\n"
"}\n";
"}\n"
fout << "\n\n";
fout << "FileTypes {\n"
"\n\n"
"FileTypes {\n"
" CmakeRule {\n"
" name = \"Custom Rule\"\n"
" action = \"&Run\"\n"
" extensions = {\"";
" extensions = {\""
#ifdef _WIN32
fout << "bat";
"bat"
#else
fout << "sh";
"sh"
#endif
fout << "\"}\n"
"\"}\n"
" grepable = false\n"
" command = \"Custom Rule Command\"\n"
" commandLine = \"$COMMAND ";
" commandLine = \"$COMMAND "
#ifdef _WIN32
fout << "/c";
"/c"
#endif
fout << " $INPUTFILE\"\n"
" $INPUTFILE\"\n"
" progress = \"Processing Custom Rule\"\n"
" promoteToFirstPass = true\n"
" outputType = \"None\"\n"
@ -328,13 +329,13 @@ void cmGlobalGhsMultiGenerator::WriteTopLevelProject(std::ostream& fout,
this->WriteHighLevelDirectives(root, fout);
GhsMultiGpj::WriteGpjTag(GhsMultiGpj::PROJECT, fout);
fout << "# Top Level Project File" << std::endl;
fout << "# Top Level Project File\n";
// Specify BSP option if supplied by user
const char* bspName =
this->GetCMakeInstance()->GetCacheDefinition("GHS_BSP_NAME");
if (!cmIsOff(bspName)) {
fout << " -bsp " << bspName << std::endl;
fout << " -bsp " << bspName << '\n';
}
// Specify OS DIR if supplied by user
@ -349,14 +350,14 @@ void cmGlobalGhsMultiGenerator::WriteTopLevelProject(std::ostream& fout,
} else {
fout << osDirOption;
}
fout << "\"" << this->OsDir << "\"" << std::endl;
fout << "\"" << this->OsDir << "\"\n";
}
}
void cmGlobalGhsMultiGenerator::WriteSubProjects(std::ostream& fout,
std::string& all_target)
{
fout << "CMakeFiles/" << all_target << " [Project]" << std::endl;
fout << "CMakeFiles/" << all_target << " [Project]\n";
// All known targets
for (cmGeneratorTarget const* target : this->ProjectTargets) {
if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
@ -367,7 +368,7 @@ void cmGlobalGhsMultiGenerator::WriteSubProjects(std::ostream& fout,
continue;
}
fout << "CMakeFiles/" << target->GetName() + ".tgt" + FILE_EXTENSION
<< " [Project]" << std::endl;
<< " [Project]\n";
}
}
@ -391,7 +392,7 @@ void cmGlobalGhsMultiGenerator::WriteProjectLine(
std::string projFile = dir + projName + FILE_EXTENSION;
fout << projFile;
fout << " " << projType << std::endl;
fout << ' ' << projType << '\n';
} else {
/* Should never happen */
std::string message =
@ -613,14 +614,14 @@ cmGlobalGhsMultiGenerator::GenerateBuildCommand(
void cmGlobalGhsMultiGenerator::WriteMacros(std::ostream& fout,
cmLocalGenerator* root)
{
fout << "macro PROJ_NAME=" << root->GetProjectName() << std::endl;
fout << "macro PROJ_NAME=" << root->GetProjectName() << '\n';
char const* ghsGpjMacros =
this->GetCMakeInstance()->GetCacheDefinition("GHS_GPJ_MACROS");
if (nullptr != ghsGpjMacros) {
std::vector<std::string> expandedList =
cmExpandedList(std::string(ghsGpjMacros));
for (std::string const& arg : expandedList) {
fout << "macro " << arg << std::endl;
fout << "macro " << arg << '\n';
}
}
}
@ -643,17 +644,19 @@ void cmGlobalGhsMultiGenerator::WriteHighLevelDirectives(
tgt = cmStrCat((a ? a : ""), '_', (p ? p : ""), ".tgt");
}
fout << "primaryTarget=" << tgt << std::endl;
fout << "customization=" << root->GetBinaryDirectory()
<< "/CMakeFiles/custom_rule.bod" << std::endl;
fout << "customization=" << root->GetBinaryDirectory()
<< "/CMakeFiles/custom_target.bod" << std::endl;
/* clang-format off */
fout << "primaryTarget=" << tgt << "\n"
"customization=" << root->GetBinaryDirectory()
<< "/CMakeFiles/custom_rule.bod\n"
"customization=" << root->GetBinaryDirectory()
<< "/CMakeFiles/custom_target.bod" << '\n';
/* clang-format on */
char const* const customization =
this->GetCMakeInstance()->GetCacheDefinition("GHS_CUSTOMIZATION");
if (nullptr != customization && strlen(customization) > 0) {
fout << "customization="
<< cmGlobalGhsMultiGenerator::TrimQuotes(customization) << std::endl;
<< cmGlobalGhsMultiGenerator::TrimQuotes(customization) << '\n';
this->GetCMakeInstance()->MarkCliAsUsed("GHS_CUSTOMIZATION");
}
}

View File

@ -298,13 +298,13 @@ void cmGraphVizWriter::WriteHeader(cmGeneratedFileStream& fs,
const std::string& name)
{
auto const escapedGraphName = EscapeForDotFile(name);
fs << "digraph \"" << escapedGraphName << "\" {" << std::endl;
fs << this->GraphHeader << std::endl;
fs << "digraph \"" << escapedGraphName << "\" {\n"
<< this->GraphHeader << '\n';
}
void cmGraphVizWriter::WriteFooter(cmGeneratedFileStream& fs)
{
fs << "}" << std::endl;
fs << "}\n";
}
void cmGraphVizWriter::WriteLegend(cmGeneratedFileStream& fs)
@ -312,52 +312,46 @@ void cmGraphVizWriter::WriteLegend(cmGeneratedFileStream& fs)
// Note that the subgraph name must start with "cluster", as done here, to
// make Graphviz layout engines do the right thing and keep the nodes
// together.
fs << "subgraph clusterLegend {" << std::endl;
fs << " label = \"Legend\";" << std::endl;
// Set the color of the box surrounding the legend.
fs << " color = black;" << std::endl;
// We use invisible edges just to enforce the layout.
fs << " edge [ style = invis ];" << std::endl;
// Nodes.
fs << " legendNode0 [ label = \"Executable\", shape = "
<< GRAPHVIZ_NODE_SHAPE_EXECUTABLE << " ];" << std::endl;
fs << " legendNode1 [ label = \"Static Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_STATIC << " ];" << std::endl;
fs << " legendNode2 [ label = \"Shared Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_SHARED << " ];" << std::endl;
fs << " legendNode3 [ label = \"Module Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_MODULE << " ];" << std::endl;
fs << " legendNode4 [ label = \"Interface Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_INTERFACE << " ];" << std::endl;
fs << " legendNode5 [ label = \"Object Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_OBJECT << " ];" << std::endl;
fs << " legendNode6 [ label = \"Unknown Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_UNKNOWN << " ];" << std::endl;
fs << " legendNode7 [ label = \"Custom Target\", shape = "
<< GRAPHVIZ_NODE_SHAPE_UTILITY << " ];" << std::endl;
// Edges.
// Some of those are dummy (invisible) edges to enforce a layout.
fs << " legendNode0 -> legendNode1 [ style = " << GRAPHVIZ_EDGE_STYLE_PUBLIC
<< " ];" << std::endl;
fs << " legendNode0 -> legendNode2 [ style = " << GRAPHVIZ_EDGE_STYLE_PUBLIC
<< " ];" << std::endl;
fs << " legendNode0 -> legendNode3;" << std::endl;
fs << " legendNode1 -> legendNode4 [ label = \"Interface\", style = "
<< GRAPHVIZ_EDGE_STYLE_INTERFACE << " ];" << std::endl;
fs << " legendNode2 -> legendNode5 [ label = \"Private\", style = "
<< GRAPHVIZ_EDGE_STYLE_PRIVATE << " ];" << std::endl;
fs << " legendNode3 -> legendNode6 [ style = " << GRAPHVIZ_EDGE_STYLE_PUBLIC
<< " ];" << std::endl;
fs << " legendNode0 -> legendNode7;" << std::endl;
fs << "}" << std::endl;
/* clang-format off */
fs << "subgraph clusterLegend {\n"
" label = \"Legend\";\n"
// Set the color of the box surrounding the legend.
" color = black;\n"
// We use invisible edges just to enforce the layout.
" edge [ style = invis ];\n"
// Nodes.
" legendNode0 [ label = \"Executable\", shape = "
<< GRAPHVIZ_NODE_SHAPE_EXECUTABLE << " ];\n"
" legendNode1 [ label = \"Static Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_STATIC << " ];\n"
" legendNode2 [ label = \"Shared Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_SHARED << " ];\n"
" legendNode3 [ label = \"Module Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_MODULE << " ];\n"
" legendNode4 [ label = \"Interface Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_INTERFACE << " ];\n"
" legendNode5 [ label = \"Object Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_OBJECT << " ];\n"
" legendNode6 [ label = \"Unknown Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_UNKNOWN << " ];\n"
" legendNode7 [ label = \"Custom Target\", shape = "
<< GRAPHVIZ_NODE_SHAPE_UTILITY << " ];\n"
// Edges.
// Some of those are dummy (invisible) edges to enforce a layout.
" legendNode0 -> legendNode1 [ style = "
<< GRAPHVIZ_EDGE_STYLE_PUBLIC << " ];\n"
" legendNode0 -> legendNode2 [ style = "
<< GRAPHVIZ_EDGE_STYLE_PUBLIC << " ];\n"
" legendNode0 -> legendNode3;\n"
" legendNode1 -> legendNode4 [ label = \"Interface\", style = "
<< GRAPHVIZ_EDGE_STYLE_INTERFACE << " ];\n"
" legendNode2 -> legendNode5 [ label = \"Private\", style = "
<< GRAPHVIZ_EDGE_STYLE_PRIVATE << " ];\n"
" legendNode3 -> legendNode6 [ style = "
<< GRAPHVIZ_EDGE_STYLE_PUBLIC << " ];\n"
" legendNode0 -> legendNode7;\n"
"}\n";
/* clang-format off */
}
void cmGraphVizWriter::WriteNode(cmGeneratedFileStream& fs,
@ -370,7 +364,7 @@ void cmGraphVizWriter::WriteNode(cmGeneratedFileStream& fs,
auto const escapedLabel = EscapeForDotFile(itemNameWithAliases);
fs << " \"" << nodeName << "\" [ label = \"" << escapedLabel
<< "\", shape = " << getShapeForTarget(item) << " ];" << std::endl;
<< "\", shape = " << getShapeForTarget(item) << " ];\n";
}
void cmGraphVizWriter::WriteConnection(cmGeneratedFileStream& fs,
@ -382,11 +376,9 @@ void cmGraphVizWriter::WriteConnection(cmGeneratedFileStream& fs,
auto const& dependeeName = dependee.AsStr();
fs << " \"" << this->NodeNames[dependerName] << "\" -> \""
<< this->NodeNames[dependeeName] << "\" ";
fs << edgeStyle;
fs << " // " << dependerName << " -> " << dependeeName << std::endl;
<< this->NodeNames[dependeeName] << "\" "
<< edgeStyle
<< " // " << dependerName << " -> " << dependeeName << '\n';
}
bool cmGraphVizWriter::ItemExcluded(cmLinkItem const& item)

View File

@ -678,7 +678,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string> const& args)
} else if (!a.empty() && a[0] == '-') {
// Environment variable and command names cannot start in '-',
// so this must be an unknown option.
std::cerr << "cmake -E env: unknown option '" << a << "'"
std::cerr << "cmake -E env: unknown option '" << a << '\''
<< std::endl;
return 1;
} else if (a.find('=') != std::string::npos) {
@ -1654,11 +1654,13 @@ int cmcmd::WindowsCEEnvironment(const char* version, const std::string& name)
cmVisualStudioWCEPlatformParser parser(name.c_str());
parser.ParseVersion(version);
if (parser.Found()) {
std::cout << "@echo off" << std::endl;
std::cout << "echo Environment Selection: " << name << std::endl;
std::cout << "set PATH=" << parser.GetPathDirectories() << std::endl;
std::cout << "set INCLUDE=" << parser.GetIncludeDirectories() << std::endl;
std::cout << "set LIB=" << parser.GetLibraryDirectories() << std::endl;
/* clang-format off */
std::cout << "@echo off\n"
"echo Environment Selection: " << name << "\n"
"set PATH=" << parser.GetPathDirectories() << "\n"
"set INCLUDE=" << parser.GetIncludeDirectories() << "\n"
"set LIB=" << parser.GetLibraryDirectories() << std::endl;
/* clang-format on */
return 0;
}
#else