Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Frontend/FrontendActions.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- FrontendActions.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/FrontendActions.h"
10
#include "clang/AST/ASTConsumer.h"
11
#include "clang/Basic/FileManager.h"
12
#include "clang/Basic/LangStandard.h"
13
#include "clang/Frontend/ASTConsumers.h"
14
#include "clang/Frontend/CompilerInstance.h"
15
#include "clang/Frontend/FrontendDiagnostic.h"
16
#include "clang/Frontend/MultiplexConsumer.h"
17
#include "clang/Frontend/Utils.h"
18
#include "clang/Lex/DependencyDirectivesSourceMinimizer.h"
19
#include "clang/Lex/HeaderSearch.h"
20
#include "clang/Lex/Preprocessor.h"
21
#include "clang/Lex/PreprocessorOptions.h"
22
#include "clang/Sema/TemplateInstCallback.h"
23
#include "clang/Serialization/ASTReader.h"
24
#include "clang/Serialization/ASTWriter.h"
25
#include "llvm/Support/FileSystem.h"
26
#include "llvm/Support/MemoryBuffer.h"
27
#include "llvm/Support/Path.h"
28
#include "llvm/Support/YAMLTraits.h"
29
#include "llvm/Support/raw_ostream.h"
30
#include <memory>
31
#include <system_error>
32
33
using namespace clang;
34
35
namespace {
36
11
CodeCompleteConsumer *GetCodeCompletionConsumer(CompilerInstance &CI) {
37
11
  return CI.hasCodeCompletionConsumer() ? 
&CI.getCodeCompletionConsumer()0
38
11
                                        : nullptr;
39
11
}
40
41
11
void EnsureSemaIsCreated(CompilerInstance &CI, FrontendAction &Action) {
42
11
  if (Action.hasCodeCompletionSupport() &&
43
11
      
!CI.getFrontendOpts().CodeCompletionAt.FileName.empty()0
)
44
0
    CI.createCodeCompletionConsumer();
45
11
46
11
  if (!CI.hasSema())
47
11
    CI.createSema(Action.getTranslationUnitKind(),
48
11
                  GetCodeCompletionConsumer(CI));
49
11
}
50
} // namespace
51
52
//===----------------------------------------------------------------------===//
53
// Custom Actions
54
//===----------------------------------------------------------------------===//
55
56
std::unique_ptr<ASTConsumer>
57
0
InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
58
0
  return std::make_unique<ASTConsumer>();
59
0
}
60
61
0
void InitOnlyAction::ExecuteAction() {
62
0
}
63
64
//===----------------------------------------------------------------------===//
65
// AST Consumer Actions
66
//===----------------------------------------------------------------------===//
67
68
std::unique_ptr<ASTConsumer>
69
476
ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
70
476
  if (std::unique_ptr<raw_ostream> OS =
71
476
          CI.createDefaultOutputFile(false, InFile))
72
476
    return CreateASTPrinter(std::move(OS), CI.getFrontendOpts().ASTDumpFilter);
73
0
  return nullptr;
74
0
}
75
76
std::unique_ptr<ASTConsumer>
77
220
ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
78
220
  const FrontendOptions &Opts = CI.getFrontendOpts();
79
220
  return CreateASTDumper(nullptr /*Dump to stdout.*/, Opts.ASTDumpFilter,
80
220
                         Opts.ASTDumpDecls, Opts.ASTDumpAll,
81
220
                         Opts.ASTDumpLookups, Opts.ASTDumpFormat);
82
220
}
83
84
std::unique_ptr<ASTConsumer>
85
0
ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
86
0
  return CreateASTDeclNodeLister();
87
0
}
88
89
std::unique_ptr<ASTConsumer>
90
0
ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
91
0
  return CreateASTViewer();
92
0
}
93
94
std::unique_ptr<ASTConsumer>
95
2.30k
GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
96
2.30k
  std::string Sysroot;
97
2.30k
  if (!ComputeASTConsumerArguments(CI, /*ref*/ Sysroot))
98
0
    return nullptr;
99
2.30k
100
2.30k
  std::string OutputFile;
101
2.30k
  std::unique_ptr<raw_pwrite_stream> OS =
102
2.30k
      CreateOutputFile(CI, InFile, /*ref*/ OutputFile);
103
2.30k
  if (!OS)
104
0
    return nullptr;
105
2.30k
106
2.30k
  if (!CI.getFrontendOpts().RelocatablePCH)
107
2.30k
    Sysroot.clear();
108
2.30k
109
2.30k
  const auto &FrontendOpts = CI.getFrontendOpts();
110
2.30k
  auto Buffer = std::make_shared<PCHBuffer>();
111
2.30k
  std::vector<std::unique_ptr<ASTConsumer>> Consumers;
112
2.30k
  Consumers.push_back(std::make_unique<PCHGenerator>(
113
2.30k
      CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer,
114
2.30k
      FrontendOpts.ModuleFileExtensions,
115
2.30k
      CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
116
2.30k
      FrontendOpts.IncludeTimestamps, +CI.getLangOpts().CacheGeneratedPCH));
117
2.30k
  Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
118
2.30k
      CI, std::string(InFile), OutputFile, std::move(OS), Buffer));
119
2.30k
120
2.30k
  return std::make_unique<MultiplexConsumer>(std::move(Consumers));
121
2.30k
}
122
123
bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
124
2.39k
                                                    std::string &Sysroot) {
125
2.39k
  Sysroot = CI.getHeaderSearchOpts().Sysroot;
126
2.39k
  if (CI.getFrontendOpts().RelocatablePCH && 
Sysroot.empty()1
) {
127
0
    CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
128
0
    return false;
129
0
  }
130
2.39k
131
2.39k
  return true;
132
2.39k
}
133
134
std::unique_ptr<llvm::raw_pwrite_stream>
135
GeneratePCHAction::CreateOutputFile(CompilerInstance &CI, StringRef InFile,
136
2.39k
                                    std::string &OutputFile) {
137
2.39k
  // We use createOutputFile here because this is exposed via libclang, and we
138
2.39k
  // must disable the RemoveFileOnSignal behavior.
139
2.39k
  // We use a temporary to avoid race conditions.
140
2.39k
  std::unique_ptr<raw_pwrite_stream> OS =
141
2.39k
      CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
142
2.39k
                          /*RemoveFileOnSignal=*/false, InFile,
143
2.39k
                          /*Extension=*/"", CI.getFrontendOpts().UseTemporary);
