[clang] 8b0df1c - [NFC] Refactor Registry loops to range for

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 18 16:40:26 PDT 2020


Author: Nathan James
Date: 2020-06-19T00:40:10+01:00
New Revision: 8b0df1c1a992d203212901c1139665261e0bbc1c

URL: https://github.com/llvm/llvm-project/commit/8b0df1c1a992d203212901c1139665261e0bbc1c
DIFF: https://github.com/llvm/llvm-project/commit/8b0df1c1a992d203212901c1139665261e0bbc1c.diff

LOG: [NFC] Refactor Registry loops to range for

Added: 
    

Modified: 
    clang-tools-extra/clang-doc/Generators.cpp
    clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
    clang-tools-extra/clangd/URI.cpp
    clang/lib/Frontend/FrontendAction.cpp
    clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
    clang/lib/Lex/Pragma.cpp
    clang/lib/Tooling/CompilationDatabase.cpp
    clang/lib/Tooling/Execution.cpp
    llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-doc/Generators.cpp b/clang-tools-extra/clang-doc/Generators.cpp
index 3b7dcf93411a..591d43322342 100644
--- a/clang-tools-extra/clang-doc/Generators.cpp
+++ b/clang-tools-extra/clang-doc/Generators.cpp
@@ -15,11 +15,10 @@ namespace doc {
 
 llvm::Expected<std::unique_ptr<Generator>>
 findGeneratorByName(llvm::StringRef Format) {
-  for (auto I = GeneratorRegistry::begin(), E = GeneratorRegistry::end();
-       I != E; ++I) {
-    if (I->getName() != Format)
+  for (const auto &Generator : GeneratorRegistry::entries()) {
+    if (Generator.getName() != Format)
       continue;
-    return I->instantiate();
+    return Generator.instantiate();
   }
   return createStringError(llvm::inconvertibleErrorCode(),
                            "can't find generator: " + Format);

diff  --git a/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp b/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
index bf146385e4ff..76cb663c8e2e 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
@@ -115,11 +115,10 @@ ClangTidyOptions ClangTidyOptions::getDefaults() {
   Options.FormatStyle = "none";
   Options.User = llvm::None;
   unsigned Priority = 0;
-  for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin(),
-                                         E = ClangTidyModuleRegistry::end();
-       I != E; ++I)
+  for (const ClangTidyModuleRegistry::entry &Module :
+       ClangTidyModuleRegistry::entries())
     Options =
-        Options.mergeWith(I->instantiate()->getModuleOptions(), ++Priority);
+        Options.mergeWith(Module.instantiate()->getModuleOptions(), ++Priority);
   return Options;
 }
 

diff  --git a/clang-tools-extra/clangd/URI.cpp b/clang-tools-extra/clangd/URI.cpp
index 1a125e73a0ca..2061a5601c58 100644
--- a/clang-tools-extra/clangd/URI.cpp
+++ b/clang-tools-extra/clangd/URI.cpp
@@ -63,11 +63,10 @@ findSchemeByName(llvm::StringRef Scheme) {
   if (Scheme == "file")
     return std::make_unique<FileSystemScheme>();
 
-  for (auto I = URISchemeRegistry::begin(), E = URISchemeRegistry::end();
-       I != E; ++I) {
-    if (I->getName() != Scheme)
+  for (const auto &URIScheme : URISchemeRegistry::entries()) {
+    if (URIScheme.getName() != Scheme)
       continue;
-    return I->instantiate();
+    return URIScheme.instantiate();
   }
   return make_string_error("Can't find scheme: " + Scheme);
 }

diff  --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp
index dc361b2fdd24..59a968b5c709 100644
--- a/clang/lib/Frontend/FrontendAction.cpp
+++ b/clang/lib/Frontend/FrontendAction.cpp
@@ -157,10 +157,9 @@ FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
   bool FoundAllPlugins = true;
   for (const std::string &Arg : CI.getFrontendOpts().AddPluginActions) {
     bool Found = false;
-    for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
-                                          ie = FrontendPluginRegistry::end();
-         it != ie; ++it) {
-      if (it->getName() == Arg)
+    for (const FrontendPluginRegistry::entry &Plugin :
+         FrontendPluginRegistry::entries()) {
+      if (Plugin.getName() == Arg)
         Found = true;
     }
     if (!Found) {
@@ -183,26 +182,24 @@ FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
   // or after it (in AfterConsumers)
   std::vector<std::unique_ptr<ASTConsumer>> Consumers;
   std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers;
-  for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
-                                        ie = FrontendPluginRegistry::end();
-       it != ie; ++it) {
-    std::unique_ptr<PluginASTAction> P = it->instantiate();
+  for (const FrontendPluginRegistry::entry &Plugin :
+       FrontendPluginRegistry::entries()) {
+    std::unique_ptr<PluginASTAction> P = Plugin.instantiate();
     PluginASTAction::ActionType ActionType = P->getActionType();
     if (ActionType == PluginASTAction::Cmdline) {
       // This is O(|plugins| * |add_plugins|), but since both numbers are
       // way below 50 in practice, that's ok.
-      for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
-           i != e; ++i) {
-        if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
-          ActionType = PluginASTAction::AddAfterMainAction;
-          break;
-        }
-      }
+      if (llvm::any_of(CI.getFrontendOpts().AddPluginActions,
+                       [&](const std::string &PluginAction) {
+                         return PluginAction == Plugin.getName();
+                       }))
+        ActionType = PluginASTAction::AddAfterMainAction;
     }
     if ((ActionType == PluginASTAction::AddBeforeMainAction ||
          ActionType == PluginASTAction::AddAfterMainAction) &&
         P->ParseArgs(
-            CI, CI.getFrontendOpts().PluginArgs[std::string(it->getName())])) {
+            CI,
+            CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())])) {
       std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile);
       if (ActionType == PluginASTAction::AddBeforeMainAction) {
         Consumers.push_back(std::move(PluginConsumer));

diff  --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
index 7c59ae42d2a2..ac64e1708da6 100644
--- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
+++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
@@ -74,16 +74,15 @@ CreateFrontendBaseAction(CompilerInstance &CI) {
   case TemplightDump:          return std::make_unique<TemplightDumpAction>();
 
   case PluginAction: {
-    for (FrontendPluginRegistry::iterator it =
-           FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
-         it != ie; ++it) {
-      if (it->getName() == CI.getFrontendOpts().ActionName) {
-        std::unique_ptr<PluginASTAction> P(it->instantiate());
+    for (const FrontendPluginRegistry::entry &Plugin :
+         FrontendPluginRegistry::entries()) {
+      if (Plugin.getName() == CI.getFrontendOpts().ActionName) {
+        std::unique_ptr<PluginASTAction> P(Plugin.instantiate());
         if ((P->getActionType() != PluginASTAction::ReplaceAction &&
              P->getActionType() != PluginASTAction::Cmdline) ||
             !P->ParseArgs(
                 CI,
-                CI.getFrontendOpts().PluginArgs[std::string(it->getName())]))
+                CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())]))
           return nullptr;
         return std::move(P);
       }
@@ -205,9 +204,7 @@ bool ExecuteCompilerInvocation(CompilerInstance *Clang) {
   }
 
   // Load any requested plugins.
-  for (unsigned i = 0,
-         e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
-    const std::string &Path = Clang->getFrontendOpts().Plugins[i];
+  for (const std::string &Path : Clang->getFrontendOpts().Plugins) {
     std::string Error;
     if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
       Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
@@ -215,13 +212,12 @@ bool ExecuteCompilerInvocation(CompilerInstance *Clang) {
   }
 
   // Check if any of the loaded plugins replaces the main AST action
-  for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
-                                        ie = FrontendPluginRegistry::end();
-       it != ie; ++it) {
-    std::unique_ptr<PluginASTAction> P(it->instantiate());
+  for (const FrontendPluginRegistry::entry &Plugin :
+       FrontendPluginRegistry::entries()) {
+    std::unique_ptr<PluginASTAction> P(Plugin.instantiate());
     if (P->getActionType() == PluginASTAction::ReplaceAction) {
       Clang->getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
-      Clang->getFrontendOpts().ActionName = std::string(it->getName());
+      Clang->getFrontendOpts().ActionName = Plugin.getName().str();
       break;
     }
   }

diff  --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp
index 8d93eb3d6cdc..b512a547de7d 100644
--- a/clang/lib/Lex/Pragma.cpp
+++ b/clang/lib/Lex/Pragma.cpp
@@ -1907,10 +1907,9 @@ void Preprocessor::RegisterBuiltinPragmas() {
   }
 
   // Pragmas added by plugins
-  for (PragmaHandlerRegistry::iterator it = PragmaHandlerRegistry::begin(),
-                                       ie = PragmaHandlerRegistry::end();
-       it != ie; ++it) {
-    AddPragmaHandler(it->instantiate().release());
+  for (const PragmaHandlerRegistry::entry &handler :
+       PragmaHandlerRegistry::entries()) {
+    AddPragmaHandler(handler.instantiate().release());
   }
 }
 

diff  --git a/clang/lib/Tooling/CompilationDatabase.cpp b/clang/lib/Tooling/CompilationDatabase.cpp
index 254314b1967f..2b4c26dab96f 100644
--- a/clang/lib/Tooling/CompilationDatabase.cpp
+++ b/clang/lib/Tooling/CompilationDatabase.cpp
@@ -64,16 +64,14 @@ std::unique_ptr<CompilationDatabase>
 CompilationDatabase::loadFromDirectory(StringRef BuildDirectory,
                                        std::string &ErrorMessage) {
   llvm::raw_string_ostream ErrorStream(ErrorMessage);
-  for (CompilationDatabasePluginRegistry::iterator
-       It = CompilationDatabasePluginRegistry::begin(),
-       Ie = CompilationDatabasePluginRegistry::end();
-       It != Ie; ++It) {
+  for (const CompilationDatabasePluginRegistry::entry &Database :
+       CompilationDatabasePluginRegistry::entries()) {
     std::string DatabaseErrorMessage;
-    std::unique_ptr<CompilationDatabasePlugin> Plugin(It->instantiate());
+    std::unique_ptr<CompilationDatabasePlugin> Plugin(Database.instantiate());
     if (std::unique_ptr<CompilationDatabase> DB =
             Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage))
       return DB;
-    ErrorStream << It->getName() << ": " << DatabaseErrorMessage << "\n";
+    ErrorStream << Database.getName() << ": " << DatabaseErrorMessage << "\n";
   }
   return nullptr;
 }

diff  --git a/clang/lib/Tooling/Execution.cpp b/clang/lib/Tooling/Execution.cpp
index c39a4fcdac82..247b260b97ed 100644
--- a/clang/lib/Tooling/Execution.cpp
+++ b/clang/lib/Tooling/Execution.cpp
@@ -63,18 +63,16 @@ createExecutorFromCommandLineArgsImpl(int &argc, const char **argv,
                                   /*Overview=*/Overview);
   if (!OptionsParser)
     return OptionsParser.takeError();
-  for (auto I = ToolExecutorPluginRegistry::begin(),
-            E = ToolExecutorPluginRegistry::end();
-       I != E; ++I) {
-    if (I->getName() != ExecutorName) {
+  for (const auto &TEPlugin : ToolExecutorPluginRegistry::entries()) {
+    if (TEPlugin.getName() != ExecutorName) {
       continue;
     }
-    std::unique_ptr<ToolExecutorPlugin> Plugin(I->instantiate());
+    std::unique_ptr<ToolExecutorPlugin> Plugin(TEPlugin.instantiate());
     llvm::Expected<std::unique_ptr<ToolExecutor>> Executor =
         Plugin->create(*OptionsParser);
     if (!Executor) {
       return llvm::make_error<llvm::StringError>(
-          llvm::Twine("Failed to create '") + I->getName() +
+          llvm::Twine("Failed to create '") + TEPlugin.getName() +
               "': " + llvm::toString(Executor.takeError()) + "\n",
           llvm::inconvertibleErrorCode());
     }

diff  --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 3925fe78f013..a81a42a6f2b4 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -3157,11 +3157,10 @@ GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy &S) {
 
   auto Name = S.getName();
 
-  for (GCMetadataPrinterRegistry::iterator
-         I = GCMetadataPrinterRegistry::begin(),
-         E = GCMetadataPrinterRegistry::end(); I != E; ++I)
-    if (Name == I->getName()) {
-      std::unique_ptr<GCMetadataPrinter> GMP = I->instantiate();
+  for (const GCMetadataPrinterRegistry::entry &GCMetaPrinter :
+       GCMetadataPrinterRegistry::entries())
+    if (Name == GCMetaPrinter.getName()) {
+      std::unique_ptr<GCMetadataPrinter> GMP = GCMetaPrinter.instantiate();
       GMP->S = &S;
       auto IterBool = GCMap.insert(std::make_pair(&S, std::move(GMP)));
       return IterBool.first->second.get();


        


More information about the cfe-commits mailing list