Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/Tooling/Tooling.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Tooling.cpp - Running clang standalone tools -----------------------===//
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
//  This file implements functions to run clang tools standalone instead
10
//  of running them as a plugin.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/Tooling/Tooling.h"
15
#include "clang/Basic/Diagnostic.h"
16
#include "clang/Basic/DiagnosticIDs.h"
17
#include "clang/Basic/DiagnosticOptions.h"
18
#include "clang/Basic/FileManager.h"
19
#include "clang/Basic/FileSystemOptions.h"
20
#include "clang/Basic/LLVM.h"
21
#include "clang/Driver/Compilation.h"
22
#include "clang/Driver/Driver.h"
23
#include "clang/Driver/Job.h"
24
#include "clang/Driver/Options.h"
25
#include "clang/Driver/Tool.h"
26
#include "clang/Driver/ToolChain.h"
27
#include "clang/Frontend/ASTUnit.h"
28
#include "clang/Frontend/CompilerInstance.h"
29
#include "clang/Frontend/CompilerInvocation.h"
30
#include "clang/Frontend/FrontendDiagnostic.h"
31
#include "clang/Frontend/FrontendOptions.h"
32
#include "clang/Frontend/TextDiagnosticPrinter.h"
33
#include "clang/Lex/HeaderSearchOptions.h"
34
#include "clang/Lex/PreprocessorOptions.h"
35
#include "clang/Tooling/ArgumentsAdjusters.h"
36
#include "clang/Tooling/CompilationDatabase.h"
37
#include "llvm/ADT/ArrayRef.h"
38
#include "llvm/ADT/IntrusiveRefCntPtr.h"
39
#include "llvm/ADT/SmallString.h"
40
#include "llvm/ADT/StringRef.h"
41
#include "llvm/ADT/Twine.h"
42
#include "llvm/Option/ArgList.h"
43
#include "llvm/Option/OptTable.h"
44
#include "llvm/Option/Option.h"
45
#include "llvm/Support/Casting.h"
46
#include "llvm/Support/CommandLine.h"
47
#include "llvm/Support/Debug.h"
48
#include "llvm/Support/ErrorHandling.h"
49
#include "llvm/Support/FileSystem.h"
50
#include "llvm/Support/MemoryBuffer.h"
51
#include "llvm/Support/Path.h"
52
#include "llvm/Support/VirtualFileSystem.h"
53
#include "llvm/Support/raw_ostream.h"
54
#include "llvm/TargetParser/Host.h"
55
#include <cassert>
56
#include <cstring>
57
#include <memory>
58
#include <string>
59
#include <system_error>
60
#include <utility>
61
#include <vector>
62
63
#define DEBUG_TYPE "clang-tooling"
64
65
using namespace clang;
66
using namespace tooling;
67
68
46
ToolAction::~ToolAction() = default;
69
70
FrontendActionFactory::~FrontendActionFactory() = default;
71
72
// FIXME: This file contains structural duplication with other parts of the
73
// code that sets up a compiler to run tools on it, and we should refactor
74
// it to be based on the same framework.
75
76
/// Builds a clang driver initialized for running clang tools.
77
static driver::Driver *
78
newDriver(DiagnosticsEngine *Diagnostics, const char *BinaryName,
79
0
          IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
80
0
  driver::Driver *CompilerDriver =
81
0
      new driver::Driver(BinaryName, llvm::sys::getDefaultTargetTriple(),
82
0
                         *Diagnostics, "clang LLVM compiler", std::move(VFS));
83
0
  CompilerDriver->setTitle("clang_based_tool");
84
0
  return CompilerDriver;
85
0
}
86
87
/// Decide whether extra compiler frontend commands can be ignored.
88
0
static bool ignoreExtraCC1Commands(const driver::Compilation *Compilation) {
89
0
  const driver::JobList &Jobs = Compilation->getJobs();
90
0
  const driver::ActionList &Actions = Compilation->getActions();
91
92
0
  bool OffloadCompilation = false;
93
94
  // Jobs and Actions look very different depending on whether the Clang tool
95
  // injected -fsyntax-only or not. Try to handle both cases here.
96
97
0
  for (const auto &Job : Jobs)
98
0
    if (StringRef(Job.getExecutable()) == "clang-offload-bundler")
99
0
      OffloadCompilation = true;
100
101
0
  if (Jobs.size() > 1) {
102
0
    for (auto *A : Actions){
103
      // On MacOSX real actions may end up being wrapped in BindArchAction
104
0
      if (isa<driver::BindArchAction>(A))
105
0
        A = *A->input_begin();
106
0
      if (isa<driver::OffloadAction>(A)) {
107
        // Offload compilation has 2 top-level actions, one (at the front) is
108
        // the original host compilation and the other is offload action
109
        // composed of at least one device compilation. For such case, general
110
        // tooling will consider host-compilation only. For tooling on device
111
        // compilation, device compilation only option, such as
112
        // `--cuda-device-only`, needs specifying.
113
0
        assert(Actions.size() > 1);
114
0
        assert(
115
0
            isa<driver::CompileJobAction>(Actions.front()) ||
116
            // On MacOSX real actions may end up being wrapped in
117
            // BindArchAction.
118
0
            (isa<driver::BindArchAction>(Actions.front()) &&
119
0
             isa<driver::CompileJobAction>(*Actions.front()->input_begin())));
120
0
        OffloadCompilation = true;
121
0
        break;
122
0
      }
123
0
    }
124
0
  }
125
126
0
  return OffloadCompilation;
127
0
}
128
129
namespace clang {
130
namespace tooling {
131
132
const llvm::opt::ArgStringList *
133
getCC1Arguments(DiagnosticsEngine *Diagnostics,
134
0
                driver::Compilation *Compilation) {
135
0
  const driver::JobList &Jobs = Compilation->getJobs();
136
137
0
  auto IsCC1Command = [](const driver::Command &Cmd) {
138
0
    return StringRef(Cmd.getCreator().getName()) == "clang";
139
0
  };
140
141
0
  auto IsSrcFile = [](const driver::InputInfo &II) {
142
0
    return isSrcFile(II.getType());
143
0
  };
144
145
0
  llvm::SmallVector<const driver::Command *, 1> CC1Jobs;
146
0
  for (const driver::Command &Job : Jobs)
147
0
    if (IsCC1Command(Job) && llvm::all_of(Job.getInputInfos(), IsSrcFile))
148
0
      CC1Jobs.push_back(&Job);
149
150
  // If there are no jobs for source files, try checking again for a single job
151
  // with any file type. This accepts a preprocessed file as input.
152
0
  if (CC1Jobs.empty())
153
0
    for (const driver::Command &Job : Jobs)
154
0
      if (IsCC1Command(Job))
155
0
        CC1Jobs.push_back(&Job);
156
157
0
  if (CC1Jobs.empty() ||
158
0
      (CC1Jobs.size() > 1 && !ignoreExtraCC1Commands(Compilation))) {
159
0
    SmallString<256> error_msg;
160
0
    llvm::raw_svector_ostream error_stream(error_msg);
161
0
    Jobs.Print(error_stream, "; ", true);
162
0
    Diagnostics->Report(diag::err_fe_expected_compiler_job)
163
0
        << error_stream.str();
164
0
    return nullptr;
165
0
  }
166
167
0
  return &CC1Jobs[0]->getArguments();
168
0
}
169
170
/// Returns a clang build invocation initialized from the CC1 flags.
171
CompilerInvocation *newInvocation(DiagnosticsEngine *Diagnostics,
172
                                  ArrayRef<const char *> CC1Args,
173
46
                                  const char *const BinaryName) {
174
46
  assert(!CC1Args.empty() && "Must at least contain the program name!");
175
0
  CompilerInvocation *Invocation = new CompilerInvocation;
176
46
  CompilerInvocation::CreateFromArgs(*Invocation, CC1Args, *Diagnostics,
177
46
                                     BinaryName);
178
46
  Invocation->getFrontendOpts().DisableFree = false;
179
46
  Invocation->getCodeGenOpts().DisableFree = false;
180
46
  return Invocation;
181
46
}
182
183
bool runToolOnCode(std::unique_ptr<FrontendAction> ToolAction,
184
                   const Twine &Code, const Twine &FileName,
185
0
                   std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
186
0
  return runToolOnCodeWithArgs(std::move(ToolAction), Code,
187
0
                               std::vector<std::string>(), FileName,
188
0
                               "clang-tool", std::move(PCHContainerOps));
189
0
}
190
191
} // namespace tooling
192
} // namespace clang
193
194
static std::vector<std::string>
195
getSyntaxOnlyToolArgs(const Twine &ToolName,
196
                      const std::vector<std::string> &ExtraArgs,
197
0
                      StringRef FileName) {
198
0
  std::vector<std::string> Args;
199
0
  Args.push_back(ToolName.str());
200
0
  Args.push_back("-fsyntax-only");
201
0
  Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
202
0
  Args.push_back(FileName.str());
203
0
  return Args;
204
0
}
205
206
namespace clang {
207
namespace tooling {
208
209
bool runToolOnCodeWithArgs(
210
    std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
211
    llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
212
    const std::vector<std::string> &Args, const Twine &FileName,
213
    const Twine &ToolName,
214
0
    std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
215
0
  SmallString<16> FileNameStorage;
216
0
  StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
217
218
0
  llvm::IntrusiveRefCntPtr<FileManager> Files(
219
0
      new FileManager(FileSystemOptions(), VFS));
220
0
  ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster();
221
0
  ToolInvocation Invocation(
222
0
      getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef),
223
0
      std::move(ToolAction), Files.get(), std::move(PCHContainerOps));
224
0
  return Invocation.run();
225
0
}
226
227
bool runToolOnCodeWithArgs(
228
    std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
229
    const std::vector<std::string> &Args, const Twine &FileName,
230
    const Twine &ToolName,
231
    std::shared_ptr<PCHContainerOperations> PCHContainerOps,
232
0
    const FileContentMappings &VirtualMappedFiles) {
233
0
  llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
234
0
      new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
235
0
  llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
236
0
      new llvm::vfs::InMemoryFileSystem);
237
0
  OverlayFileSystem->pushOverlay(InMemoryFileSystem);
238
239
0
  SmallString<1024> CodeStorage;
240
0
  InMemoryFileSystem->addFile(FileName, 0,
241
0
                              llvm::MemoryBuffer::getMemBuffer(
242
0
                                  Code.toNullTerminatedStringRef(CodeStorage)));
243
244
0
  for (auto &FilenameWithContent : VirtualMappedFiles) {
245
0
    InMemoryFileSystem->addFile(
246
0
        FilenameWithContent.first, 0,
247
0
        llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
248
0
  }
249
250
0
  return runToolOnCodeWithArgs(std::move(ToolAction), Code, OverlayFileSystem,
251
0
                               Args, FileName, ToolName);
252
0
}
253
254
llvm::Expected<std::string> getAbsolutePath(llvm::vfs::FileSystem &FS,
255
0
                                            StringRef File) {
256
0
  StringRef RelativePath(File);
257
  // FIXME: Should '.\\' be accepted on Win32?
258
0
  RelativePath.consume_front("./");
259
260
0
  SmallString<1024> AbsolutePath = RelativePath;
261
0
  if (auto EC = FS.makeAbsolute(AbsolutePath))
262
0
    return llvm::errorCodeToError(EC);
263
0
  llvm::sys::path::native(AbsolutePath);
264
0
  return std::string(AbsolutePath.str());
265
0
}
266
267
0
std::string getAbsolutePath(StringRef File) {
268
0
  return llvm::cantFail(getAbsolutePath(*llvm::vfs::getRealFileSystem(), File));
269
0
}
270
271
void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine,
272
0
                                    StringRef InvokedAs) {
273
0
  if (CommandLine.empty() || InvokedAs.empty())
274
0
    return;
275
0
  const auto &Table = driver::getDriverOptTable();
276
  // --target=X
277
0
  StringRef TargetOPT =
278
0
      Table.getOption(driver::options::OPT_target).getPrefixedName();
279
  // -target X
280
0
  StringRef TargetOPTLegacy =
281
0
      Table.getOption(driver::options::OPT_target_legacy_spelling)
282
0
          .getPrefixedName();
283
  // --driver-mode=X
284
0
  StringRef DriverModeOPT =
285
0
      Table.getOption(driver::options::OPT_driver_mode).getPrefixedName();
286
0
  auto TargetMode =
287
0
      driver::ToolChain::getTargetAndModeFromProgramName(InvokedAs);
288
  // No need to search for target args if we don't have a target/mode to insert.
289
0
  bool ShouldAddTarget = TargetMode.TargetIsValid;
290
0
  bool ShouldAddMode = TargetMode.DriverMode != nullptr;
291
  // Skip CommandLine[0].
292
0
  for (auto Token = ++CommandLine.begin(); Token != CommandLine.end();
293
0
       ++Token) {
294
0
    StringRef TokenRef(*Token);
295
0
    ShouldAddTarget = ShouldAddTarget && !TokenRef.starts_with(TargetOPT) &&
296
0
                      !TokenRef.equals(TargetOPTLegacy);
297
0
    ShouldAddMode = ShouldAddMode && !TokenRef.starts_with(DriverModeOPT);
298
0
  }
299
0
  if (ShouldAddMode) {
300
0
    CommandLine.insert(++CommandLine.begin(), TargetMode.DriverMode);
301
0
  }
302
0
  if (ShouldAddTarget) {
303
0
    CommandLine.insert(++CommandLine.begin(),
304
0
                       (TargetOPT + TargetMode.TargetPrefix).str());
305
0
  }
306
0
}
307
308
void addExpandedResponseFiles(std::vector<std::string> &CommandLine,
309
                              llvm::StringRef WorkingDir,
310
                              llvm::cl::TokenizerCallback Tokenizer,
311
0
                              llvm::vfs::FileSystem &FS) {
312
0
  bool SeenRSPFile = false;
313
0
  llvm::SmallVector<const char *, 20> Argv;
314
0
  Argv.reserve(CommandLine.size());
315
0
  for (auto &Arg : CommandLine) {
316
0
    Argv.push_back(Arg.c_str());
317
0
    if (!Arg.empty())
318
0
      SeenRSPFile |= Arg.front() == '@';
319
0
  }
320
0
  if (!SeenRSPFile)
321
0
    return;
322
0
  llvm::BumpPtrAllocator Alloc;
323
0
  llvm::cl::ExpansionContext ECtx(Alloc, Tokenizer);
324
0
  llvm::Error Err =
325
0
      ECtx.setVFS(&FS).setCurrentDir(WorkingDir).expandResponseFiles(Argv);
326
0
  if (Err)
327
0
    llvm::errs() << Err;
328
  // Don't assign directly, Argv aliases CommandLine.
329
0
  std::vector<std::string> ExpandedArgv(Argv.begin(), Argv.end());
330
0
  CommandLine = std::move(ExpandedArgv);
331
0
}
332
333
} // namespace tooling
334
} // namespace clang
335
336
namespace {
337
338
class SingleFrontendActionFactory : public FrontendActionFactory {
339
  std::unique_ptr<FrontendAction> Action;
340
341
public:
342
  SingleFrontendActionFactory(std::unique_ptr<FrontendAction> Action)
343
0
      : Action(std::move(Action)) {}
344
345
0
  std::unique_ptr<FrontendAction> create() override {
346
0
    return std::move(Action);
347
0
  }
348
};
349
350
} // namespace
351
352
ToolInvocation::ToolInvocation(
353
    std::vector<std::string> CommandLine, ToolAction *Action,
354
    FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps)
