Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Frontend/FrontendAction.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- FrontendAction.cpp -----------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "clang/Frontend/FrontendAction.h"
10
#include "clang/AST/ASTConsumer.h"
11
#include "clang/AST/ASTContext.h"
12
#include "clang/AST/DeclGroup.h"
13
#include "clang/Basic/Builtins.h"
14
#include "clang/Basic/LangStandard.h"
15
#include "clang/Frontend/ASTUnit.h"
16
#include "clang/Frontend/CompilerInstance.h"
17
#include "clang/Frontend/FrontendDiagnostic.h"
18
#include "clang/Frontend/FrontendPluginRegistry.h"
19
#include "clang/Frontend/LayoutOverrideSource.h"
20
#include "clang/Frontend/MultiplexConsumer.h"
21
#include "clang/Frontend/Utils.h"
22
#include "clang/Lex/HeaderSearch.h"
23
#include "clang/Lex/LiteralSupport.h"
24
#include "clang/Lex/Preprocessor.h"
25
#include "clang/Lex/PreprocessorOptions.h"
26
#include "clang/Parse/ParseAST.h"
27
#include "clang/Sema/HLSLExternalSemaSource.h"
28
#include "clang/Serialization/ASTDeserializationListener.h"
29
#include "clang/Serialization/ASTReader.h"
30
#include "clang/Serialization/GlobalModuleIndex.h"
31
#include "llvm/ADT/ScopeExit.h"
32
#include "llvm/Support/BuryPointer.h"
33
#include "llvm/Support/ErrorHandling.h"
34
#include "llvm/Support/FileSystem.h"
35
#include "llvm/Support/Path.h"
36
#include "llvm/Support/Timer.h"
37
#include "llvm/Support/raw_ostream.h"
38
#include <system_error>
39
using namespace clang;
40
41
LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry)
42
43
namespace {
44
45
class DelegatingDeserializationListener : public ASTDeserializationListener {
46
  ASTDeserializationListener *Previous;
47
  bool DeletePrevious;
48
49
public:
50
  explicit DelegatingDeserializationListener(
51
      ASTDeserializationListener *Previous, bool DeletePrevious)
52
106
      : Previous(Previous), DeletePrevious(DeletePrevious) {}
53
104
  ~DelegatingDeserializationListener() override {
54
104
    if (DeletePrevious)
55
0
      delete Previous;
56
104
  }
57
58
212
  void ReaderInitialized(ASTReader *Reader) override {
59
212
    if (Previous)
60
26
      Previous->ReaderInitialized(Reader);
61
212
  }
62
  void IdentifierRead(serialization::IdentID ID,
63
7.65k
                      IdentifierInfo *II) override {
64
7.65k
    if (Previous)
65
261
      Previous->IdentifierRead(ID, II);
66
7.65k
  }
67
1.13k
  void TypeRead(serialization::TypeIdx Idx, QualType T) override {
68
1.13k
    if (Previous)
69
28
      Previous->TypeRead(Idx, T);
70
1.13k
  }
71
1.27k
  void DeclRead(serialization::DeclID ID, const Decl *D) override {
72
1.27k
    if (Previous)
73
15
      Previous->DeclRead(ID, D);
74
1.27k
  }
75
0
  void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
76
0
    if (Previous)
77
0
      Previous->SelectorRead(ID, Sel);
78
0
  }
79
  void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
80
99
                           MacroDefinitionRecord *MD) override {
81
99
    if (Previous)
82
12
      Previous->MacroDefinitionRead(PPID, MD);
83
99
  }
84
};
85
86
/// Dumps deserialized declarations.
87
class DeserializedDeclsDumper : public DelegatingDeserializationListener {
88
public:
89
  explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
90
                                   bool DeletePrevious)
91
0
      : DelegatingDeserializationListener(Previous, DeletePrevious) {}
92
93
0
  void DeclRead(serialization::DeclID ID, const Decl *D) override {
94
0
    llvm::outs() << "PCH DECL: " << D->getDeclKindName();
95
0
    if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
96
0
      llvm::outs() << " - ";
97
0
      ND->printQualifiedName(llvm::outs());
98
0
    }
99
0
    llvm::outs() << "\n";
100
101
0
    DelegatingDeserializationListener::DeclRead(ID, D);
102
0
  }
103
};
104
105
/// Checks deserialized declarations and emits error if a name
106
/// matches one given in command-line using -error-on-deserialized-decl.
107
class DeserializedDeclsChecker : public DelegatingDeserializationListener {
108
  ASTContext &Ctx;
109
  std::set<std::string> NamesToCheck;
110
111
public:
112
  DeserializedDeclsChecker(ASTContext &Ctx,
113
                           const std::set<std::string> &NamesToCheck,
114
                           ASTDeserializationListener *Previous,
115
                           bool DeletePrevious)
116
      : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
117
106
        NamesToCheck(NamesToCheck) {}
118
119
1.27k
  void DeclRead(serialization::DeclID ID, const Decl *D) override {
120
1.27k
    if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
121
1.15k
      if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
122
2
        unsigned DiagID
123
2
          = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
124
2
                                                 "%0 was deserialized");
125
2
        Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
126
2
            << ND;
127
2
      }
128
129
1.27k
    DelegatingDeserializationListener::DeclRead(ID, D);
130
1.27k
  }
131
};
132
133
} // end anonymous namespace
134
135
75.2k
FrontendAction::FrontendAction() : Instance(nullptr) {}
136
137
69.0k
FrontendAction::~FrontendAction() {}
138
139
void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
140
148k
                                     std::unique_ptr<ASTUnit> AST) {
141
148k
  this->CurrentInput = CurrentInput;
142
148k
  CurrentASTUnit = std::move(AST);
143
148k
}
144
145
19
Module *FrontendAction::getCurrentModule() const {
146
19
  CompilerInstance &CI = getCompilerInstance();
147
19
  return CI.getPreprocessor().getHeaderSearchInfo().lookupModule(
148
19
      CI.getLangOpts().CurrentModule, SourceLocation(), /*AllowSearch*/false);
149
19
}
150
151
std::unique_ptr<ASTConsumer>
152
FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
153
72.4k
                                         StringRef InFile) {
154
72.4k
  std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
155
72.4k
  if (!Consumer)
156
3
    return nullptr;
157
158
  // Validate -add-plugin args.
159
72.4k
  bool FoundAllPlugins = true;
160
72.4k
  for (const std::string &Arg : CI.getFrontendOpts().AddPluginActions) {
161
1
    bool Found = false;
162
1
    for (const FrontendPluginRegistry::entry &Plugin :
163
1
         FrontendPluginRegistry::entries()) {
164
0
      if (Plugin.getName() == Arg)
165
0
        Found = true;
166
0
    }
167
1
    if (!Found) {
168
1
      CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) << Arg;
169
1
      FoundAllPlugins = false;
170
1
    }
171
1
  }
172
72.4k
  if (!FoundAllPlugins)
173
1
    return nullptr;
174
175
  // If there are no registered plugins we don't need to wrap the consumer
176
72.4k
  if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end())
