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/Lex/PreprocessorExcludedConditionalDirectiveSkipMapping.h"
17 #include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
18 #include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"
19 #include "llvm/Support/Error.h"
20 #include "llvm/Support/FileSystem.h"
21 #include <string>
22 
23 namespace clang {
24 
25 class DependencyOutputOptions;
26 
27 namespace tooling {
28 namespace dependencies {
29 
30 class DependencyScanningWorkerFilesystem;
31 
32 class DependencyConsumer {
33 public:
34   virtual ~DependencyConsumer() {}
35 
36   virtual void
37   handleDependencyOutputOpts(const DependencyOutputOptions &Opts) = 0;
38 
39   virtual void handleFileDependency(StringRef Filename) = 0;
40 
41   virtual void handlePrebuiltModuleDependency(PrebuiltModuleDep PMD) = 0;
42 
43   virtual void handleModuleDependency(ModuleDeps MD) = 0;
44 
45   virtual void handleContextHash(std::string Hash) = 0;
46 };
47 
48 /// An individual dependency scanning worker that is able to run on its own
49 /// thread.
50 ///
51 /// The worker computes the dependencies for the input files by preprocessing
52 /// sources either using a fast mode where the source files are minimized, or
53 /// using the regular processing run.
54 class DependencyScanningWorker {
55 public:
56   DependencyScanningWorker(DependencyScanningService &Service);
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   std::unique_ptr<ExcludedPreprocessorDirectiveSkipMapping> PPSkipMappings;
73 
74   /// The physical filesystem overlaid by `InMemoryFS`.
75   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> RealFS;
76   /// The in-memory filesystem laid on top the physical filesystem in `RealFS`.
77   llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFS;
78   /// The file system that is used by each worker when scanning for
79   /// dependencies. This filesystem persists across multiple compiler
80   /// invocations.
81   llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS;
82   /// The file manager that is reused across multiple invocations by this
83   /// worker. If null, the file manager will not be reused.
84   llvm::IntrusiveRefCntPtr<FileManager> Files;
85   ScanningOutputFormat Format;
86   /// Whether to optimize the modules' command-line arguments.
87   bool OptimizeArgs;
88 };
89 
90 } // end namespace dependencies
91 } // end namespace tooling
92 } // end namespace clang
93 
94 #endif // LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGWORKER_H
95