355
    : CommandLine(std::move(CommandLine)), Action(Action), OwnsAction(false),
356
0
      Files(Files), PCHContainerOps(std::move(PCHContainerOps)) {}
357
358
ToolInvocation::ToolInvocation(
359
    std::vector<std::string> CommandLine,
360
    std::unique_ptr<FrontendAction> FAction, FileManager *Files,
361
    std::shared_ptr<PCHContainerOperations> PCHContainerOps)
362
    : CommandLine(std::move(CommandLine)),
363
      Action(new SingleFrontendActionFactory(std::move(FAction))),
364
      OwnsAction(true), Files(Files),
365
0
      PCHContainerOps(std::move(PCHContainerOps)) {}
366
367
0
ToolInvocation::~ToolInvocation() {
368
0
  if (OwnsAction)
369
0
    delete Action;
370
0
}
371
372
0
bool ToolInvocation::run() {
373
0
  llvm::opt::ArgStringList Argv;
374
0
  for (const std::string &Str : CommandLine)
375
0
    Argv.push_back(Str.c_str());
376
0
  const char *const BinaryName = Argv[0];
377
378
  // Parse diagnostic options from the driver command-line only if none were
379
  // explicitly set.
380
0
  IntrusiveRefCntPtr<DiagnosticOptions> ParsedDiagOpts;
381
0
  DiagnosticOptions *DiagOpts = this->DiagOpts;
382
0
  if (!DiagOpts) {
383
0
    ParsedDiagOpts = CreateAndPopulateDiagOpts(Argv);
384
0
    DiagOpts = &*ParsedDiagOpts;
385
0
  }
386
387
0
  TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), DiagOpts);
