Coverage Report

Created: 2018-11-16 02:38

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