177
72.4k
    return Consumer;
178
179
  // If this is a code completion run, avoid invoking the plugin consumers
180
1
  if (CI.hasCodeCompletionConsumer())
181
0
    return Consumer;
182
183
  // Collect the list of plugins that go before the main action (in Consumers)
184
  // or after it (in AfterConsumers)
185
1
  std::vector<std::unique_ptr<ASTConsumer>> Consumers;
186
1
  std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers;
187
1
  for (const FrontendPluginRegistry::entry &Plugin :
188
1
       FrontendPluginRegistry::entries()) {
189
0
    std::unique_ptr<PluginASTAction> P = Plugin.instantiate();
190
0
    PluginASTAction::ActionType ActionType = P->getActionType();
191
0
    if (ActionType == PluginASTAction::CmdlineAfterMainAction ||
192
0
        ActionType == PluginASTAction::CmdlineBeforeMainAction) {
193
      // This is O(|plugins| * |add_plugins|), but since both numbers are
194
      // way below 50 in practice, that's ok.
195
0
      if (llvm::any_of(CI.getFrontendOpts().AddPluginActions,
196
0
                       [&](const std::string &PluginAction) {
197
0
                         return PluginAction == Plugin.getName();
198
0
                       })) {
199
0
        if (ActionType == PluginASTAction::CmdlineBeforeMainAction)
200
0
          ActionType = PluginASTAction::AddBeforeMainAction;
201
0
        else
202
0
          ActionType = PluginASTAction::AddAfterMainAction;
203
0
      }
204
0
    }
205
0
    if ((ActionType == PluginASTAction::AddBeforeMainAction ||
206
0
         ActionType == PluginASTAction::AddAfterMainAction) &&
207
0
        P->ParseArgs(
208
0
            CI,
209
0
            CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())])) {
210
0
      std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile);
211
0
      if (ActionType == PluginASTAction::AddBeforeMainAction) {
212
0
        Consumers.push_back(std::move(PluginConsumer));
213
0
      } else {
214
0
        AfterConsumers.push_back(std::move(PluginConsumer));
215
0
      }
216
0
    }
217
0
  }
218
219
  // Add to Consumers the main consumer, then all the plugins that go after it
220
1
  Consumers.push_back(std::move(Consumer));
221
1
  if (!AfterConsumers.empty()) {
222
    // If we have plugins after the main consumer, which may be the codegen
223
    // action, they likely will need the ASTContext, so don't clear it in the
224
    // codegen action.
225
0
    CI.getCodeGenOpts().ClearASTBeforeBackend = false;
226
0
    for (auto &C : AfterConsumers)
227
0
      Consumers.push_back(std::move(C));
228
0
  }
229
230
1
  return std::make_unique<MultiplexConsumer>(std::move(Consumers));
231
1
}
232
233
/// For preprocessed files, if the first line is the linemarker and specifies
234
/// the original source file name, use that name as the input file name.
235
/// Returns the location of the first token after the line marker directive.
236
///
237
/// \param CI The compiler instance.
238
/// \param InputFile Populated with the filename from the line marker.
239
/// \param IsModuleMap If \c true, add a line note corresponding to this line
240
///        directive. (We need to do this because the directive will not be
241
///        visited by the preprocessor.)
242
static SourceLocation ReadOriginalFileName(CompilerInstance &CI,
243
                                           std::string &InputFile,
244
180
                                           bool IsModuleMap = false) {
245
180
  auto &SourceMgr = CI.getSourceManager();
246
180
  auto MainFileID = SourceMgr.getMainFileID();
247
248
180
  auto MainFileBuf = SourceMgr.getBufferOrNone(MainFileID);
249
180
  if (!MainFileBuf)
250
0
    return SourceLocation();
251
252
180
  std::unique_ptr<Lexer> RawLexer(
253
180
      new Lexer(MainFileID, *MainFileBuf, SourceMgr, CI.getLangOpts()));
254
255
  // If the first line has the syntax of
256
  //
257
  // # NUM "FILENAME"
258
  //
259
  // we use FILENAME as the input file name.
260
180
  Token T;
261
180
  if (RawLexer->LexFromRawLexer(T) || 
T.getKind() != tok::hash178
)
262
137
    return SourceLocation();
263
43
  if (RawLexer->LexFromRawLexer(T) || T.isAtStartOfLine() ||
264
43
      T.getKind() != tok::numeric_constant)
265
4
    return SourceLocation();
266
267
39
  unsigned LineNo;
268
39
  SourceLocation LineNoLoc = T.getLocation();
269
39
  if (IsModuleMap) {
270
16
    llvm::SmallString<16> Buffer;
271
16
    if (Lexer::getSpelling(LineNoLoc, Buffer, SourceMgr, CI.getLangOpts())
272
16
            .getAsInteger(10, LineNo))
273
0
      return SourceLocation();
274
16
  }
275
276
39
  RawLexer->LexFromRawLexer(T);
277
39
  if (T.isAtStartOfLine() || T.getKind() != tok::string_literal)
278
0
    return SourceLocation();
279
280
39
  StringLiteralParser Literal(T, CI.getPreprocessor());
281
39
  if (Literal.hadError)
282
0
    return SourceLocation();
283
39
  RawLexer->LexFromRawLexer(T);
284
39
  if (T.isNot(tok::eof) && !T.isAtStartOfLine())
285
0
    return SourceLocation();
286
39
  InputFile = Literal.GetString().str();
287
288
39
  if (IsModuleMap)
289
16
    CI.getSourceManager().AddLineNote(
290
16
        LineNoLoc, LineNo, SourceMgr.getLineTableFilenameID(InputFile), false,
291
16
        false, SrcMgr::C_User_ModuleMap);
292
293
39
  return T.getLocation();
294
39
}
295
296
static SmallVectorImpl<char> &
297
82.7k
operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
298
82.7k
  Includes.append(RHS.begin(), RHS.end());
299
82.7k
  return Includes;