144
2.39k
  if (!OS)
145
0
    return nullptr;
146
2.39k
147
2.39k
  OutputFile = CI.getFrontendOpts().OutputFile;
148
2.39k
  return OS;
149
2.39k
}
150
151
2.30k
bool GeneratePCHAction::shouldEraseOutputFiles() {
152
2.30k
  if (getCompilerInstance().getPreprocessorOpts().AllowPCHWithCompilerErrors)
153
1
    return false;
154
2.30k
  return ASTFrontendAction::shouldEraseOutputFiles();
155
2.30k
}
156
157
2.30k
bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI) {
158
2.30k
  CI.getLangOpts().CompilingPCH = true;
159
2.30k
  return true;
160
2.30k
}
161
162
std::unique_ptr<ASTConsumer>
163
GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
164
1.88k
                                        StringRef InFile) {
165
1.88k
  std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile);
166
1.88k
  if (!OS)
167
0
    return nullptr;
168
1.88k
169
1.88k
  std::string OutputFile = CI.getFrontendOpts().OutputFile;
170
1.88k
  std::string Sysroot;
171
1.88k
172
1.88k
  auto Buffer = std::make_shared<PCHBuffer>();
173
1.88k
  std::vector<std::unique_ptr<ASTConsumer>> Consumers;
174
1.88k
175
1.88k
  Consumers.push_back(std::make_unique<PCHGenerator>(
176
1.88k
      CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer,
177
1.88k
      CI.getFrontendOpts().ModuleFileExtensions,
178
1.88k
      /*AllowASTWithErrors=*/false,
179
1.88k
      /*IncludeTimestamps=*/
180
1.88k
      +CI.getFrontendOpts().BuildingImplicitModule,
181
1.88k
      /*ShouldCacheASTInMemory=*/
182
1.88k
      +CI.getFrontendOpts().BuildingImplicitModule));
183
1.88k
  Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
184
1.88k
      CI, std::string(InFile), OutputFile, std::move(OS), Buffer));
185
1.88k
  return std::make_unique<MultiplexConsumer>(std::move(Consumers));
186
1.88k
}
187
188
bool GenerateModuleFromModuleMapAction::BeginSourceFileAction(
189
1.82k
    CompilerInstance &CI) {
190
1.82k
  if (!CI.getLangOpts().Modules) {
191
1
    CI.getDiagnostics().Report(diag::err_module_build_requires_fmodules);
192
1
    return false;
193
1
  }
194
1.82k
195
1.82k
  return GenerateModuleAction::BeginSourceFileAction(CI);
196
1.82k
}
197
198
std::unique_ptr<raw_pwrite_stream>
199
GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,
200
1.82k
                                                    StringRef InFile) {
201
1.82k
  // If no output file was provided, figure out where this module would go
202
1.82k
  // in the module cache.
203
1.82k
  if (CI.getFrontendOpts().OutputFile.empty()) {
204
45
    StringRef ModuleMapFile = CI.getFrontendOpts().OriginalModuleMap;
205
45
    if (ModuleMapFile.empty())
206
45
      ModuleMapFile = InFile;
207
45
208
45
    HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
209
45
    CI.getFrontendOpts().OutputFile =
210
45
        HS.getCachedModuleFileName(CI.getLangOpts().CurrentModule,
211
45
                                   ModuleMapFile);
212
45
  }
213
1.82k
214
1.82k
  // We use createOutputFile here because this is exposed via libclang, and we
215
1.82k
  // must disable the RemoveFileOnSignal behavior.
216
1.82k
  // We use a temporary to avoid race conditions.
217
1.82k
  return CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
218
1.82k
                             /*RemoveFileOnSignal=*/false, InFile,
219
1.82k
                             /*Extension=*/"", /*UseTemporary=*/true,
220
1.82k
                             /*CreateMissingDirectories=*/true);
