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                            llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS);
57 
58   /// Run the dependency scanning tool for a given clang driver command-line,
59   /// and report the discovered dependencies to the provided consumer. If \p
60   /// ModuleName isn't empty, this function reports the dependencies of module
61   /// \p ModuleName.
62   ///
63   /// \returns A \c StringError with the diagnostic output if clang errors
64   /// occurred, success otherwise.
65   llvm::Error computeDependencies(StringRef WorkingDirectory,
66                                   const std::vector<std::string> &CommandLine,
67                                   DependencyConsumer &Consumer,
68                                   llvm::Optional<StringRef> ModuleName = None);
69 
70 private:
71   std::shared_ptr<PCHContainerOperations> PCHContainerOps;
72 
73   /// The physical filesystem overlaid by `InMemoryFS`.
74   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> RealFS;
75   /// The in-memory filesystem laid on top the physical filesystem in `RealFS`.
76   llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFS;
77   /// The file system that is used by each worker when scanning for
78   /// dependencies. This filesystem persists across multiple compiler
79   /// invocations.
80   llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS;
81   /// The file manager that is reused across multiple invocations by this
82   /// worker. If null, the file manager will not be reused.
83   llvm::IntrusiveRefCntPtr<FileManager> Files;
84   ScanningOutputFormat Format;
85   /// Whether to optimize the modules' command-line arguments.
86   bool OptimizeArgs;
87 };
88 
89 } // end namespace dependencies
90 } // end namespace tooling
91 } // end namespace clang
92 
93 #endif // LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGWORKER_H
94