clang 23.0.0git
DependencyScanningWorker.cpp
Go to the documentation of this file.
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
15#include "llvm/ADT/IntrusiveRefCntPtr.h"
16#include "llvm/Support/VirtualFileSystem.h"
17
18using namespace clang;
19using namespace dependencies;
20
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 std::make_unique<ObjectFilePCHContainerReader>());
28 // The scanner itself writes only raw ast files.
29 PCHContainerOps->registerWriter(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 std::move(BaseFS));
36 BaseFS = TracingFS;
37 }
38
39 DepFS = llvm::makeIntrusiveRefCnt<DependencyScanningWorkerFilesystem>(
40 Service, std::move(BaseFS));
41}
42
44
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(CommandLine[0], std::move(Invocation),
55 std::move(FS), PCHContainerOps,
56 Diags.getClient());
57}
58
59IntrusiveRefCntPtr<llvm::vfs::FileSystem>
61 StringRef WorkingDirectory,
64 if (OverlayFS) {
65 auto NewFS =
66 llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(std::move(FS));
67 NewFS->pushOverlay(std::move(OverlayFS));
68 FS = std::move(NewFS);
69 }
70 FS->setCurrentWorkingDirectory(WorkingDirectory);
71 return FS;
72}
73
75 StringRef WorkingDirectory, ArrayRef<ArrayRef<std::string>> CommandLines,
76 DependencyConsumer &DepConsumer, DependencyActionController &Controller,
77 DiagnosticConsumer &DiagConsumer,
79 auto FS = makeEffectiveVFS(WorkingDirectory, std::move(OverlayFS));
80
81 DependencyScanningAction Action(Service, WorkingDirectory, DepConsumer,
82 Controller, DepFS);
83
84 const bool Success = llvm::all_of(CommandLines, [&](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.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}
Defines the Diagnostic-related interfaces.
static bool createAndRunToolInvocation(ArrayRef< std::string > CommandLine, DependencyScanningAction &Action, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS, std::shared_ptr< clang::PCHContainerOperations > &PCHContainerOps, DiagnosticsEngine &Diags)
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:233
DiagnosticConsumer * getClient()
Definition Diagnostic.h:613
Dependency scanner callbacks that are used during scanning to influence the behaviour of the scan - f...
virtual void handleBuildCommand(Command Cmd)
bool runInvocation(std::string Executable, std::unique_ptr< CompilerInvocation > Invocation, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS, std::shared_ptr< PCHContainerOperations > PCHContainerOps, DiagnosticConsumer *DiagConsumer)
The dependency scanning service contains shared configuration and state that is used by the individua...
DependencyScanningWorker(DependencyScanningService &Service)
Construct a dependency scanning worker.
bool computeDependencies(StringRef WorkingDirectory, ArrayRef< ArrayRef< std::string > > CommandLines, DependencyConsumer &DepConsumer, DependencyActionController &Controller, DiagnosticConsumer &DiagConsumer, IntrusiveRefCntPtr< llvm::vfs::FileSystem > OverlayFS=nullptr)
Run the dependency scanning tool for all given frontend command-lines, and report the discovered depe...
IntrusiveRefCntPtr< llvm::vfs::FileSystem > makeEffectiveVFS(StringRef WorkingDirectory, IntrusiveRefCntPtr< llvm::vfs::FileSystem > OverlayFS=nullptr) const
Creates the effective VFS that will be used for the scan.
std::unique_ptr< CompilerInvocation > createCompilerInvocation(ArrayRef< std::string > CommandLine, DiagnosticsEngine &Diags)
The JSON file list parser is used to communicate input to InstallAPI.
@ Success
Annotation was successful.
Definition Parser.h:65