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