Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Frontend/CompilerInstance.h
Line
Count
Source (jump to first uncovered line)
1
//===-- CompilerInstance.h - Clang Compiler Instance ------------*- 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_FRONTEND_COMPILERINSTANCE_H_
10
#define LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_
11
12
#include "clang/AST/ASTConsumer.h"
13
#include "clang/Basic/Diagnostic.h"
14
#include "clang/Basic/SourceManager.h"
15
#include "clang/Frontend/CompilerInvocation.h"
16
#include "clang/Frontend/PCHContainerOperations.h"
17
#include "clang/Frontend/Utils.h"
18
#include "clang/Lex/HeaderSearchOptions.h"
19
#include "clang/Lex/ModuleLoader.h"
20
#include "llvm/ADT/ArrayRef.h"
21
#include "llvm/ADT/DenseMap.h"
22
#include "llvm/ADT/IntrusiveRefCntPtr.h"
23
#include "llvm/ADT/StringRef.h"
24
#include "llvm/Support/BuryPointer.h"
25
#include "llvm/Support/FileSystem.h"
26
#include <cassert>
27
#include <list>
28
#include <memory>
29
#include <string>
30
#include <utility>
31
32
namespace llvm {
33
class raw_fd_ostream;
34
class Timer;
35
class TimerGroup;
36
}
37
38
namespace clang {
39
class ASTContext;
40
class ASTReader;
41
class CodeCompleteConsumer;
42
class DiagnosticsEngine;
43
class DiagnosticConsumer;
44
class FileManager;
45
class FrontendAction;
46
class InMemoryModuleCache;
47
class Module;
48
class Preprocessor;
49
class Sema;
50
class SourceManager;
51
class TargetInfo;
52
enum class DisableValidationForModuleKind;
53
54
/// CompilerInstance - Helper class for managing a single instance of the Clang
55
/// compiler.
56
///
57
/// The CompilerInstance serves two purposes:
58
///  (1) It manages the various objects which are necessary to run the compiler,
59
///      for example the preprocessor, the target information, and the AST
60
///      context.
61
///  (2) It provides utility routines for constructing and manipulating the
62
///      common Clang objects.
63
///
64
/// The compiler instance generally owns the instance of all the objects that it
65
/// manages. However, clients can still share objects by manually setting the
66
/// object and retaking ownership prior to destroying the CompilerInstance.
67
///
68
/// The compiler instance is intended to simplify clients, but not to lock them
69
/// in to the compiler instance for everything. When possible, utility functions
70
/// come in two forms; a short form that reuses the CompilerInstance objects,
71
/// and a long form that takes explicit instances of any required objects.
72
class CompilerInstance : public ModuleLoader {
73
  /// The options used in this compiler instance.
74
  std::shared_ptr<CompilerInvocation> Invocation;
75
76
  /// The diagnostics engine instance.
77
  IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
78
79
  /// The target being compiled for.
80
  IntrusiveRefCntPtr<TargetInfo> Target;
81
82
  /// Auxiliary Target info.
83
  IntrusiveRefCntPtr<TargetInfo> AuxTarget;
84
85
  /// The file manager.
86
  IntrusiveRefCntPtr<FileManager> FileMgr;
87
88
  /// The source manager.
89
  IntrusiveRefCntPtr<SourceManager> SourceMgr;
90
91
  /// The cache of PCM files.
92
  IntrusiveRefCntPtr<InMemoryModuleCache> ModuleCache;
93
94
  /// The preprocessor.
95
  std::shared_ptr<Preprocessor> PP;
96
97
  /// The AST context.
98
  IntrusiveRefCntPtr<ASTContext> Context;
99
100
  /// An optional sema source that will be attached to sema.
101
  IntrusiveRefCntPtr<ExternalSemaSource> ExternalSemaSrc;
102
103
  /// The AST consumer.
104
  std::unique_ptr<ASTConsumer> Consumer;
105
106
  /// The code completion consumer.
107
  std::unique_ptr<CodeCompleteConsumer> CompletionConsumer;
108
109
  /// The semantic analysis object.
110
  std::unique_ptr<Sema> TheSema;
111
112
  /// The frontend timer group.
113
  std::unique_ptr<llvm::TimerGroup> FrontendTimerGroup;
114
115
  /// The frontend timer.
116
  std::unique_ptr<llvm::Timer> FrontendTimer;
117
118
  /// The ASTReader, if one exists.
119
  IntrusiveRefCntPtr<ASTReader> TheASTReader;
120
121
  /// The module dependency collector for crashdumps
122
  std::shared_ptr<ModuleDependencyCollector> ModuleDepCollector;
123
124
  /// The module provider.
125
  std::shared_ptr<PCHContainerOperations> ThePCHContainerOperations;
126
127
  std::vector<std::shared_ptr<DependencyCollector>> DependencyCollectors;
128
129
  /// The set of top-level modules that has already been built on the
130
  /// fly as part of this overall compilation action.
131
  std::map<std::string, std::string, std::less<>> BuiltModules;
132
133
  /// Should we delete the BuiltModules when we're done?
134
  bool DeleteBuiltModules = true;
135
136
  /// The location of the module-import keyword for the last module
137
  /// import.
138
  SourceLocation LastModuleImportLoc;
139
140
  /// The result of the last module import.
141
  ///
142
  ModuleLoadResult LastModuleImportResult;
143
144
  /// Whether we should (re)build the global module index once we
145
  /// have finished with this translation unit.
146
  bool BuildGlobalModuleIndex = false;
147
148
  /// We have a full global module index, with all modules.
149
  bool HaveFullGlobalModuleIndex = false;
150
151
  /// One or more modules failed to build.
152
  bool DisableGeneratingGlobalModuleIndex = false;
153
154
  /// The stream for verbose output if owned, otherwise nullptr.
155
  std::unique_ptr<raw_ostream> OwnedVerboseOutputStream;
156
157
  /// The stream for verbose output.
158
  raw_ostream *VerboseOutputStream = &llvm::errs();
159
160
  /// Holds information about the output file.
161
  ///
162
  /// If TempFilename is not empty we must rename it to Filename at the end.
163
  /// TempFilename may be empty and Filename non-empty if creating the temporary
164
  /// failed.
165
  struct OutputFile {
166
    std::string Filename;
167
    Optional<llvm::sys::fs::TempFile> File;
168
169
    OutputFile(std::string filename, Optional<llvm::sys::fs::TempFile> file)
170
28.8k
        : Filename(std::move(filename)), File(std::move(file)) {}
171
  };
172
173
  /// The list of active output files.
174
  std::list<OutputFile> OutputFiles;
175
176
  /// Force an output buffer.
177
  std::unique_ptr<llvm::raw_pwrite_stream> OutputStream;
178
179
  CompilerInstance(const CompilerInstance &) = delete;
180
  void operator=(const CompilerInstance &) = delete;
181
public:
182
  explicit CompilerInstance(
183
      std::shared_ptr<PCHContainerOperations> PCHContainerOps =
184
          std::make_shared<PCHContainerOperations>(),
185
      InMemoryModuleCache *SharedModuleCache = nullptr);
186
  ~CompilerInstance() override;
187
188
  /// @name High-Level Operations
189
  /// {
190
191
  /// ExecuteAction - Execute the provided action against the compiler's
192
  /// CompilerInvocation object.
193
  ///
194
  /// This function makes the following assumptions:
195
  ///
196
  ///  - The invocation options should be initialized. This function does not
197
  ///    handle the '-help' or '-version' options, clients should handle those
198
  ///    directly.
199
  ///
200
  ///  - The diagnostics engine should have already been created by the client.
201
  ///
202
  ///  - No other CompilerInstance state should have been initialized (this is
203
  ///    an unchecked error).
204
  ///
205
  ///  - Clients should have initialized any LLVM target features that may be
206
  ///    required.
207
  ///
208
  ///  - Clients should eventually call llvm_shutdown() upon the completion of
209
  ///    this routine to ensure that any managed objects are properly destroyed.
210
  ///
211
  /// Note that this routine may write output to 'stderr'.
212
  ///
213
  /// \param Act - The action to execute.
214
  /// \return - True on success.
215
  //
216
  // FIXME: Eliminate the llvm_shutdown requirement, that should either be part
217
  // of the context or else not CompilerInstance specific.
218
  bool ExecuteAction(FrontendAction &Act);
219
220
  /// Load the list of plugins requested in the \c FrontendOptions.
221
  void LoadRequestedPlugins();
222
223
  /// }
224
  /// @name Compiler Invocation and Options
225
  /// {
226
227
9.31k
  bool hasInvocation() const { return Invocation != nullptr; }
228
229
213k
  CompilerInvocation &getInvocation() {
230
213k
    assert(Invocation && "Compiler instance has no invocation!");
231
0
    return *Invocation;
232
213k
  }
233
234
  /// setInvocation - Replace the current invocation.
235
  void setInvocation(std::shared_ptr<CompilerInvocation> Value);
236
237
  /// Indicates whether we should (re)build the global module index.
238
  bool shouldBuildGlobalModuleIndex() const;
239
240
  /// Set the flag indicating whether we should (re)build the global
241
  /// module index.
242
1.12k
  void setBuildGlobalModuleIndex(bool Build) {
243
1.12k
    BuildGlobalModuleIndex = Build;
244
1.12k
  }
245
246
  /// }
247
  /// @name Forwarding Methods
248
  /// {
249
250
66.9k
  AnalyzerOptionsRef getAnalyzerOpts() {
251
66.9k
    return Invocation->getAnalyzerOpts();
252
66.9k
  }
253
254
415k
  CodeGenOptions &getCodeGenOpts() {
255
415k
    return Invocation->getCodeGenOpts();
256
415k
  }
257
0
  const CodeGenOptions &getCodeGenOpts() const {
258
0
    return Invocation->getCodeGenOpts();
259
0
  }
260
261
90.8k
  DependencyOutputOptions &getDependencyOutputOpts() {
262
90.8k
    return Invocation->getDependencyOutputOpts();
263
90.8k
  }
264
0
  const DependencyOutputOptions &getDependencyOutputOpts() const {
265
0
    return Invocation->getDependencyOutputOpts();
266
0
  }
267
268
141k
  DiagnosticOptions &getDiagnosticOpts() {
269
141k
    return Invocation->getDiagnosticOpts();
270
141k
  }
271
9
  const DiagnosticOptions &getDiagnosticOpts() const {
272
9
    return Invocation->getDiagnosticOpts();
273
9
  }
274
275
84.7k
  FileSystemOptions &getFileSystemOpts() {
276
84.7k
    return Invocation->getFileSystemOpts();
277
84.7k
  }
278
0
  const FileSystemOptions &getFileSystemOpts() const {
279
0
    return Invocation->getFileSystemOpts();
280
0
  }
281
282
2.08M
  FrontendOptions &getFrontendOpts() {
283
2.08M
    return Invocation->getFrontendOpts();
284
2.08M
  }
285
654
  const FrontendOptions &getFrontendOpts() const {
286
654
    return Invocation->getFrontendOpts();
287
654
  }
288
289
371k
  HeaderSearchOptions &getHeaderSearchOpts() {
290
371k
    return Invocation->getHeaderSearchOpts();
291
371k
  }
292
111k
  const HeaderSearchOptions &getHeaderSearchOpts() const {
293
111k
    return Invocation->getHeaderSearchOpts();
294
111k
  }
295
90.8k
  std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() const {
296
90.8k
    return Invocation->getHeaderSearchOptsPtr();
297
90.8k
  }
298
299
1.44M
  LangOptions &getLangOpts() {
300
1.44M
    return *Invocation->getLangOpts();
301
1.44M
  }
302
0
  const LangOptions &getLangOpts() const {
303
0
    return *Invocation->getLangOpts();
304
0
  }
305
306
415k
  PreprocessorOptions &getPreprocessorOpts() {
307
415k
    return Invocation->getPreprocessorOpts();
308
415k
  }
309
0
  const PreprocessorOptions &getPreprocessorOpts() const {
310
0
    return Invocation->getPreprocessorOpts();
311
0
  }
312
313
98.1k
  PreprocessorOutputOptions &getPreprocessorOutputOpts() {
314
98.1k
    return Invocation->getPreprocessorOutputOpts();
315
98.1k
  }
316
0
  const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
317
0
    return Invocation->getPreprocessorOutputOpts();
318
0
  }
319
320
220k
  TargetOptions &getTargetOpts() {
321
220k
    return Invocation->getTargetOpts();
322
220k
  }
323
0
  const TargetOptions &getTargetOpts() const {
324
0
    return Invocation->getTargetOpts();
325
0
  }
326
327
  /// }
328
  /// @name Diagnostics Engine
329
  /// {
330
331
123k
  bool hasDiagnostics() const { return Diagnostics != nullptr; }
332
333
  /// Get the current diagnostics engine.
334
1.21M
  DiagnosticsEngine &getDiagnostics() const {
335
1.21M
    assert(Diagnostics && "Compiler instance has no diagnostics!");
336
0
    return *Diagnostics;
337
1.21M
  }
338
339
  /// setDiagnostics - Replace the current diagnostics engine.
340
  void setDiagnostics(DiagnosticsEngine *Value);
341
342
214k
  DiagnosticConsumer &getDiagnosticClient() const {
343
214k
    assert(Diagnostics && Diagnostics->getClient() &&
344
214k
           "Compiler instance has no diagnostic client!");
345
0
    return *Diagnostics->getClient();
346
214k
  }
347
348
  /// }
349
  /// @name VerboseOutputStream
350
  /// }
351
352
  /// Replace the current stream for verbose output.
353
  void setVerboseOutputStream(raw_ostream &Value);
354
355
  /// Replace the current stream for verbose output.
356
  void setVerboseOutputStream(std::unique_ptr<raw_ostream> Value);
357
358
  /// Get the current stream for verbose output.
359
63.5k
  raw_ostream &getVerboseOutputStream() {
360
63.5k
    return *VerboseOutputStream;
361
63.5k
  }
362
363
  /// }
364
  /// @name Target Info
365
  /// {
366
367
100k
  bool hasTarget() const { return Target != nullptr; }
368
369
631k
  TargetInfo &getTarget() const {
370
631k
    assert(Target && "Compiler instance has no target!");
371
0
    return *Target;
372
631k
  }
373
374
  /// Replace the current Target.
375
  void setTarget(TargetInfo *Value);
376
377
  /// }
378
  /// @name AuxTarget Info
379
  /// {
380
381
326k
  TargetInfo *getAuxTarget() const { return AuxTarget.get(); }
382
383
  /// Replace the current AuxTarget.
384
  void setAuxTarget(TargetInfo *Value);
385
386
  // Create Target and AuxTarget based on current options
387
  bool createTarget();
388
389
  /// }
390
  /// @name Virtual File System
391
  /// {
392
393
  llvm::vfs::FileSystem &getVirtualFileSystem() const;
394
395
  /// }
396
  /// @name File Manager
397
  /// {
398
399
75.8k
  bool hasFileManager() const { return FileMgr != nullptr; }
400
401
  /// Return the current file manager to the caller.
402
155k
  FileManager &getFileManager() const {
403
155k
    assert(FileMgr && "Compiler instance has no file manager!");
404
0
    return *FileMgr;
405
155k
  }
406
407
12
  void resetAndLeakFileManager() {
408
12
    llvm::BuryPointer(FileMgr.get());
409
12
    FileMgr.resetWithoutRelease();
410
12
  }
411
412
  /// Replace the current file manager and virtual file system.
413
  void setFileManager(FileManager *Value);
414
415
  /// }
416
  /// @name Source Manager
417
  /// {
418
419
154k
  bool hasSourceManager() const { return SourceMgr != nullptr; }
420
421
  /// Return the current source manager.
422
4.08M
  SourceManager &getSourceManager() const {
423
4.08M
    assert(SourceMgr && "Compiler instance has no source manager!");
424
0
    return *SourceMgr;
425
4.08M
  }
426
427
12
  void resetAndLeakSourceManager() {
428
12
    llvm::BuryPointer(SourceMgr.get());
429
12
    SourceMgr.resetWithoutRelease();
430
12
  }
431
432
  /// setSourceManager - Replace the current source manager.
433
  void setSourceManager(SourceManager *Value);
434
435
  /// }
436
  /// @name Preprocessor
437
  /// {
438
439
154k
  bool hasPreprocessor() const { return PP != nullptr; }
440
441
  /// Return the current preprocessor.
442
10.7M
  Preprocessor &getPreprocessor() const {
443
10.7M
    assert(PP && "Compiler instance has no preprocessor!");
444
0
    return *PP;
445
10.7M
  }
446
447
9.45k
  std::shared_ptr<Preprocessor> getPreprocessorPtr() { return PP; }
448
449
12
  void resetAndLeakPreprocessor() {
450
12
    llvm::BuryPointer(new std::shared_ptr<Preprocessor>(PP));
451
12
  }
452
453
  /// Replace the current preprocessor.
454
  void setPreprocessor(std::shared_ptr<Preprocessor> Value);
455
456
  /// }
457
  /// @name ASTContext
458
  /// {
459
460
88.3k
  bool hasASTContext() const { return Context != nullptr; }
461
462
378k
  ASTContext &getASTContext() const {
463
378k
    assert(Context && "Compiler instance has no AST context!");
464
0
    return *Context;
465
378k
  }
466
467
6.08k
  void resetAndLeakASTContext() {
468
6.08k
    llvm::BuryPointer(Context.get());
469
6.08k
    Context.resetWithoutRelease();
470
6.08k
  }
471
472
  /// setASTContext - Replace the current AST context.
473
  void setASTContext(ASTContext *Value);
474
475
  /// Replace the current Sema; the compiler instance takes ownership
476
  /// of S.
477
  void setSema(Sema *S);
478
479
  /// }
480
  /// @name ASTConsumer
481
  /// {
482
483
186k
  bool hasASTConsumer() const { return (bool)Consumer; }
484
485
176k
  ASTConsumer &getASTConsumer() const {
486
176k
    assert(Consumer && "Compiler instance has no AST consumer!");
487
0
    return *Consumer;
488
176k
  }
489
490
  /// takeASTConsumer - Remove the current AST consumer and give ownership to
491
  /// the caller.
492
15.4k
  std::unique_ptr<ASTConsumer> takeASTConsumer() { return std::move(Consumer); }
493
494
  /// setASTConsumer - Replace the current AST consumer; the compiler instance
495
  /// takes ownership of \p Value.
496
  void setASTConsumer(std::unique_ptr<ASTConsumer> Value);
497
498
  /// }
499
  /// @name Semantic analysis
500
  /// {
501
81.0k
  bool hasSema() const { return (bool)TheSema; }
502
503
109k
  Sema &getSema() const {
504
109k
    assert(TheSema && "Compiler instance has no Sema object!");
505
0
    return *TheSema;
506
109k
  }
507
508
  std::unique_ptr<Sema> takeSema();
509
  void resetAndLeakSema();
510
511
  /// }
512
  /// @name Module Management
513
  /// {
514
515
  IntrusiveRefCntPtr<ASTReader> getASTReader() const;
516
  void setASTReader(IntrusiveRefCntPtr<ASTReader> Reader);
517
518
  std::shared_ptr<ModuleDependencyCollector> getModuleDepCollector() const;
519
  void setModuleDepCollector(
520
      std::shared_ptr<ModuleDependencyCollector> Collector);
521
522
43.5k
  std::shared_ptr<PCHContainerOperations> getPCHContainerOperations() const {
523
43.5k
    return ThePCHContainerOperations;
524
43.5k
  }
525
526
  /// Return the appropriate PCHContainerWriter depending on the
527
  /// current CodeGenOptions.
528
5.48k
  const PCHContainerWriter &getPCHContainerWriter() const {
529
5.48k
    assert(Invocation && "cannot determine module format without invocation");
530
0
    StringRef Format = getHeaderSearchOpts().ModuleFormat;
531
5.48k
    auto *Writer = ThePCHContainerOperations->getWriterOrNull(Format);
532
5.48k
    if (!Writer) {
533
0
      if (Diagnostics)
534
0
        Diagnostics->Report(diag::err_module_format_unhandled) << Format;
535
0
      llvm::report_fatal_error("unknown module format");
536
0
    }
537
5.48k
    return *Writer;
538
5.48k
  }
539
540
  /// Return the appropriate PCHContainerReader depending on the
541
  /// current CodeGenOptions.
542
105k
  const PCHContainerReader &getPCHContainerReader() const {
543
105k
    assert(Invocation && "cannot determine module format without invocation");
544
0
    StringRef Format = getHeaderSearchOpts().ModuleFormat;
545
105k
    auto *Reader = ThePCHContainerOperations->getReaderOrNull(Format);
546
105k
    if (!Reader) {
547
0
      if (Diagnostics)
548
0
        Diagnostics->Report(diag::err_module_format_unhandled) << Format;
549
0
      llvm::report_fatal_error("unknown module format");
550
0
    }
551
105k
    return *Reader;
552
105k
  }
553
554
  /// }
555
  /// @name Code Completion
556
  /// {
557
558
70.7k
  bool hasCodeCompletionConsumer() const { return (bool)CompletionConsumer; }
559
560
1.21k
  CodeCompleteConsumer &getCodeCompletionConsumer() const {
561
1.21k
    assert(CompletionConsumer &&
562
1.21k
           "Compiler instance has no code completion consumer!");
563
0
    return *CompletionConsumer;
564
1.21k
  }
565
566
  /// setCodeCompletionConsumer - Replace the current code completion consumer;
567
  /// the compiler instance takes ownership of \p Value.
568
  void setCodeCompletionConsumer(CodeCompleteConsumer *Value);
569
570
  /// }
571
  /// @name Frontend timer
572
  /// {
573
574
73.5k
  bool hasFrontendTimer() const { return (bool)FrontendTimer; }
575
576
17
  llvm::Timer &getFrontendTimer() const {
577
17
    assert(FrontendTimer && "Compiler instance has no frontend timer!");
578
0
    return *FrontendTimer;
579
17
  }
580
581
  /// }
582
  /// @name Output Files
583
  /// {
584
585
  /// clearOutputFiles - Clear the output file list. The underlying output
586
  /// streams must have been closed beforehand.
587
  ///
588
  /// \param EraseFiles - If true, attempt to erase the files from disk.
589
  void clearOutputFiles(bool EraseFiles);
590
591
  /// }
592
  /// @name Construction Utility Methods
593
  /// {
594
595
  /// Create the diagnostics engine using the invocation's diagnostic options
596
  /// and replace any existing one with it.
597
  ///
598
  /// Note that this routine also replaces the diagnostic client,
599
  /// allocating one if one is not provided.
600
  ///
601
  /// \param Client If non-NULL, a diagnostic client that will be
602
  /// attached to (and, then, owned by) the DiagnosticsEngine inside this AST
603
  /// unit.
604
  ///
605
  /// \param ShouldOwnClient If Client is non-NULL, specifies whether
606
  /// the diagnostic object should take ownership of the client.
607
  void createDiagnostics(DiagnosticConsumer *Client = nullptr,
608
                         bool ShouldOwnClient = true);
609
610
  /// Create a DiagnosticsEngine object with a the TextDiagnosticPrinter.
611
  ///
612
  /// If no diagnostic client is provided, this creates a
613
  /// DiagnosticConsumer that is owned by the returned diagnostic
614
  /// object, if using directly the caller is responsible for
615
  /// releasing the returned DiagnosticsEngine's client eventually.
616
  ///
617
  /// \param Opts - The diagnostic options; note that the created text
618
  /// diagnostic object contains a reference to these options.
619
  ///
620
  /// \param Client If non-NULL, a diagnostic client that will be
621
  /// attached to (and, then, owned by) the returned DiagnosticsEngine
622
  /// object.
623
  ///
624
  /// \param CodeGenOpts If non-NULL, the code gen options in use, which may be
625
  /// used by some diagnostics printers (for logging purposes only).
626
  ///
627
  /// \return The new object on success, or null on failure.
628
  static IntrusiveRefCntPtr<DiagnosticsEngine>
629
  createDiagnostics(DiagnosticOptions *Opts,
630
                    DiagnosticConsumer *Client = nullptr,
631
                    bool ShouldOwnClient = true,
632
                    const CodeGenOptions *CodeGenOpts = nullptr);
633
634
  /// Create the file manager and replace any existing one with it.
635
  ///
636
  /// \return The new file manager on success, or null on failure.
637
  FileManager *
638
  createFileManager(IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr);
639
640
  /// Create the source manager and replace any existing one with it.
641
  void createSourceManager(FileManager &FileMgr);
642
643
  /// Create the preprocessor, using the invocation, file, and source managers,
644
  /// and replace any existing one with it.
645
  void createPreprocessor(TranslationUnitKind TUKind);
646
647
  std::string getSpecificModuleCachePath(StringRef ModuleHash);
648
3.71k
  std::string getSpecificModuleCachePath() {
649
3.71k
    return getSpecificModuleCachePath(getInvocation().getModuleHash());
650
3.71k
  }
651
652
  /// Create the AST context.
653
  void createASTContext();
654
655
  /// Create an external AST source to read a PCH file and attach it to the AST
656
  /// context.
657
  void createPCHExternalASTSource(
658
      StringRef Path, DisableValidationForModuleKind DisableValidation,
659
      bool AllowPCHWithCompilerErrors, void *DeserializationListener,
660
      bool OwnDeserializationListener);
661
662
  /// Create an external AST source to read a PCH file.
663
  ///
664
  /// \return - The new object on success, or null on failure.
665
  static IntrusiveRefCntPtr<ASTReader> createPCHExternalASTSource(
666
      StringRef Path, StringRef Sysroot,
667
      DisableValidationForModuleKind DisableValidation,
668
      bool AllowPCHWithCompilerErrors, Preprocessor &PP,
669
      InMemoryModuleCache &ModuleCache, ASTContext &Context,
670
      const PCHContainerReader &PCHContainerRdr,
671
      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
672
      ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors,
673
      void *DeserializationListener, bool OwnDeserializationListener,
674
      bool Preamble, bool UseGlobalModuleIndex);
675
676
  /// Create a code completion consumer using the invocation; note that this
677
  /// will cause the source manager to truncate the input source file at the
678
  /// completion point.
679
  void createCodeCompletionConsumer();
680
681
  /// Create a code completion consumer to print code completion results, at
682
  /// \p Filename, \p Line, and \p Column, to the given output stream \p OS.
683
  static CodeCompleteConsumer *createCodeCompletionConsumer(
684
      Preprocessor &PP, StringRef Filename, unsigned Line, unsigned Column,
685
      const CodeCompleteOptions &Opts, raw_ostream &OS);
686
687
  /// Create the Sema object to be used for parsing.
688
  void createSema(TranslationUnitKind TUKind,
689
                  CodeCompleteConsumer *CompletionConsumer);
690
691
  /// Create the frontend timer and replace any existing one with it.
692
  void createFrontendTimer();
693
694
  /// Create the default output file (from the invocation's options) and add it
695
  /// to the list of tracked output files.
696
  ///
697
  /// The files created by this are usually removed on signal, and, depending
698
  /// on FrontendOptions, may also use a temporary file (that is, the data is
699
  /// written to a temporary file which will atomically replace the target
700
  /// output on success).
701
  ///
702
  /// \return - Null on error.
703
  std::unique_ptr<raw_pwrite_stream> createDefaultOutputFile(
704
      bool Binary = true, StringRef BaseInput = "", StringRef Extension = "",
705
      bool RemoveFileOnSignal = true, bool CreateMissingDirectories = false,
706
      bool ForceUseTemporary = false);
707
708
  /// Create a new output file, optionally deriving the output path name, and
709
  /// add it to the list of tracked output files.
710
  ///
711
  /// \return - Null on error.
712
  std::unique_ptr<raw_pwrite_stream>
713
  createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal,
714
                   bool UseTemporary, bool CreateMissingDirectories = false);