221
1.82k
}
222
223
bool GenerateModuleInterfaceAction::BeginSourceFileAction(
224
49
    CompilerInstance &CI) {
225
49
  if (!CI.getLangOpts().ModulesTS && 
!CI.getLangOpts().CPlusPlusModules10
) {
226
0
    CI.getDiagnostics().Report(diag::err_module_interface_requires_cpp_modules);
227
0
    return false;
228
0
  }
229
49
230
49
  CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
231
49
232
49
  return GenerateModuleAction::BeginSourceFileAction(CI);
233
49
}
234
235
std::unique_ptr<raw_pwrite_stream>
236
GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,
237
49
                                                StringRef InFile) {
238
49
  return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
239
49
}
240
241
bool GenerateHeaderModuleAction::PrepareToExecuteAction(
242
6
    CompilerInstance &CI) {
243
6
  if (!CI.getLangOpts().Modules) {
244
0
    CI.getDiagnostics().Report(diag::err_header_module_requires_modules);
245
0
    return false;
246
0
  }
247
6
248
6
  auto &Inputs = CI.getFrontendOpts().Inputs;
249
6
  if (Inputs.empty())
250
0
    return GenerateModuleAction::BeginInvocation(CI);
251
6
252
6
  auto Kind = Inputs[0].getKind();
253
6
254
6
  // Convert the header file inputs into a single module input buffer.
255
6
  SmallString<256> HeaderContents;
256
6
  ModuleHeaders.reserve(Inputs.size());
257
11
  for (const FrontendInputFile &FIF : Inputs) {
258
11
    // FIXME: We should support re-compiling from an AST file.
259
11
    if (FIF.getKind().getFormat() != InputKind::Source || !FIF.isFile()) {
260
0
      CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
261
0
          << (FIF.isFile() ? FIF.getFile()
262
0
                           : FIF.getBuffer()->getBufferIdentifier());
263
0
      return true;
264
0
    }
265
11
266
11
    HeaderContents += "#include \"";
267
11
    HeaderContents += FIF.getFile();
268
11
    HeaderContents += "\"\n";
269
11
    ModuleHeaders.push_back(std::string(FIF.getFile()));
270
11
  }
271
6
  Buffer = llvm::MemoryBuffer::getMemBufferCopy(
272
6
      HeaderContents, Module::getModuleInputBufferName());
273
6
274
6
  // Set that buffer up as our "real" input.
275
6
  Inputs.clear();
276
6
  Inputs.push_back(FrontendInputFile(Buffer.get(), Kind, /*IsSystem*/false));
277
6
278
6
  return GenerateModuleAction::PrepareToExecuteAction(CI);
279
6
}
280
281
bool GenerateHeaderModuleAction::BeginSourceFileAction(
282
6
    CompilerInstance &CI) {
283
6
  CI.getLangOpts().setCompilingModule(LangOptions::CMK_HeaderModule);
284
6
285
6
  // Synthesize a Module object for the given headers.
286
6
  auto &HS = CI.getPreprocessor().getHeaderSearchInfo();
287
6
  SmallVector<Module::Header, 16> Headers;
288
11
  for (StringRef Name : ModuleHeaders) {
289
11
    const DirectoryLookup *CurDir = nullptr;
290
11
    Optional<FileEntryRef> FE = HS.LookupFile(
291
11
        Name, SourceLocation(), /*Angled*/ false, nullptr, CurDir, None,
292
11
        nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
293
11
    if (!FE) {
294
0
      CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
295
0
        << Name;
296
0
      continue;
297
0
    }
298
11
    Headers.push_back({std::string(Name), &FE->getFileEntry()});
299
11
  }
300
6
  HS.getModuleMap().createHeaderModule(CI.getLangOpts().CurrentModule, Headers);
301
6
302
6
  return GenerateModuleAction::BeginSourceFileAction(CI);
303
6
}
304
305
std::unique_ptr<raw_pwrite_stream>
306
GenerateHeaderModuleAction::CreateOutputFile(CompilerInstance &CI,
307
6
                                             StringRef InFile) {
308
6
  return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
309
6
}
310
311
11.4k
SyntaxOnlyAction::~SyntaxOnlyAction() {
312
11.4k
}
313
314
std::unique_ptr<ASTConsumer>
315
11.6k
SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
316
11.6k
  return std::make_unique<ASTConsumer>();
317
11.6k
}
318
319
std::unique_ptr<ASTConsumer>
320
DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
321
5
                                        StringRef InFile) {
322
5
  return std::make_unique<ASTConsumer>();
323
5
}
324
325
std::unique_ptr<ASTConsumer>
326
7
VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
327
7
  return std::make_unique<ASTConsumer>();
