Skip to content

Commit 7891f69

Browse files
bradkingkwrobot
authored andcommitted
Merge topic 'autogen_cmStrCat'
ca29231 Autogen: Modernize to use cmStrCat for string concatenation d02a99d Autogen: Modernize code to use cm::string_view for the info writer Acked-by: Kitware Robot <[email protected]> Merge-request: !3663
2 parents 6f3c429 + ca29231 commit 7891f69

7 files changed

+174
-285
lines changed

Source/cmQtAutoGen.cxx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,8 @@ bool cmQtAutoGen::RccLister::list(std::string const& qrcFile,
353353

354354
// Log command
355355
if (verbose) {
356-
std::string msg = "Running command:\n";
357-
msg += QuotedCommand(cmd);
358-
msg += '\n';
356+
std::string msg =
357+
cmStrCat("Running command:\n", QuotedCommand(cmd), '\n');
359358
cmSystemTools::Stdout(msg);
360359
}
361360

Source/cmQtAutoGenGlobalInitializer.cxx

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
22
file Copyright.txt or https://cmake.org/licensing for details. */
33
#include "cmQtAutoGenGlobalInitializer.h"
4-
#include "cmQtAutoGen.h"
5-
#include "cmQtAutoGenInitializer.h"
64

75
#include "cmCustomCommandLines.h"
86
#include "cmDuration.h"
@@ -11,15 +9,18 @@
119
#include "cmMakefile.h"
1210
#include "cmMessageType.h"
1311
#include "cmProcessOutput.h"
12+
#include "cmQtAutoGen.h"
13+
#include "cmQtAutoGenInitializer.h"
1414
#include "cmState.h"
1515
#include "cmStateTypes.h"
16+
#include "cmStringAlgorithms.h"
1617
#include "cmSystemTools.h"
1718
#include "cmTarget.h"
1819

19-
#include <utility>
20-
2120
#include "cm_memory.hxx"
2221

22+
#include <utility>
23+
2324
cmQtAutoGenGlobalInitializer::Keywords::Keywords()
2425
: AUTOMOC("AUTOMOC")
2526
, AUTOUIC("AUTOUIC")
@@ -119,23 +120,17 @@ cmQtAutoGenGlobalInitializer::cmQtAutoGenGlobalInitializer(
119120
bool const uicDisabled = (uic && !uicAvailable);
120121
bool const rccDisabled = (rcc && !rccAvailable);
121122
if (mocDisabled || uicDisabled || rccDisabled) {
122-
std::string msg = "AUTOGEN: No valid Qt version found for target ";
123-
msg += target->GetName();
124-
msg += ". ";
125-
msg += cmQtAutoGen::Tools(mocDisabled, uicDisabled, rccDisabled);
126-
msg += " disabled. Consider adding:\n";
127-
{
128-
std::string version = (qtVersion.second == 0)
129-
? std::string("<QTVERSION>")
130-
: std::to_string(qtVersion.second);
131-
std::string comp = uicDisabled ? "Widgets" : "Core";
132-
msg += " find_package(Qt";
133-
msg += version;
134-
msg += " COMPONENTS ";
135-
msg += comp;
136-
msg += ")\n";
137-
}
138-
msg += "to your CMakeLists.txt file.";
123+
cmAlphaNum version = (qtVersion.second == 0)
124+
? cmAlphaNum("<QTVERSION>")
125+
: cmAlphaNum(qtVersion.second);
126+
cmAlphaNum component = uicDisabled ? "Widgets" : "Core";
127+
128+
std::string const msg = cmStrCat(
129+
"AUTOGEN: No valid Qt version found for target ",
130+
target->GetName(), ". ",
131+
cmQtAutoGen::Tools(mocDisabled, uicDisabled, rccDisabled),
132+
" disabled. Consider adding:\n", " find_package(Qt", version,
133+
" COMPONENTS ", component, ")\n", "to your CMakeLists.txt file.");
139134
target->Makefile->IssueMessage(MessageType::AUTHOR_WARNING, msg);
140135
}
141136
if (mocIsValid || uicIsValid || rccIsValid) {

Source/cmQtAutoGenInitializer.cxx

Lines changed: 49 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -156,62 +156,57 @@ std::string cmQtAutoGenInitializer::InfoWriter::ListJoin(IT it_begin,
156156
return res;
157157
}
158158

159-
std::string cmQtAutoGenInitializer::InfoWriter::ConfigKey(
160-
const char* key, std::string const& config)
159+
inline std::string cmQtAutoGenInitializer::InfoWriter::ConfigKey(
160+
cm::string_view key, std::string const& config)
161161
{
162-
std::string ckey = key;
163-
ckey += '_';
164-
ckey += config;
165-
return ckey;
162+
return cmStrCat(key, "_", config);
166163
}
167164

168-
void cmQtAutoGenInitializer::InfoWriter::Write(const char* key,
165+
void cmQtAutoGenInitializer::InfoWriter::Write(cm::string_view key,
169166
std::string const& value)
170167
{
171168
Ofs_ << "set(" << key << " " << cmOutputConverter::EscapeForCMake(value)
172169
<< ")\n";
173170
};
174171

175-
void cmQtAutoGenInitializer::InfoWriter::WriteUInt(const char* key,
172+
void cmQtAutoGenInitializer::InfoWriter::WriteUInt(cm::string_view key,
176173
unsigned int value)
177174
{
178175
Ofs_ << "set(" << key << " " << value << ")\n";
179176
};
180177

181178
template <class C>
182-
void cmQtAutoGenInitializer::InfoWriter::WriteStrings(const char* key,
179+
void cmQtAutoGenInitializer::InfoWriter::WriteStrings(cm::string_view key,
183180
C const& container)
184181
{
185182
Ofs_ << "set(" << key << " \""
186183
<< ListJoin(container.begin(), container.end()) << "\")\n";
187184
}
188185

189186
void cmQtAutoGenInitializer::InfoWriter::WriteConfig(
190-
const char* key, std::map<std::string, std::string> const& map)
187+
cm::string_view key, std::map<std::string, std::string> const& map)
191188
{
192189
for (auto const& item : map) {
193-
Write(ConfigKey(key, item.first).c_str(), item.second);
190+
Write(ConfigKey(key, item.first), item.second);
194191
}
195192
};
196193

197194
template <class C>
198195
void cmQtAutoGenInitializer::InfoWriter::WriteConfigStrings(
199-
const char* key, std::map<std::string, C> const& map)
196+
cm::string_view key, std::map<std::string, C> const& map)
200197
{
201198
for (auto const& item : map) {
202-
WriteStrings(ConfigKey(key, item.first).c_str(), item.second);
199+
WriteStrings(ConfigKey(key, item.first), item.second);
203200
}
204201
}
205202

206203
void cmQtAutoGenInitializer::InfoWriter::WriteNestedLists(
207-
const char* key, std::vector<std::vector<std::string>> const& lists)
204+
cm::string_view key, std::vector<std::vector<std::string>> const& lists)
208205
{
209206
std::vector<std::string> seplist;
210-
for (const std::vector<std::string>& list : lists) {
211-
std::string blist = "{";
212-
blist += ListJoin(list.begin(), list.end());
213-
blist += "}";
214-
seplist.push_back(std::move(blist));
207+
seplist.reserve(lists.size());
208+
for (std::vector<std::string> const& list : lists) {
209+
seplist.push_back(cmStrCat("{", ListJoin(list.begin(), list.end()), "}"));
215210
}
216211
Write(key, cmJoin(seplist, cmQtAutoGen::ListSep));
217212
};
@@ -721,14 +716,13 @@ bool cmQtAutoGenInitializer::InitScanFiles()
721716
if (muf.MocIt || muf.UicIt) {
722717
// Search for the default header file and a private header
723718
std::string const& srcPath = muf.SF->GetFullPath();
724-
std::string basePath = cmQtAutoGen::SubDirPrefix(srcPath);
725-
basePath += cmSystemTools::GetFilenameWithoutLastExtension(srcPath);
719+
std::string basePath =
720+
cmStrCat(cmQtAutoGen::SubDirPrefix(srcPath),
721+
cmSystemTools::GetFilenameWithoutLastExtension(srcPath));
726722
for (auto const& suffix : suffixes) {
727723
std::string const suffixedPath = basePath + suffix;
728724
for (auto const& ext : exts) {
729-
std::string fullPath = suffixedPath;
730-
fullPath += '.';
731-
fullPath += ext;
725+
std::string fullPath = cmStrCat(suffixedPath, '.', ext);
732726

733727
auto constexpr locationKind = cmSourceFileLocationKind::Known;
734728
cmSourceFile* sf = makefile->GetSource(fullPath, locationKind);
@@ -833,9 +827,8 @@ bool cmQtAutoGenInitializer::InitScanFiles()
833827
this->AutogenTarget.DependFiles.insert(muf->RealPath);
834828
}
835829
} else if (this->CMP0071Warn) {
836-
std::string msg;
837-
msg += cmPolicies::GetPolicyWarning(cmPolicies::CMP0071);
838-
msg += '\n';
830+
std::string msg =
831+
cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0071), '\n');
839832
std::string property;
840833
if (this->Moc.Enabled && this->Uic.Enabled) {
841834
property = kw.SKIP_AUTOGEN;
@@ -888,18 +881,10 @@ bool cmQtAutoGenInitializer::InitScanFiles()
888881
for (Qrc& qrc : this->Rcc.Qrcs) {
889882
qrc.PathChecksum = fpathCheckSum.getPart(qrc.QrcFile);
890883
// RCC output file name
884+
qrc.RccFile = cmStrCat(this->Dir.Build + "/", qrc.PathChecksum,
885+
"/qrc_", qrc.QrcName, ".cpp");
891886
{
892-
std::string rccFile = this->Dir.Build + "/";
893-
rccFile += qrc.PathChecksum;
894-
rccFile += "/qrc_";
895-
rccFile += qrc.QrcName;
896-
rccFile += ".cpp";
897-
qrc.RccFile = std::move(rccFile);
898-
}
899-
{
900-
std::string base = this->Dir.Info;
901-
base += "/RCC";
902-
base += qrc.QrcName;
887+
std::string base = cmStrCat(this->Dir.Info, "/RCC", qrc.QrcName);
903888
if (!qrc.Unique) {
904889
base += qrc.PathChecksum;
905890
}
@@ -932,8 +917,7 @@ bool cmQtAutoGenInitializer::InitScanFiles()
932917
// Replace '-' with '_'. The former is not valid for symbol names.
933918
std::replace(name.begin(), name.end(), '-', '_');
934919
if (!qrc.Unique) {
935-
name += "_";
936-
name += qrc.PathChecksum;
920+
name += cmStrCat("_", qrc.PathChecksum);
937921
}
938922
std::vector<std::string> nameOpts;
939923
nameOpts.emplace_back("-name");
@@ -1157,8 +1141,8 @@ bool cmQtAutoGenInitializer::InitRccTargets()
11571141
currentLine.push_back("$<CONFIG>");
11581142
commandLines.push_back(std::move(currentLine));
11591143
}
1160-
std::string ccComment = "Automatic RCC for ";
1161-
ccComment += FileProjectRelativePath(makefile, qrc.QrcFile);
1144+
std::string ccComment = cmStrCat(
1145+
"Automatic RCC for ", FileProjectRelativePath(makefile, qrc.QrcFile));
11621146

11631147
if (qrc.Generated || this->Rcc.GlobalTarget) {
11641148
// Create custom rcc target
@@ -1226,9 +1210,8 @@ bool cmQtAutoGenInitializer::SetupCustomTargets()
12261210
{
12271211
// Create info directory on demand
12281212
if (!cmSystemTools::MakeDirectory(this->Dir.Info)) {
1229-
std::string emsg = ("AutoGen: Could not create directory: ");
1230-
emsg += Quoted(this->Dir.Info);
1231-
cmSystemTools::Error(emsg);
1213+
cmSystemTools::Error(cmStrCat("AutoGen: Could not create directory: ",
1214+
Quoted(this->Dir.Info)));
12321215
return false;
12331216
}
12341217

@@ -1311,10 +1294,8 @@ bool cmQtAutoGenInitializer::SetupWriteAutogenInfo()
13111294
}
13121295
if (muf->MocIt || muf->UicIt) {
13131296
headers.emplace_back(muf->RealPath);
1314-
std::string flags;
1315-
flags += muf->MocIt ? 'M' : 'm';
1316-
flags += muf->UicIt ? 'U' : 'u';
1317-
headersFlags.emplace_back(std::move(flags));
1297+
headersFlags.emplace_back(
1298+
cmStrCat(muf->MocIt ? "M" : "m", muf->UicIt ? "U" : "u"));
13181299
}
13191300
}
13201301
}
@@ -1323,14 +1304,13 @@ bool cmQtAutoGenInitializer::SetupWriteAutogenInfo()
13231304
cmFilePathChecksum const fpathCheckSum(makefile);
13241305
std::unordered_set<std::string> emitted;
13251306
for (std::string const& hdr : headers) {
1326-
std::string basePath = fpathCheckSum.getPart(hdr);
1327-
basePath += "/moc_";
1328-
basePath += cmSystemTools::GetFilenameWithoutLastExtension(hdr);
1329-
for (unsigned int ii = 1; ii != 1024; ++ii) {
1307+
std::string basePath =
1308+
cmStrCat(fpathCheckSum.getPart(hdr), "/moc_",
1309+
cmSystemTools::GetFilenameWithoutLastExtension(hdr));
1310+
for (int ii = 1; ii != 1024; ++ii) {
13301311
std::string path = basePath;
13311312
if (ii > 1) {
1332-
path += '_';
1333-
path += std::to_string(ii);
1313+
path += cmStrCat("_", ii);
13341314
}
13351315
path += ".cpp";
13361316
if (emitted.emplace(path).second) {
@@ -1369,10 +1349,8 @@ bool cmQtAutoGenInitializer::SetupWriteAutogenInfo()
13691349
}
13701350
if (muf->MocIt || muf->UicIt) {
13711351
sources.emplace_back(muf->RealPath);
1372-
std::string flags;
1373-
flags += muf->MocIt ? 'M' : 'm';
1374-
flags += muf->UicIt ? 'U' : 'u';
1375-
sourcesFlags.emplace_back(std::move(flags));
1352+
sourcesFlags.emplace_back(
1353+
cmStrCat(muf->MocIt ? "M" : "m", muf->UicIt ? "U" : "u"));
13761354
}
13771355
}
13781356
}
@@ -1426,9 +1404,8 @@ bool cmQtAutoGenInitializer::SetupWriteAutogenInfo()
14261404
ofs.WriteStrings("AM_UIC_SEARCH_PATHS", this->Uic.SearchPaths);
14271405
}
14281406
} else {
1429-
std::string err = "AutoGen: Could not write file ";
1430-
err += this->AutogenTarget.InfoFile;
1431-
cmSystemTools::Error(err);
1407+
cmSystemTools::Error(cmStrCat("AutoGen: Could not write file ",
1408+
this->AutogenTarget.InfoFile));
14321409
return false;
14331410
}
14341411

@@ -1467,9 +1444,8 @@ bool cmQtAutoGenInitializer::SetupWriteRccInfo()
14671444
ofs.WriteStrings("ARCC_OPTIONS", qrc.Options);
14681445
ofs.WriteStrings("ARCC_INPUTS", qrc.Resources);
14691446
} else {
1470-
std::string err = "AutoRcc: Could not write file ";
1471-
err += qrc.InfoFile;
1472-
cmSystemTools::Error(err);
1447+
cmSystemTools::Error(
1448+
cmStrCat("AutoRcc: Could not write file ", qrc.InfoFile));
14731449
return false;
14741450
}
14751451
}
@@ -1524,13 +1500,10 @@ bool cmQtAutoGenInitializer::AddToSourceGroup(std::string const& fileName,
15241500
if (!groupName.empty()) {
15251501
sourceGroup = makefile->GetOrCreateSourceGroup(groupName);
15261502
if (sourceGroup == nullptr) {
1527-
std::string err;
1528-
err += genNameUpper;
1529-
err += " error in ";
1530-
err += property;
1531-
err += ": Could not find or create the source group ";
1532-
err += cmQtAutoGen::Quoted(groupName);
1533-
cmSystemTools::Error(err);
1503+
cmSystemTools::Error(
1504+
cmStrCat(genNameUpper, " error in ", property,
1505+
": Could not find or create the source group ",
1506+
cmQtAutoGen::Quoted(groupName)));
15341507
return false;
15351508
}
15361509
}
@@ -1622,12 +1595,8 @@ bool cmQtAutoGenInitializer::GetQtExecutable(GenVarsT& genVars,
16221595
bool ignoreMissingTarget) const
16231596
{
16241597
auto print_err = [this, &genVars](std::string const& err) {
1625-
std::string msg = genVars.GenNameUpper;
1626-
msg += " for target ";
1627-
msg += this->Target->GetName();
1628-
msg += ": ";
1629-
msg += err;
1630-
cmSystemTools::Error(msg);
1598+
cmSystemTools::Error(cmStrCat(genVars.GenNameUpper, " for target ",
1599+
this->Target->GetName(), ": ", err));
16311600
};
16321601

16331602
// Custom executable
@@ -1687,11 +1656,8 @@ bool cmQtAutoGenInitializer::GetQtExecutable(GenVarsT& genVars,
16871656
std::make_shared<cmQtAutoGen::CompilerFeatures>();
16881657
return true;
16891658
}
1690-
std::string err = "Could not find ";
1691-
err += executable;
1692-
err += " executable target ";
1693-
err += targetName;
1694-
print_err(err);
1659+
print_err(cmStrCat("Could not find ", executable, " executable target ",
1660+
targetName));
16951661
return false;
16961662
}
16971663
}

Source/cmQtAutoGenInitializer.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "cmConfigure.h" // IWYU pragma: keep
77
#include "cmGeneratedFileStream.h"
88
#include "cmQtAutoGen.h"
9+
#include "cm_string_view.hxx"
910

1011
#include <map>
1112
#include <memory>
@@ -85,24 +86,24 @@ class cmQtAutoGenInitializer : public cmQtAutoGen
8586
/// @return True if the file is open
8687
explicit operator bool() const { return static_cast<bool>(Ofs_); }
8788

88-
void Write(const char* text) { Ofs_ << text; }
89-
void Write(const char* key, std::string const& value);
90-
void WriteUInt(const char* key, unsigned int value);
89+
void Write(cm::string_view text) { Ofs_ << text; }
90+
void Write(cm::string_view, std::string const& value);
91+
void WriteUInt(cm::string_view, unsigned int value);
9192

9293
template <class C>
93-
void WriteStrings(const char* key, C const& container);
94-
void WriteConfig(const char* key,
94+
void WriteStrings(cm::string_view, C const& container);
95+
void WriteConfig(cm::string_view,
9596
std::map<std::string, std::string> const& map);
9697
template <class C>
97-
void WriteConfigStrings(const char* key,
98+
void WriteConfigStrings(cm::string_view,
9899
std::map<std::string, C> const& map);
99-
void WriteNestedLists(const char* key,
100+
void WriteNestedLists(cm::string_view,
100101
std::vector<std::vector<std::string>> const& lists);
101102

102103
private:
103104
template <class IT>
104105
static std::string ListJoin(IT it_begin, IT it_end);
105-
static std::string ConfigKey(const char* key, std::string const& config);
106+
static std::string ConfigKey(cm::string_view, std::string const& config);
106107

107108
private:
108109
cmGeneratedFileStream Ofs_;

0 commit comments

Comments
 (0)