388
0
  IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics =
389
0
      CompilerInstance::createDiagnostics(
390
0
          &*DiagOpts, DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false);
391
  // Although `Diagnostics` are used only for command-line parsing, the custom
392
  // `DiagConsumer` might expect a `SourceManager` to be present.
393
0
  SourceManager SrcMgr(*Diagnostics, *Files);
394
0
  Diagnostics->setSourceManager(&SrcMgr);
395
396
  // We already have a cc1, just create an invocation.
397
0
  if (CommandLine.size() >= 2 && CommandLine[1] == "-cc1") {
398
0
    ArrayRef<const char *> CC1Args = ArrayRef(Argv).drop_front();
399
0
    std::unique_ptr<CompilerInvocation> Invocation(
400
0
        newInvocation(&*Diagnostics, CC1Args, BinaryName));
401
0
    if (Diagnostics->hasErrorOccurred())
402
0
      return false;
403
0
    return Action->runInvocation(std::move(Invocation), Files,
404
0
                                 std::move(PCHContainerOps), DiagConsumer);
405
0
  }
406
407
0
  const std::unique_ptr<driver::Driver> Driver(
408
0
      newDriver(&*Diagnostics, BinaryName, &Files->getVirtualFileSystem()));
409
  // The "input file not found" diagnostics from the driver are useful.
