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
13#include "clang/Driver/Driver.h"
14#include "clang/Driver/Tool.h"
16#include "llvm/ADT/IntrusiveRefCntPtr.h"
17#include "llvm/Support/VirtualFileSystem.h"
18
19using namespace clang;
20using namespace dependencies;
21
24 : Service(Service) {
25 PCHContainerOps = std::make_shared<PCHContainerOperations>();
26 // We need to read object files from PCH built outside the scanner.
27 PCHContainerOps->registerReader(
28 std::make_unique<ObjectFilePCHContainerReader>());
29 // The scanner itself writes only raw ast files.
30 PCHContainerOps->registerWriter(std::make_unique<RawPCHContainerWriter>());
31
32 auto BaseFS = Service.getOpts().MakeVFS();
33
34 if (Service.getOpts().TraceVFS)
35 BaseFS = llvm::makeIntrusiveRefCnt<llvm::vfs::TracingFileSystem>(
36 std::move(BaseFS));
37
38 DepFS = llvm::makeIntrusiveRefCnt<DependencyScanningWorkerFilesystem>(
39 Service.getSharedCache(), std::move(BaseFS));
40}
41
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
60 StringRef WorkingDirectory, ArrayRef<std::string> CommandLine,
61 DependencyConsumer &DepConsumer, DependencyActionController &Controller,
62 DiagnosticConsumer &DiagConsumer,
64 return computeDependencies(WorkingDirectory,
65 ArrayRef<ArrayRef<std::string>>(CommandLine),
66 DepConsumer, Controller, DiagConsumer, OverlayFS);
67}
68
70 StringRef WorkingDirectory, ArrayRef<ArrayRef<std::string>> CommandLines,
71 DependencyConsumer &DepConsumer, DependencyActionController &Controller,
72 DiagnosticConsumer &DiagConsumer,
75 if (OverlayFS) {
76#ifndef NDEBUG
77 bool SawDepFS = false;
78 OverlayFS->visit(
79 [&](llvm::vfs::FileSystem &VFS) { SawDepFS |= &VFS == DepFS.get(); });
80 assert(SawDepFS && "OverlayFS not based on DepFS");
81#endif
82 FS = std::move(OverlayFS);
83 } else {
84 FS = DepFS;
85 FS->setCurrentWorkingDirectory(WorkingDirectory);
86 }
87
88 DependencyScanningAction Action(Service, WorkingDirectory, DepConsumer,
89 Controller, DepFS);
90
91 const bool Success = llvm::all_of(CommandLines, [&](const auto &Cmd) {
92 if (StringRef(Cmd[1]) != "-cc1") {
93 // Non-clang command. Just pass through to the dependency consumer.
94 DepConsumer.handleBuildCommand(
95 {Cmd.front(), {Cmd.begin() + 1, Cmd.end()}});
96 return true;
97 }
98
99 auto DiagEngineWithDiagOpts =
100 DiagnosticsEngineWithDiagOpts(Cmd, FS, DiagConsumer);
101 auto &Diags = *DiagEngineWithDiagOpts.DiagEngine;
102
103 // Create an invocation that uses the underlying file system to ensure that
104 // any file system requests that are made by the driver do not go through
105 // the dependency scanning filesystem.
106 return createAndRunToolInvocation(Cmd, Action, FS, PCHContainerOps, Diags);
107 });
108
109 return Success && Action.hasScanned();
110}
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