715
716
private:
717
  /// Create a new output file and add it to the list of tracked output files.
718
  ///
719
  /// If \p OutputPath is empty, then createOutputFile will derive an output
720
  /// path location as \p BaseInput, with any suffix removed, and \p Extension
721
  /// appended. If \p OutputPath is not stdout and \p UseTemporary
722
  /// is true, createOutputFile will create a new temporary file that must be
723
  /// renamed to \p OutputPath in the end.
724
  ///
725
  /// \param OutputPath - If given, the path to the output file.
726
  /// \param Binary - The mode to open the file in.
727
  /// \param RemoveFileOnSignal - Whether the file should be registered with
728
  /// llvm::sys::RemoveFileOnSignal. Note that this is not safe for
729
  /// multithreaded use, as the underlying signal mechanism is not reentrant
730
  /// \param UseTemporary - Create a new temporary file that must be renamed to
731
  /// OutputPath in the end.
732
  /// \param CreateMissingDirectories - When \p UseTemporary is true, create
733
  /// missing directories in the output path.
734
  Expected<std::unique_ptr<raw_pwrite_stream>>
735
  createOutputFileImpl(StringRef OutputPath, bool Binary,
736
                       bool RemoveFileOnSignal, bool UseTemporary,
737
                       bool CreateMissingDirectories);
