From 706b93fa558c5a2e0e45a7068b5039d1f40fbc90 Mon Sep 17 00:00:00 2001 From: Artur Ryt Date: Thu, 14 Feb 2019 20:39:24 +0100 Subject: Modernize: C-arrays and loops over them It replaces C arrays with deduced std::initializer_lists or std::array what makes enables for-loop over them. --- Source/cmGlobalGenerator.cxx | 23 ++++++++++----------- Source/cmLocalGenerator.cxx | 49 +++++++++++++++++++++----------------------- Source/cmQtAutoGen.cxx | 20 ++++++++++++------ Source/cmTarget.cxx | 11 +++++----- Source/cmake.cxx | 23 ++++++++++----------- Source/cmcmd.cxx | 30 +++++++++++++-------------- 6 files changed, 80 insertions(+), 76 deletions(-) diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx index 11c382f..fa0ffcc 100644 --- a/Source/cmGlobalGenerator.cxx +++ b/Source/cmGlobalGenerator.cxx @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -2280,10 +2281,9 @@ void cmGlobalGenerator::AddGlobalTarget_Package( return; } - const char* reservedTargets[] = { "package", "PACKAGE" }; - for (const char* const* tn = cm::cbegin(reservedTargets); - tn != cm::cend(reservedTargets); ++tn) { - if (!this->CheckCMP0037(*tn, "when CPack packaging is enabled")) { + static const auto reservedTargets = { "package", "PACKAGE" }; + for (auto const& target : reservedTargets) { + if (!this->CheckCMP0037(target, "when CPack packaging is enabled")) { return; } } @@ -2330,10 +2330,10 @@ void cmGlobalGenerator::AddGlobalTarget_PackageSource( return; } - const char* reservedTargets[] = { "package_source" }; - for (const char* const* tn = cm::cbegin(reservedTargets); - tn != cm::cend(reservedTargets); ++tn) { - if (!this->CheckCMP0037(*tn, "when CPack source packaging is enabled")) { + static const auto reservedTargets = { "package_source" }; + for (auto const& target : reservedTargets) { + if (!this->CheckCMP0037(target, + "when CPack source packaging is enabled")) { return; } } @@ -2360,10 +2360,9 @@ void cmGlobalGenerator::AddGlobalTarget_Test( return; } - const char* reservedTargets[] = { "test", "RUN_TESTS" }; - for (const char* const* tn = cm::cbegin(reservedTargets); - tn != cm::cend(reservedTargets); ++tn) { - if (!this->CheckCMP0037(*tn, "when CTest testing is enabled")) { + static const auto reservedTargets = { "test", "RUN_TESTS" }; + for (auto const& target : reservedTargets) { + if (!this->CheckCMP0037(target, "when CTest testing is enabled")) { return; } } diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index a2d0efe..e392cff 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -34,6 +34,7 @@ #include "cmsys/RegularExpression.hxx" #include #include +#include #include #include #include @@ -51,25 +52,23 @@ // replaced in the form with GetSafeDefinition(var). // ${LANG} is replaced in the variable first with all enabled // languages. -static const char* ruleReplaceVars[] = { - "CMAKE_${LANG}_COMPILER", - "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS", - "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS", - "CMAKE_SHARED_MODULE_${LANG}_FLAGS", - "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS", - "CMAKE_${LANG}_LINK_FLAGS", - "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG", - "CMAKE_${LANG}_ARCHIVE", - "CMAKE_AR", - "CMAKE_CURRENT_SOURCE_DIR", - "CMAKE_CURRENT_BINARY_DIR", - "CMAKE_RANLIB", - "CMAKE_LINKER", - "CMAKE_MT", - "CMAKE_CUDA_HOST_COMPILER", - "CMAKE_CUDA_HOST_LINK_LAUNCHER", - "CMAKE_CL_SHOWINCLUDES_PREFIX" -}; +static auto ruleReplaceVars = { "CMAKE_${LANG}_COMPILER", + "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS", + "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS", + "CMAKE_SHARED_MODULE_${LANG}_FLAGS", + "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS", + "CMAKE_${LANG}_LINK_FLAGS", + "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG", + "CMAKE_${LANG}_ARCHIVE", + "CMAKE_AR", + "CMAKE_CURRENT_SOURCE_DIR", + "CMAKE_CURRENT_BINARY_DIR", + "CMAKE_RANLIB", + "CMAKE_LINKER", + "CMAKE_MT", + "CMAKE_CUDA_HOST_COMPILER", + "CMAKE_CUDA_HOST_LINK_LAUNCHER", + "CMAKE_CL_SHOWINCLUDES_PREFIX" }; cmLocalGenerator::cmLocalGenerator(cmGlobalGenerator* gg, cmMakefile* makefile) : cmOutputConverter(makefile->GetStateSnapshot()) @@ -138,15 +137,13 @@ cmLocalGenerator::cmLocalGenerator(cmGlobalGenerator* gg, cmMakefile* makefile) this->VariableMappings[compilerOptionSysroot] = this->Makefile->GetSafeDefinition(compilerOptionSysroot); - for (const char* const* replaceIter = cm::cbegin(ruleReplaceVars); - replaceIter != cm::cend(ruleReplaceVars); ++replaceIter) { - std::string actualReplace = *replaceIter; - if (actualReplace.find("${LANG}") != std::string::npos) { - cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang); + for (std::string replaceVar : ruleReplaceVars) { + if (replaceVar.find("${LANG}") != std::string::npos) { + cmSystemTools::ReplaceString(replaceVar, "${LANG}", lang); } - this->VariableMappings[actualReplace] = - this->Makefile->GetSafeDefinition(actualReplace); + this->VariableMappings[replaceVar] = + this->Makefile->GetSafeDefinition(replaceVar); } } } diff --git a/Source/cmQtAutoGen.cxx b/Source/cmQtAutoGen.cxx index 653caf7..f437138 100644 --- a/Source/cmQtAutoGen.cxx +++ b/Source/cmQtAutoGen.cxx @@ -7,7 +7,7 @@ #include "cmsys/RegularExpression.hxx" #include -#include +#include #include #include @@ -137,13 +137,21 @@ std::string cmQtAutoGen::Tools(bool moc, bool uic, bool rcc) std::string cmQtAutoGen::Quoted(std::string const& text) { - static const char* rep[18] = { "\\", "\\\\", "\"", "\\\"", "\a", "\\a", - "\b", "\\b", "\f", "\\f", "\n", "\\n", - "\r", "\\r", "\t", "\\t", "\v", "\\v" }; + const std::array, 9> replaces = { + { { "\\", "\\\\" }, + { "\"", "\\\"" }, + { "\a", "\\a" }, + { "\b", "\\b" }, + { "\f", "\\f" }, + { "\n", "\\n" }, + { "\r", "\\r" }, + { "\t", "\\t" }, + { "\v", "\\v" } } + }; std::string res = text; - for (const char* const* it = cm::cbegin(rep); it != cm::cend(rep); it += 2) { - cmSystemTools::ReplaceString(res, *it, *(it + 1)); + for (auto const& pair : replaces) { + cmSystemTools::ReplaceString(res, pair.first, pair.second); } res = '"' + res; res += '"'; diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index e871634..d1e4f06 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -5,6 +5,7 @@ #include "cmsys/RegularExpression.hxx" #include #include +#include #include #include #include @@ -312,23 +313,23 @@ cmTarget::cmTarget(std::string const& name, cmStateEnums::TargetType type, // Setup per-configuration property default values. if (this->GetType() != cmStateEnums::UTILITY) { - const char* configProps[] = { + static const auto configProps = { /* clang-format needs this comment to break after the opening brace */ "ARCHIVE_OUTPUT_DIRECTORY_", "LIBRARY_OUTPUT_DIRECTORY_", "RUNTIME_OUTPUT_DIRECTORY_", "PDB_OUTPUT_DIRECTORY_", "COMPILE_PDB_OUTPUT_DIRECTORY_", "MAP_IMPORTED_CONFIG_", - "INTERPROCEDURAL_OPTIMIZATION_", nullptr + "INTERPROCEDURAL_OPTIMIZATION_" }; for (std::string const& configName : configNames) { std::string configUpper = cmSystemTools::UpperCase(configName); - for (const char** p = configProps; *p; ++p) { + for (auto const& prop : configProps) { // Interface libraries have no output locations, so honor only // the configuration map. if (this->TargetTypeValue == cmStateEnums::INTERFACE_LIBRARY && - strcmp(*p, "MAP_IMPORTED_CONFIG_") != 0) { + strcmp(prop, "MAP_IMPORTED_CONFIG_") != 0) { continue; } - std::string property = *p; + std::string property = prop; property += configUpper; this->SetPropertyDefault(property, nullptr); } diff --git a/Source/cmake.cxx b/Source/cmake.cxx index ff6b04b..af3e465 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -99,6 +99,7 @@ #include "cmsys/RegularExpression.hxx" #include #include +#include #include #include #include // IWYU pragma: keep @@ -1892,11 +1893,10 @@ bool cmake::LoadCache(const std::string& path, bool internal, std::set& includes) { bool result = this->State->LoadCache(path, internal, excludes, includes); - static const char* entries[] = { "CMAKE_CACHE_MAJOR_VERSION", - "CMAKE_CACHE_MINOR_VERSION" }; - for (const char* const* nameIt = cm::cbegin(entries); - nameIt != cm::cend(entries); ++nameIt) { - this->UnwatchUnusedCli(*nameIt); + static const auto entries = { "CMAKE_CACHE_MAJOR_VERSION", + "CMAKE_CACHE_MINOR_VERSION" }; + for (auto const& entry : entries) { + this->UnwatchUnusedCli(entry); } return result; } @@ -1904,13 +1904,12 @@ bool cmake::LoadCache(const std::string& path, bool internal, bool cmake::SaveCache(const std::string& path) { bool result = this->State->SaveCache(path, this->GetMessenger()); - static const char* entries[] = { "CMAKE_CACHE_MAJOR_VERSION", - "CMAKE_CACHE_MINOR_VERSION", - "CMAKE_CACHE_PATCH_VERSION", - "CMAKE_CACHEFILE_DIR" }; - for (const char* const* nameIt = cm::cbegin(entries); - nameIt != cm::cend(entries); ++nameIt) { - this->UnwatchUnusedCli(*nameIt); + static const auto entries = { "CMAKE_CACHE_MAJOR_VERSION", + "CMAKE_CACHE_MINOR_VERSION", + "CMAKE_CACHE_PATCH_VERSION", + "CMAKE_CACHEFILE_DIR" }; + for (auto const& entry : entries) { + this->UnwatchUnusedCli(entry); } return result; } diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx index 4415ba6..4171624 100644 --- a/Source/cmcmd.cxx +++ b/Source/cmcmd.cxx @@ -37,6 +37,7 @@ #include "cmsys/Process.h" #include "cmsys/Terminal.h" #include +#include #include #include #include // IWYU pragma: keep @@ -350,12 +351,13 @@ struct CoCompiler bool NoOriginalCommand; }; -static CoCompiler CoCompilers[] = { // Table of options and handlers. - { "--cppcheck=", HandleCppCheck, false }, - { "--cpplint=", HandleCppLint, false }, - { "--iwyu=", HandleIWYU, false }, - { "--lwyu=", HandleLWYU, true }, - { "--tidy=", HandleTidy, false } +static const std::array CoCompilers = { + { // Table of options and handlers. + { "--cppcheck=", HandleCppCheck, false }, + { "--cpplint=", HandleCppLint, false }, + { "--iwyu=", HandleIWYU, false }, + { "--lwyu=", HandleLWYU, true }, + { "--tidy=", HandleTidy, false } } }; struct CoCompileJob @@ -386,16 +388,15 @@ int cmcmd::HandleCoCompileCommands(std::vector& args) doing_options = false; } else if (doing_options) { bool optionFound = false; - for (CoCompiler const* cc = cm::cbegin(CoCompilers); - cc != cm::cend(CoCompilers); ++cc) { - size_t optionLen = strlen(cc->Option); - if (arg.compare(0, optionLen, cc->Option) == 0) { + for (CoCompiler const& cc : CoCompilers) { + size_t optionLen = strlen(cc.Option); + if (arg.compare(0, optionLen, cc.Option) == 0) { optionFound = true; CoCompileJob job; job.Command = arg.substr(optionLen); - job.Handler = cc->Handler; + job.Handler = cc.Handler; jobs.push_back(std::move(job)); - if (cc->NoOriginalCommand) { + if (cc.NoOriginalCommand) { runOriginalCmd = false; } } @@ -419,9 +420,8 @@ int cmcmd::HandleCoCompileCommands(std::vector& args) if (jobs.empty()) { std::cerr << "__run_co_compile missing command to run. " "Looking for one or more of the following:\n"; - for (CoCompiler const* cc = cm::cbegin(CoCompilers); - cc != cm::cend(CoCompilers); ++cc) { - std::cerr << cc->Option << "\n"; + for (CoCompiler const& cc : CoCompilers) { + std::cerr << cc.Option << "\n"; } return 1; } -- cgit v0.12