1 //===- DependencyScanningService.h - clang-scan-deps service ===-*- 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_DEPENDENCYSCANNINGSERVICE_H
10 #define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGSERVICE_H
11 
12 #include "clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h"
13 #include "clang/Tooling/DependencyScanning/InProcessModuleCache.h"
14 #include "llvm/ADT/BitmaskEnum.h"
15 #include "llvm/Support/Chrono.h"
16 
17 namespace clang {
18 namespace tooling {
19 namespace dependencies {
20 
21 /// The mode in which the dependency scanner will operate to find the
22 /// dependencies.
23 enum class ScanningMode {
24   /// This mode is used to compute the dependencies by running the preprocessor
25   /// over the source files.
26   CanonicalPreprocessing,
27 
28   /// This mode is used to compute the dependencies by running the preprocessor
29   /// with special kind of lexing after scanning header and source files to get
30   /// the minimum necessary preprocessor directives for evaluating includes.
31   DependencyDirectivesScan,
32 };
33 
34 /// The format that is output by the dependency scanner.
35 enum class ScanningOutputFormat {
36   /// This is the Makefile compatible dep format. This will include all of the
37   /// deps necessary for an implicit modules build, but won't include any
38   /// intermodule dependency information.
39   Make,
40 
41   /// This outputs the full clang module dependency graph suitable for use for
42   /// explicitly building modules.
43   Full,
44 
45   /// This outputs the dependency graph for standard c++ modules in P1689R5
46   /// format.
47   P1689,
48 };
49 
50 #define DSS_LAST_BITMASK_ENUM(Id)                                              \
51   LLVM_MARK_AS_BITMASK_ENUM(Id), All = llvm::NextPowerOf2(Id) - 1
52 
53 enum class ScanningOptimizations {
54   None = 0,
55 
56   /// Remove unused header search paths including header maps.
57   HeaderSearch = 1,
58 
59   /// Remove warnings from system modules.
60   SystemWarnings = (1 << 1),
61 
62   /// Remove unused -ivfsoverlay arguments.
63   VFS = (1 << 2),
64 
65   /// Canonicalize -D and -U options.
66   Macros = (1 << 3),
67 
68   /// Ignore the compiler's working directory if it is safe.
69   IgnoreCWD = (1 << 4),
70 
71   DSS_LAST_BITMASK_ENUM(IgnoreCWD),
72 
73   // The build system needs to be aware that the current working
74   // directory is ignored. Without a good way of notifying the build
75   // system, it is less risky to default to off.
76   Default = All & (~IgnoreCWD)
77 };
78 
79 #undef DSS_LAST_BITMASK_ENUM
80 
81 /// The dependency scanning service contains shared configuration and state that
82 /// is used by the individual dependency scanning workers.
83 class DependencyScanningService {
84 public:
85   DependencyScanningService(
86       ScanningMode Mode, ScanningOutputFormat Format,
87       ScanningOptimizations OptimizeArgs = ScanningOptimizations::Default,
88       bool EagerLoadModules = false, bool TraceVFS = false,
89       std::time_t BuildSessionTimestamp =
90           llvm::sys::toTimeT(std::chrono::system_clock::now()));
91 
getMode()92   ScanningMode getMode() const { return Mode; }
93 
getFormat()94   ScanningOutputFormat getFormat() const { return Format; }
95 
getOptimizeArgs()96   ScanningOptimizations getOptimizeArgs() const { return OptimizeArgs; }
97 
shouldEagerLoadModules()98   bool shouldEagerLoadModules() const { return EagerLoadModules; }
99 
shouldTraceVFS()100   bool shouldTraceVFS() const { return TraceVFS; }
101 
getSharedCache()102   DependencyScanningFilesystemSharedCache &getSharedCache() {
103     return SharedCache;
104   }
105 
getModuleCacheEntries()106   ModuleCacheEntries &getModuleCacheEntries() { return ModCacheEntries; }
107 
getBuildSessionTimestamp()108   std::time_t getBuildSessionTimestamp() const { return BuildSessionTimestamp; }
109 
110 private:
111   const ScanningMode Mode;
112   const ScanningOutputFormat Format;
113   /// Whether to optimize the modules' command-line arguments.
114   const ScanningOptimizations OptimizeArgs;
115   /// Whether to set up command-lines to load PCM files eagerly.
116   const bool EagerLoadModules;
117   /// Whether to trace VFS accesses.
118   const bool TraceVFS;
119   /// The global file system cache.
120   DependencyScanningFilesystemSharedCache SharedCache;
121   /// The global module cache entries.
122   ModuleCacheEntries ModCacheEntries;
123   /// The build session timestamp.
124   std::time_t BuildSessionTimestamp;
125 };
126 
127 } // end namespace dependencies
128 } // end namespace tooling
129 } // end namespace clang
130 
131 #endif // LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGSERVICE_H
132