738
739
public:
740
  std::unique_ptr<raw_pwrite_stream> createNullOutputFile();
741
742
  /// }
743
  /// @name Initialization Utility Methods
744
  /// {
745
746
  /// InitializeSourceManager - Initialize the source manager to set InputFile
747
  /// as the main file.
748
  ///
749
  /// \return True on success.
750
  bool InitializeSourceManager(const FrontendInputFile &Input);
751
752
  /// InitializeSourceManager - Initialize the source manager to set InputFile
753
  /// as the main file.
754
  ///
755
  /// \return True on success.
756
  static bool InitializeSourceManager(const FrontendInputFile &Input,
757
                                      DiagnosticsEngine &Diags,
758
                                      FileManager &FileMgr,
759
                                      SourceManager &SourceMgr);
760
761
  /// }
762
763
  void setOutputStream(std::unique_ptr<llvm::raw_pwrite_stream> OutStream) {
764
    OutputStream = std::move(OutStream);
765
  }
766
767
20.2k
  std::unique_ptr<llvm::raw_pwrite_stream> takeOutputStream() {
768
20.2k
    return std::move(OutputStream);
769
20.2k
  }
770
771
  void createASTReader();
772
773
  bool loadModuleFile(StringRef FileName);
774
775
private:
776
  /// Find a module, potentially compiling it, before reading its AST.  This is
