1//===- DependencyScanningWorker.cpp - Thread-Safe Scanning Worker ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "clang/DependencyScanning/DependencyScanningWorker.h"
10#include "clang/Basic/Diagnostic.h"
11#include "clang/Basic/DiagnosticFrontend.h"
12#include "clang/DependencyScanning/DependencyConsumer.h"
13#include "clang/DependencyScanning/DependencyScannerImpl.h"
14#include "clang/Serialization/ObjectFilePCHContainerReader.h"
15#include "llvm/ADT/IntrusiveRefCntPtr.h"
16#include "llvm/Support/VirtualFileSystem.h"
17
18using namespace clang;
19using namespace dependencies;
20
21DependencyScanningWorker::DependencyScanningWorker(
22 DependencyScanningService &Service)
23 : Service(Service) {
24 PCHContainerOps = std::make_shared<PCHContainerOperations>();
25 // We need to read object files from PCH built outside the scanner.
26 PCHContainerOps->registerReader(
27 Reader: std::make_unique<ObjectFilePCHContainerReader>());
28 // The scanner itself writes only raw ast files.
29 PCHContainerOps->registerWriter(Writer: std::make_unique<RawPCHContainerWriter>());
30
31 auto BaseFS = Service.getOpts().MakeVFS();
32
33 if (Service.getOpts().TraceVFS) {
34 TracingFS = llvm::makeIntrusiveRefCnt<llvm::vfs::TracingFileSystem>(
35 A: std::move(BaseFS));
36 BaseFS = TracingFS;
37 }
38
39 DepFS = llvm::makeIntrusiveRefCnt<DependencyScanningWorkerFilesystem>(
40 A&: Service, A: std::move(BaseFS));
41}
42
43DependencyScanningWorker::~DependencyScanningWorker() = default;
44
45static bool createAndRunToolInvocation(
46 ArrayRef<std::string> CommandLine, DependencyScanningAction &Action,
47 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
48 std::shared_ptr<clang::PCHContainerOperations> &PCHContainerOps,
49 DiagnosticsEngine &Diags) {
50 auto Invocation = createCompilerInvocation(CommandLine, Diags);
51 if (!Invocation)
52 return false;
53
54 return Action.runInvocation(Executable: CommandLine[0], Invocation: std::move(Invocation),
55 FS: std::move(FS), PCHContainerOps,
56 DiagConsumer: Diags.getClient());
57}
58
59IntrusiveRefCntPtr<llvm::vfs::FileSystem>
60DependencyScanningWorker::makeEffectiveVFS(
61 StringRef WorkingDirectory,
62 IntrusiveRefCntPtr<llvm::vfs::FileSystem> OverlayFS) const {
63 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = DepFS;
64 if (OverlayFS) {
65 auto NewFS =
66 llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(A: std::move(FS));
67 NewFS->pushOverlay(FS: std::move(OverlayFS));
68 FS = std::move(NewFS);
69 }
70 FS->setCurrentWorkingDirectory(WorkingDirectory);
71 return FS;
72}
73
74bool DependencyScanningWorker::computeDependencies(
75 StringRef WorkingDirectory, ArrayRef<ArrayRef<std::string>> CommandLines,
76 DependencyConsumer &DepConsumer, DependencyActionController &Controller,
77 DiagnosticConsumer &DiagConsumer,
78 IntrusiveRefCntPtr<llvm::vfs::FileSystem> OverlayFS) {
79 auto FS = makeEffectiveVFS(WorkingDirectory, OverlayFS: std::move(OverlayFS));
80
81 DependencyScanningAction Action(Service, WorkingDirectory, DepConsumer,
82 Controller, DepFS);
83
84 const bool Success = llvm::all_of(Range&: CommandLines, P: [&](const auto &Cmd) {
85 if (StringRef(Cmd[1]) != "-cc1") {
86 // Non-clang command. Just pass through to the dependency consumer.
87 DepConsumer.handleBuildCommand(
88 Cmd: {Cmd.front(), {Cmd.begin() + 1, Cmd.end()}});
89 return true;
90 }
91
92 auto DiagEngineWithDiagOpts =
93 DiagnosticsEngineWithDiagOpts(Cmd, FS, DiagConsumer);
94 auto &Diags = *DiagEngineWithDiagOpts.DiagEngine;
95
96 // Create an invocation that uses the underlying file system to ensure that
97 // any file system requests that are made by the driver do not go through
98 // the dependency scanning filesystem.
99 return createAndRunToolInvocation(Cmd, Action, FS, PCHContainerOps, Diags);
100 });
101
102 return Success && Action.hasScanned();
103}
104