Index: include/clang/Tooling/Tooling.h =================================================================== --- include/clang/Tooling/Tooling.h +++ include/clang/Tooling/Tooling.h @@ -202,12 +202,16 @@ /// \param PCHContainerOps The PCHContainerOperations for loading and creating /// clang modules. /// +/// \param HonorDependencyOutputOptions generate dependency files if -M or +/// a related option appears on the command line. +/// /// \return The resulting AST or null if an error occurred. std::unique_ptr buildASTFromCodeWithArgs( const Twine &Code, const std::vector &Args, const Twine &FileName = "input.cc", const Twine &ToolName = "clang-tool", std::shared_ptr PCHContainerOps = - std::make_shared()); + std::make_shared(), + bool HonorDependencyOutputOptions = false); /// \brief Utility to run a FrontendAction in a single clang invocation. class ToolInvocation { @@ -241,6 +245,9 @@ ~ToolInvocation(); + /// \brief Remove -M, -MD, -MF, -MM from the CommandLine. + void RemoveDependencyFileArgs(); + /// \brief Set a \c DiagnosticConsumer to use during parsing. void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer) { this->DiagConsumer = DiagConsumer; @@ -255,8 +262,11 @@ /// \brief Run the clang invocation. /// + /// \param HonorDependencyOutputOptions generate a .d file if + /// -M or a related option appears on the command line. + /// /// \returns True if there were no errors during execution. - bool run(); + bool run(bool HonorDependencyOutputOptions = false); private: void addFileMappingsTo(SourceManager &SourceManager); @@ -451,9 +461,14 @@ StringRef InvokedAs); /// \brief Creates a \c CompilerInvocation. +/// \param Diagnostics where and how to report diagnostics. +/// \param CC1Args arguments to the compiler. +/// \param HonorDependencyOutputOptions generate dependency files if -M or +/// a related option appears on the command line. clang::CompilerInvocation *newInvocation( clang::DiagnosticsEngine *Diagnostics, - const llvm::opt::ArgStringList &CC1Args); + const llvm::opt::ArgStringList &CC1Args, + bool HonorDependencyOutputOptions = false); } // end namespace tooling } // end namespace clang Index: lib/Tooling/Tooling.cpp =================================================================== --- lib/Tooling/Tooling.cpp +++ lib/Tooling/Tooling.cpp @@ -92,7 +92,8 @@ /// \brief Returns a clang build invocation initialized from the CC1 flags. clang::CompilerInvocation *newInvocation( clang::DiagnosticsEngine *Diagnostics, - const llvm::opt::ArgStringList &CC1Args) { + const llvm::opt::ArgStringList &CC1Args, + bool HonorDependencyOutputOptions) { assert(!CC1Args.empty() && "Must at least contain the program name!"); clang::CompilerInvocation *Invocation = new clang::CompilerInvocation; clang::CompilerInvocation::CreateFromArgs( @@ -100,7 +101,8 @@ *Diagnostics); Invocation->getFrontendOpts().DisableFree = false; Invocation->getCodeGenOpts().DisableFree = false; - Invocation->getDependencyOutputOpts() = DependencyOutputOptions(); + if (!HonorDependencyOutputOptions) + Invocation->getDependencyOutputOpts() = DependencyOutputOptions(); return Invocation; } @@ -237,7 +239,7 @@ MappedFileContents[PathStorage] = Content; } -bool ToolInvocation::run() { +bool ToolInvocation::run(bool HonorDependencyOutputOptions) { std::vector Argv; for (const std::string &Str : CommandLine) Argv.push_back(Str.c_str()); @@ -268,7 +270,7 @@ return false; } std::unique_ptr Invocation( - newInvocation(&Diagnostics, *CC1Args)); + newInvocation(&Diagnostics, *CC1Args, HonorDependencyOutputOptions)); // FIXME: remove this when all users have migrated! for (const auto &It : MappedFileContents) { // Inject the code as the given file name into the preprocessor options. @@ -510,7 +512,8 @@ std::unique_ptr buildASTFromCodeWithArgs( const Twine &Code, const std::vector &Args, const Twine &FileName, const Twine &ToolName, - std::shared_ptr PCHContainerOps) { + std::shared_ptr PCHContainerOps, + bool HonorDependencyOutputOptions) { SmallString<16> FileNameStorage; StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); @@ -530,7 +533,7 @@ InMemoryFileSystem->addFile(FileNameRef, 0, llvm::MemoryBuffer::getMemBuffer( Code.toNullTerminatedStringRef(CodeStorage))); - if (!Invocation.run()) + if (!Invocation.run(HonorDependencyOutputOptions)) return nullptr; assert(ASTs.size() == 1);