300
82.7k
}
301
302
static void addHeaderInclude(StringRef HeaderName,
303
                             SmallVectorImpl<char> &Includes,
304
                             const LangOptions &LangOpts,
305
22.9k
                             bool IsExternC) {
306
22.9k
  if (IsExternC && 
LangOpts.CPlusPlus12.6k
)
307
6.98k
    Includes += "extern \"C\" {\n";
308
22.9k
  if (LangOpts.ObjC)
309
7.48k
    Includes += "#import \"";
310
15.4k
  else
311
15.4k
    Includes += "#include \"";
312
313
22.9k
  Includes += HeaderName;
314
315
22.9k
  Includes += "\"\n";
316
22.9k
  if (IsExternC && 
LangOpts.CPlusPlus12.6k
)
317
6.98k
    Includes += "}\n";
318
22.9k
}
319
320
/// Collect the set of header includes needed to construct the given
321
/// module and update the TopHeaders file set of the module.
322
///
323
/// \param Module The module we're collecting includes from.
324
///
325
/// \param Includes Will be augmented with the set of \#includes or \#imports
326
/// needed to load all of the named headers.
327
static std::error_code collectModuleHeaderIncludes(
328
    const LangOptions &LangOpts, FileManager &FileMgr, DiagnosticsEngine &Diag,
329
20.4k
    ModuleMap &ModMap, clang::Module *Module, SmallVectorImpl<char> &Includes) {
330
  // Don't collect any headers for unavailable modules.
331
20.4k
  if (!Module->isAvailable())
332
396
    return std::error_code();
333
334
  // Resolve all lazy header directives to header files.
335
20.0k
  ModMap.resolveHeaderDirectives(Module, /*File=*/llvm::None);
336
337
  // If any headers are missing, we can't build this module. In most cases,
338
  // diagnostics for this should have already been produced; we only get here
339
  // if explicit stat information was provided.
340
  // FIXME: If the name resolves to a file with different stat information,
341
  // produce a better diagnostic.
342
20.0k
  if (!Module->MissingHeaders.empty()) {
343
1
    auto &MissingHeader = Module->MissingHeaders.front();
344
1
    Diag.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
345
1
      << MissingHeader.IsUmbrella << MissingHeader.FileName;
346
1
    return std::error_code();
347
1
  }
348
349
  // Add includes for each of these headers.
350
40.0k
  
for (auto HK : {Module::HK_Normal, Module::HK_Private})20.0k
{
351
40.0k
    for (Module::Header &H : Module->Headers[HK]) {
352
18.8k
      Module->addTopHeader(H.Entry);
353
      // Use the path as specified in the module map file. We'll look for this
354
      // file relative to the module build directory (the directory containing
355
      // the module map file) so this will find the same file that we found
356
      // while parsing the module map.
357
18.8k
      addHeaderInclude(H.PathRelativeToRootModuleDirectory, Includes, LangOpts,
358
18.8k
                       Module->IsExternC);
359
18.8k
    }
360
40.0k
  }
361
  // Note that Module->PrivateHeaders will not be a TopHeader.
362
363
20.0k
  if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) {
364
520
    Module->addTopHeader(UmbrellaHeader.Entry);
365
520
    if (Module->Parent)
366
      // Include the umbrella header for submodules.
367
241
      addHeaderInclude(UmbrellaHeader.PathRelativeToRootModuleDirectory,
368
241
                       Includes, LangOpts, Module->IsExternC);
369
19.5k
  } else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
370
    // Add all of the headers we find in this subdirectory.
371
217
    std::error_code EC;
372
217
    SmallString<128> DirNative;
373
217
    llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
374
375
217
    llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
376
217
    SmallVector<std::pair<std::string, const FileEntry *>, 8> Headers;
377
217
    for (llvm::vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
378
3.81k
         Dir != End && 
!EC3.59k
;
Dir.increment(EC)3.59k
) {
379
      // Check whether this entry has an extension typically associated with
380
      // headers.
381
3.59k
      if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
382
3.59k
               .Cases(".h", ".H", ".hh", ".hpp", true)
383
3.59k
               .Default(false))
384
42
        continue;
385
386
3.55k
      auto Header = FileMgr.getFile(Dir->path());
387
      // FIXME: This shouldn't happen unless there is a file system race. Is
388
      // that worth diagnosing?
389
3.55k
      if (!Header)
390
1
        continue;
391
392
      // If this header is marked 'unavailable' in this module, don't include
393
      // it.
394
3.55k
      if (ModMap.isHeaderUnavailableInModule(*Header, Module))
395
8
        continue;
396
397
      // Compute the relative path from the directory to this file.
398
3.54k
      SmallVector<StringRef, 16> Components;
399
3.54k
      auto PathIt = llvm::sys::path::rbegin(Dir->path());
400
7.10k
      for (int I = 0; I != Dir.level() + 1; 
++I, ++PathIt3.55k
)
401
3.55k
        Components.push_back(*PathIt);
402
3.54k
      SmallString<128> RelativeHeader(
403
3.54k
          UmbrellaDir.PathRelativeToRootModuleDirectory);
404
7.10k
      for (auto It = Components.rbegin(), End = Components.rend(); It != End;
405
3.55k
           ++It)
406
3.55k
        llvm::sys::path::append(RelativeHeader, *It);
407
408
3.54k
      std::string RelName = RelativeHeader.c_str();
409
3.54k
      Headers.push_back(std::make_pair(RelName, *Header));
410
3.54k
    }
411
412
217
    if (EC)
413
0
      return EC;
414
415
    // Sort header paths and make the header inclusion order deterministic
416
    // across different OSs and filesystems.
417
217
    llvm::sort(Headers.begin(), Headers.end(), llvm::less_first());
418
3.54k
    for (auto &H : Headers) {
419
      // Include this header as part of the umbrella directory.
420
3.54k
      Module->addTopHeader(H.second);
421
3.54k
      addHeaderInclude(H.first, Includes, LangOpts, Module->IsExternC);
422
3.54k
    }
423
217
  }
424
425
  // Recurse into submodules.
426
20.0k
  for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
427
20.0k
                                      SubEnd = Module->submodule_end();
428
38.5k
       Sub != SubEnd; 
++Sub18.4k
)
429
18.4k
    if (std::error_code Err = collectModuleHeaderIncludes(
430
18.4k
            LangOpts, FileMgr, Diag, ModMap, *Sub, Includes))
431
0
      return Err;
432
433
20.0k
  return std::error_code();
434
20.0k
}
435
436
static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
437
                                        bool IsPreprocessed,
438
                                        std::string &PresumedModuleMapFile,
439
2.06k
                                        unsigned &Offset) {
440
2.06k
  auto &SrcMgr = CI.getSourceManager();
441
2.06k
  HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
442
443
  // Map the current input to a file.
444
2.06k
  FileID ModuleMapID = SrcMgr.getMainFileID();
445
2.06k
  const FileEntry *ModuleMap = SrcMgr.getFileEntryForID(ModuleMapID);
446
447
  // If the module map is preprocessed, handle the initial line marker;
448
  // line directives are not part of the module map syntax in general.
449
2.06k
  Offset = 0;
450
2.06k
  if (IsPreprocessed) {
451
82
    SourceLocation EndOfLineMarker =
452
82
        ReadOriginalFileName(CI, PresumedModuleMapFile, /*IsModuleMap*/ true);
453
82
    if (EndOfLineMarker.isValid())
454
16
      Offset = CI.getSourceManager().getDecomposedLoc(EndOfLineMarker).second;
455
82
  }
456
457
  // Load the module map file.
458
2.06k
  if (HS.loadModuleMapFile(ModuleMap, IsSystem, ModuleMapID, &Offset,
459
2.06k
                           PresumedModuleMapFile))
460
0
    return true;
461
462
2.06k
  if (SrcMgr.getBufferOrFake(ModuleMapID).getBufferSize() == Offset)
463
1.97k
    Offset = 0;
464
465
  // Infer framework module if possible.
466
2.06k
  if (HS.getModuleMap().canInferFrameworkModule(ModuleMap->getDir())) {
467
289
    SmallString<128> InferredFrameworkPath = ModuleMap->getDir()->getName();
468
289
    llvm::sys::path::append(InferredFrameworkPath,
469
289
                            CI.getLangOpts().ModuleName + ".framework");
470
289
    if (auto Dir = CI.getFileManager().getDirectory(InferredFrameworkPath))
471
2
      (void)HS.getModuleMap().inferFrameworkModule(*Dir, IsSystem, nullptr);
472
289
  }
473
474
2.06k
  return false;
475
2.06k
}
476
477
static Module *prepareToBuildModule(CompilerInstance &CI,
478
2.06k
                                    StringRef ModuleMapFilename) {
479
2.06k
  if (CI.getLangOpts().CurrentModule.empty()) {
480
0
    CI.getDiagnostics().Report(diag::err_missing_module_name);
481
482
    // FIXME: Eventually, we could consider asking whether there was just
483
    // a single module described in the module map, and use that as a
484
    // default. Then it would be fairly trivial to just "compile" a module
485
    // map with a single module (the common case).
486
0
    return nullptr;
487
0
  }
488
489
  // Dig out the module definition.
490
2.06k
  HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
491
2.06k
  Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule, SourceLocation(),
492
2.06k
                              /*AllowSearch=*/true);
