1 //===- DependencyScanningWorker.h - clang-scan-deps worker ===---*- C++ -*-===//
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 #ifndef LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGWORKER_H
10 #define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGWORKER_H
11 
12 #include "clang/Basic/DiagnosticOptions.h"
13 #include "clang/Basic/FileManager.h"
14 #include "clang/Basic/LLVM.h"
15 #include "clang/Frontend/PCHContainerOperations.h"
16 #include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
17 #include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"
18 #include "llvm/Support/Error.h"
19 #include "llvm/Support/FileSystem.h"
20 #include <string>
21 
22 namespace clang {
23 
24 class DependencyOutputOptions;
25 
26 namespace tooling {
27 namespace dependencies {
28 
29 class DependencyScanningWorkerFilesystem;
30 
31 class DependencyConsumer {
32 public:
33   virtual ~DependencyConsumer() {}
34 
35   virtual void
36   handleDependencyOutputOpts(const DependencyOutputOptions &Opts) = 0;
37 
38   virtual void handleFileDependency(StringRef Filename) = 0;
39 
40   virtual void handlePrebuiltModuleDependency(PrebuiltModuleDep PMD) = 0;
41 
42   virtual void handleModuleDependency(ModuleDeps MD) = 0;
43 
44   virtual void handleContextHash(std::string Hash) = 0;
45 };
46 
47 /// An individual dependency scanning worker that is able to run on its own
48 /// thread.
49 ///
50 /// The worker computes the dependencies for the input files by preprocessing
51 /// sources either using a fast mode where the source files are minimized, or
52 /// using the regular processing run.
53 class DependencyScanningWorker {
54 public:
55   DependencyScanningWorker(DependencyScanningService &Service);
56 
57   /// Run the dependency scanning tool for a given clang driver command-line,
58   /// and report the discovered dependencies to the provided consumer. If \p
59   /// ModuleName isn't empty, this function reports the dependencies of module
60   /// \p ModuleName.
61   ///
62   /// \returns A \c StringError with the diagnostic output if clang errors
63   /// occurred, success otherwise.
64   llvm::Error computeDependencies(StringRef WorkingDirectory,
65                                   const std::vector<std::string> &CommandLine,
66                                   DependencyConsumer &Consumer,
67                                   llvm::Optional<StringRef> ModuleName = None);
68 
69 private:
70   std::shared_ptr<PCHContainerOperations> PCHContainerOps;
71 
72   /// The physical filesystem overlaid by `InMemoryFS`.
73   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> RealFS;
74   /// The in-memory filesystem laid on top the physical filesystem in `RealFS`.
75   llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFS;
76   /// The file system that is used by each worker when scanning for
77   /// dependencies. This filesystem persists across multiple compiler
78   /// invocations.
79   llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS;
80   /// The file manager that is reused across multiple invocations by this
81   /// worker. If null, the file manager will not be reused.
82   llvm::IntrusiveRefCntPtr<FileManager> Files;
83   ScanningOutputFormat Format;
84   /// Whether to optimize the modules' command-line arguments.
85   bool OptimizeArgs;
86 };
87 
88 } // end namespace dependencies
89 } // end namespace tooling
90 } // end namespace clang
91 
92 #endif // LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGWORKER_H
93