From c797148e8573614a66465cd4ca6ed4bf5e8a5584 Mon Sep 17 00:00:00 2001 From: Sebastian Holtermann Date: Mon, 26 Aug 2019 15:31:55 +0200 Subject: [PATCH] Autogen: Use cm::string_view for AUTO{MOC,UIC,RCC} generator names - Store `AUTO{MOC,UIC,RCC}` generator name as `cm::string_view` - Use `std::initializer_list` instead of `std::array` --- Source/cmQtAutoGen.cxx | 34 +++++++++--------------- Source/cmQtAutoGen.h | 4 +-- Source/cmQtAutoGenInitializer.cxx | 43 ++++++++++++++++++------------- Source/cmQtAutoGenInitializer.h | 4 +-- Source/cmQtAutoGenerator.cxx | 6 ++--- 5 files changed, 44 insertions(+), 47 deletions(-) diff --git a/Source/cmQtAutoGen.cxx b/Source/cmQtAutoGen.cxx index dc7424e07e..d890f8e001 100644 --- a/Source/cmQtAutoGen.cxx +++ b/Source/cmQtAutoGen.cxx @@ -76,44 +76,34 @@ void MergeOptions(std::vector& baseOpts, unsigned int const cmQtAutoGen::ParallelMax = 64; std::string const cmQtAutoGen::ListSep = "<<>>"; -std::string const& cmQtAutoGen::GeneratorName(GenT genType) +cm::string_view cmQtAutoGen::GeneratorName(GenT genType) { - static const std::string AutoGen("AutoGen"); - static const std::string AutoMoc("AutoMoc"); - static const std::string AutoUic("AutoUic"); - static const std::string AutoRcc("AutoRcc"); - switch (genType) { case GenT::GEN: - return AutoGen; + return "AutoGen"; case GenT::MOC: - return AutoMoc; + return "AutoMoc"; case GenT::UIC: - return AutoUic; + return "AutoUic"; case GenT::RCC: - return AutoRcc; + return "AutoRcc"; } - return AutoGen; + return "AutoGen"; } -std::string const& cmQtAutoGen::GeneratorNameUpper(GenT genType) +cm::string_view cmQtAutoGen::GeneratorNameUpper(GenT genType) { - static const std::string AUTOGEN("AUTOGEN"); - static const std::string AUTOMOC("AUTOMOC"); - static const std::string AUTOUIC("AUTOUIC"); - static const std::string AUTORCC("AUTORCC"); - switch (genType) { case GenT::GEN: - return AUTOGEN; + return "AUTOGEN"; case GenT::MOC: - return AUTOMOC; + return "AUTOMOC"; case GenT::UIC: - return AUTOUIC; + return "AUTOUIC"; case GenT::RCC: - return AUTORCC; + return "AUTORCC"; } - return AUTOGEN; + return "AUTOGEN"; } std::string cmQtAutoGen::Tools(bool moc, bool uic, bool rcc) diff --git a/Source/cmQtAutoGen.h b/Source/cmQtAutoGen.h index 03468ecf65..939ceb3ea0 100644 --- a/Source/cmQtAutoGen.h +++ b/Source/cmQtAutoGen.h @@ -68,9 +68,9 @@ public: public: /// @brief Returns the generator name - static std::string const& GeneratorName(GenT genType); + static cm::string_view GeneratorName(GenT genType); /// @brief Returns the generator name in upper case - static std::string const& GeneratorNameUpper(GenT genType); + static cm::string_view GeneratorNameUpper(GenT genType); /// @brief Returns a string with the requested tool names static std::string Tools(bool moc, bool uic, bool rcc); diff --git a/Source/cmQtAutoGenInitializer.cxx b/Source/cmQtAutoGenInitializer.cxx index c355a5fe2e..6b9011b656 100644 --- a/Source/cmQtAutoGenInitializer.cxx +++ b/Source/cmQtAutoGenInitializer.cxx @@ -29,8 +29,8 @@ #include "cmsys/SystemInformation.hxx" #include -#include #include +#include #include #include #include @@ -696,7 +696,7 @@ bool cmQtAutoGenInitializer::InitScanFiles() std::vector extraHeaders; extraHeaders.reserve(this->AutogenTarget.Sources.size() * 2); // Header search suffixes and extensions - std::array const suffixes{ { "", "_p" } }; + static std::initializer_list const suffixes{ "", "_p" }; auto const& exts = cm->GetHeaderExtensions(); // Scan through sources for (auto const& pair : this->AutogenTarget.Sources) { @@ -708,7 +708,7 @@ bool cmQtAutoGenInitializer::InitScanFiles() cmStrCat(cmQtAutoGen::SubDirPrefix(srcPath), cmSystemTools::GetFilenameWithoutLastExtension(srcPath)); for (auto const& suffix : suffixes) { - std::string const suffixedPath = basePath + suffix; + std::string const suffixedPath = cmStrCat(basePath, suffix); for (auto const& ext : exts) { std::string fullPath = cmStrCat(suffixedPath, '.', ext); @@ -1454,7 +1454,7 @@ bool cmQtAutoGenInitializer::AddGeneratedSource(std::string const& filename, } bool cmQtAutoGenInitializer::AddToSourceGroup(std::string const& fileName, - std::string const& genNameUpper) + cm::string_view genNameUpper) { cmMakefile* makefile = this->Target->Target->GetMakefile(); cmSourceGroup* sourceGroup = nullptr; @@ -1464,13 +1464,14 @@ bool cmQtAutoGenInitializer::AddToSourceGroup(std::string const& fileName, std::string groupName; { // Prefer generator specific source group name - std::array props{ { genNameUpper + "_SOURCE_GROUP", - "AUTOGEN_SOURCE_GROUP" } }; - for (std::string& prop : props) { + std::initializer_list const props{ + cmStrCat(genNameUpper, "_SOURCE_GROUP"), "AUTOGEN_SOURCE_GROUP" + }; + for (std::string const& prop : props) { const char* propName = makefile->GetState()->GetGlobalProperty(prop); if ((propName != nullptr) && (*propName != '\0')) { groupName = propName; - property = std::move(prop); + property = prop; break; } } @@ -1512,11 +1513,16 @@ static std::vector GetKnownQtVersions( cmGeneratorTarget const* target) { // Qt version variable prefixes - static std::array const prefixes{ { "Qt6Core", "Qt5Core", - "QT" } }; + static std::initializer_list< + std::pair> const keys{ + { "Qt6Core_VERSION_MAJOR", "Qt6Core_VERSION_MINOR" }, + { "Qt5Core_VERSION_MAJOR", "Qt5Core_VERSION_MINOR" }, + { "QT_VERSION_MAJOR", "QT_VERSION_MINOR" }, + }; std::vector result; - result.reserve(prefixes.size() * 2); + result.reserve(keys.size() * 2); + // Adds a version to the result (nullptr safe) auto addVersion = [&result](const char* major, const char* minor) { cmQtAutoGen::IntegerVersion ver(CharPtrToUInt(major), @@ -1525,18 +1531,19 @@ static std::vector GetKnownQtVersions( result.emplace_back(ver); } }; + cmMakefile* makefile = target->Target->GetMakefile(); // Read versions from variables - for (const std::string& prefix : prefixes) { - addVersion(makefile->GetDefinition(prefix + "_VERSION_MAJOR"), - makefile->GetDefinition(prefix + "_VERSION_MINOR")); + for (auto const& keyPair : keys) { + addVersion(makefile->GetDefinition(std::string(keyPair.first)), + makefile->GetDefinition(std::string(keyPair.second))); } // Read versions from directory properties - for (const std::string& prefix : prefixes) { - addVersion(makefile->GetProperty(prefix + "_VERSION_MAJOR"), - makefile->GetProperty(prefix + "_VERSION_MINOR")); + for (auto const& keyPair : keys) { + addVersion(makefile->GetProperty(std::string(keyPair.first)), + makefile->GetProperty(std::string(keyPair.second))); } return result; @@ -1580,7 +1587,7 @@ bool cmQtAutoGenInitializer::GetQtExecutable(GenVarsT& genVars, // Custom executable { - std::string const prop = genVars.GenNameUpper + "_EXECUTABLE"; + std::string const prop = cmStrCat(genVars.GenNameUpper, "_EXECUTABLE"); std::string const val = this->Target->Target->GetSafeProperty(prop); if (!val.empty()) { // Evaluate generator expression diff --git a/Source/cmQtAutoGenInitializer.h b/Source/cmQtAutoGenInitializer.h index 7d72cad0d9..65666a626a 100644 --- a/Source/cmQtAutoGenInitializer.h +++ b/Source/cmQtAutoGenInitializer.h @@ -63,7 +63,7 @@ public: bool Enabled = false; // Generator type/name GenT Gen; - std::string const& GenNameUpper; + cm::string_view GenNameUpper; // Executable std::string ExecutableTargetName; cmGeneratorTarget* ExecutableTarget = nullptr; @@ -145,7 +145,7 @@ private: bool AddGeneratedSource(std::string const& filename, GenVarsT const& genVars, bool prepend = false); bool AddToSourceGroup(std::string const& fileName, - std::string const& genNameUpper); + cm::string_view genNameUpper); void AddCleanFile(std::string const& fileName); bool GetQtExecutable(GenVarsT& genVars, const std::string& executable, diff --git a/Source/cmQtAutoGenerator.cxx b/Source/cmQtAutoGenerator.cxx index 90361a9e5b..2820ef49e1 100644 --- a/Source/cmQtAutoGenerator.cxx +++ b/Source/cmQtAutoGenerator.cxx @@ -86,7 +86,7 @@ void cmQtAutoGenerator::Logger::Warning(GenT genType, msg += " warning: "; } else { // Multi line message - msg += HeadLine(GeneratorName(genType) + " warning"); + msg += HeadLine(cmStrCat(GeneratorName(genType), " warning")); } // Message msg += message; @@ -110,7 +110,7 @@ void cmQtAutoGenerator::Logger::WarningFile(GenT genType, void cmQtAutoGenerator::Logger::Error(GenT genType, std::string const& message) const { - std::string msg = HeadLine(GeneratorName(genType) + " error"); + std::string msg = HeadLine(cmStrCat(GeneratorName(genType), " error")); // Message msg += message; if (msg.back() != '\n') { @@ -136,7 +136,7 @@ void cmQtAutoGenerator::Logger::ErrorCommand( { std::string msg; msg.push_back('\n'); - msg += HeadLine(GeneratorName(genType) + " subprocess error"); + msg += HeadLine(cmStrCat(GeneratorName(genType), " subprocess error")); msg += message; if (msg.back() != '\n') { msg.push_back('\n');