1 //===- DependencyScanningTool.h - clang-scan-deps service -----------------===//
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_DEPENDENCY_SCANNING_TOOL_H
10 #define LLVM_CLANG_TOOLING_DEPENDENCY_SCANNING_TOOL_H
11 
12 #include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
13 #include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
14 #include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"
15 #include "clang/Tooling/JSONCompilationDatabase.h"
16 #include "llvm/ADT/StringSet.h"
17 #include <string>
18 
19 namespace clang{
20 namespace tooling{
21 namespace dependencies{
22 
23 /// The full dependencies and module graph for a specific input.
24 struct FullDependencies {
25   /// The identifier of the C++20 module this translation unit exports.
26   ///
27   /// If the translation unit is not a module then \c ID.ModuleName is empty.
28   ModuleID ID;
29 
30   /// A collection of absolute paths to files that this translation unit
31   /// directly depends on, not including transitive dependencies.
32   std::vector<std::string> FileDeps;
33 
34   /// A collection of prebuilt modules this translation unit directly depends
35   /// on, not including transitive dependencies.
36   std::vector<PrebuiltModuleDep> PrebuiltModuleDeps;
37 
38   /// A list of modules this translation unit directly depends on, not including
39   /// transitive dependencies.
40   ///
41   /// This may include modules with a different context hash when it can be
42   /// determined that the differences are benign for this compilation.
43   std::vector<ModuleID> ClangModuleDeps;
44 
45   /// Get additional arguments suitable for appending to the original Clang
46   /// command line.
47   ///
48   /// \param LookupPCMPath This function is called to fill in "-fmodule-file="
49   ///                      arguments and the "-o" argument. It needs to return
50   ///                      a path for where the PCM for the given module is to
51   ///                      be located.
52   /// \param LookupModuleDeps This function is called to collect the full
53   ///                         transitive set of dependencies for this
54   ///                         compilation and fill in "-fmodule-map-file="
55   ///                         arguments.
56   std::vector<std::string> getAdditionalArgs(
57       std::function<StringRef(ModuleID)> LookupPCMPath,
58       std::function<const ModuleDeps &(ModuleID)> LookupModuleDeps) const;
59 
60   /// Get additional arguments suitable for appending to the original Clang
61   /// command line, excluding arguments containing modules-related paths:
62   /// "-fmodule-file=", "-fmodule-map-file=".
63   std::vector<std::string> getAdditionalArgsWithoutModulePaths() const;
64 };
65 
66 struct FullDependenciesResult {
67   FullDependencies FullDeps;
68   std::vector<ModuleDeps> DiscoveredModules;
69 };
70 
71 /// The high-level implementation of the dependency discovery tool that runs on
72 /// an individual worker thread.
73 class DependencyScanningTool {
74 public:
75   /// Construct a dependency scanning tool.
76   DependencyScanningTool(DependencyScanningService &Service);
77 
78   /// Print out the dependency information into a string using the dependency
79   /// file format that is specified in the options (-MD is the default) and
80   /// return it.
81   ///
82   /// \returns A \c StringError with the diagnostic output if clang errors
83   /// occurred, dependency file contents otherwise.
84   llvm::Expected<std::string>
85   getDependencyFile(const tooling::CompilationDatabase &Compilations,
86                     StringRef CWD);
87 
88   /// Collect the full module dependency graph for the input, ignoring any
89   /// modules which have already been seen.
90   ///
91   /// \param AlreadySeen This stores modules which have previously been
92   ///                    reported. Use the same instance for all calls to this
93   ///                    function for a single \c DependencyScanningTool in a
94   ///                    single build. Use a different one for different tools,
95   ///                    and clear it between builds.
96   ///
97   /// \returns a \c StringError with the diagnostic output if clang errors
98   /// occurred, \c FullDependencies otherwise.
99   llvm::Expected<FullDependenciesResult>
100   getFullDependencies(const tooling::CompilationDatabase &Compilations,
101                       StringRef CWD, const llvm::StringSet<> &AlreadySeen);
102 
103 private:
104   DependencyScanningWorker Worker;
105 };
106 
107 } // end namespace dependencies
108 } // end namespace tooling
109 } // end namespace clang
110 
111 #endif // LLVM_CLANG_TOOLING_DEPENDENCY_SCANNING_TOOL_H
112