493
2.06k
  if (!M) {
494
2
    CI.getDiagnostics().Report(diag::err_missing_module)
495
2
      << CI.getLangOpts().CurrentModule << ModuleMapFilename;
496
497
2
    return nullptr;
498
2
  }
499
500
  // Check whether we can build this module at all.
501
2.05k
  if (Preprocessor::checkModuleIsAvailable(CI.getLangOpts(), CI.getTarget(),
502
2.05k
                                           CI.getDiagnostics(), M))
503
3
    return nullptr;
504
505
  // Inform the preprocessor that includes from within the input buffer should
506
  // be resolved relative to the build directory of the module map file.
507
2.05k
  CI.getPreprocessor().setMainFileDir(M->Directory);
508
509
  // If the module was inferred from a different module map (via an expanded
510
  // umbrella module definition), track that fact.
511
  // FIXME: It would be preferable to fill this in as part of processing
512
  // the module map, rather than adding it after the fact.
513
2.05k
  StringRef OriginalModuleMapName = CI.getFrontendOpts().OriginalModuleMap;
514
2.05k
  if (!OriginalModuleMapName.empty()) {
515
1.71k
    auto OriginalModuleMap =
516
1.71k
        CI.getFileManager().getFile(OriginalModuleMapName,
517
1.71k
                                    /*openFile*/ true);
518
1.71k
    if (!OriginalModuleMap) {
519
0
      CI.getDiagnostics().Report(diag::err_module_map_not_found)
520
0
        << OriginalModuleMapName;
521
0
      return nullptr;
522
0
    }
523
1.71k
    if (*OriginalModuleMap != CI.getSourceManager().getFileEntryForID(
524
1.71k
                                 CI.getSourceManager().getMainFileID())) {
525
110
      M->IsInferred = true;
526
110
      CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()
527
110
        .setInferredModuleAllowedBy(M, *OriginalModuleMap);
528
110
    }
529
1.71k
  }
530
531
  // If we're being run from the command-line, the module build stack will not
532
  // have been filled in yet, so complete it now in order to allow us to detect
533
  // module cycles.
534
2.05k
  SourceManager &SourceMgr = CI.getSourceManager();
535
2.05k
  if (SourceMgr.getModuleBuildStack().empty())
536
266
    SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule,
537
266
                                   FullSourceLoc(SourceLocation(), SourceMgr));
538
2.05k
  return M;
539
2.05k
}
540
541
/// Compute the input buffer that should be used to build the specified module.
542
static std::unique_ptr<llvm::MemoryBuffer>
543
1.96k
getInputBufferForModule(CompilerInstance &CI, Module *M) {
544
1.96k
  FileManager &FileMgr = CI.getFileManager();
545
546
  // Collect the set of #includes we need to build the module.
547
1.96k
  SmallString<256> HeaderContents;
548
1.96k
  std::error_code Err = std::error_code();
549
1.96k
  if (Module::Header UmbrellaHeader = M->getUmbrellaHeader())
550
279
    addHeaderInclude(UmbrellaHeader.PathRelativeToRootModuleDirectory,
551
279
                     HeaderContents, CI.getLangOpts(), M->IsExternC);
552
1.96k
  Err = collectModuleHeaderIncludes(
553
1.96k
      CI.getLangOpts(), FileMgr, CI.getDiagnostics(),
554
1.96k
      CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), M,
555
1.96k
      HeaderContents);
556
557
1.96k
  if (Err) {
558
0
    CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
559
0
      << M->getFullModuleName() << Err.message();
560
0
    return nullptr;
561
0
  }
562
563
1.96k
  return llvm::MemoryBuffer::getMemBufferCopy(
564
1.96k
      HeaderContents, Module::getModuleInputBufferName());
