@@ -334,22 +334,9 @@ class ScanningDependencyDirectivesGetter : public DependencyDirectivesGetter {
334334 return DepFS->getDirectiveTokens (File.getName ());
335335 }
336336};
337- } // namespace
338-
339- std::unique_ptr<DiagnosticOptions>
340- clang::tooling::dependencies::createDiagOptions (
341- const std::vector<std::string> &CommandLine) {
342- std::vector<const char *> CLI;
343- for (const std::string &Arg : CommandLine)
344- CLI.push_back (Arg.c_str ());
345- auto DiagOpts = CreateAndPopulateDiagOpts (CLI);
346- sanitizeDiagOpts (*DiagOpts);
347- return DiagOpts;
348- }
349337
350338// / Sanitize diagnostic options for dependency scan.
351- void clang::tooling::dependencies::sanitizeDiagOpts (
352- DiagnosticOptions &DiagOpts) {
339+ void sanitizeDiagOpts (DiagnosticOptions &DiagOpts) {
353340 // Don't print 'X warnings and Y errors generated'.
354341 DiagOpts.ShowCarets = false ;
355342 // Don't write out diagnostic file.
@@ -368,6 +355,31 @@ void clang::tooling::dependencies::sanitizeDiagOpts(
368355 .Default (true );
369356 });
370357}
358+ } // namespace
359+
360+ std::unique_ptr<DiagnosticOptions>
361+ clang::tooling::dependencies::createDiagOptions (
362+ const std::vector<std::string> &CommandLine) {
363+ std::vector<const char *> CLI;
364+ for (const std::string &Arg : CommandLine)
365+ CLI.push_back (Arg.c_str ());
366+ auto DiagOpts = CreateAndPopulateDiagOpts (CLI);
367+ sanitizeDiagOpts (*DiagOpts);
368+ return DiagOpts;
369+ }
370+
371+ clang::tooling::dependencies::DignosticsEngineWithCCommandLineAndDiagOpts::
372+ DignosticsEngineWithCCommandLineAndDiagOpts (
373+ const std::vector<std::string> CommandLine,
374+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS, DiagnosticConsumer &DC)
375+ : CCommandLine(CommandLine.size(), nullptr) {
376+ llvm::transform (CommandLine, CCommandLine.begin (),
377+ [](const std::string &Str) { return Str.c_str (); });
378+ DiagOpts = CreateAndPopulateDiagOpts (CCommandLine);
379+ sanitizeDiagOpts (*DiagOpts);
380+ DiagEngine = CompilerInstance::createDiagnostics (*FS, *DiagOpts, &DC,
381+ /* ShouldOwnClient=*/ false );
382+ }
371383
372384std::pair<std::unique_ptr<driver::Driver>, std::unique_ptr<driver::Compilation>>
373385clang::tooling::dependencies::buildCompilation (
@@ -395,7 +407,7 @@ clang::tooling::dependencies::buildCompilation(
395407 }
396408
397409 std::unique_ptr<driver::Compilation> Compilation (
398- Driver->BuildCompilation (llvm::ArrayRef ( Argv) ));
410+ Driver->BuildCompilation (Argv));
399411 if (!Compilation)
400412 return std::make_pair (nullptr , nullptr );
401413
@@ -405,45 +417,37 @@ clang::tooling::dependencies::buildCompilation(
405417 return std::make_pair (std::move (Driver), std::move (Compilation));
406418}
407419
408- llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
409- clang::tooling::dependencies::initVFSForTUBufferScanning (
410- llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
411- std::optional<std::vector<std::string>> &ModifiedCommandLine,
420+ std::pair<IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::vector<std::string>>
421+ clang::tooling::dependencies::initVFSForTUBuferScanning (
422+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
412423 const std::vector<std::string> &CommandLine, StringRef WorkingDirectory,
413- std::optional< llvm::MemoryBufferRef> TUBuffer) {
424+ llvm::MemoryBufferRef TUBuffer) {
414425 // Reset what might have been modified in the previous worker invocation.
415426 BaseFS->setCurrentWorkingDirectory (WorkingDirectory);
416427
417- llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> ModifiedFS;
418- if (TUBuffer) {
419- auto OverlayFS =
420- llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(BaseFS);
421- auto InMemoryFS =
422- llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
423- InMemoryFS->setCurrentWorkingDirectory (WorkingDirectory);
424- auto InputPath = TUBuffer->getBufferIdentifier ();
425- InMemoryFS->addFile (
426- InputPath, 0 ,
427- llvm::MemoryBuffer::getMemBufferCopy (TUBuffer->getBuffer ()));
428- llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> InMemoryOverlay =
429- InMemoryFS;
430-
431- OverlayFS->pushOverlay (InMemoryOverlay);
432- ModifiedFS = OverlayFS;
433- ModifiedCommandLine = CommandLine;
434- ModifiedCommandLine->emplace_back (InputPath);
435-
436- return ModifiedFS;
437- }
428+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> ModifiedFS;
429+ auto OverlayFS =
430+ llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(BaseFS);
431+ auto InMemoryFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
432+ InMemoryFS->setCurrentWorkingDirectory (WorkingDirectory);
433+ auto InputPath = TUBuffer.getBufferIdentifier ();
434+ InMemoryFS->addFile (
435+ InputPath, 0 , llvm::MemoryBuffer::getMemBufferCopy (TUBuffer.getBuffer ()));
436+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> InMemoryOverlay = InMemoryFS;
437+
438+ OverlayFS->pushOverlay (InMemoryOverlay);
439+ ModifiedFS = OverlayFS;
440+ auto ModifiedCommandLine = CommandLine;
441+ ModifiedCommandLine.emplace_back (InputPath);
438442
439- return BaseFS ;
443+ return std::make_pair (ModifiedFS, ModifiedCommandLine) ;
440444}
441445
442- llvm:: IntrusiveRefCntPtr<llvm::vfs::FileSystem>
446+ std::pair< IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::vector<std::string> >
443447clang::tooling::dependencies::initVFSForByNameScanning (
444- llvm:: IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
445- std::vector<std::string> &CommandLine, StringRef WorkingDirectory,
446- StringRef FakeFileNamePrefix ) {
448+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
449+ const std::vector<std::string> &CommandLine, StringRef WorkingDirectory,
450+ StringRef ModuleName ) {
447451 // Reset what might have been modified in the previous worker invocation.
448452 BaseFS->setCurrentWorkingDirectory (WorkingDirectory);
449453
@@ -456,16 +460,16 @@ clang::tooling::dependencies::initVFSForByNameScanning(
456460 InMemoryFS->setCurrentWorkingDirectory (WorkingDirectory);
457461 SmallString<128 > FakeInputPath;
458462 // TODO: We should retry the creation if the path already exists.
459- llvm::sys::fs::createUniquePath (FakeFileNamePrefix + " -%%%%%%%%.input" ,
460- FakeInputPath,
463+ llvm::sys::fs::createUniquePath (ModuleName + " -%%%%%%%%.input" , FakeInputPath,
461464 /* MakeAbsolute=*/ false );
462465 InMemoryFS->addFile (FakeInputPath, 0 , llvm::MemoryBuffer::getMemBuffer (" " ));
463- llvm:: IntrusiveRefCntPtr<llvm::vfs::FileSystem> InMemoryOverlay = InMemoryFS;
466+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> InMemoryOverlay = InMemoryFS;
464467 OverlayFS->pushOverlay (InMemoryOverlay);
465468
466- CommandLine.emplace_back (FakeInputPath);
469+ auto ModifiedCommandLine = CommandLine;
470+ ModifiedCommandLine.emplace_back (FakeInputPath);
467471
468- return OverlayFS;
472+ return std::make_pair ( OverlayFS, ModifiedCommandLine) ;
469473}
470474
471475std::unique_ptr<CompilerInvocation>
@@ -487,7 +491,7 @@ bool clang::tooling::dependencies::initializeScanCompilerInstance(
487491 CompilerInstance &ScanInstance,
488492 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
489493 DiagnosticConsumer *DiagConsumer, DependencyScanningService &Service,
490- llvm:: IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS) {
494+ IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS) {
491495 ScanInstance.setBuildingModule (false );
492496
493497 ScanInstance.createVirtualFileSystem (FS, DiagConsumer);
@@ -554,7 +558,7 @@ bool clang::tooling::dependencies::initializeScanCompilerInstance(
554558}
555559
556560llvm::SmallVector<StringRef> clang::tooling::dependencies::computeStableDirs (
557- CompilerInstance &ScanInstance) {
561+ const CompilerInstance &ScanInstance) {
558562 // Create a collection of stable directories derived from the ScanInstance
559563 // for determining whether module dependencies would fully resolve from
560564 // those directories.
@@ -600,7 +604,7 @@ void clang::tooling::dependencies::initializeModuleDepCollector(
600604 // and thus won't write out the extra '.d' files to disk.
601605 auto Opts = std::make_unique<DependencyOutputOptions>();
602606
603- // We rely on the behaviour that that ScanInstance's Invocation instance's
607+ // We rely on the behaviour that the ScanInstance's Invocation instance's
604608 // dependency output opts are cleared here.
605609 // TODO: we will need to preserve and recover the original dependency output
606610 // opts if we want to reuse ScanInstance.
0 commit comments