diff options
Diffstat (limited to 'clang')
-rw-r--r-- | clang/include/clang/Tooling/Tooling.h | 4 | ||||
-rw-r--r-- | clang/lib/Tooling/Tooling.cpp | 59 | ||||
-rw-r--r-- | clang/tools/libclang/CXCompilationDatabase.cpp | 14 |
3 files changed, 35 insertions, 42 deletions
diff --git a/clang/include/clang/Tooling/Tooling.h b/clang/include/clang/Tooling/Tooling.h index 88188b613a3..097a7a8a0bd 100644 --- a/clang/include/clang/Tooling/Tooling.h +++ b/clang/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<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(); diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp index 46aa0ee1cee..7425e3b4e8f 100644 --- a/clang/lib/Tooling/Tooling.cpp +++ b/clang/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<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(StringRef FilePath, StringRef Content) { 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 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<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; } } diff --git a/clang/tools/libclang/CXCompilationDatabase.cpp b/clang/tools/libclang/CXCompilationDatabase.cpp index 156a3e22a61..f4728fe0a1c 100644 --- a/clang/tools/libclang/CXCompilationDatabase.cpp +++ b/clang/tools/libclang/CXCompilationDatabase.cpp @@ -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_getCompileCommands(CXCompilationDatabase CDb, 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_getCompileCommands(CXCompilationDatabase CDb, 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; |