777
  /// the guts of loadModule.
778
  ///
779
  /// For prebuilt modules, the Module is not expected to exist in
780
  /// HeaderSearch's ModuleMap.  If a ModuleFile by that name is in the
781
  /// ModuleManager, then it will be loaded and looked up.
782
  ///
783
  /// For implicit modules, the Module is expected to already be in the
784
  /// ModuleMap.  First attempt to load it from the given path on disk.  If that
785
  /// fails, defer to compileModuleAndReadAST, which will first build and then
786
  /// load it.
787
  ModuleLoadResult findOrCompileModuleAndReadAST(StringRef ModuleName,
788
                                                 SourceLocation ImportLoc,
789
                                                 SourceLocation ModuleNameLoc,
790
                                                 bool IsInclusionDirective);
791
792
public:
793
  ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
794
                              Module::NameVisibilityKind Visibility,
795
                              bool IsInclusionDirective) override;
796
797
  void createModuleFromSource(SourceLocation ImportLoc, StringRef ModuleName,
798
                              StringRef Source) override;
799
800
  void makeModuleVisible(Module *Mod, Module::NameVisibilityKind Visibility,
801
                         SourceLocation ImportLoc) override;
802
803
14.4k
  bool hadModuleLoaderFatalFailure() const {
804
14.4k
    return ModuleLoader::HadFatalFailure;
805
14.4k
  }
806
807
  GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override;
808
809
  bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override;
810
811
3.72k
  void addDependencyCollector(std::shared_ptr<DependencyCollector> Listener) {
812
3.72k
    DependencyCollectors.push_back(std::move(Listener));
813
3.72k
  }
814
815
  void setExternalSemaSource(IntrusiveRefCntPtr<ExternalSemaSource> ESS);
816
817
23.3k
  InMemoryModuleCache &getModuleCache() const { return *ModuleCache; }
818
};
819
820
} // end namespace clang
821
822
#endif