565
1.96k
}
566
567
bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
568
75.0k
                                     const FrontendInputFile &RealInput) {
569
75.0k
  FrontendInputFile Input(RealInput);
570
75.0k
  assert(!Instance && "Already processing a source file!");
571
0
  assert(!Input.isEmpty() && "Unexpected empty filename!");
572
0
  setCurrentInput(Input);
573
75.0k
  setCompilerInstance(&CI);
574
575
75.0k
  bool HasBegunSourceFile = false;
576
75.0k
  bool ReplayASTFile = Input.getKind().getFormat() == InputKind::Precompiled &&
577
75.0k
                       
usesPreprocessorOnly()67
;
578
579
  // If we fail, reset state since the client will not end up calling the
580
  // matching EndSourceFile(). All paths that return true should release this.
581
75.0k
  auto FailureCleanup = llvm::make_scope_exit([&]() {
582
68
    if (HasBegunSourceFile)
583
53
      CI.getDiagnosticClient().EndSourceFile();
584
68
    CI.setASTConsumer(nullptr);
585
68
    CI.clearOutputFiles(/*EraseFiles=*/true);
586
68
    CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
587
68
    setCurrentInput(FrontendInputFile());
588
68
    setCompilerInstance(nullptr);
589
68
  });
590
591
75.0k
  if (!BeginInvocation(CI))
592
12
    return false;
593
594
  // If we're replaying the build of an AST file, import it and set up
595
  // the initial state from its build.
596
74.9k
  if (ReplayASTFile) {
597
15
    IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
598
599
    // The AST unit populates its own diagnostics engine rather than ours.
600
15
    IntrusiveRefCntPtr<DiagnosticsEngine> ASTDiags(
601
15
        new DiagnosticsEngine(Diags->getDiagnosticIDs(),
602
15
                              &Diags->getDiagnosticOptions()));
603
15
    ASTDiags->setClient(Diags->getClient(), /*OwnsClient*/false);
604
605
    // FIXME: What if the input is a memory buffer?
606
15
    StringRef InputFile = Input.getFile();
607
608
15
    std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
609
15
        std::string(InputFile), CI.getPCHContainerReader(),
610
15
        ASTUnit::LoadPreprocessorOnly, ASTDiags, CI.getFileSystemOpts(),
611
15
        CI.getCodeGenOpts().DebugTypeExtRefs);
612
15
    if (!AST)
613
0
      return false;
614
615
    // Options relating to how we treat the input (but not what we do with it)
616
    // are inherited from the AST unit.
617
15
    CI.getHeaderSearchOpts() = AST->getHeaderSearchOpts();
618
15
    CI.getPreprocessorOpts() = AST->getPreprocessorOpts();
619
15
    CI.getLangOpts() = AST->getLangOpts();
620
621
    // Set the shared objects, these are reset when we finish processing the
622
    // file, otherwise the CompilerInstance will happily destroy them.
623
15
    CI.setFileManager(&AST->getFileManager());
624
15
    CI.createSourceManager(CI.getFileManager());
625
15
    CI.getSourceManager().initializeForReplay(AST->getSourceManager());
626
627
    // Preload all the module files loaded transitively by the AST unit. Also
628
    // load all module map files that were parsed as part of building the AST
629
    // unit.
630
15
    if (auto ASTReader = AST->getASTReader()) {
631
15
      auto &MM = ASTReader->getModuleManager();
632
15
      auto &PrimaryModule = MM.getPrimaryModule();
633
634
15
      for (serialization::ModuleFile &MF : MM)
635
25
        if (&MF != &PrimaryModule)
636
10
          CI.getFrontendOpts().ModuleFiles.push_back(MF.FileName);
637
638
15
      ASTReader->visitTopLevelModuleMaps(
639
15
          PrimaryModule, [&](const FileEntry *FE) {
640
14
            CI.getFrontendOpts().ModuleMapFiles.push_back(
641
14
                std::string(FE->getName()));
642
14
          });
643
15
    }
644
645
    // Set up the input file for replay purposes.
646
15
    auto Kind = AST->getInputKind();
647
15
    if (Kind.getFormat() == InputKind::ModuleMap) {
648
13
      Module *ASTModule =
649
13
          AST->getPreprocessor().getHeaderSearchInfo().lookupModule(
650
13
              AST->getLangOpts().CurrentModule, SourceLocation(),
651
13
              /*AllowSearch*/ false);
652
13
      assert(ASTModule && "module file does not define its own module");
653
0
      Input = FrontendInputFile(ASTModule->PresumedModuleMapFile, Kind);
654
13
    } else {
655
2
      auto &OldSM = AST->getSourceManager();
656
2
      FileID ID = OldSM.getMainFileID();
657
2
      if (auto *File = OldSM.getFileEntryForID(ID))
658
0
        Input = FrontendInputFile(File->getName(), Kind);
659
2
      else
660
2
        Input = FrontendInputFile(OldSM.getBufferOrFake(ID), Kind);
661
2
    }
662
0
    setCurrentInput(Input, std::move(AST));
663
15
  }
664
665
  // AST files follow a very different path, since they share objects via the
666
  // AST unit.
667
74.9k
  if (Input.getKind().getFormat() == InputKind::Precompiled) {
668
52
    assert(!usesPreprocessorOnly() && "this case was handled above");
669
0
    assert(hasASTFileSupport() &&
670
52
           "This action does not have AST file support!");
671
672
0
    IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
673
674
    // FIXME: What if the input is a memory buffer?
675
52
    StringRef InputFile = Input.getFile();
676
677
52
    std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
678
52
        std::string(InputFile), CI.getPCHContainerReader(),
679
52
        ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts(),
680
52
        CI.getCodeGenOpts().DebugTypeExtRefs);
681
682
52
    if (!AST)
683
0
      return false;
684
685
    // Inform the diagnostic client we are processing a source file.
686
52
    CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
687
52
    HasBegunSourceFile = true;
688
689
    // Set the shared objects, these are reset when we finish processing the
690
    // file, otherwise the CompilerInstance will happily destroy them.
691
52
    CI.setFileManager(&AST->getFileManager());
692
52
    CI.setSourceManager(&AST->getSourceManager());
693
52
    CI.setPreprocessor(AST->getPreprocessorPtr());
694
52
    Preprocessor &PP = CI.getPreprocessor();
695
52
    PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
696
52
                                           PP.getLangOpts());
697
52
    CI.setASTContext(&AST->getASTContext());
698
699
52
    setCurrentInput(Input, std::move(AST));
700
701
    // Initialize the action.
702
52
    if (!BeginSourceFileAction(CI))
703
0
      return false;
704
705
    // Create the AST consumer.
706
52
    CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
707
52
    if (!CI.hasASTConsumer())
708
0
      return false;
709
710
52
    FailureCleanup.release();
711
52
    return true;
712
52
  }
713
714
  // Set up the file and source managers, if needed.
715
74.9k
  if (!CI.hasFileManager()) {
716
41.2k
    if (!CI.createFileManager()) {
717
0
      return false;
718
0
    }
719
41.2k
  }
720
74.9k
  if (!CI.hasSourceManager())
721
42.7k
    CI.createSourceManager(CI.getFileManager());
722
723
  // Set up embedding for any specified files. Do this before we load any
724
  // source files, including the primary module map for the compilation.
725
74.9k
  for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) {
726
3
    if (auto FE = CI.getFileManager().getFile(F, /*openFile*/true))
727
2
      CI.getSourceManager().setFileIsTransient(*FE);
728
1
    else
729
1
      CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F;
730
3
  }
731
74.9k
  if (CI.getFrontendOpts().ModulesEmbedAllFiles)
732
10
    CI.getSourceManager().setAllFilesAreTransient(true);
733
734
  // IR files bypass the rest of initialization.
735
74.9k
  if (Input.getKind().getLanguage() == Language::LLVM_IR) {
736
106
    assert(hasIRSupport() &&
737
106
           "This action does not have IR file support!");
738
739
    // Inform the diagnostic client we are processing a source file.
740
0
    CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
741
106
    HasBegunSourceFile = true;
742
743
    // Initialize the action.
744
106
    if (!BeginSourceFileAction(CI))
745
0
      return false;
746
747
    // Initialize the main file entry.
748
106
    if (!CI.InitializeSourceManager(CurrentInput))
749
0
      return false;
750
751
106
    FailureCleanup.release();
752
106
    return true;
753
106
  }
754
755
  // If the implicit PCH include is actually a directory, rather than
756
  // a single file, search for a suitable PCH file in that directory.
757
74.8k
  if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
758
3.71k
    FileManager &FileMgr = CI.getFileManager();
759
3.71k
    PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
760
3.71k
    StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
761
3.71k
    std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath();
762
3.71k
    if (auto PCHDir = FileMgr.getOptionalDirectoryRef(PCHInclude)) {
763
6
      std::error_code EC;
764
6
      SmallString<128> DirNative;
765
6
      llvm::sys::path::native(PCHDir->getName(), DirNative);
766
6
      bool Found = false;
767
6
      llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
768
6
      for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC),
769
6
                                         DirEnd;
770
16
           Dir != DirEnd && 