410
  // The driver is only aware of the VFS working directory, but some clients
411
  // change this at the FileManager level instead.
412
  // In this case the checks have false positives, so skip them.
413
0
  if (!Files->getFileSystemOpts().WorkingDir.empty())
414
0
    Driver->setCheckInputsExist(false);
415
0
  const std::unique_ptr<driver::Compilation> Compilation(
416
0
      Driver->BuildCompilation(llvm::ArrayRef(Argv)));
417
0
  if (!Compilation)
418
0
    return false;
419
0
  const llvm::opt::ArgStringList *const CC1Args = getCC1Arguments(
420
0
      &*Diagnostics, Compilation.get());
421
0
  if (!CC1Args)
422
0
    return false;
423
0
  std::unique_ptr<CompilerInvocation> Invocation(
424
0
      newInvocation(&*Diagnostics, *CC1Args, BinaryName));
425
0
  return runInvocation(BinaryName, Compilation.get(), std::move(Invocation),
426
0
                       std::move(PCHContainerOps));
427
0
}
428
429
bool ToolInvocation::runInvocation(
430
    const char *BinaryName, driver::Compilation *Compilation,
431
    std::shared_ptr<CompilerInvocation> Invocation,
432
0
    std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
433
  // Show the invocation, with -v.
434
0
  if (Invocation->getHeaderSearchOpts().Verbose) {
435
0
    llvm::errs() << "clang Invocation:\n";
436
0
    Compilation->getJobs().Print(llvm::errs(), "\n", true);
437
0
    llvm::errs() << "\n";
438
0
  }
439
440
0
  return Action->runInvocation(std::move(Invocation), Files,
441
0
                               std::move(PCHContainerOps), DiagConsumer);
442
0
}
443
444
bool FrontendActionFactory::runInvocation(
445
    std::shared_ptr<CompilerInvocation> Invocation, FileManager *Files,
446
    std::shared_ptr<PCHContainerOperations> PCHContainerOps,
447
46
    DiagnosticConsumer *DiagConsumer) {
448
  // Create a compiler instance to handle the actual work.
449
46
  CompilerInstance Compiler(std::move(PCHContainerOps));
450
46
  Compiler.setInvocation(std::move(Invocation));
451
46
  Compiler.setFileManager(Files);
452
453
  // The FrontendAction can have lifetime requirements for Compiler or its
454
  // members, and we need to ensure it's deleted earlier than Compiler. So we
455
  // pass it to an std::unique_ptr declared after the Compiler variable.
456
46
  std::unique_ptr<FrontendAction> ScopedToolAction(create());
457
458
  // Create the compiler's actual diagnostics engine.
459
46
  Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
460
46
  if (!Compiler.hasDiagnostics())
461
0
    return false;
462
463
46
  Compiler.createSourceManager(*Files);
464
465
46
  const bool Success = Compiler.ExecuteAction(*ScopedToolAction);
466
467
46
  Files->clearStatCache();
468
46
  return Success;
469
46
}
470
471
ClangTool::ClangTool(const CompilationDatabase &Compilations,
472
                     ArrayRef<std::string> SourcePaths,
473
                     std::shared_ptr<PCHContainerOperations> PCHContainerOps,
474
                     IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
475
                     IntrusiveRefCntPtr<FileManager> Files)
