r204338 - Tooling: Move heavyweight vectors around instead of copying.

Benjamin Kramer benny.kra at googlemail.com
Thu Mar 20 05:48:37 PDT 2014


Author: d0k
Date: Thu Mar 20 07:48:36 2014
New Revision: 204338

URL: http://llvm.org/viewvc/llvm-project?rev=204338&view=rev
Log:
Tooling: Move heavyweight vectors around instead of copying.

While there convert to range-based for loops. No functionality change.

Modified:
    cfe/trunk/include/clang/Tooling/Tooling.h
    cfe/trunk/lib/Tooling/Tooling.cpp
    cfe/trunk/tools/libclang/CXCompilationDatabase.cpp

Modified: cfe/trunk/include/clang/Tooling/Tooling.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Tooling.h?rev=204338&r1=204337&r2=204338&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/Tooling.h (original)
+++ cfe/trunk/include/clang/Tooling/Tooling.h Thu Mar 20 07:48:36 2014
@@ -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<std::string> CommandLine, FrontendAction *FAction,
+  ToolInvocation(std::vector<std::string> 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<std::string> CommandLine, ToolAction *Action,
+  ToolInvocation(std::vector<std::string> CommandLine, ToolAction *Action,
                  FileManager *Files);
 
   ~ToolInvocation();

Modified: cfe/trunk/lib/Tooling/Tooling.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Tooling.cpp?rev=204338&r1=204337&r2=204338&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/Tooling.cpp (original)
+++ cfe/trunk/lib/Tooling/Tooling.cpp Thu Mar 20 07:48:36 2014
@@ -52,7 +52,7 @@ FrontendActionFactory::~FrontendActionFa
 /// \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<std::string> CommandLine,
+ToolInvocation::ToolInvocation(std::vector<std::string> CommandLine,
                                ToolAction *Action, FileManager *Files)
-    : CommandLine(CommandLine.vec()),
+    : CommandLine(std::move(CommandLine)),
       Action(Action),
       OwnsAction(false),
       Files(Files),
       DiagConsumer(NULL) {}
 
-ToolInvocation::ToolInvocation(ArrayRef<std::string> CommandLine,
+ToolInvocation::ToolInvocation(std::vector<std::string> 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(Stri
 
 bool ToolInvocation::run() {
   std::vector<const char*> 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<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
   TextDiagnosticPrinter DiagnosticPrinter(
@@ -221,13 +221,10 @@ bool ToolInvocation::run() {
   }
   std::unique_ptr<clang::CompilerInvocation> Invocation(
       newInvocation(&Diagnostics, *CC1Args));
-  for (llvm::StringMap<StringRef>::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 CompilationDa
     : 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<CompileCommand> 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<std::string> 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<std::string> 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;
     }
   }

Modified: cfe/trunk/tools/libclang/CXCompilationDatabase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXCompilationDatabase.cpp?rev=204338&r1=204337&r2=204338&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXCompilationDatabase.cpp (original)
+++ cfe/trunk/tools/libclang/CXCompilationDatabase.cpp Thu Mar 20 07:48:36 2014
@@ -40,9 +40,8 @@ struct AllocatedCXCompileCommands
 {
   std::vector<CompileCommand> CCmd;
 
-  AllocatedCXCompileCommands(const std::vector<CompileCommand>& Cmd)
-    : CCmd(Cmd)
-  { }
+  AllocatedCXCompileCommands(std::vector<CompileCommand> Cmd)
+      : CCmd(std::move(Cmd)) {}
 };
 
 CXCompileCommands
@@ -50,10 +49,9 @@ clang_CompilationDatabase_getCompileComm
                                              const char *CompleteFileName)
 {
   if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
-    const std::vector<CompileCommand>
-      CCmd(db->getCompileCommands(CompleteFileName));
+    std::vector<CompileCommand> 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_getCompileComm
 CXCompileCommands
 clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) {
   if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
-    const std::vector<CompileCommand> CCmd(db->getAllCompileCommands());
+    std::vector<CompileCommand> CCmd(db->getAllCompileCommands());
     if (!CCmd.empty())
-      return new AllocatedCXCompileCommands( CCmd );
+      return new AllocatedCXCompileCommands(std::move(CCmd));
   }
 
   return 0;





More information about the cfe-commits mailing list