!EC13
;
Dir.increment(EC)10
) {
771
        // Check whether this is an acceptable AST file.
772
13
        if (ASTReader::isAcceptableASTFile(
773
13
                Dir->path(), FileMgr, CI.getPCHContainerReader(),
774
13
                CI.getLangOpts(), CI.getTargetOpts(), CI.getPreprocessorOpts(),
775
13
                SpecificModuleCachePath)) {
776
3
          PPOpts.ImplicitPCHInclude = std::string(Dir->path());
777
3
          Found = true;
778
3
          break;
779
3
        }
780
13
      }
781
782
6
      if (!Found) {
783
3
        CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
784
3
        return false;
785
3
      }
786
6
    }
787
3.71k
  }
788
789
  // Set up the preprocessor if needed. When parsing model files the
790
  // preprocessor of the original source is reused.
791
74.8k
  if (!isModelParsingAction())
792
74.8k
    CI.createPreprocessor(getTranslationUnitKind());
793
794
  // Inform the diagnostic client we are processing a source file.
795
74.8k
  CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
796
74.8k
                                           &CI.getPreprocessor());
797
74.8k
  HasBegunSourceFile = true;
798
799
  // Handle C++20 header units.
800
  // Here, the user has the option to specify that the header name should be
801
  // looked up in the pre-processor search paths (and the main filename as
802
  // passed by the driver might therefore be incomplete until that look-up).
803
74.8k
  if (CI.getLangOpts().CPlusPlusModules && 
Input.getKind().isHeaderUnit()3.95k
&&
804
74.8k
      
!Input.getKind().isPreprocessed()19
) {
805
17
    StringRef FileName = Input.getFile();
806
17
    InputKind Kind = Input.getKind();
807
17
    if (Kind.getHeaderUnitKind() != InputKind::HeaderUnit_Abs) {
808
14
      assert(CI.hasPreprocessor() &&
809
14
             "trying to build a header unit without a Pre-processor?");
810
0
      HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
811
      // Relative searches begin from CWD.
812
14
      const DirectoryEntry *Dir = nullptr;
813
14
      if (auto DirOrErr = CI.getFileManager().getDirectory("."))
814
14
        Dir = *DirOrErr;
815
14
      SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 1> CWD;
816
14
      CWD.push_back({nullptr, Dir});
817
14
      Optional<FileEntryRef> FE =
818
14
          HS.LookupFile(FileName, SourceLocation(),
819
14
                        /*Angled*/ Input.getKind().getHeaderUnitKind() ==
820
14
                            InputKind::HeaderUnit_System,
821
14
                        nullptr, nullptr, CWD, nullptr, nullptr, nullptr,
822
14
                        nullptr, nullptr, nullptr);
823
14
      if (!FE) {
824
0
        CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
825
0
            << FileName;
826
0
        return false;
827
0
      }
828
      // We now have the filename...
829
14
      FileName = FE->getFileEntry().getName();
830
      // ... still a header unit, but now use the path as written.
831
14
      Kind = Input.getKind().withHeaderUnit(InputKind::HeaderUnit_Abs);
832
14
      Input = FrontendInputFile(FileName, Kind, Input.isSystem());
833
14
    }
834
    // Unless the user has overridden the name, the header unit module name is
835
    // the pathname for the file.
836
17
    if (CI.getLangOpts().ModuleName.empty())
837
17
      CI.getLangOpts().ModuleName = std::string(FileName);
838
17
    CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName;
839
17
  }
840
841
74.8k
  if (!CI.InitializeSourceManager(Input))
842
4
    return false;
843
844
74.8k
  if (CI.getLangOpts().CPlusPlusModules && 
Input.getKind().isHeaderUnit()3.95k
&&
845
74.8k
      
Input.getKind().isPreprocessed()19
&&
!usesPreprocessorOnly()2
) {
846
    // We have an input filename like foo.iih, but we want to find the right
847
    // module name (and original file, to build the map entry).
848
    // Check if the first line specifies the original source file name with a
849
    // linemarker.
850
2
    std::string PresumedInputFile = std::string(getCurrentFileOrBufferName());
851
2
    ReadOriginalFileName(CI, PresumedInputFile);
852
    // Unless the user overrides this, the module name is the name by which the
853
    // original file was known.
854
2
    if (CI.getLangOpts().ModuleName.empty())
855
2
      CI.getLangOpts().ModuleName = std::string(PresumedInputFile);
856
2
    CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName;
857
2
  }
858
859
  // For module map files, we first parse the module map and synthesize a
860
  // "<module-includes>" buffer before more conventional processing.
861
74.8k
  if (Input.getKind().getFormat() == InputKind::ModuleMap) {
862
2.06k
    CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleMap);
863
864
2.06k
    std::string PresumedModuleMapFile;
865
2.06k
    unsigned OffsetToContents;
866
2.06k
    if (loadModuleMapForModuleBuild(CI, Input.isSystem(),
867
2.06k
                                    Input.isPreprocessed(),
868
2.06k
                                    PresumedModuleMapFile, OffsetToContents))
869
0
      return false;
870
871
2.06k
    auto *CurrentModule = prepareToBuildModule(CI, Input.getFile());
872
2.06k
    if (!CurrentModule)
873
5
      return false;
874
875
2.05k
    CurrentModule->PresumedModuleMapFile = PresumedModuleMapFile;
876
877
2.05k
    if (OffsetToContents)
878
      // If the module contents are in the same file, skip to them.
879
87
      CI.getPreprocessor().setSkipMainFilePreamble(OffsetToContents, true);
880
1.96k
    else {
881
      // Otherwise, convert the module description to a suitable input buffer.
882
1.96k
      auto Buffer = getInputBufferForModule(CI, CurrentModule);
883
1.96k
      if (!Buffer)
884
0
        return false;
885
886
      // Reinitialize the main file entry to refer to the new input.
887
1.96k
      auto Kind = CurrentModule->IsSystem ? 
SrcMgr::C_System355
:
SrcMgr::C_User1.61k
;
888
1.96k
      auto &SourceMgr = CI.getSourceManager();
889
1.96k
      auto BufferID = SourceMgr.createFileID(std::move(Buffer), Kind);
890
1.96k
      assert(BufferID.isValid() && "couldn't create module buffer ID");
891
0
      SourceMgr.setMainFileID(BufferID);
892
1.96k
    }
893
2.05k
  }
894
895
  // Initialize the action.
896
74.8k
  if (!BeginSourceFileAction(CI))
897
1
    return false;
898
899
  // If we were asked to load any module map files, do so now.
900
74.8k
  for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
901
292
    if (auto File = CI.getFileManager().getFile(Filename))
902
291
      CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
903
291
          *File, /*IsSystem*/false);
904
1
    else
905
1
      CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
906
292
  }
907
908
  // Add a module declaration scope so that modules from -fmodule-map-file
909
  // arguments may shadow modules found implicitly in search paths.
910
74.8k
  CI.getPreprocessor()
911
74.8k
      .getHeaderSearchInfo()
912
74.8k
      .getModuleMap()