328
7
}
329
330
7
void VerifyPCHAction::ExecuteAction() {
331
7
  CompilerInstance &CI = getCompilerInstance();
332
7
  bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
333
7
  const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
334
7
  std::unique_ptr<ASTReader> Reader(new ASTReader(
335
7
      CI.getPreprocessor(), CI.getModuleCache(), &CI.getASTContext(),
336
7
      CI.getPCHContainerReader(), CI.getFrontendOpts().ModuleFileExtensions,
337
7
      Sysroot.empty() ? 
""0
: Sysroot.c_str(),
338
7
      /*DisableValidation*/ false,
339
7
      /*AllowPCHWithCompilerErrors*/ false,
340
7
      /*AllowConfigurationMismatch*/ true,
341
7
      /*ValidateSystemInputs*/ true));
342
7
343
7
  Reader->ReadAST(getCurrentFile(),
344
7
                  Preamble ? 
serialization::MK_Preamble0
345
7
                           : serialization::MK_PCH,
346
7
                  SourceLocation(),
347
7
                  ASTReader::ARR_ConfigurationMismatch);
348
7
}
349
350
namespace {
351
struct TemplightEntry {
352
  std::string Name;
353
  std::string Kind;
354
  std::string Event;
355
  std::string DefinitionLocation;
356
  std::string PointOfInstantiation;
357
};
358
} // namespace
359
360
namespace llvm {
361
namespace yaml {
362
template <> struct MappingTraits<TemplightEntry> {
363
152
  static void mapping(IO &io, TemplightEntry &fields) {
364
152
    io.mapRequired("name", fields.Name);
365
152
    io.mapRequired("kind", fields.Kind);
366
152
    io.mapRequired("event", fields.Event);
367
152
    io.mapRequired("orig", fields.DefinitionLocation);
368
152
    io.mapRequired("poi", fields.PointOfInstantiation);
369
152
  }
370
};
371
} // namespace yaml
372
} // namespace llvm
373
374
namespace {
375
class DefaultTemplateInstCallback : public TemplateInstantiationCallback {
376
  using CodeSynthesisContext = Sema::CodeSynthesisContext;
377
378
public:
379
11
  void initialize(const Sema &) override {}
380
381
11
  void finalize(const Sema &) override {}
382
383
  void atTemplateBegin(const Sema &TheSema,
384
76
                       const CodeSynthesisContext &Inst) override {
385
76
    displayTemplightEntry<true>(llvm::outs(), TheSema, Inst);
386
76
  }
387
388
  void atTemplateEnd(const Sema &TheSema,
389
76
                     const CodeSynthesisContext &Inst) override {
390
76
    displayTemplightEntry<false>(llvm::outs(), TheSema, Inst);
391
76
  }
392
393
private:
394
152
  static std::string toString(CodeSynthesisContext::SynthesisKind Kind) {
395
152
    switch (Kind) {
396
56
    case CodeSynthesisContext::TemplateInstantiation:
397
56
      return "TemplateInstantiation";
398
2
    case CodeSynthesisContext::DefaultTemplateArgumentInstantiation:
399
2
      return "DefaultTemplateArgumentInstantiation";
400
2
    case CodeSynthesisContext::DefaultFunctionArgumentInstantiation:
401
2
      return "DefaultFunctionArgumentInstantiation";
402
6
    case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution:
403
6
      return "ExplicitTemplateArgumentSubstitution";
404
8
    case CodeSynthesisContext::DeducedTemplateArgumentSubstitution:
405
8
      return "DeducedTemplateArgumentSubstitution";
406
2
    case CodeSynthesisContext::PriorTemplateArgumentSubstitution:
407
2
      return "PriorTemplateArgumentSubstitution";
408
4
    case CodeSynthesisContext::DefaultTemplateArgumentChecking:
409
4
      return "DefaultTemplateArgumentChecking";
410
0
    case CodeSynthesisContext::ExceptionSpecEvaluation:
411
0
      return "ExceptionSpecEvaluation";
412
2
    case CodeSynthesisContext::ExceptionSpecInstantiation:
413
2
      return "ExceptionSpecInstantiation";
414
0
    case CodeSynthesisContext::DeclaringSpecialMember:
415
0
      return "DeclaringSpecialMember";
416
0
    case CodeSynthesisContext::DeclaringImplicitEqualityComparison:
417
0
      return "DeclaringImplicitEqualityComparison";
418
0
    case CodeSynthesisContext::DefiningSynthesizedFunction:
419
0
      return "DefiningSynthesizedFunction";
420
0
    case CodeSynthesisContext::RewritingOperatorAsSpaceship:
421
0
      return "RewritingOperatorAsSpaceship";
422
70
    case CodeSynthesisContext::Memoization:
423
70
      return "Memoization";
424
0
    case CodeSynthesisContext::ConstraintsCheck:
425
0
      return "ConstraintsCheck";
426
0
    case CodeSynthesisContext::ConstraintSubstitution:
427
0
      return "ConstraintSubstitution";
428
0
    case CodeSynthesisContext::ConstraintNormalization:
429
0
      return "ConstraintNormalization";
430
0
    case CodeSynthesisContext::ParameterMappingSubstitution:
431
0
      return "ParameterMappingSubstitution";
432
0
    case CodeSynthesisContext::RequirementInstantiation:
433
0
      return "RequirementInstantiation";
434
0
    case CodeSynthesisContext::NestedRequirementConstraintsCheck:
435
0
      return "NestedRequirementConstraintsCheck";
436
0
    }
437
0
    return "";
438
0
  }
439
440
  template <bool BeginInstantiation>
441
  static void displayTemplightEntry(llvm::raw_ostream &Out, const Sema &TheSema,
442
152
                                    const CodeSynthesisContext &Inst) {
443
152
    std::string YAML;
444
152
    {
445
152
      llvm::raw_string_ostream OS(YAML);
446
152
      llvm::yaml::Output YO(OS);
447
152
      TemplightEntry Entry =
448
152
          getTemplightEntry<BeginInstantiation>(TheSema, Inst);
449
152
      llvm::yaml::EmptyContext Context;
450
152
      llvm::yaml::yamlize(YO, Entry, true, Context);
451
152
    }
452
152
    Out << "---" << YAML << "\n";
453
152
  }
FrontendActions.cpp:void (anonymous namespace)::DefaultTemplateInstCallback::displayTemplightEntry<true>(llvm::raw_ostream&, clang::Sema const&, clang::Sema::CodeSynthesisContext const&)
Line
Count
Source
442
76
                                    const CodeSynthesisContext &Inst) {
443
76
    std::string YAML;
444
76
    {
445
76
      llvm::raw_string_ostream OS(YAML);
446
76
      llvm::yaml::Output YO(OS);
447
76
      TemplightEntry Entry =
448
76
          getTemplightEntry<BeginInstantiation>(TheSema, Inst);
449
76
      llvm::yaml::EmptyContext Context;
450
76
      llvm::yaml::yamlize(YO, Entry, true, Context);
451
76
    }
452
76
    Out << "---" << YAML << "\n";
453
76
  }
FrontendActions.cpp:void (anonymous namespace)::DefaultTemplateInstCallback::displayTemplightEntry<false>(llvm::raw_ostream&, clang::Sema const&, clang::Sema::CodeSynthesisContext const&)
Line
Count
Source
442
76
                                    const CodeSynthesisContext &Inst) {
443
76
    std::string YAML;
444
76
    {
445
76
      llvm::raw_string_ostream OS(YAML);
446
76
      llvm::yaml::Output YO(OS);
447
76
      TemplightEntry Entry =
448
76
          getTemplightEntry<BeginInstantiation>(TheSema, Inst);
449
76
      llvm::yaml::EmptyContext Context;
450
76
      llvm::yaml::yamlize(YO, Entry, true, Context);
451
76
    }
452
76
    Out << "---" << YAML << "\n";
453
76
  }
454
455
  template <bool BeginInstantiation>
456
  static TemplightEntry getTemplightEntry(const Sema &TheSema,
457
152
                                          const CodeSynthesisContext &Inst) {
458
152
    TemplightEntry Entry;
459
152
    Entry.Kind = toString(Inst.Kind);
460
152
    Entry.Event = BeginInstantiation ? 
"Begin"76
:
"End"76
;
461
152
    if (auto *NamedTemplate = dyn_cast_or_null<NamedDecl>(Inst.Entity)) {
462
152
      llvm::raw_string_ostream OS(Entry.Name);
463
152
      NamedTemplate->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
464
152
      const PresumedLoc DefLoc =
465
152
        TheSema.getSourceManager().getPresumedLoc(Inst.Entity->getLocation());
466
152
      if(!DefLoc.isInvalid())
467
152
        Entry.DefinitionLocation = std::string(DefLoc.getFilename()) + ":" +
468
152
                                   std::to_string(DefLoc.getLine()) + ":" +
469
152
                                   std::to_string(DefLoc.getColumn());
470
152
    }
471
152
    const PresumedLoc PoiLoc =
472
152
        TheSema.getSourceManager().getPresumedLoc(Inst.PointOfInstantiation);
473
152
    if (!PoiLoc.isInvalid()) {
474
152
      Entry.PointOfInstantiation = std::string(PoiLoc.getFilename()) + ":" +
475
152
                                   std::to_string(PoiLoc.getLine()) + ":" +
476
152
                                   std::to_string(PoiLoc.getColumn());
477
152
    }
478
152
    return Entry;
479
152
  }
