================
@@ -160,43 +333,92 @@ DependencyScanningTool::getModuleDependencies(
StringRef ModuleName, const std::vector<std::string> &CommandLine,
StringRef CWD, const llvm::DenseSet<ModuleID> &AlreadySeen,
LookupModuleOutputCallback LookupModuleOutput) {
- FullDependencyConsumer Consumer(AlreadySeen);
- CallbackActionController Controller(LookupModuleOutput);
if (auto Error =
- Worker.initializeCompilerInstanceWithContextOrError(CWD,
CommandLine))
- return std::move(Error);
+ initializeCompilerInstanceWithContextOrError(CWD, CommandLine))
+ return Error;
- auto Result = Worker.computeDependenciesByNameWithContextOrError(
- ModuleName, Consumer, Controller);
+ auto Result = computeDependenciesByNameWithContextOrError(
+ ModuleName, AlreadySeen, LookupModuleOutput);
- if (auto Error = Worker.finalizeCompilerInstanceWithContextOrError())
- return std::move(Error);
+ if (auto Error = finalizeCompilerInstanceWithContextOrError())
+ return Error;
- if (Result)
- return std::move(Result);
+ return Result;
+}
- return Consumer.takeTranslationUnitDeps();
+static std::optional<std::vector<std::string>> getFirstCC1CommandLine(
+ ArrayRef<std::string> CommandLine, DiagnosticsEngine &Diags,
+ llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> ScanFS) {
+ // Compilation holds a non-owning a reference to the Driver, hence we need to
+ // keep the Driver alive when we use Compilation. Arguments to commands may
be
+ // owned by Alloc when expanded from response files.
+ llvm::BumpPtrAllocator Alloc;
+ const auto [Driver, Compilation] =
+ buildCompilation(CommandLine, Diags, ScanFS, Alloc);
+ if (!Compilation)
+ return std::nullopt;
+
+ const auto IsClangCmd = [](const driver::Command &Cmd) {
+ return StringRef(Cmd.getCreator().getName()) == "clang";
+ };
+ const auto CC1CommandLineRange = llvm::map_range(
+ llvm::make_filter_range(Compilation->getJobs(), IsClangCmd),
+ buildCC1CommandLine);
+
+ if (CC1CommandLineRange.empty())
+ return std::nullopt;
+ return *CC1CommandLineRange.begin();
}
-llvm::Error DependencyScanningTool::initializeCompilerInstanceWithContext(
- StringRef CWD, const std::vector<std::string> &CommandLine) {
- return Worker.initializeCompilerInstanceWithContextOrError(CWD, CommandLine);
+llvm::Error
+DependencyScanningTool::initializeCompilerInstanceWithContextOrError(
+ StringRef CWD, ArrayRef<std::string> CommandLine) {
+ // For by name scanning, we allow command lines without an actual input file
+ // by adding an in-memory placeholder input.
+ auto OverlayFSAndArgs = initVFSForByNameScanning(
+ &Worker.getVFS(), CommandLine, CWD, "ScanningByName");
+ auto &OverlayFS = OverlayFSAndArgs.first;
+ const auto &ModifiedCommandLine = OverlayFSAndArgs.second;
+
+ DiagPrinterWithOS =
+ std::make_unique<TextDiagnosticsPrinterWithOutput>(CommandLine);
+ auto DiagEngineWithCmdAndOpts =
+ std::make_unique<DignosticsEngineWithDiagOpts>(
+ CommandLine, OverlayFS, DiagPrinterWithOS->DiagPrinter);
+
+ const auto InitWithCommandLine =
+ [&](ArrayRef<std::string> CommandLine) -> llvm::Error {
+ if (Worker.initializeCompilerInstanceWithContext(
+ CWD, CommandLine, std::move(DiagEngineWithCmdAndOpts), OverlayFS))
+ return llvm::Error::success();
+ return makeErrorFromDiagnosticsOS(*DiagPrinterWithOS);
+ };
+
+ if (CommandLine.size() >= 2 && CommandLine[1] == "-cc1")
+ return InitWithCommandLine(CommandLine);
+
+ const auto MaybeFirstCC1 = getFirstCC1CommandLine(
+ ModifiedCommandLine, *DiagEngineWithCmdAndOpts->DiagEngine, OverlayFS);
+ if (!MaybeFirstCC1)
+ return makeErrorFromDiagnosticsOS(*DiagPrinterWithOS);
----------------
jansvoboda11 wrote:
It seems that `getFirstCC1CommandLine()` may return `std::nullopt` without
emitting any diagnostics, so this seems incomplete.
https://github.com/llvm/llvm-project/pull/169964
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits