Coverage Report

Created: 2022-01-22 13:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/clang-scan-deps/ClangScanDeps.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ClangScanDeps.cpp - Implementation of clang-scan-deps --------------===//
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
#include "clang/Frontend/CompilerInstance.h"
10
#include "clang/Tooling/CommonOptionsParser.h"
11
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
12
#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
13
#include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
14
#include "clang/Tooling/JSONCompilationDatabase.h"
15
#include "llvm/ADT/STLExtras.h"
16
#include "llvm/ADT/Twine.h"
17
#include "llvm/Support/CommandLine.h"
18
#include "llvm/Support/FileUtilities.h"
19
#include "llvm/Support/InitLLVM.h"
20
#include "llvm/Support/JSON.h"
21
#include "llvm/Support/Program.h"
22
#include "llvm/Support/Signals.h"
23
#include "llvm/Support/ThreadPool.h"
24
#include "llvm/Support/Threading.h"
25
#include <mutex>
26
#include <thread>
27
28
using namespace clang;
29
using namespace tooling::dependencies;
30
31
namespace {
32
33
class SharedStream {
34
public:
35
148
  SharedStream(raw_ostream &OS) : OS(OS) {}
36
136
  void applyLocked(llvm::function_ref<void(raw_ostream &OS)> Fn) {
37
136
    std::unique_lock<std::mutex> LockGuard(Lock);
38
136
    Fn(OS);
39
136
    OS.flush();
40
136
  }
41
42
private:
43
  std::mutex Lock;
44
  raw_ostream &OS;
45
};
46
47
class ResourceDirectoryCache {
48
public:
49
  /// findResourceDir finds the resource directory relative to the clang
50
  /// compiler being used in Args, by running it with "-print-resource-dir"
51
  /// option and cache the results for reuse. \returns resource directory path
52
  /// associated with the given invocation command or empty string if the
53
  /// compiler path is NOT an absolute path.
54
  StringRef findResourceDir(const tooling::CommandLineArguments &Args,
55
1
                            bool ClangCLMode) {
56
1
    if (Args.size() < 1)
57
0
      return "";
58
59
1
    const std::string &ClangBinaryPath = Args[0];
60
1
    if (!llvm::sys::path::is_absolute(ClangBinaryPath))
61
0
      return "";
62
63
1
    const std::string &ClangBinaryName =
64
1
        std::string(llvm::sys::path::filename(ClangBinaryPath));
65
66
1
    std::unique_lock<std::mutex> LockGuard(CacheLock);
67
1
    const auto &CachedResourceDir = Cache.find(ClangBinaryPath);
68
1
    if (CachedResourceDir != Cache.end())
69
0
      return CachedResourceDir->second;
70
71
1
    std::vector<StringRef> PrintResourceDirArgs{ClangBinaryName};
72
1
    if (ClangCLMode)
73
0
      PrintResourceDirArgs.push_back("/clang:-print-resource-dir");
74
1
    else
75
1
      PrintResourceDirArgs.push_back("-print-resource-dir");
76
77
1
    llvm::SmallString<64> OutputFile, ErrorFile;
78
1
    llvm::sys::fs::createTemporaryFile("print-resource-dir-output",
79
1
                                       "" /*no-suffix*/, OutputFile);
80
1
    llvm::sys::fs::createTemporaryFile("print-resource-dir-error",
81
1
                                       "" /*no-suffix*/, ErrorFile);
82
1
    llvm::FileRemover OutputRemover(OutputFile.c_str());
83
1
    llvm::FileRemover ErrorRemover(ErrorFile.c_str());
84
1
    llvm::Optional<StringRef> Redirects[] = {
85
1
        {""}, // Stdin
86
1
        OutputFile.str(),
87
1
        ErrorFile.str(),
88
1
    };
89
1
    if (const int RC = llvm::sys::ExecuteAndWait(
90
1
            ClangBinaryPath, PrintResourceDirArgs, {}, Redirects)) {
91
0
      auto ErrorBuf = llvm::MemoryBuffer::getFile(ErrorFile.c_str());
92
0
      llvm::errs() << ErrorBuf.get()->getBuffer();
93
0
      return "";
94
0
    }
95
96
1
    auto OutputBuf = llvm::MemoryBuffer::getFile(OutputFile.c_str());
97
1
    if (!OutputBuf)
98
0
      return "";
99
1
    StringRef Output = OutputBuf.get()->getBuffer().rtrim('\n');
100
101
1
    Cache[ClangBinaryPath] = Output.str();
102
1
    return Cache[ClangBinaryPath];
103
1
  }
104
105
private:
106
  std::map<std::string, std::string> Cache;
107
  std::mutex CacheLock;
108
};
109
110
llvm::cl::opt<bool> Help("h", llvm::cl::desc("Alias for -help"),
111
                         llvm::cl::Hidden);
112
113
llvm::cl::OptionCategory DependencyScannerCategory("Tool options");
114
115
static llvm::cl::opt<ScanningMode> ScanMode(
116
    "mode",
117
    llvm::cl::desc("The preprocessing mode used to compute the dependencies"),
118
    llvm::cl::values(
119
        clEnumValN(ScanningMode::MinimizedSourcePreprocessing,
120
                   "preprocess-minimized-sources",
121
                   "The set of dependencies is computed by preprocessing the "
122
                   "source files that were minimized to only include the "
123
                   "contents that might affect the dependencies"),
124
        clEnumValN(ScanningMode::CanonicalPreprocessing, "preprocess",
125
                   "The set of dependencies is computed by preprocessing the "
126
                   "unmodified source files")),
127
    llvm::cl::init(ScanningMode::MinimizedSourcePreprocessing),
128
    llvm::cl::cat(DependencyScannerCategory));
129
130
static llvm::cl::opt<ScanningOutputFormat> Format(
131
    "format", llvm::cl::desc("The output format for the dependencies"),
132
    llvm::cl::values(clEnumValN(ScanningOutputFormat::Make, "make",
133
                                "Makefile compatible dep file"),
134
                     clEnumValN(ScanningOutputFormat::Full, "experimental-full",
135
                                "Full dependency graph suitable"
136
                                " for explicitly building modules. This format "
137
                                "is experimental and will change.")),
138
    llvm::cl::init(ScanningOutputFormat::Make),
139
    llvm::cl::cat(DependencyScannerCategory));
140
141
// This mode is mostly useful for development of explicitly built modules.
142
// Command lines will contain arguments specifying modulemap file paths and
143
// absolute paths to PCM files in the module cache directory.
144
//
145
// Build tools that want to put the PCM files in a different location should use
146
// the C++ APIs instead, of which there are two flavors:
147
//
148
// 1. APIs that generate arguments with paths to modulemap and PCM files via
149
//    callbacks provided by the client:
150
//     * ModuleDeps::getCanonicalCommandLine(LookupPCMPath, LookupModuleDeps)
151
//     * FullDependencies::getAdditionalArgs(LookupPCMPath, LookupModuleDeps)
152
//
153
// 2. APIs that don't generate arguments with paths to modulemap or PCM files
154
//    and instead expect the client to append them manually after the fact:
155
//     * ModuleDeps::getCanonicalCommandLineWithoutModulePaths()
156
//     * FullDependencies::getAdditionalArgsWithoutModulePaths()
157
//
158
static llvm::cl::opt<bool> GenerateModulesPathArgs(
159
    "generate-modules-path-args",
160
    llvm::cl::desc(
161
        "With '-format experimental-full', include arguments specifying "
162
        "modules-related paths in the generated command lines: "
163
        "'-fmodule-file=', '-o', '-fmodule-map-file='."),
164
    llvm::cl::init(false), llvm::cl::cat(DependencyScannerCategory));
165
166
static llvm::cl::opt<std::string> ModuleFilesDir(
167
    "module-files-dir",
168
    llvm::cl::desc("With '-generate-modules-path-args', paths to module files "
169
                   "in the generated command lines will begin with the "
170
                   "specified directory instead the module cache directory."),
171
    llvm::cl::cat(DependencyScannerCategory));
172
173
static llvm::cl::opt<bool> OptimizeArgs(
174
    "optimize-args",
175
    llvm::cl::desc("Whether to optimize command-line arguments of modules."),
176
    llvm::cl::init(false), llvm::cl::cat(DependencyScannerCategory));
177
178
llvm::cl::opt<unsigned>
179
    NumThreads("j", llvm::cl::Optional,
180
               llvm::cl::desc("Number of worker threads to use (default: use "
181
                              "all concurrent threads)"),
182
               llvm::cl::init(0), llvm::cl::cat(DependencyScannerCategory));
183
184
llvm::cl::opt<std::string>
185
    CompilationDB("compilation-database",
186
                  llvm::cl::desc("Compilation database"), llvm::cl::Required,
187
                  llvm::cl::cat(DependencyScannerCategory));
188
189
llvm::cl::opt<bool> ReuseFileManager(
190
    "reuse-filemanager",
191
    llvm::cl::desc("Reuse the file manager and its cache between invocations."),
192
    llvm::cl::init(true), llvm::cl::cat(DependencyScannerCategory));
193
194
llvm::cl::opt<bool> SkipExcludedPPRanges(
195
    "skip-excluded-pp-ranges",
196
    llvm::cl::desc(
197
        "Use the preprocessor optimization that skips excluded conditionals by "
198
        "bumping the buffer pointer in the lexer instead of lexing the tokens  "
199
        "until reaching the end directive."),
200
    llvm::cl::init(true), llvm::cl::cat(DependencyScannerCategory));
201
202
llvm::cl::opt<std::string> ModuleName(
203
    "module-name", llvm::cl::Optional,
204
    llvm::cl::desc("the module of which the dependencies are to be computed"),
205
    llvm::cl::cat(DependencyScannerCategory));
206
207
enum ResourceDirRecipeKind {
208
  RDRK_ModifyCompilerPath,
209
  RDRK_InvokeCompiler,
210
};
211
212
static llvm::cl::opt<ResourceDirRecipeKind> ResourceDirRecipe(
213
    "resource-dir-recipe",
214
    llvm::cl::desc("How to produce missing '-resource-dir' argument"),
215
    llvm::cl::values(
216
        clEnumValN(RDRK_ModifyCompilerPath, "modify-compiler-path",
217
                   "Construct the resource directory from the compiler path in "
218
                   "the compilation database. This assumes it's part of the "
219
                   "same toolchain as this clang-scan-deps. (default)"),
220
        clEnumValN(RDRK_InvokeCompiler, "invoke-compiler",
221
                   "Invoke the compiler with '-print-resource-dir' and use the "
222
                   "reported path as the resource directory. (deprecated)")),
223
    llvm::cl::init(RDRK_ModifyCompilerPath),
224
    llvm::cl::cat(DependencyScannerCategory));
225
226
llvm::cl::opt<bool> Verbose("v", llvm::cl::Optional,
227
                            llvm::cl::desc("Use verbose output."),
228
                            llvm::cl::init(false),
229
                            llvm::cl::cat(DependencyScannerCategory));
230
231
} // end anonymous namespace
232
233
/// Takes the result of a dependency scan and prints error / dependency files
234
/// based on the result.
235
///
236
/// \returns True on error.
237
static bool
238
handleMakeDependencyToolResult(const std::string &Input,
239
                               llvm::Expected<std::string> &MaybeFile,
240
136
                               SharedStream &OS, SharedStream &Errs) {
241
136
  if (!MaybeFile) {
242
6
    llvm::handleAllErrors(
243
6
        MaybeFile.takeError(), [&Input, &Errs](llvm::StringError &Err) {
244
6
          Errs.applyLocked([&](raw_ostream &OS) {
245
6
            OS << "Error while scanning dependencies for " << Input << ":\n";
246
6
            OS << Err.getMessage();
247
6
          });
248
6
        });
249
6
    return true;
250
6
  }
251
130
  OS.applyLocked([&](raw_ostream &OS) { OS << *MaybeFile; });
252
130
  return false;
253
136
}
254
255
44
static llvm::json::Array toJSONSorted(const llvm::StringSet<> &Set) {
256
44
  std::vector<llvm::StringRef> Strings;
257
44
  for (auto &&I : Set)
258
104
    Strings.push_back(I.getKey());
259
44
  llvm::sort(Strings);
260
44
  return llvm::json::Array(Strings);
261
44
}
262
263
87
static llvm::json::Array toJSONSorted(std::vector<ModuleID> V) {
264
87
  llvm::sort(V, [](const ModuleID &A, const ModuleID &B) {
265
3
    return std::tie(A.ModuleName, A.ContextHash) <
266
3
           std::tie(B.ModuleName, B.ContextHash);
267
3
  });
268
269
87
  llvm::json::Array Ret;
270
87
  for (const ModuleID &MID : V)
271
52
    Ret.push_back(llvm::json::Object(
272
52
        {{"module-name", MID.ModuleName}, {"context-hash", MID.ContextHash}}));
273
87
  return Ret;
274
87
}
275
276
// Thread safe.
277
class FullDeps {
278
public:
279
  void mergeDeps(StringRef Input, FullDependenciesResult FDR,
280
43
                 size_t InputIndex) {
281
43
    const FullDependencies &FD = FDR.FullDeps;
282
283
43
    InputDeps ID;
284
43
    ID.FileName = std::string(Input);
285
43
    ID.ContextHash = std::move(FD.ID.ContextHash);
286
43
    ID.FileDeps = std::move(FD.FileDeps);
287
43
    ID.ModuleDeps = std::move(FD.ClangModuleDeps);
288
289
43
    std::unique_lock<std::mutex> ul(Lock);
290
52
    for (const ModuleDeps &MD : FDR.DiscoveredModules) {
291
52
      auto I = Modules.find({MD.ID, 0});
292
52
      if (I != Modules.end()) {
293
8
        I->first.InputIndex = std::min(I->first.InputIndex, InputIndex);
294
8
        continue;
295
8
      }
296
44
      Modules.insert(I, {{MD.ID, InputIndex}, std::move(MD)});
297
44
    }
298
299
43
    ID.AdditionalCommandLine =
300
43
        GenerateModulesPathArgs
301
43
            ? FD.getAdditionalArgs(
302
25
                  [&](ModuleID MID) { return lookupPCMPath(MID); },
303
25
                  [&](ModuleID MID) -> const ModuleDeps & {
304
25
                    return lookupModuleDeps(MID);
305
25
                  })
306
43
            : 
FD.getAdditionalArgsWithoutModulePaths()22
;
307
308
43
    Inputs.push_back(std::move(ID));
309
43
  }
310
311
29
  void printFullOutput(raw_ostream &OS) {
312
    // Sort the modules by name to get a deterministic order.
313
29
    std::vector<IndexedModuleID> ModuleIDs;
314
29
    for (auto &&M : Modules)
315
44
      ModuleIDs.push_back(M.first);
316
29
    llvm::sort(ModuleIDs,
317
29
               [](const IndexedModuleID &A, const IndexedModuleID &B) {
318
22
                 return std::tie(A.ID.ModuleName, A.InputIndex) <
319
22
                        std::tie(B.ID.ModuleName, B.InputIndex);
320
22
               });
321
322
29
    llvm::sort(Inputs, [](const InputDeps &A, const InputDeps &B) {
323
18
      return A.FileName < B.FileName;
324
18
    });
325
326
29
    using namespace llvm::json;
327
328
29
    Array OutModules;
329
44
    for (auto &&ModID : ModuleIDs) {
330
44
      auto &MD = Modules[ModID];
331
44
      Object O{
332
44
          {"name", MD.ID.ModuleName},
333
44
          {"context-hash", MD.ID.ContextHash},
334
44
          {"file-deps", toJSONSorted(MD.FileDeps)},
335
44
          {"clang-module-deps", toJSONSorted(MD.ClangModuleDeps)},
336
44
          {"clang-modulemap-file", MD.ClangModuleMapFile},
337
44
          {"command-line",
338
44
           GenerateModulesPathArgs
339
44
               ? MD.getCanonicalCommandLine(
340
24
                     [&](ModuleID MID) { return lookupPCMPath(MID); },
341
21
                     [&](ModuleID MID) -> const ModuleDeps & {
342
3
                       return lookupModuleDeps(MID);
343
3
                     })
344
44
               : 
MD.getCanonicalCommandLineWithoutModulePaths()23
},
345
44
      };
346
44
      OutModules.push_back(std::move(O));
347
44
    }
348
349
29
    Array TUs;
350
43
    for (auto &&I : Inputs) {
351
43
      Object O{
352
43
          {"input-file", I.FileName},
353
43
          {"clang-context-hash", I.ContextHash},
354
43
          {"file-deps", I.FileDeps},
355
43
          {"clang-module-deps", toJSONSorted(I.ModuleDeps)},
356
43
          {"command-line", I.AdditionalCommandLine},
357
43
      };
358
43
      TUs.push_back(std::move(O));
359
43
    }
360
361
29
    Object Output{
362
29
        {"modules", std::move(OutModules)},
363
29
        {"translation-units", std::move(TUs)},
364
29
    };
365
366
29
    OS << llvm::formatv("{0:2}\n", Value(std::move(Output)));
367
29
  }
368
369
private:
370
49
  StringRef lookupPCMPath(ModuleID MID) {
371
49
    auto PCMPath = PCMPaths.insert({MID, ""});
372
49
    if (PCMPath.second)
373
21
      PCMPath.first->second = constructPCMPath(lookupModuleDeps(MID));
374
49
    return PCMPath.first->second;
375
49
  }
376
377
  /// Construct a path for the explicitly built PCM.
378
21
  std::string constructPCMPath(const ModuleDeps &MD) const {
379
21
    StringRef Filename = llvm::sys::path::filename(MD.ImplicitModulePCMPath);
380
381
21
    SmallString<256> ExplicitPCMPath(
382
21
        !ModuleFilesDir.empty()
383
21
            ? 
ModuleFilesDir13
384
21
            : 
MD.BuildInvocation.getHeaderSearchOpts().ModuleCachePath8
);
385
21
    llvm::sys::path::append(ExplicitPCMPath, MD.ID.ContextHash, Filename);
386
21
    return std::string(ExplicitPCMPath);
387
21
  }
388
389
49
  const ModuleDeps &lookupModuleDeps(ModuleID MID) {
390
49
    auto I = Modules.find(IndexedModuleID{MID, 0});
391
49
    assert(I != Modules.end());
392
0
    return I->second;
393
49
  };
394
395
  struct IndexedModuleID {
396
    ModuleID ID;
397
    mutable size_t InputIndex;
398
399
112
    bool operator==(const IndexedModuleID &Other) const {
400
112
      return ID.ModuleName == Other.ID.ModuleName &&
401
112
             
ID.ContextHash == Other.ID.ContextHash106
;
402
112
    }
403
  };
404
405
  struct IndexedModuleIDHasher {
406
189
    std::size_t operator()(const IndexedModuleID &IMID) const {
407
189
      using llvm::hash_combine;
408
409
189
      return hash_combine(IMID.ID.ModuleName, IMID.ID.ContextHash);
410
189
    }
411
  };
412
413
  struct InputDeps {
414
    std::string FileName;
415
    std::string ContextHash;
416
    std::vector<std::string> FileDeps;
417
    std::vector<ModuleID> ModuleDeps;
418
    std::vector<std::string> AdditionalCommandLine;
419
  };
420
421
  std::mutex Lock;
422
  std::unordered_map<IndexedModuleID, ModuleDeps, IndexedModuleIDHasher>
423
      Modules;
424
  std::unordered_map<ModuleID, std::string, ModuleIDHasher> PCMPaths;
425
  std::vector<InputDeps> Inputs;
426
};
427
428
static bool handleFullDependencyToolResult(
429
    const std::string &Input,
430
    llvm::Expected<FullDependenciesResult> &MaybeFullDeps, FullDeps &FD,
431
43
    size_t InputIndex, SharedStream &OS, SharedStream &Errs) {
432
43
  if (!MaybeFullDeps) {
433
0
    llvm::handleAllErrors(
434
0
        MaybeFullDeps.takeError(), [&Input, &Errs](llvm::StringError &Err) {
435
0
          Errs.applyLocked([&](raw_ostream &OS) {
436
0
            OS << "Error while scanning dependencies for " << Input << ":\n";
437
0
            OS << Err.getMessage();
438
0
          });
439
0
        });
440
0
    return true;
441
0
  }
442
43
  FD.mergeDeps(Input, std::move(*MaybeFullDeps), InputIndex);
443
43
  return false;
444
43
}
445
446
74
int main(int argc, const char **argv) {
447
74
  llvm::InitLLVM X(argc, argv);
448
74
  llvm::cl::HideUnrelatedOptions(DependencyScannerCategory);
449
74
  if (!llvm::cl::ParseCommandLineOptions(argc, argv))
450
0
    return 1;
451
452
74
  std::string ErrorMessage;
453
74
  std::unique_ptr<tooling::JSONCompilationDatabase> Compilations =
454
74
      tooling::JSONCompilationDatabase::loadFromFile(
455
74
          CompilationDB, ErrorMessage,
456
74
          tooling::JSONCommandLineSyntax::AutoDetect);
457
74
  if (!Compilations) {
458
0
    llvm::errs() << "error: " << ErrorMessage << "\n";
459
0
    return 1;
460
0
  }
461
462
74
  llvm::cl::PrintOptionValues();
463
464
  // The command options are rewritten to run Clang in preprocessor only mode.
465
74
  auto AdjustingCompilations =
466
74
      std::make_unique<tooling::ArgumentsAdjustingCompilations>(
467
74
          std::move(Compilations));
468
74
  ResourceDirectoryCache ResourceDirCache;
469
470
74
  AdjustingCompilations->appendArgumentsAdjuster(
471
74
      [&ResourceDirCache](const tooling::CommandLineArguments &Args,
472
179
                          StringRef FileName) {
473
179
        std::string LastO;
474
179
        bool HasResourceDir = false;
475
179
        bool ClangCLMode = false;
476
179
        auto FlagsEnd = llvm::find(Args, "--");
477
179
        if (FlagsEnd != Args.begin()) {
478
179
          ClangCLMode =
479
179
              llvm::sys::path::stem(Args[0]).contains_insensitive("clang-cl") ||
480
179
              
llvm::is_contained(Args, "--driver-mode=cl")120
;
481
482
          // Reverse scan, starting at the end or at the element before "--".
483
179
          auto R = std::make_reverse_iterator(FlagsEnd);
484
1.61k
          for (auto I = R, E = Args.rend(); I != E; 
++I1.43k
) {
485
1.43k
            StringRef Arg = *I;
486
1.43k
            if (ClangCLMode) {
487
              // Ignore arguments that are preceded by "-Xclang".
488
470
              if ((I + 1) != E && 
I[1] == "-Xclang"403
)
489
4
                continue;
490
466
              if (LastO.empty()) {
491
                // With clang-cl, the output obj file can be specified with
492
                // "/opath", "/o path", "/Fopath", and the dash counterparts.
493
                // Also, clang-cl adds ".obj" extension if none is found.
494
324
                if ((Arg == "-o" || 
Arg == "/o"310
) &&
I != R15
)
495
15
                  LastO = I[-1]; // Next argument (reverse iterator)
496
309
                else if (Arg.startswith("/Fo") || 
Arg.startswith("-Fo")299
)
497
10
                  LastO = Arg.drop_front(3).str();
498
299
                else if (Arg.startswith("/o") || 
Arg.startswith("-o")297
)
499
3
                  LastO = Arg.drop_front(2).str();
500
501
324
                if (!LastO.empty() && 
!llvm::sys::path::has_extension(LastO)28
)
502
0
                  LastO.append(".obj");
503
324
              }
504
466
            }
505
1.42k
            if (Arg == "-resource-dir")
506
0
              HasResourceDir = true;
507
1.42k
          }
508
179
        }
509
179
        tooling::CommandLineArguments AdjustedArgs(Args.begin(), FlagsEnd);
510
        // The clang-cl driver passes "-o -" to the frontend. Inject the real
511
        // file here to ensure "-MT" can be deduced if need be.
512
179
        if (ClangCLMode && 
!LastO.empty()67
) {
513
28
          AdjustedArgs.push_back("/clang:-o");
514
28
          AdjustedArgs.push_back("/clang:" + LastO);
515
28
        }
516
517
179
        if (!HasResourceDir && ResourceDirRecipe == RDRK_InvokeCompiler) {
518
1
          StringRef ResourceDir =
519
1
              ResourceDirCache.findResourceDir(Args, ClangCLMode);
520
1
          if (!ResourceDir.empty()) {
521
1
            AdjustedArgs.push_back("-resource-dir");
522
1
            AdjustedArgs.push_back(std::string(ResourceDir));
523
1
          }
524
1
        }
525
179
        AdjustedArgs.insert(AdjustedArgs.end(), FlagsEnd, Args.end());
526
179
        return AdjustedArgs;
527
179
      });
528
529
74
  SharedStream Errs(llvm::errs());
530
  // Print out the dependency results to STDOUT by default.
531
74
  SharedStream DependencyOS(llvm::outs());
532
533
74
  DependencyScanningService Service(ScanMode, Format, ReuseFileManager,
534
74
                                    SkipExcludedPPRanges, OptimizeArgs);
535
74
  llvm::ThreadPool Pool(llvm::hardware_concurrency(NumThreads));
536
74
  std::vector<std::unique_ptr<DependencyScanningTool>> WorkerTools;
537
439
  for (unsigned I = 0; I < Pool.getThreadCount(); 
++I365
)
538
365
    WorkerTools.push_back(std::make_unique<DependencyScanningTool>(Service));
539
540
74
  std::vector<tooling::CompileCommand> Inputs =
541
74
      AdjustingCompilations->getAllCompileCommands();
542
543
74
  std::atomic<bool> HadErrors(false);
544
74
  FullDeps FD;
545
74
  std::mutex Lock;
546
74
  size_t Index = 0;
547
548
74
  if (Verbose) {
549
0
    llvm::outs() << "Running clang-scan-deps on " << Inputs.size()
550
0
                 << " files using " << Pool.getThreadCount() << " workers\n";
551
0
  }
552
439
  for (unsigned I = 0; I < Pool.getThreadCount(); 
++I365
) {
553
365
    Pool.async([I, &Lock, &Index, &Inputs, &HadErrors, &FD, &WorkerTools,
554
365
                &DependencyOS, &Errs]() {
555
365
      llvm::StringSet<> AlreadySeenModules;
556
543
      while (true) {
557
543
        const tooling::CompileCommand *Input;
558
543
        std::string Filename;
559
543
        std::string CWD;
560
543
        size_t LocalIndex;
561
        // Take the next input.
562
543
        {
563
543
          std::unique_lock<std::mutex> LockGuard(Lock);
564
543
          if (Index >= Inputs.size())
565
365
            return;
566
178
          LocalIndex = Index;
567
178
          Input = &Inputs[Index++];
568
178
          Filename = std::move(Input->Filename);
569
178
          CWD = std::move(Input->Directory);
570
178
        }
571
0
        Optional<StringRef> MaybeModuleName;
572
178
        if (!ModuleName.empty())
573
4
          MaybeModuleName = ModuleName;
574
        // Run the tool on it.
575
178
        if (Format == ScanningOutputFormat::Make) {
576
136
          auto MaybeFile = WorkerTools[I]->getDependencyFile(
577
136
              Input->CommandLine, CWD, MaybeModuleName);
578
136
          if (handleMakeDependencyToolResult(Filename, MaybeFile, DependencyOS,
579
136
                                             Errs))
580
6
            HadErrors = true;
581
136
        } else {
582
42
          auto MaybeFullDeps = WorkerTools[I]->getFullDependencies(
583
42
              Input->CommandLine, CWD, AlreadySeenModules, MaybeModuleName);
584
42
          if (handleFullDependencyToolResult(Filename, MaybeFullDeps, FD,
585
42
                                             LocalIndex, DependencyOS, Errs))
586
0
            HadErrors = true;
587
42
        }
588
178
      }
589
365
    });
590
365
  }
591
74
  Pool.wait();
592
593
74
  if (Format == ScanningOutputFormat::Full)
594
29
    FD.printFullOutput(llvm::outs());
595
596
74
  return HadErrors;
597
74
}