FrontendActions.cpp:(anonymous namespace)::TemplightEntry (anonymous namespace)::DefaultTemplateInstCallback::getTemplightEntry<true>(clang::Sema const&, clang::Sema::CodeSynthesisContext const&)
Line
Count
Source
457
76
                                          const CodeSynthesisContext &Inst) {
458
76
    TemplightEntry Entry;
459
76
    Entry.Kind = toString(Inst.Kind);
460
76
    Entry.Event = BeginInstantiation ? "Begin" : 
"End"0
;
461
76
    if (auto *NamedTemplate = dyn_cast_or_null<NamedDecl>(Inst.Entity)) {
462
76
      llvm::raw_string_ostream OS(Entry.Name);
463
76
      NamedTemplate->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
464
76
      const PresumedLoc DefLoc =
465
76
        TheSema.getSourceManager().getPresumedLoc(Inst.Entity->getLocation());
466
76
      if(!DefLoc.isInvalid())
467
76
        Entry.DefinitionLocation = std::string(DefLoc.getFilename()) + ":" +
468
76
                                   std::to_string(DefLoc.getLine()) + ":" +
469
76
                                   std::to_string(DefLoc.getColumn());
470
76
    }
471
76
    const PresumedLoc PoiLoc =
472
76
        TheSema.getSourceManager().getPresumedLoc(Inst.PointOfInstantiation);
473
76
    if (!PoiLoc.isInvalid()) {
474
76
      Entry.PointOfInstantiation = std::string(PoiLoc.getFilename()) + ":" +
475
76
                                   std::to_string(PoiLoc.getLine()) + ":" +
476
76
                                   std::to_string(PoiLoc.getColumn());
477
76
    }
478
76
    return Entry;
479
76
  }
FrontendActions.cpp:(anonymous namespace)::TemplightEntry (anonymous namespace)::DefaultTemplateInstCallback::getTemplightEntry<false>(clang::Sema const&, clang::Sema::CodeSynthesisContext const&)
Line
Count
Source
457
76
                                          const CodeSynthesisContext &Inst) {
458
76
    TemplightEntry Entry;
459
76
    Entry.Kind = toString(Inst.Kind);
460
76
    Entry.Event = BeginInstantiation ? 
"Begin"0
: "End";
461
76
    if (auto *NamedTemplate = dyn_cast_or_null<NamedDecl>(Inst.Entity)) {
462
76
      llvm::raw_string_ostream OS(Entry.Name);
463
76
      NamedTemplate->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
464
76
      const PresumedLoc DefLoc =
465
76
        TheSema.getSourceManager().getPresumedLoc(Inst.Entity->getLocation());
466
76
      if(!DefLoc.isInvalid())
467
76
        Entry.DefinitionLocation = std::string(DefLoc.getFilename()) + ":" +
468
76
                                   std::to_string(DefLoc.getLine()) + ":" +
469
76
                                   std::to_string(DefLoc.getColumn());
470
76
    }
471
76
    const PresumedLoc PoiLoc =
472
76
        TheSema.getSourceManager().getPresumedLoc(Inst.PointOfInstantiation);
473
76
    if (!PoiLoc.isInvalid()) {
474
76
      Entry.PointOfInstantiation = std::string(PoiLoc.getFilename()) + ":" +
475
76
                                   std::to_string(PoiLoc.getLine()) + ":" +
476
76
                                   std::to_string(PoiLoc.getColumn());
477
76
    }
478
76
    return Entry;
479
76
  }
480
};
481
} // namespace
482
483
std::unique_ptr<ASTConsumer>
484
11
TemplightDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
485
11
  return std::make_unique<ASTConsumer>();
486
11
}
487
488
11
void TemplightDumpAction::ExecuteAction() {
489
11
  CompilerInstance &CI = getCompilerInstance();
490
11
491
11
  // This part is normally done by ASTFrontEndAction, but needs to happen
492
11
  // before Templight observers can be created
493
11
  // FIXME: Move the truncation aspect of this into Sema, we delayed this till
494
11
  // here so the source manager would be initialized.
495
11
  EnsureSemaIsCreated(CI, *this);
496
11
497
11
  CI.getSema().TemplateInstCallbacks.push_back(
498
11
      std::make_unique<DefaultTemplateInstCallback>());
499
11
  ASTFrontendAction::ExecuteAction();
500
11
}
501
502
namespace {
503
  /// AST reader listener that dumps module information for a module
504
  /// file.
505
  class DumpModuleInfoListener : public ASTReaderListener {
506
    llvm::raw_ostream &Out;
507
508
  public:
509
5
    DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
510
511
#define DUMP_BOOLEAN(Value, Text)                       \
512
835
    Out.indent(4) << Text << ": " << (Value? 
"Yes"143
:
"No"692
) << "\n"
513
514
5
    bool ReadFullVersionInformation(StringRef FullVersion) override {
515
5
      Out.indent(2)
516
5
        << "Generated by "
517
5
        << (FullVersion == getClangFullRepositoryVersion()? "this"
518
5
                                                          : 
"a different"0
)
519
5
        << " Clang: " << FullVersion << "\n";
520
5
      return ASTReaderListener::ReadFullVersionInformation(FullVersion);
521
5
    }
522
523
5
    void ReadModuleName(StringRef ModuleName) override {
524
5
      Out.indent(2) << "Module name: " << ModuleName << "\n";
525
5
    }
526
5
    void ReadModuleMapFile(StringRef ModuleMapPath) override {
527
5
      Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
528
5
    }
529
530
    bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
531
5
                             bool AllowCompatibleDifferences) override {
532
5
      Out.indent(2) << "Language options:\n";
533
5
#define LANGOPT(Name, Bits, Default, Description) \
534
705
      DUMP_BOOLEAN(LangOpts.Name, Description);
535
5
#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
536
75
      Out.indent(4) << Description << ": "                   \
537
75
                    << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
538
5
#define VALUE_LANGOPT(Name, Bits, Default, Description) \
539
50
      Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
540
5
#define BENIGN_LANGOPT(Name, Bits, Default, Description)
541
5
#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
542
5
#include "clang/Basic/LangOptions.def"
543
5
544
5
      if (!LangOpts.ModuleFeatures.empty()) {
545
4
        Out.indent(4) << "Module features:\n";
546
4
        for (StringRef Feature : LangOpts.ModuleFeatures)
547
4
          Out.indent(6) << Feature << "\n";
548
4
      }
549
5
550
5
      return false;
551
5
    }
552
553
    bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
554
5
                           bool AllowCompatibleDifferences) override {
555
5
      Out.indent(2) << "Target options:\n";
556
5
      Out.indent(4) << "  Triple: " << TargetOpts.Triple << "\n";
557
5
      Out.indent(4) << "  CPU: " << TargetOpts.CPU << "\n";
558
5
      Out.indent(4) << "  ABI: " << TargetOpts.ABI << "\n";
559
5
560
5
      if (!TargetOpts.FeaturesAsWritten.empty()) {
561
0
        Out.indent(4) << "Target features:\n";
562
0
        for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
563
0
             I != N; ++I) {
564
0
          Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
565
0
        }
566
0
      }