913
74.8k
      .finishModuleDeclarationScope();
914
915
  // Create the AST context and consumer unless this is a preprocessor only
916
  // action.
917
74.8k
  if (!usesPreprocessorOnly()) {
918
    // Parsing a model file should reuse the existing ASTContext.
919
72.3k
    if (!isModelParsingAction())
920
72.3k
      CI.createASTContext();
921
922
    // For preprocessed files, check if the first line specifies the original
923
    // source file name with a linemarker.
924
72.3k
    std::string PresumedInputFile = std::string(getCurrentFileOrBufferName());
925
72.3k
    if (Input.isPreprocessed())
926
96
      ReadOriginalFileName(CI, PresumedInputFile);
927
928
72.3k
    std::unique_ptr<ASTConsumer> Consumer =
929
72.3k
        CreateWrappedASTConsumer(CI, PresumedInputFile);
930
72.3k
    if (!Consumer)
931
4
      return false;
932
933
    // FIXME: should not overwrite ASTMutationListener when parsing model files?
934
72.3k
    if (!isModelParsingAction())
935
72.3k
      CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
936
937
72.3k
    if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
938
      // Convert headers to PCH and chain them.
939
27
      IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
940
27
      source = createChainedIncludesSource(CI, FinalReader);
941
27
      if (!source)
942
0
        return false;
943
27
      CI.setASTReader(static_cast<ASTReader *>(FinalReader.get()));
944
27
      CI.getASTContext().setExternalSource(source);
945
72.3k
    } else if (CI.getLangOpts().Modules ||
946
72.3k
               
!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()62.7k
) {
947
      // Use PCM or PCH.
948
13.1k
      assert(hasPCHSupport() && "This action does not have PCH support!");
949
0
      ASTDeserializationListener *DeserialListener =
950
13.1k
          Consumer->GetASTDeserializationListener();
951
13.1k
      bool DeleteDeserialListener = false;
952
13.1k
      if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
953
0
        DeserialListener = new DeserializedDeclsDumper(DeserialListener,
954
0
                                                       DeleteDeserialListener);
955
0
        DeleteDeserialListener = true;
956
0
      }
957
13.1k
      if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
958
106
        DeserialListener = new DeserializedDeclsChecker(
959
106
            CI.getASTContext(),
960
106
            CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
961
106
            DeserialListener, DeleteDeserialListener);
962
106
        DeleteDeserialListener = true;
963
106
      }
964
13.1k
      if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
965
3.70k
        CI.createPCHExternalASTSource(
966
3.70k
            CI.getPreprocessorOpts().ImplicitPCHInclude,
967
3.70k
            CI.getPreprocessorOpts().DisablePCHOrModuleValidation,
968
3.70k
            CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
969
3.70k
            DeserialListener, DeleteDeserialListener);
970
3.70k
        if (!CI.getASTContext().getExternalSource())
971
28
          return false;
972
3.70k
      }
973
      // If modules are enabled, create the AST reader before creating
974
      // any builtins, so that all declarations know that they might be
975
      // extended by an external source.
976
13.1k
      if (CI.getLangOpts().Modules || 
!CI.hasASTContext()3.56k
||
977
13.1k
          
!CI.getASTContext().getExternalSource()3.56k
) {
978
9.54k
        CI.createASTReader();
979
9.54k
        CI.getASTReader()->setDeserializationListener(DeserialListener,
980
9.54k
                                                      DeleteDeserialListener);
981
9.54k
      }
982
13.1k
    }
983
984
72.3k
    CI.setASTConsumer(std::move(Consumer));
985
72.3k
    if (!CI.hasASTConsumer())
986
0
      return false;
987
72.3k
  }
988
989
  // Initialize built-in info as long as we aren't using an external AST
990
  // source.
991
74.7k
  if (CI.getLangOpts().Modules || 
!CI.hasASTContext()65.1k
||
992
74.7k
      
!CI.getASTContext().getExternalSource()62.7k
) {
993
71.1k
    Preprocessor &PP = CI.getPreprocessor();
994
71.1k
    PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
995
71.1k
                                           PP.getLangOpts());
996
71.1k
  } else {
997
    // FIXME: If this is a problem, recover from it by creating a multiplex
998
    // source.
999
3.59k
    assert((!CI.getLangOpts().Modules || CI.getASTReader()) &&
1000
3.59k
           "modules enabled but created an external source that "
1001
3.59k
           "doesn't support modules");
1002
3.59k
  }
1003
1004
  // If we were asked to load any module files, do so now.
1005
0
  for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
1006
533
    if (!CI.loadModuleFile(ModuleFile))
1007
11
      return false;
1008
1009
  // If there is a layout overrides file, attach an external AST source that
1010
  // provides the layouts from that file.
1011
74.7k
  if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
1012
74.7k
      
CI.hasASTContext()6
&&
!CI.getASTContext().getExternalSource()6
) {
1013
6
    IntrusiveRefCntPtr<ExternalASTSource>
1014
6
      Override(new LayoutOverrideSource(
1015
6
                     CI.getFrontendOpts().OverrideRecordLayoutsFile));
1016
6
    CI.getASTContext().setExternalSource(Override);
1017
6
  }
1018
1019
  // Setup HLSL External Sema Source
1020
74.7k
  if (CI.getLangOpts().HLSL && 
CI.hasASTContext()46
) {
1021
31
    IntrusiveRefCntPtr<ExternalASTSource> HLSLSema(
1022
31
        new HLSLExternalSemaSource());
1023
31
    CI.getASTContext().setExternalSource(HLSLSema);
1024
31
  }
1025
1026
74.7k
  FailureCleanup.release();
1027
74.7k
  return true;
