From 2b15c2a755466bc5460fbdc42c438bcbc196583a Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Thu, 20 Mar 2014 12:48:36 +0000 Subject: [PATCH] Tooling: Move heavyweight vectors around instead of copying. While there convert to range-based for loops. No functionality change. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@204338 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Tooling/Tooling.h | 4 +- lib/Tooling/Tooling.cpp | 59 +++++++++++------------- tools/libclang/CXCompilationDatabase.cpp | 14 +++--- 3 files changed, 35 insertions(+), 42 deletions(-) diff --git a/include/clang/Tooling/Tooling.h b/include/clang/Tooling/Tooling.h index 88188b613a..097a7a8a0b 100644 --- a/include/clang/Tooling/Tooling.h +++ b/include/clang/Tooling/Tooling.h @@ -186,7 +186,7 @@ class ToolInvocation { /// \param FAction The action to be executed. Class takes ownership. /// \param Files The FileManager used for the execution. Class does not take /// ownership. - ToolInvocation(ArrayRef CommandLine, FrontendAction *FAction, + ToolInvocation(std::vector CommandLine, FrontendAction *FAction, FileManager *Files); /// \brief Create a tool invocation. @@ -194,7 +194,7 @@ class ToolInvocation { /// \param CommandLine The command line arguments to clang. /// \param Action The action to be executed. /// \param Files The FileManager used for the execution. - ToolInvocation(ArrayRef CommandLine, ToolAction *Action, + ToolInvocation(std::vector CommandLine, ToolAction *Action, FileManager *Files); ~ToolInvocation(); diff --git a/lib/Tooling/Tooling.cpp b/lib/Tooling/Tooling.cpp index 46aa0ee1ce..7425e3b4e8 100644 --- a/lib/Tooling/Tooling.cpp +++ b/lib/Tooling/Tooling.cpp @@ -52,7 +52,7 @@ FrontendActionFactory::~FrontendActionFactory() {} /// \brief Builds a clang driver initialized for running clang tools. static clang::driver::Driver *newDriver(clang::DiagnosticsEngine *Diagnostics, const char *BinaryName) { - const std::string DefaultOutputName = "a.out"; + const char *DefaultOutputName = "a.out"; clang::driver::Driver *CompilerDriver = new clang::driver::Driver( BinaryName, llvm::sys::getDefaultTargetTriple(), DefaultOutputName, *Diagnostics); @@ -165,17 +165,17 @@ public: } -ToolInvocation::ToolInvocation(ArrayRef CommandLine, +ToolInvocation::ToolInvocation(std::vector CommandLine, ToolAction *Action, FileManager *Files) - : CommandLine(CommandLine.vec()), + : CommandLine(std::move(CommandLine)), Action(Action), OwnsAction(false), Files(Files), DiagConsumer(NULL) {} -ToolInvocation::ToolInvocation(ArrayRef CommandLine, +ToolInvocation::ToolInvocation(std::vector CommandLine, FrontendAction *FAction, FileManager *Files) - : CommandLine(CommandLine.vec()), + : CommandLine(std::move(CommandLine)), Action(new SingleFrontendActionFactory(FAction)), OwnsAction(true), Files(Files), @@ -198,8 +198,8 @@ void ToolInvocation::mapVirtualFile(StringRef FilePath, StringRef Content) { bool ToolInvocation::run() { std::vector Argv; - for (int I = 0, E = CommandLine.size(); I != E; ++I) - Argv.push_back(CommandLine[I].c_str()); + for (const std::string &Str : CommandLine) + Argv.push_back(Str.c_str()); const char *const BinaryName = Argv[0]; IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions(); TextDiagnosticPrinter DiagnosticPrinter( @@ -221,13 +221,10 @@ bool ToolInvocation::run() { } std::unique_ptr Invocation( newInvocation(&Diagnostics, *CC1Args)); - for (llvm::StringMap::const_iterator - It = MappedFileContents.begin(), End = MappedFileContents.end(); - It != End; ++It) { + for (const auto &It : MappedFileContents) { // Inject the code as the given file name into the preprocessor options. - const llvm::MemoryBuffer *Input = - llvm::MemoryBuffer::getMemBuffer(It->getValue()); - Invocation->getPreprocessorOpts().addRemappedFile(It->getKey(), Input); + auto *Input = llvm::MemoryBuffer::getMemBuffer(It.getValue()); + Invocation->getPreprocessorOpts().addRemappedFile(It.getKey(), Input); } return runInvocation(BinaryName, Compilation.get(), Invocation.release()); } @@ -277,15 +274,15 @@ ClangTool::ClangTool(const CompilationDatabase &Compilations, : Files(new FileManager(FileSystemOptions())), DiagConsumer(NULL) { ArgsAdjusters.push_back(new ClangStripOutputAdjuster()); ArgsAdjusters.push_back(new ClangSyntaxOnlyAdjuster()); - for (unsigned I = 0, E = SourcePaths.size(); I != E; ++I) { - SmallString<1024> File(getAbsolutePath(SourcePaths[I])); + for (const auto &SourcePath : SourcePaths) { + std::string File(getAbsolutePath(SourcePath)); std::vector CompileCommandsForFile = - Compilations.getCompileCommands(File.str()); + Compilations.getCompileCommands(File); if (!CompileCommandsForFile.empty()) { - for (int I = 0, E = CompileCommandsForFile.size(); I != E; ++I) { - CompileCommands.push_back(std::make_pair(File.str(), - CompileCommandsForFile[I])); + for (CompileCommand &CompileCommand : CompileCommandsForFile) { + CompileCommands.push_back( + std::make_pair(File, std::move(CompileCommand))); } } else { // FIXME: There are two use cases here: doing a fuzzy @@ -334,8 +331,7 @@ int ClangTool::run(ToolAction *Action) { llvm::sys::fs::getMainExecutable("clang_tool", &StaticSymbol); bool ProcessingFailed = false; - for (unsigned I = 0; I < CompileCommands.size(); ++I) { - std::string File = CompileCommands[I].first; + for (const auto &Command : CompileCommands) { // FIXME: chdir is thread hostile; on the other hand, creating the same // behavior as chdir is complex: chdir resolves the path once, thus // guaranteeing that all subsequent relative path operations work @@ -343,28 +339,27 @@ int ClangTool::run(ToolAction *Action) { // for example on network filesystems, where symlinks might be switched // during runtime of the tool. Fixing this depends on having a file system // abstraction that allows openat() style interactions. - if (chdir(CompileCommands[I].second.Directory.c_str())) + if (chdir(Command.second.Directory.c_str())) llvm::report_fatal_error("Cannot chdir into \"" + - CompileCommands[I].second.Directory + "\n!"); - std::vector CommandLine = CompileCommands[I].second.CommandLine; - for (unsigned I = 0, E = ArgsAdjusters.size(); I != E; ++I) - CommandLine = ArgsAdjusters[I]->Adjust(CommandLine); + Twine(Command.second.Directory) + "\n!"); + std::vector CommandLine = Command.second.CommandLine; + for (ArgumentsAdjuster *Adjuster : ArgsAdjusters) + CommandLine = Adjuster->Adjust(CommandLine); assert(!CommandLine.empty()); CommandLine[0] = MainExecutable; // FIXME: We need a callback mechanism for the tool writer to output a // customized message for each file. DEBUG({ - llvm::dbgs() << "Processing: " << File << ".\n"; + llvm::dbgs() << "Processing: " << Command.first << ".\n"; }); - ToolInvocation Invocation(CommandLine, Action, Files.getPtr()); + ToolInvocation Invocation(std::move(CommandLine), Action, Files.getPtr()); Invocation.setDiagnosticConsumer(DiagConsumer); - for (int I = 0, E = MappedFileContents.size(); I != E; ++I) { - Invocation.mapVirtualFile(MappedFileContents[I].first, - MappedFileContents[I].second); + for (const auto &MappedFile : MappedFileContents) { + Invocation.mapVirtualFile(MappedFile.first, MappedFile.second); } if (!Invocation.run()) { // FIXME: Diagnostics should be used instead. - llvm::errs() << "Error while processing " << File << ".\n"; + llvm::errs() << "Error while processing " << Command.first << ".\n"; ProcessingFailed = true; } } diff --git a/tools/libclang/CXCompilationDatabase.cpp b/tools/libclang/CXCompilationDatabase.cpp index 156a3e22a6..f4728fe0a1 100644 --- a/tools/libclang/CXCompilationDatabase.cpp +++ b/tools/libclang/CXCompilationDatabase.cpp @@ -40,9 +40,8 @@ struct AllocatedCXCompileCommands { std::vector CCmd; - AllocatedCXCompileCommands(const std::vector& Cmd) - : CCmd(Cmd) - { } + AllocatedCXCompileCommands(std::vector Cmd) + : CCmd(std::move(Cmd)) {} }; CXCompileCommands @@ -50,10 +49,9 @@ clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb, const char *CompleteFileName) { if (CompilationDatabase *db = static_cast(CDb)) { - const std::vector - CCmd(db->getCompileCommands(CompleteFileName)); + std::vector CCmd(db->getCompileCommands(CompleteFileName)); if (!CCmd.empty()) - return new AllocatedCXCompileCommands( CCmd ); + return new AllocatedCXCompileCommands(std::move(CCmd)); } return 0; @@ -62,9 +60,9 @@ clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb, CXCompileCommands clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) { if (CompilationDatabase *db = static_cast(CDb)) { - const std::vector CCmd(db->getAllCompileCommands()); + std::vector CCmd(db->getAllCompileCommands()); if (!CCmd.empty()) - return new AllocatedCXCompileCommands( CCmd ); + return new AllocatedCXCompileCommands(std::move(CCmd)); } return 0; -- 2.40.0