567
5
568
5
      return false;
569
5
    }
570
571
    bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
572
5
                               bool Complain) override {
573
5
      Out.indent(2) << "Diagnostic options:\n";
574
100
#define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
575
5
#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
576
15
      Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
577
5
#define VALUE_DIAGOPT(Name, Bits, Default) \
578
45
      Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
579
5
#include "clang/Basic/DiagnosticOptions.def"
580
5
581
5
      Out.indent(4) << "Diagnostic flags:\n";
582
5
      for (const std::string &Warning : DiagOpts->Warnings)
583
4
        Out.indent(6) << "-W" << Warning << "\n";
584
5
      for (const std::string &Remark : DiagOpts->Remarks)
585
0
        Out.indent(6) << "-R" << Remark << "\n";
586
5
587
5
      return false;
588
5
    }
589
590
    bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
591
                                 StringRef SpecificModuleCachePath,
592
5
                                 bool Complain) override {
593
5
      Out.indent(2) << "Header search options:\n";
594
5
      Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
595
5
      Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n";
596
5
      Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
597
5
      DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
598
5
                   "Use builtin include directories [-nobuiltininc]");
599
5
      DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
600
5
                   "Use standard system include directories [-nostdinc]");
601
5
      DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
602
5
                   "Use standard C++ include directories [-nostdinc++]");
603
5
      DUMP_BOOLEAN(HSOpts.UseLibcxx,
604
5
                   "Use libc++ (rather than libstdc++) [-stdlib=]");
605
5
      return false;
606
5
    }
607
608
    bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
609
                                 bool Complain,
610
5
                                 std::string &SuggestedPredefines) override {
611
5
      Out.indent(2) << "Preprocessor options:\n";
612
5
      DUMP_BOOLEAN(PPOpts.UsePredefines,
613
5
                   "Uses compiler/target-specific predefines [-undef]");
614
5
      DUMP_BOOLEAN(PPOpts.DetailedRecord,
615
5
                   "Uses detailed preprocessing record (for indexing)");
616
5
617
5
      if (!PPOpts.Macros.empty()) {
618
4
        Out.indent(4) << "Predefined macros:\n";
619
4
      }
620
5
621
5
      for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
622
5
             I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
623
13
           I != IEnd; 
++I8
) {
624
8
        Out.indent(6);
625
8
        if (I->second)
626
0
          Out << "-U";
627
8
        else
628
8
          Out << "-D";
629
8
        Out << I->first << "\n";
630
8
      }
631
5
      return false;
632
5
    }
633
634
    /// Indicates that a particular module file extension has been read.
635
    void readModuleFileExtension(
636
2
           const ModuleFileExtensionMetadata &Metadata) override {
637
2
      Out.indent(2) << "Module file extension '"
638
2
                    << Metadata.BlockName << "' " << Metadata.MajorVersion
639
2
                    << "." << Metadata.MinorVersion;
640
2
      if (!Metadata.UserInfo.empty()) {
641
2
        Out << ": ";
642
2
        Out.write_escaped(Metadata.UserInfo);
643
2
      }
644
2
645
2
      Out << "\n";
646
2
    }
647
648
    /// Tells the \c ASTReaderListener that we want to receive the
649
    /// input files of the AST file via \c visitInputFile.
650
5
    bool needsInputFileVisitation() override { return true; }
651
652
    /// Tells the \c ASTReaderListener that we want to receive the
653
    /// input files of the AST file via \c visitInputFile.
654
5
    bool needsSystemInputFileVisitation() override { return true; }
655
656
    /// Indicates that the AST file contains particular input file.
657
    ///
658
    /// \returns true to continue receiving the next input file, false to stop.
659
    bool visitInputFile(StringRef Filename, bool isSystem,
660
42
                        bool isOverridden, bool isExplicitModule) override {
661
42
662
42
      Out.indent(2) << "Input file: " << Filename;
663
42
664
42
      if (isSystem || isOverridden || isExplicitModule) {
665
0
        Out << " [";
666
0
        if (isSystem) {
667
0
          Out << "System";
668
0
          if (isOverridden || isExplicitModule)
669
0
            Out << ", ";
670
0
        }
671
0
        if (isOverridden) {
672
0
          Out << "Overridden";
673
0
          if (isExplicitModule)
674
0
            Out << ", ";
675
0
        }
676
0
        if (isExplicitModule)
677
0
          Out << "ExplicitModule";
678
0
679
0
        Out << "]";
680
0
      }
681
42
682
42
      Out << "\n";
683
42
684
42
      return true;
685
42
    }
686
687
    /// Returns true if this \c ASTReaderListener wants to receive the
688
    /// imports of the AST file via \c visitImport, false otherwise.
689
5
    bool needsImportVisitation() const override { return true; }
690
691
    /// If needsImportVisitation returns \c true, this is called for each
692
    /// AST file imported by this AST file.