1028
74.7k
}
1029
1030
73.5k
llvm::Error FrontendAction::Execute() {
1031
73.5k
  CompilerInstance &CI = getCompilerInstance();
1032
1033
73.5k
  if (CI.hasFrontendTimer()) {
1034
17
    llvm::TimeRegion Timer(CI.getFrontendTimer());
1035
17
    ExecuteAction();
1036
17
  }
1037
73.5k
  else ExecuteAction();
1038
1039
  // If we are supposed to rebuild the global module index, do so now unless
1040
  // there were any module-build failures.
1041
73.5k
  if (CI.shouldBuildGlobalModuleIndex() && 
CI.hasFileManager()905
&&
1042
73.5k
      
CI.hasPreprocessor()905
) {
1043
905
    StringRef Cache =
1044
905
        CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
1045
905
    if (!Cache.empty()) {
1046
790
      if (llvm::Error Err = GlobalModuleIndex::writeIndex(
1047
790
              CI.getFileManager(), CI.getPCHContainerReader(), Cache)) {
1048
        // FIXME this drops the error on the floor, but
1049
        // Index/pch-from-libclang.c seems to rely on dropping at least some of
1050
        // the error conditions!
1051
36
        consumeError(std::move(Err));
1052
36
      }
1053
790
    }
1054
905
  }
1055
1056
73.5k
  return llvm::Error::success();
1057
73.5k
}
1058
1059
73.4k
void FrontendAction::EndSourceFile() {
1060
73.4k
  CompilerInstance &CI = getCompilerInstance();
1061
1062
  // Inform the diagnostic client we are done with this source file.
1063
73.4k
  CI.getDiagnosticClient().EndSourceFile();
1064
1065
  // Inform the preprocessor we are done.
1066
73.4k
  if (CI.hasPreprocessor())
1067
73.3k
    CI.getPreprocessor().EndSourceFile();
1068
1069
  // Finalize the action.
1070
73.4k
  EndSourceFileAction();
1071
1072
  // Sema references the ast consumer, so reset sema first.
1073
  //
1074
  // FIXME: There is more per-file stuff we could just drop here?
1075
73.4k
  bool DisableFree = CI.getFrontendOpts().DisableFree;
1076
73.4k
  if (DisableFree) {
1077
6.08k
    CI.resetAndLeakSema();
1078
6.08k
    CI.resetAndLeakASTContext();
1079
6.08k
    llvm::BuryPointer(CI.takeASTConsumer().get());
1080
67.4k
  } else {
1081
67.4k
    CI.setSema(nullptr);
1082
67.4k
    CI.setASTContext(nullptr);
1083
67.4k
    CI.setASTConsumer(nullptr);
1084
67.4k
  }
1085
1086
73.4k
  if (CI.getFrontendOpts().ShowStats) {
1087
4
    llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFileOrBufferName() << "':\n";
1088
4
    CI.getPreprocessor().PrintStats();
1089
4
    CI.getPreprocessor().getIdentifierTable().PrintStats();
1090
4
    CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
1091
4
    CI.getSourceManager().PrintStats();
1092
4
    llvm::errs() << "\n";
1093
4
  }
1094
1095
  // Cleanup the output streams, and erase the output files if instructed by the
1096
  // FrontendAction.
1097
73.4k
  CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
1098
1099
73.4k
  if (isCurrentFileAST()) {
1100
67
    if (DisableFree) {
1101
10
      CI.resetAndLeakPreprocessor();
1102
10
      CI.resetAndLeakSourceManager();
1103
10
      CI.resetAndLeakFileManager();
1104
10
      llvm::BuryPointer(std::move(CurrentASTUnit));
1105
57
    } else {
1106
57
      CI.setPreprocessor(nullptr);
1107
57
      CI.setSourceManager(nullptr);
1108
57
      CI.setFileManager(nullptr);
1109
57
    }
1110
67
  }
1111
1112
73.4k
  setCompilerInstance(nullptr);
1113
73.4k
  setCurrentInput(FrontendInputFile());
1114
73.4k
  CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
1115
73.4k
}
1116
1117
73.3k
bool FrontendAction::shouldEraseOutputFiles() {
1118
73.3k
  return getCompilerInstance().getDiagnostics().hasErrorOccurred();
1119
73.3k
}
1120
1121
//===----------------------------------------------------------------------===//
1122
// Utility Actions
1123
//===----------------------------------------------------------------------===//
1124
1125
70.7k
void ASTFrontendAction::ExecuteAction() {
1126
70.7k
  CompilerInstance &CI = getCompilerInstance();
1127
70.7k
  if (!CI.hasPreprocessor())
1128
0
    return;
1129
1130
  // FIXME: Move the truncation aspect of this into Sema, we delayed this till
1131
  // here so the source manager would be initialized.
1132
70.7k
  if (hasCodeCompletionSupport() &&
1133
70.7k
      
!CI.getFrontendOpts().CodeCompletionAt.FileName.empty()12.7k
)
1134
1.21k
    CI.createCodeCompletionConsumer();
1135
1136
  // Use a code completion consumer?
1137
70.7k
  CodeCompleteConsumer *CompletionConsumer = nullptr;
1138
70.7k
  if (CI.hasCodeCompletionConsumer())
1139
1.21k
    CompletionConsumer = &CI.getCodeCompletionConsumer();
1140
1141
70.7k
  if (!CI.hasSema())
1142
70.7k
    CI.createSema(getTranslationUnitKind(), CompletionConsumer);
1143
1144
70.7k
  ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
1145
70.7k
           CI.getFrontendOpts().SkipFunctionBodies);
1146
70.7k
}
1147
1148
0
void PluginASTAction::anchor() { }
1149
1150
std::unique_ptr<ASTConsumer>
1151
PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
1152
0
                                              StringRef InFile) {
1153
0
  llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
1154
0
}
1155
1156
69
bool WrapperFrontendAction::PrepareToExecuteAction(CompilerInstance &CI) {
1157
69
  return WrappedAction->PrepareToExecuteAction(CI);
1158
69
}
1159
std::unique_ptr<ASTConsumer>
1160
WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
1161
54
                                         StringRef InFile) {
1162
54
  return WrappedAction->CreateASTConsumer(CI, InFile);
1163
54
}
1164
18
bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
1165
18
  return WrappedAction->BeginInvocation(CI);
1166
18
}
1167
56
bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI) {
1168
56
  WrappedAction->setCurrentInput(getCurrentInput());
1169
56
  WrappedAction->setCompilerInstance(&CI);
1170
56
  auto Ret = WrappedAction->BeginSourceFileAction(CI);
1171
  // BeginSourceFileAction may change CurrentInput, e.g. during module builds.
1172
56
  setCurrentInput(WrappedAction->getCurrentInput());
1173
56
  return Ret;
1174
56
}
1175
38
void WrapperFrontendAction::ExecuteAction() {
1176
38
  WrappedAction->ExecuteAction();
1177
38
}
1178
56
void WrapperFrontendAction::EndSourceFile() { WrappedAction->EndSourceFile(); }
1179
0
void WrapperFrontendAction::EndSourceFileAction() {
1180
0
  WrappedAction->EndSourceFileAction();
1181
0
}
1182
0
bool WrapperFrontendAction::shouldEraseOutputFiles() {
1183
0
  return WrappedAction->shouldEraseOutputFiles();
1184
0
}
1185
1186
56
bool WrapperFrontendAction::usesPreprocessorOnly() const {
1187
56
  return WrappedAction->usesPreprocessorOnly();
1188
56
}
1189
39
TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
1190
39
  return WrappedAction->getTranslationUnitKind();
1191
39
}
1192
4
bool WrapperFrontendAction::hasPCHSupport() const {
1193
4
  return WrappedAction->hasPCHSupport();
1194
4
}
1195
0
bool WrapperFrontendAction::hasASTFileSupport() const {
1196
0
  return WrappedAction->hasASTFileSupport();
1197
0
}
1198
0
bool WrapperFrontendAction::hasIRSupport() const {
1199
0
  return WrappedAction->hasIRSupport();
1200
0
}
1201
18
bool WrapperFrontendAction::hasCodeCompletionSupport() const {
1202
18
  return WrappedAction->hasCodeCompletionSupport();
1203
18
}
1204
1205
WrapperFrontendAction::WrapperFrontendAction(
1206
    std::unique_ptr<FrontendAction> WrappedAction)
1207
69
  : WrappedAction(std::move(WrappedAction)) {}
1208