476
    : Compilations(Compilations), SourcePaths(SourcePaths),
477
      PCHContainerOps(std::move(PCHContainerOps)),
478
      OverlayFileSystem(new llvm::vfs::OverlayFileSystem(std::move(BaseFS))),
479
      InMemoryFileSystem(new llvm::vfs::InMemoryFileSystem),
480
      Files(Files ? Files
481
0
                  : new FileManager(FileSystemOptions(), OverlayFileSystem)) {
482
0
  OverlayFileSystem->pushOverlay(InMemoryFileSystem);
483
0
  appendArgumentsAdjuster(getClangStripOutputAdjuster());
484
0
  appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster());
485
0
  appendArgumentsAdjuster(getClangStripDependencyFileAdjuster());
486
0
  if (Files)
487
0
    Files->setVirtualFileSystem(OverlayFileSystem);
488
0
}
489
490
0
ClangTool::~ClangTool() = default;
491
492
0
void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) {
493
0
  MappedFileContents.push_back(std::make_pair(FilePath, Content));
494
0
}
495
496
0
void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster Adjuster) {
497
0
  ArgsAdjuster = combineAdjusters(std::move(ArgsAdjuster), std::move(Adjuster));
498
0
}
499
500
0
void ClangTool::clearArgumentsAdjusters() {
501
0
  ArgsAdjuster = nullptr;
502
0
}
503
504
static void injectResourceDir(CommandLineArguments &Args, const char *Argv0,
505
0
                              void *MainAddr) {
506
  // Allow users to override the resource dir.
507
0
  for (StringRef Arg : Args)
508
0
    if (Arg.starts_with("-resource-dir"))
509
0
      return;
510
511
  // If there's no override in place add our resource dir.
512
0
  Args = getInsertArgumentAdjuster(
513
0
      ("-resource-dir=" + CompilerInvocation::GetResourcesPath(Argv0, MainAddr))
514
0
          .c_str())(Args, "");
515
0
}
516
517
0
int ClangTool::run(ToolAction *Action) {
518
  // Exists solely for the purpose of lookup of the resource path.
519
  // This just needs to be some symbol in the binary.
520
0
  static int StaticSymbol;
521
522
  // First insert all absolute paths into the in-memory VFS. These are global
523
  // for all compile commands.
524
0
  if (SeenWorkingDirectories.insert("/").second)
525
0
    for (const auto &MappedFile : MappedFileContents)
526
0
      if (llvm::sys::path::is_absolute(MappedFile.first))
527
0
        InMemoryFileSystem->addFile(
528
0
            MappedFile.first, 0,
529
0
            llvm::MemoryBuffer::getMemBuffer(MappedFile.second));
530
531
0
  bool ProcessingFailed = false;
532
0
  bool FileSkipped = false;
533
  // Compute all absolute paths before we run any actions, as those will change
534
  // the working directory.
535
0
  std::vector<std::string> AbsolutePaths;
536
0
  AbsolutePaths.reserve(SourcePaths.size());
537
0
  for (const auto &SourcePath : SourcePaths) {
538
0
    auto AbsPath = getAbsolutePath(*OverlayFileSystem, SourcePath);
539
0
    if (!AbsPath) {
540
0
      llvm::errs() << "Skipping " << SourcePath
541
0
                   << ". Error while getting an absolute path: "
542
0
                   << llvm::toString(AbsPath.takeError()) << "\n";
543
0
      continue;
544
0
    }
545
0
    AbsolutePaths.push_back(std::move(*AbsPath));
546
0
  }
547
548
  // Remember the working directory in case we need to restore it.
549
0
  std::string InitialWorkingDir;
550
0
  if (auto CWD = OverlayFileSystem->getCurrentWorkingDirectory()) {
551
0
    InitialWorkingDir = std::move(*CWD);
552
0
  } else {
553
0
    llvm::errs() << "Could not get working directory: "
554
0
                 << CWD.getError().message() << "\n";
555
0
  }
556
557
0
  size_t NumOfTotalFiles = AbsolutePaths.size();
558
0
  unsigned ProcessedFileCounter = 0;
559
0
  for (llvm::StringRef File : AbsolutePaths) {
560
    // Currently implementations of CompilationDatabase::getCompileCommands can
561
    // change the state of the file system (e.g.  prepare generated headers), so
562
    // this method needs to run right before we invoke the tool, as the next
563
    // file may require a different (incompatible) state of the file system.
564
    //
565
    // FIXME: Make the compilation database interface more explicit about the
566
    // requirements to the order of invocation of its members.
567
0
    std::vector<CompileCommand> CompileCommandsForFile =
568
0
        Compilations.getCompileCommands(File);
569
0
    if (CompileCommandsForFile.empty()) {
570
0
      llvm::errs() << "Skipping " << File << ". Compile command not found.\n";
571
0
      FileSkipped = true;
572
0
      continue;
573
0
    }
574
0
    for (CompileCommand &CompileCommand : CompileCommandsForFile) {
575
      // FIXME: chdir is thread hostile; on the other hand, creating the same
576
      // behavior as chdir is complex: chdir resolves the path once, thus
577
      // guaranteeing that all subsequent relative path operations work
578
      // on the same path the original chdir resulted in. This makes a
579
      // difference for example on network filesystems, where symlinks might be
580
      // switched during runtime of the tool. Fixing this depends on having a
581
      // file system abstraction that allows openat() style interactions.
582
0
      if (OverlayFileSystem->setCurrentWorkingDirectory(
583
0
              CompileCommand.Directory))
584
0
        llvm::report_fatal_error("Cannot chdir into \"" +
585
0
                                 Twine(CompileCommand.Directory) + "\"!");
586
587
      // Now fill the in-memory VFS with the relative file mappings so it will
588
      // have the correct relative paths. We never remove mappings but that
589
      // should be fine.
590
0
      if (SeenWorkingDirectories.insert(CompileCommand.Directory).second)
591
0
        for (const auto &MappedFile : MappedFileContents)
592
0
          if (!llvm::sys::path::is_absolute(MappedFile.first))
593
0
            InMemoryFileSystem->addFile(
594
0
                MappedFile.first, 0,
595
0
                llvm::MemoryBuffer::getMemBuffer(MappedFile.second));
596
597
0
      std::vector<std::string> CommandLine = CompileCommand.CommandLine;
598
0
      if (ArgsAdjuster)
599
0
        CommandLine = ArgsAdjuster(CommandLine, CompileCommand.Filename);
600
0
      assert(!CommandLine.empty());
601
602
      // Add the resource dir based on the binary of this tool. argv[0] in the
603
      // compilation database may refer to a different compiler and we want to
604
      // pick up the very same standard library that compiler is using. The
605
      // builtin headers in the resource dir need to match the exact clang
606
      // version the tool is using.
607
      // FIXME: On linux, GetMainExecutable is independent of the value of the
608
      // first argument, thus allowing ClangTool and runToolOnCode to just
609
      // pass in made-up names here. Make sure this works on other platforms.
610
0
      injectResourceDir(CommandLine, "clang_tool", &StaticSymbol);
611
612
      // FIXME: We need a callback mechanism for the tool writer to output a
613
      // customized message for each file.
614
0
      if (NumOfTotalFiles > 1)
615
0
        llvm::errs() << "[" + std::to_string(++ProcessedFileCounter) + "/" +
616
0
                            std::to_string(NumOfTotalFiles) +
617
0
                            "] Processing file " + File
618
0
                     << ".\n";
619
0
      ToolInvocation Invocation(std::move(CommandLine), Action, Files.get(),
620
0
                                PCHContainerOps);
621
0
      Invocation.setDiagnosticConsumer(DiagConsumer);
622
623
0
      if (!Invocation.run()) {
624
        // FIXME: Diagnostics should be used instead.
625
0
        if (PrintErrorMessage)
626
0
          llvm::errs() << "Error while processing " << File << ".\n";
627
0
        ProcessingFailed = true;
628
0
      }
629
0
    }
630
0
  }
631
632
0
  if (!InitialWorkingDir.empty()) {
633
0
    if (auto EC =
634
0
            OverlayFileSystem->setCurrentWorkingDirectory(InitialWorkingDir))
635
0
      llvm::errs() << "Error when trying to restore working dir: "
636
0
                   << EC.message() << "\n";
637
0
  }
638
0
  return ProcessingFailed ? 1 : (FileSkipped ? 2 : 0);
639
0
}
640
641
namespace {
642
643
class ASTBuilderAction : public ToolAction {
644
  std::vector<std::unique_ptr<ASTUnit>> &ASTs;
645
646
public:
647
0
  ASTBuilderAction(std::vector<std::unique_ptr<ASTUnit>> &ASTs) : ASTs(ASTs) {}
648
649
  bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
650
                     FileManager *Files,
651
                     std::shared_ptr<PCHContainerOperations> PCHContainerOps,
652
0
                     DiagnosticConsumer *DiagConsumer) override {
653
0
    std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation(
654
0
        Invocation, std::move(PCHContainerOps),
655
0
        CompilerInstance::createDiagnostics(&Invocation->getDiagnosticOpts(),
656
0
                                            DiagConsumer,
657
0
                                            /*ShouldOwnClient=*/false),
658
0
        Files);
659
0
    if (!AST)
660
0
      return false;
661
662
0
    ASTs.push_back(std::move(AST));
663
0
    return true;
664
0
  }
665
};
666
667
} // namespace
668
669
0
int ClangTool::buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs) {
670
0
  ASTBuilderAction Action(ASTs);
671
0
  return run(&Action);
672
0
}
673
674
0
void ClangTool::setPrintErrorMessage(bool PrintErrorMessage) {
675
0
  this->PrintErrorMessage = PrintErrorMessage;
676
0
}
677
678
namespace clang {
679
namespace tooling {
680
681
std::unique_ptr<ASTUnit>
682
buildASTFromCode(StringRef Code, StringRef FileName,
683
0
                 std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
684
0
  return buildASTFromCodeWithArgs(Code, std::vector<std::string>(), FileName,
685
0
                                  "clang-tool", std::move(PCHContainerOps));
686
0
}
687
688
std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
689
    StringRef Code, const std::vector<std::string> &Args, StringRef FileName,
690
    StringRef ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps,
691
    ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles,
692
0
    DiagnosticConsumer *DiagConsumer) {
693
0
  std::vector<std::unique_ptr<ASTUnit>> ASTs;
694
0
  ASTBuilderAction Action(ASTs);
695
0
  llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
696
0
      new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
697
0
  llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
698
0
      new llvm::vfs::InMemoryFileSystem);
699
0
  OverlayFileSystem->pushOverlay(InMemoryFileSystem);
700
0
  llvm::IntrusiveRefCntPtr<FileManager> Files(
701
0
      new FileManager(FileSystemOptions(), OverlayFileSystem));
702
703
0
  ToolInvocation Invocation(
704
0
      getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileName), FileName),
705
0
      &Action, Files.get(), std::move(PCHContainerOps));
706
0
  Invocation.setDiagnosticConsumer(DiagConsumer);
707
708
0
  InMemoryFileSystem->addFile(FileName, 0,
709
0
                              llvm::MemoryBuffer::getMemBufferCopy(Code));
710
0
  for (auto &FilenameWithContent : VirtualMappedFiles) {
711
0
    InMemoryFileSystem->addFile(
712
0
        FilenameWithContent.first, 0,
713
0
        llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
714
0
  }
715
716
0
  if (!Invocation.run())
717
0
    return nullptr;
718
719
0
  assert(ASTs.size() == 1);
720
0
  return std::move(ASTs[0]);
721
0
}
722
723
} // namespace tooling
724
} // namespace clang