693
4
    void visitImport(StringRef ModuleName, StringRef Filename) override {
694
4
      Out.indent(2) << "Imports module '" << ModuleName
695
4
                    << "': " << Filename.str() << "\n";
696
4
    }
697
#undef DUMP_BOOLEAN
698
  };
699
}
700
701
5
bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) {
702
5
  // The Object file reader also supports raw ast files and there is no point in
703
5
  // being strict about the module file format in -module-file-info mode.
704
5
  CI.getHeaderSearchOpts().ModuleFormat = "obj";
705
5
  return true;
706
5
}
707
708
5
void DumpModuleInfoAction::ExecuteAction() {
709
5
  // Set up the output file.
710
5
  std::unique_ptr<llvm::raw_fd_ostream> OutFile;
711
5
  StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
712
5
  if (!OutputFileName.empty() && 
OutputFileName != "-"0
) {
713
0
    std::error_code EC;
714
0
    OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
715
0
                                           llvm::sys::fs::OF_Text));
716
0
  }
717
5
  llvm::raw_ostream &Out = OutFile.get()? 
*OutFile.get()0
: llvm::outs();
718
5
719
5
  Out << "Information for module file '" << getCurrentFile() << "':\n";
720
5
  auto &FileMgr = getCompilerInstance().getFileManager();
721
5
  auto Buffer = FileMgr.getBufferForFile(getCurrentFile());
722
5
  StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();
723
5
  bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && 
Magic[1] == 'P'3
&&
724
5
                
Magic[2] == 'C'3
&&
Magic[3] == 'H'3
);
725
5
  Out << "  Module format: " << (IsRaw ? 
"raw"3
:
"obj"2
) << "\n";
726
5
727
5
  Preprocessor &PP = getCompilerInstance().getPreprocessor();
728
5
  DumpModuleInfoListener Listener(Out);
729
5
  HeaderSearchOptions &HSOpts =
730
5
      PP.getHeaderSearchInfo().getHeaderSearchOpts();
731
5
  ASTReader::readASTFileControlBlock(
732
5
      getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(),
733
5
      /*FindModuleFileExtensions=*/true, Listener,
734
5
      HSOpts.ModulesValidateDiagnosticOptions);
735
5
}
736
737
//===----------------------------------------------------------------------===//
738
// Preprocessor Actions
739
//===----------------------------------------------------------------------===//
740
741
0
void DumpRawTokensAction::ExecuteAction() {
742
0
  Preprocessor &PP = getCompilerInstance().getPreprocessor();
743
0
  SourceManager &SM = PP.getSourceManager();
744
0
745
0
  // Start lexing the specified input file.
746
0
  const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
747
0
  Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
748
0
  RawLex.SetKeepWhitespaceMode(true);
749
0
750
0
  Token RawTok;
751
0
  RawLex.LexFromRawLexer(RawTok);
752
0
  while (RawTok.isNot(tok::eof)) {
753
0
    PP.DumpToken(RawTok, true);
754
0
    llvm::errs() << "\n";
755
0
    RawLex.LexFromRawLexer(RawTok);
756
0
  }
757
0
}
758
759
3
void DumpTokensAction::ExecuteAction() {
760
3
  Preprocessor &PP = getCompilerInstance().getPreprocessor();
761
3
  // Start preprocessing the specified input file.
762
3
  Token Tok;
763
3
  PP.EnterMainSourceFile();
764
7
  do {
765
7
    PP.Lex(Tok);
766
7
    PP.DumpToken(Tok, true);
767
7
    llvm::errs() << "\n";
768
7
  } while (Tok.isNot(tok::eof));
769
3
}
770
771
2.82k
void PreprocessOnlyAction::ExecuteAction() {
772
2.82k
  Preprocessor &PP = getCompilerInstance().getPreprocessor();
773
2.82k
774
2.82k
  // Ignore unknown pragmas.
775
2.82k
  PP.IgnorePragmas();
776
2.82k
777
2.82k
  Token Tok;
778
2.82k
  // Start parsing the specified input file.
779
2.82k
  PP.EnterMainSourceFile();
780
267M
  do {
781
267M
    PP.Lex(Tok);
782
267M
  } while (Tok.isNot(tok::eof));
783
2.82k
}
784
785
1.75k
void PrintPreprocessedAction::ExecuteAction() {
786
1.75k
  CompilerInstance &CI = getCompilerInstance();
787
1.75k
  // Output file may need to be set to 'Binary', to avoid converting Unix style
788
1.75k
  // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
789
1.75k
  //
790
1.75k
  // Look to see what type of line endings the file uses. If there's a
791
1.75k
  // CRLF, then we won't open the file up in binary mode. If there is
792
1.75k
  // just an LF or CR, then we will open the file up in binary mode.
793
1.75k
  // In this fashion, the output format should match the input format, unless
794
1.75k
  // the input format has inconsistent line endings.
795
1.75k
  //
796
1.75k
  // This should be a relatively fast operation since most files won't have
797
1.75k
  // all of their source code on a single line. However, that is still a
798
1.75k
  // concern, so if we scan for too long, we'll just assume the file should
799
1.75k
  // be opened in binary mode.
800
1.75k
  bool BinaryMode = true;
801
1.75k
  bool InvalidFile = false;
802
1.75k
  const SourceManager& SM = CI.getSourceManager();
803
1.75k
  const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
804
1.75k
                                                     &InvalidFile);
805
1.75k
  if (!InvalidFile) {
806
1.75k
    const char *cur = Buffer->getBufferStart();
807
1.75k
    const char *end = Buffer->getBufferEnd();
808
1.75k
    const char *next = (cur != end) ? 
cur + 11.43k
:
end325
;
809
1.75k
810
1.75k
    // Limit ourselves to only scanning 256 characters into the source
811
1.75k
    // file.  This is mostly a sanity check in case the file has no
812
1.75k
    // newlines whatsoever.
813
1.75k
    if (end - cur > 256) 
end = cur + 2561.33k
;
814
1.75k
815
114k
    while (next < end) {
816
114k
      if (*cur == 0x0D) {  // CR
817
3
        if (*next == 0x0A)  // CRLF
818
3
          BinaryMode = false;
819
3
820
3
        break;
821
114k
      } else if (*cur == 0x0A)  // LF
822
1.42k
        break;
823
113k
824
113k
      ++cur;
825
113k
      ++next;
826
113k
    }
827
1.75k
  }
