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
14#include "llvm/ADT/IntrusiveRefCntPtr.h"
15#include "llvm/Support/VirtualFileSystem.h"
16
17using namespace clang;
18using namespace dependencies;
19
22 : Service(Service) {
23 PCHContainerOps = std::make_shared<PCHContainerOperations>();
24 // We need to read object files from PCH built outside the scanner.
25 PCHContainerOps->registerReader(
26 std::make_unique<ObjectFilePCHContainerReader>());
27 // The scanner itself writes only raw ast files.
28 PCHContainerOps->registerWriter(std::make_unique<RawPCHContainerWriter>());
29
30 auto BaseFS = Service.getOpts().MakeVFS();
31
32 if (Service.getOpts().TraceVFS)
33 BaseFS = llvm::makeIntrusiveRefCnt<llvm::vfs::TracingFileSystem>(
34 std::move(BaseFS));
35
36 DepFS = llvm::makeIntrusiveRefCnt<DependencyScanningWorkerFilesystem>(
37 Service.getSharedCache(), std::move(BaseFS));
38}
39
42
46 std::shared_ptr<clang::PCHContainerOperations> &PCHContainerOps,
47 DiagnosticsEngine &Diags) {
48 auto Invocation = createCompilerInvocation(CommandLine, Diags);
49 if (!Invocation)
50 return false;
51
52 return Action.runInvocation(CommandLine[0], std::move(Invocation),
53 std::move(FS), PCHContainerOps,
54 Diags.getClient());
55}
56
58 StringRef WorkingDirectory, ArrayRef<std::string> CommandLine,
59 DependencyConsumer &DepConsumer, DependencyActionController &Controller,
60 DiagnosticConsumer &DiagConsumer,
62 return computeDependencies(WorkingDirectory,
63 ArrayRef<ArrayRef<std::string>>(CommandLine),
64 DepConsumer, Controller, DiagConsumer, OverlayFS);
65}
66
68 StringRef WorkingDirectory, ArrayRef<ArrayRef<std::string>> CommandLines,
69 DependencyConsumer &DepConsumer, DependencyActionController &Controller,
70 DiagnosticConsumer &DiagConsumer,
73 if (OverlayFS) {
74#ifndef NDEBUG
75 bool SawDepFS = false;
76 OverlayFS->visit(
77 [&](llvm::vfs::FileSystem &VFS) { SawDepFS |= &VFS == DepFS.get(); });
78 assert(SawDepFS && "OverlayFS not based on DepFS");
79#endif
80 FS = std::move(OverlayFS);
81 } else {
82 FS = DepFS;
83 FS->setCurrentWorkingDirectory(WorkingDirectory);
84 }
85
86 DependencyScanningAction Action(Service, WorkingDirectory, DepConsumer,
87 Controller, DepFS);
88
89 const bool Success = llvm::all_of(CommandLines, [&](const auto &Cmd) {
90 if (StringRef(Cmd[1]) != "-cc1") {
91 // Non-clang command. Just pass through to the dependency consumer.
92 DepConsumer.handleBuildCommand(
93 {Cmd.front(), {Cmd.begin() + 1, Cmd.end()}});
94 return true;
95 }
96
97 auto DiagEngineWithDiagOpts =
98 DiagnosticsEngineWithDiagOpts(Cmd, FS, DiagConsumer);
99 auto &Diags = *DiagEngineWithDiagOpts.DiagEngine;
100
101 // Create an invocation that uses the underlying file system to ensure that
102 // any file system requests that are made by the driver do not go through
103 // the dependency scanning filesystem.
104 return createAndRunToolInvocation(Cmd, Action, FS, PCHContainerOps, Diags);
105 });
106
107 return Success && Action.hasScanned();
108}
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:232
DiagnosticConsumer * getClient()
Definition Diagnostic.h:612
Dependency scanner callbacks that are used during scanning to influence the behaviour of the scan - f...
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< std::string > CommandLine, DependencyConsumer &DepConsumer, DependencyActionController &Controller, DiagnosticConsumer &DiagConsumer, llvm::IntrusiveRefCntPtr< llvm::vfs::OverlayFileSystem > OverlayFS=nullptr)
Run the dependency scanning worker for the given frontend command-line, and report the discovered dep...
@ VFS
Remove unused -ivfsoverlay arguments.
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