828
1.75k
829
1.75k
  std::unique_ptr<raw_ostream> OS =
830
1.75k
      CI.createDefaultOutputFile(BinaryMode, getCurrentFileOrBufferName());
831
1.75k
  if (!OS) 
return1
;
832
1.75k
833
1.75k
  // If we're preprocessing a module map, start by dumping the contents of the
834
1.75k
  // module itself before switching to the input buffer.
835
1.75k
  auto &Input = getCurrentInput();
836
1.75k
  if (Input.getKind().getFormat() == InputKind::ModuleMap) {
837
5
    if (Input.isFile()) {
838
5
      (*OS) << "# 1 \"";
839
5
      OS->write_escaped(Input.getFile());
840
5
      (*OS) << "\"\n";
841
5
    }
842
5
    getCurrentModule()->print(*OS);
843
5
    (*OS) << "#pragma clang module contents\n";
844
5
  }
845
1.75k
846
1.75k
  DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(),
847
1.75k
                           CI.getPreprocessorOutputOpts());
848
1.75k
}
849
850
2
void PrintPreambleAction::ExecuteAction() {
851
2
  switch (getCurrentFileKind().getLanguage()) {
852
2
  case Language::C:
853
2
  case Language::CXX:
854
2
  case Language::ObjC:
855
2
  case Language::ObjCXX:
856
2
  case Language::OpenCL:
857
2
  case Language::CUDA:
858
2
  case Language::HIP:
859
2
    break;
860
2
861
2
  case Language::Unknown:
862
0
  case Language::Asm:
863
0
  case Language::LLVM_IR:
864
0
  case Language::RenderScript:
865
0
    // We can't do anything with these.
866
0
    return;
867
2
  }
868
2
869
2
  // We don't expect to find any #include directives in a preprocessed input.
870
2
  if (getCurrentFileKind().isPreprocessed())
871
0
    return;
872
2
873
2
  CompilerInstance &CI = getCompilerInstance();
874
2
  auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
875
2
  if (Buffer) {
876
2
    unsigned Preamble =
877
2
        Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).Size;
878
2
    llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
879
2
  }
880
2
}
881
882
3
void DumpCompilerOptionsAction::ExecuteAction() {
883
3
  CompilerInstance &CI = getCompilerInstance();
884
3
  std::unique_ptr<raw_ostream> OSP =
885
3
      CI.createDefaultOutputFile(false, getCurrentFile());
886
3
  if (!OSP)
887
0
    return;
888
3
889
3
  raw_ostream &OS = *OSP;
890
3
  const Preprocessor &PP = CI.getPreprocessor();
891
3
  const LangOptions &LangOpts = PP.getLangOpts();
892
3
893
3
  // FIXME: Rather than manually format the JSON (which is awkward due to
894
3
  // needing to remove trailing commas), this should make use of a JSON library.
895
3
  // FIXME: Instead of printing enums as an integral value and specifying the
896
3
  // type as a separate field, use introspection to print the enumerator.
897
3
898
3
  OS << "{\n";
899
3
  OS << "\n\"features\" : [\n";
900
3
  {
901
3
    llvm::SmallString<128> Str;
902
3
#define FEATURE(Name, Predicate)                                               \
903
480
  ("\t{\"" #Name "\" : " + llvm::Twine(
Predicate20
?
"true"243
:
"false"237
) + "},\n") \
904
480
      .toVector(Str);
905
3
#include "clang/Basic/Features.def"
906
3
#undef FEATURE
907
3
    // Remove the newline and comma from the last entry to ensure this remains
908
3
    // valid JSON.
909
3
    OS << Str.substr(0, Str.size() - 2);
910
3
  }
911
3
  OS << "\n],\n";
912
3
913
3
  OS << "\n\"extensions\" : [\n";
914
3
  {
915
3
    llvm::SmallString<128> Str;
916
3
#define EXTENSION(Name, Predicate)                                             \
917
78
  ("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? 
"true"64
:
"false"14
) + "},\n") \
918
78
      .toVector(Str);
919
3
#include "clang/Basic/Features.def"
920
3
#undef EXTENSION
921
3
    // Remove the newline and comma from the last entry to ensure this remains
922
3
    // valid JSON.
923
3
    OS << Str.substr(0, Str.size() - 2);
924
3
  }
925
3
  OS << "\n]\n";
926
3
927
3
  OS << "}";
928
3
}
929
930
7
void PrintDependencyDirectivesSourceMinimizerAction::ExecuteAction() {
931
7
  CompilerInstance &CI = getCompilerInstance();
932
7
  SourceManager &SM = CI.getPreprocessor().getSourceManager();
933
7
  const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
934
7
935
7
  llvm::SmallString<1024> Output;
936
7
  llvm::SmallVector<minimize_source_to_dependency_directives::Token, 32> Toks;
937
7
  if (minimizeSourceToDependencyDirectives(
938
7
          FromFile->getBuffer(), Output, Toks, &CI.getDiagnostics(),
939
7
          SM.getLocForStartOfFile(SM.getMainFileID()))) {
940
3
    assert(CI.getDiagnostics().hasErrorOccurred() &&
941
3
           "no errors reported for failure");
942
3
943
3
    // Preprocess the source when verifying the diagnostics to capture the
944
3
    // 'expected' comments.
945
3
    if (CI.getDiagnosticOpts().VerifyDiagnostics) {
946
3
      // Make sure we don't emit new diagnostics!
947
3
      CI.getDiagnostics().setSuppressAllDiagnostics(true);
948
3
      Preprocessor &PP = getCompilerInstance().getPreprocessor();
949
3
      PP.EnterMainSourceFile();
950
3
      Token Tok;
951
11
      do {
952
11
        PP.Lex(Tok);
953
11
      } while (Tok.isNot(tok::eof));
954
3
    }
955
3
    return;
956
3
  }
957
4
  llvm::outs() << Output;
958
4
}