1 //===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===//
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/CodeGen/CodeGenAction.h"
10 #include "CodeGenModule.h"
11 #include "CoverageMappingGen.h"
12 #include "MacroPPCallbacks.h"
13 #include "clang/AST/ASTConsumer.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/DeclCXX.h"
16 #include "clang/AST/DeclGroup.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "clang/Basic/TargetInfo.h"
20 #include "clang/CodeGen/BackendUtil.h"
21 #include "clang/CodeGen/ModuleBuilder.h"
22 #include "clang/Frontend/CompilerInstance.h"
23 #include "clang/Frontend/FrontendDiagnostic.h"
24 #include "clang/Lex/Preprocessor.h"
25 #include "llvm/Bitcode/BitcodeReader.h"
26 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
27 #include "llvm/IR/DebugInfo.h"
28 #include "llvm/IR/DiagnosticInfo.h"
29 #include "llvm/IR/DiagnosticPrinter.h"
30 #include "llvm/IR/GlobalValue.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IRReader/IRReader.h"
34 #include "llvm/Linker/Linker.h"
35 #include "llvm/Pass.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/SourceMgr.h"
38 #include "llvm/Support/Timer.h"
39 #include "llvm/Support/ToolOutputFile.h"
40 #include "llvm/Support/YAMLTraits.h"
41 #include "llvm/Transforms/IPO/Internalize.h"
42 
43 #include <memory>
44 using namespace clang;
45 using namespace llvm;
46 
47 namespace clang {
48   class BackendConsumer;
49   class ClangDiagnosticHandler final : public DiagnosticHandler {
50   public:
51     ClangDiagnosticHandler(const CodeGenOptions &CGOpts, BackendConsumer *BCon)
52         : CodeGenOpts(CGOpts), BackendCon(BCon) {}
53 
54     bool handleDiagnostics(const DiagnosticInfo &DI) override;
55 
56     bool isAnalysisRemarkEnabled(StringRef PassName) const override {
57       return (CodeGenOpts.OptimizationRemarkAnalysisPattern &&
58               CodeGenOpts.OptimizationRemarkAnalysisPattern->match(PassName));
59     }
60     bool isMissedOptRemarkEnabled(StringRef PassName) const override {
61       return (CodeGenOpts.OptimizationRemarkMissedPattern &&
62               CodeGenOpts.OptimizationRemarkMissedPattern->match(PassName));
63     }
64     bool isPassedOptRemarkEnabled(StringRef PassName) const override {
65       return (CodeGenOpts.OptimizationRemarkPattern &&
66               CodeGenOpts.OptimizationRemarkPattern->match(PassName));
67     }
68 
69     bool isAnyRemarkEnabled() const override {
70       return (CodeGenOpts.OptimizationRemarkAnalysisPattern ||
71               CodeGenOpts.OptimizationRemarkMissedPattern ||
72               CodeGenOpts.OptimizationRemarkPattern);
73     }
74 
75   private:
76     const CodeGenOptions &CodeGenOpts;
77     BackendConsumer *BackendCon;
78   };
79 
80   class BackendConsumer : public ASTConsumer {
81     using LinkModule = CodeGenAction::LinkModule;
82 
83     virtual void anchor();
84     DiagnosticsEngine &Diags;
85     BackendAction Action;
86     const HeaderSearchOptions &HeaderSearchOpts;
87     const CodeGenOptions &CodeGenOpts;
88     const TargetOptions &TargetOpts;
89     const LangOptions &LangOpts;
90     std::unique_ptr<raw_pwrite_stream> AsmOutStream;
91     ASTContext *Context;
92 
93     Timer LLVMIRGeneration;
94     unsigned LLVMIRGenerationRefCount;
95 
96     /// True if we've finished generating IR. This prevents us from generating
97     /// additional LLVM IR after emitting output in HandleTranslationUnit. This
98     /// can happen when Clang plugins trigger additional AST deserialization.
99     bool IRGenFinished = false;
100 
101     std::unique_ptr<CodeGenerator> Gen;
102 
103     SmallVector<LinkModule, 4> LinkModules;
104 
105     // This is here so that the diagnostic printer knows the module a diagnostic
106     // refers to.
107     llvm::Module *CurLinkModule = nullptr;
108 
109   public:
110     BackendConsumer(BackendAction Action, DiagnosticsEngine &Diags,
111                     const HeaderSearchOptions &HeaderSearchOpts,
112                     const PreprocessorOptions &PPOpts,
113                     const CodeGenOptions &CodeGenOpts,
114                     const TargetOptions &TargetOpts,
115                     const LangOptions &LangOpts, bool TimePasses,
116                     const std::string &InFile,
117                     SmallVector<LinkModule, 4> LinkModules,
118                     std::unique_ptr<raw_pwrite_stream> OS, LLVMContext &C,
119                     CoverageSourceInfo *CoverageInfo = nullptr)
120         : Diags(Diags), Action(Action), HeaderSearchOpts(HeaderSearchOpts),
121           CodeGenOpts(CodeGenOpts), TargetOpts(TargetOpts), LangOpts(LangOpts),
122           AsmOutStream(std::move(OS)), Context(nullptr),
123           LLVMIRGeneration("irgen", "LLVM IR Generation Time"),
124           LLVMIRGenerationRefCount(0),
125           Gen(CreateLLVMCodeGen(Diags, InFile, HeaderSearchOpts, PPOpts,
126                                 CodeGenOpts, C, CoverageInfo)),
127           LinkModules(std::move(LinkModules)) {
128       FrontendTimesIsEnabled = TimePasses;
129       llvm::TimePassesIsEnabled = TimePasses;
130     }
131     llvm::Module *getModule() const { return Gen->GetModule(); }
132     std::unique_ptr<llvm::Module> takeModule() {
133       return std::unique_ptr<llvm::Module>(Gen->ReleaseModule());
134     }
135 
136     CodeGenerator *getCodeGenerator() { return Gen.get(); }
137 
138     void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
139       Gen->HandleCXXStaticMemberVarInstantiation(VD);
140     }
141 
142     void Initialize(ASTContext &Ctx) override {
143       assert(!Context && "initialized multiple times");
144 
145       Context = &Ctx;
146 
147       if (FrontendTimesIsEnabled)
148         LLVMIRGeneration.startTimer();
149 
150       Gen->Initialize(Ctx);
151 
152       if (FrontendTimesIsEnabled)
153         LLVMIRGeneration.stopTimer();
154     }
155 
156     bool HandleTopLevelDecl(DeclGroupRef D) override {
157       PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
158                                      Context->getSourceManager(),
159                                      "LLVM IR generation of declaration");
160 
161       // Recurse.
162       if (FrontendTimesIsEnabled) {
163         LLVMIRGenerationRefCount += 1;
164         if (LLVMIRGenerationRefCount == 1)
165           LLVMIRGeneration.startTimer();
166       }
167 
168       Gen->HandleTopLevelDecl(D);
169 
170       if (FrontendTimesIsEnabled) {
171         LLVMIRGenerationRefCount -= 1;
172         if (LLVMIRGenerationRefCount == 0)
173           LLVMIRGeneration.stopTimer();
174       }
175 
176       return true;
177     }
178 
179     void HandleInlineFunctionDefinition(FunctionDecl *D) override {
180       PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
181                                      Context->getSourceManager(),
182                                      "LLVM IR generation of inline function");
183       if (FrontendTimesIsEnabled)
184         LLVMIRGeneration.startTimer();
185 
186       Gen->HandleInlineFunctionDefinition(D);
187 
188       if (FrontendTimesIsEnabled)
189         LLVMIRGeneration.stopTimer();
190     }
191 
192     void HandleInterestingDecl(DeclGroupRef D) override {
193       // Ignore interesting decls from the AST reader after IRGen is finished.
194       if (!IRGenFinished)
195         HandleTopLevelDecl(D);
196     }
197 
198     // Links each entry in LinkModules into our module.  Returns true on error.
199     bool LinkInModules() {
200       for (auto &LM : LinkModules) {
201         if (LM.PropagateAttrs)
202           for (Function &F : *LM.Module)
203             Gen->CGM().AddDefaultFnAttrs(F);
204 
205         CurLinkModule = LM.Module.get();
206 
207         bool Err;
208         if (LM.Internalize) {
209           Err = Linker::linkModules(
210               *getModule(), std::move(LM.Module), LM.LinkFlags,
211               [](llvm::Module &M, const llvm::StringSet<> &GVS) {
212                 internalizeModule(M, [&GVS](const llvm::GlobalValue &GV) {
213                   return !GV.hasName() || (GVS.count(GV.getName()) == 0);
214                 });
215               });
216         } else {
217           Err = Linker::linkModules(*getModule(), std::move(LM.Module),
218                                     LM.LinkFlags);
219         }
220 
221         if (Err)
222           return true;
223       }
224       return false; // success
225     }
226 
227     void HandleTranslationUnit(ASTContext &C) override {
228       {
229         PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
230         if (FrontendTimesIsEnabled) {
231           LLVMIRGenerationRefCount += 1;
232           if (LLVMIRGenerationRefCount == 1)
233             LLVMIRGeneration.startTimer();
234         }
235 
236         Gen->HandleTranslationUnit(C);
237 
238         if (FrontendTimesIsEnabled) {
239           LLVMIRGenerationRefCount -= 1;
240           if (LLVMIRGenerationRefCount == 0)
241             LLVMIRGeneration.stopTimer();
242         }
243 
244         IRGenFinished = true;
245       }
246 
247       // Silently ignore if we weren't initialized for some reason.
248       if (!getModule())
249         return;
250 
251       // Install an inline asm handler so that diagnostics get printed through
252       // our diagnostics hooks.
253       LLVMContext &Ctx = getModule()->getContext();
254       LLVMContext::InlineAsmDiagHandlerTy OldHandler =
255         Ctx.getInlineAsmDiagnosticHandler();
256       void *OldContext = Ctx.getInlineAsmDiagnosticContext();
257       Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
258 
259       std::unique_ptr<DiagnosticHandler> OldDiagnosticHandler =
260           Ctx.getDiagnosticHandler();
261       Ctx.setDiagnosticHandler(llvm::make_unique<ClangDiagnosticHandler>(
262         CodeGenOpts, this));
263       Ctx.setDiagnosticsHotnessRequested(CodeGenOpts.DiagnosticsWithHotness);
264       if (CodeGenOpts.DiagnosticsHotnessThreshold != 0)
265         Ctx.setDiagnosticsHotnessThreshold(
266             CodeGenOpts.DiagnosticsHotnessThreshold);
267 
268       std::unique_ptr<llvm::ToolOutputFile> OptRecordFile;
269       if (!CodeGenOpts.OptRecordFile.empty()) {
270         std::error_code EC;
271         OptRecordFile = llvm::make_unique<llvm::ToolOutputFile>(
272             CodeGenOpts.OptRecordFile, EC, sys::fs::F_None);
273         if (EC) {
274           Diags.Report(diag::err_cannot_open_file) <<
275             CodeGenOpts.OptRecordFile << EC.message();
276           return;
277         }
278 
279         Ctx.setDiagnosticsOutputFile(
280             llvm::make_unique<yaml::Output>(OptRecordFile->os()));
281 
282         if (CodeGenOpts.getProfileUse() != CodeGenOptions::ProfileNone)
283           Ctx.setDiagnosticsHotnessRequested(true);
284       }
285 
286       // Link each LinkModule into our module.
287       if (LinkInModules())
288         return;
289 
290       EmbedBitcode(getModule(), CodeGenOpts, llvm::MemoryBufferRef());
291 
292       EmitBackendOutput(Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts,
293                         LangOpts, C.getTargetInfo().getDataLayout(),
294                         getModule(), Action, std::move(AsmOutStream));
295 
296       Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
297 
298       Ctx.setDiagnosticHandler(std::move(OldDiagnosticHandler));
299 
300       if (OptRecordFile)
301         OptRecordFile->keep();
302     }
303 
304     void HandleTagDeclDefinition(TagDecl *D) override {
305       PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
306                                      Context->getSourceManager(),
307                                      "LLVM IR generation of declaration");
308       Gen->HandleTagDeclDefinition(D);
309     }
310 
311     void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
312       Gen->HandleTagDeclRequiredDefinition(D);
313     }
314 
315     void CompleteTentativeDefinition(VarDecl *D) override {
316       Gen->CompleteTentativeDefinition(D);
317     }
318 
319     void AssignInheritanceModel(CXXRecordDecl *RD) override {
320       Gen->AssignInheritanceModel(RD);
321     }
322 
323     void HandleVTable(CXXRecordDecl *RD) override {
324       Gen->HandleVTable(RD);
325     }
326 
327     static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
328                                      unsigned LocCookie) {
329       SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie);
330       ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
331     }
332 
333     /// Get the best possible source location to represent a diagnostic that
334     /// may have associated debug info.
335     const FullSourceLoc
336     getBestLocationFromDebugLoc(const llvm::DiagnosticInfoWithLocationBase &D,
337                                 bool &BadDebugInfo, StringRef &Filename,
338                                 unsigned &Line, unsigned &Column) const;
339 
340     void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
341                                SourceLocation LocCookie);
342 
343     void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI);
344     /// Specialized handler for InlineAsm diagnostic.
345     /// \return True if the diagnostic has been successfully reported, false
346     /// otherwise.
347     bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D);
348     /// Specialized handler for StackSize diagnostic.
349     /// \return True if the diagnostic has been successfully reported, false
350     /// otherwise.
351     bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D);
352     /// Specialized handler for unsupported backend feature diagnostic.
353     void UnsupportedDiagHandler(const llvm::DiagnosticInfoUnsupported &D);
354     /// Specialized handlers for optimization remarks.
355     /// Note that these handlers only accept remarks and they always handle
356     /// them.
357     void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D,
358                                  unsigned DiagID);
359     void
360     OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationBase &D);
361     void OptimizationRemarkHandler(
362         const llvm::OptimizationRemarkAnalysisFPCommute &D);
363     void OptimizationRemarkHandler(
364         const llvm::OptimizationRemarkAnalysisAliasing &D);
365     void OptimizationFailureHandler(
366         const llvm::DiagnosticInfoOptimizationFailure &D);
367   };
368 
369   void BackendConsumer::anchor() {}
370 }
371 
372 bool ClangDiagnosticHandler::handleDiagnostics(const DiagnosticInfo &DI) {
373   BackendCon->DiagnosticHandlerImpl(DI);
374   return true;
375 }
376 
377 /// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
378 /// buffer to be a valid FullSourceLoc.
379 static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
380                                             SourceManager &CSM) {
381   // Get both the clang and llvm source managers.  The location is relative to
382   // a memory buffer that the LLVM Source Manager is handling, we need to add
383   // a copy to the Clang source manager.
384   const llvm::SourceMgr &LSM = *D.getSourceMgr();
385 
386   // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
387   // already owns its one and clang::SourceManager wants to own its one.
388   const MemoryBuffer *LBuf =
389   LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
390 
391   // Create the copy and transfer ownership to clang::SourceManager.
392   // TODO: Avoid copying files into memory.
393   std::unique_ptr<llvm::MemoryBuffer> CBuf =
394       llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
395                                            LBuf->getBufferIdentifier());
396   // FIXME: Keep a file ID map instead of creating new IDs for each location.
397   FileID FID = CSM.createFileID(std::move(CBuf));
398 
399   // Translate the offset into the file.
400   unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
401   SourceLocation NewLoc =
402   CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
403   return FullSourceLoc(NewLoc, CSM);
404 }
405 
406 
407 /// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
408 /// error parsing inline asm.  The SMDiagnostic indicates the error relative to
409 /// the temporary memory buffer that the inline asm parser has set up.
410 void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
411                                             SourceLocation LocCookie) {
412   // There are a couple of different kinds of errors we could get here.  First,
413   // we re-format the SMDiagnostic in terms of a clang diagnostic.
414 
415   // Strip "error: " off the start of the message string.
416   StringRef Message = D.getMessage();
417   if (Message.startswith("error: "))
418     Message = Message.substr(7);
419 
420   // If the SMDiagnostic has an inline asm source location, translate it.
421   FullSourceLoc Loc;
422   if (D.getLoc() != SMLoc())
423     Loc = ConvertBackendLocation(D, Context->getSourceManager());
424 
425   unsigned DiagID;
426   switch (D.getKind()) {
427   case llvm::SourceMgr::DK_Error:
428     DiagID = diag::err_fe_inline_asm;
429     break;
430   case llvm::SourceMgr::DK_Warning:
431     DiagID = diag::warn_fe_inline_asm;
432     break;
433   case llvm::SourceMgr::DK_Note:
434     DiagID = diag::note_fe_inline_asm;
435     break;
436   case llvm::SourceMgr::DK_Remark:
437     llvm_unreachable("remarks unexpected");
438   }
439   // If this problem has clang-level source location information, report the
440   // issue in the source with a note showing the instantiated
441   // code.
442   if (LocCookie.isValid()) {
443     Diags.Report(LocCookie, DiagID).AddString(Message);
444 
445     if (D.getLoc().isValid()) {
446       DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
447       // Convert the SMDiagnostic ranges into SourceRange and attach them
448       // to the diagnostic.
449       for (const std::pair<unsigned, unsigned> &Range : D.getRanges()) {
450         unsigned Column = D.getColumnNo();
451         B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
452                          Loc.getLocWithOffset(Range.second - Column));
453       }
454     }
455     return;
456   }
457 
458   // Otherwise, report the backend issue as occurring in the generated .s file.
459   // If Loc is invalid, we still need to report the issue, it just gets no
460   // location info.
461   Diags.Report(Loc, DiagID).AddString(Message);
462 }
463 
464 #define ComputeDiagID(Severity, GroupName, DiagID)                             \
465   do {                                                                         \
466     switch (Severity) {                                                        \
467     case llvm::DS_Error:                                                       \
468       DiagID = diag::err_fe_##GroupName;                                       \
469       break;                                                                   \
470     case llvm::DS_Warning:                                                     \
471       DiagID = diag::warn_fe_##GroupName;                                      \
472       break;                                                                   \
473     case llvm::DS_Remark:                                                      \
474       llvm_unreachable("'remark' severity not expected");                      \
475       break;                                                                   \
476     case llvm::DS_Note:                                                        \
477       DiagID = diag::note_fe_##GroupName;                                      \
478       break;                                                                   \
479     }                                                                          \
480   } while (false)
481 
482 #define ComputeDiagRemarkID(Severity, GroupName, DiagID)                       \
483   do {                                                                         \
484     switch (Severity) {                                                        \
485     case llvm::DS_Error:                                                       \
486       DiagID = diag::err_fe_##GroupName;                                       \
487       break;                                                                   \
488     case llvm::DS_Warning:                                                     \
489       DiagID = diag::warn_fe_##GroupName;                                      \
490       break;                                                                   \
491     case llvm::DS_Remark:                                                      \
492       DiagID = diag::remark_fe_##GroupName;                                    \
493       break;                                                                   \
494     case llvm::DS_Note:                                                        \
495       DiagID = diag::note_fe_##GroupName;                                      \
496       break;                                                                   \
497     }                                                                          \
498   } while (false)
499 
500 bool
501 BackendConsumer::InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D) {
502   unsigned DiagID;
503   ComputeDiagID(D.getSeverity(), inline_asm, DiagID);
504   std::string Message = D.getMsgStr().str();
505 
506   // If this problem has clang-level source location information, report the
507   // issue as being a problem in the source with a note showing the instantiated
508   // code.
509   SourceLocation LocCookie =
510       SourceLocation::getFromRawEncoding(D.getLocCookie());
511   if (LocCookie.isValid())
512     Diags.Report(LocCookie, DiagID).AddString(Message);
513   else {
514     // Otherwise, report the backend diagnostic as occurring in the generated
515     // .s file.
516     // If Loc is invalid, we still need to report the diagnostic, it just gets
517     // no location info.
518     FullSourceLoc Loc;
519     Diags.Report(Loc, DiagID).AddString(Message);
520   }
521   // We handled all the possible severities.
522   return true;
523 }
524 
525 bool
526 BackendConsumer::StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D) {
527   if (D.getSeverity() != llvm::DS_Warning)
528     // For now, the only support we have for StackSize diagnostic is warning.
529     // We do not know how to format other severities.
530     return false;
531 
532   if (const Decl *ND = Gen->GetDeclForMangledName(D.getFunction().getName())) {
533     // FIXME: Shouldn't need to truncate to uint32_t
534     Diags.Report(ND->getASTContext().getFullLoc(ND->getLocation()),
535                  diag::warn_fe_frame_larger_than)
536       << static_cast<uint32_t>(D.getStackSize()) << Decl::castToDeclContext(ND);
537     return true;
538   }
539 
540   return false;
541 }
542 
543 const FullSourceLoc BackendConsumer::getBestLocationFromDebugLoc(
544     const llvm::DiagnosticInfoWithLocationBase &D, bool &BadDebugInfo,
545     StringRef &Filename, unsigned &Line, unsigned &Column) const {
546   SourceManager &SourceMgr = Context->getSourceManager();
547   FileManager &FileMgr = SourceMgr.getFileManager();
548   SourceLocation DILoc;
549 
550   if (D.isLocationAvailable()) {
551     D.getLocation(Filename, Line, Column);
552     if (Line > 0) {
553       const FileEntry *FE = FileMgr.getFile(Filename);
554       if (!FE)
555         FE = FileMgr.getFile(D.getAbsolutePath());
556       if (FE) {
557         // If -gcolumn-info was not used, Column will be 0. This upsets the
558         // source manager, so pass 1 if Column is not set.
559         DILoc = SourceMgr.translateFileLineCol(FE, Line, Column ? Column : 1);
560       }
561     }
562     BadDebugInfo = DILoc.isInvalid();
563   }
564 
565   // If a location isn't available, try to approximate it using the associated
566   // function definition. We use the definition's right brace to differentiate
567   // from diagnostics that genuinely relate to the function itself.
568   FullSourceLoc Loc(DILoc, SourceMgr);
569   if (Loc.isInvalid())
570     if (const Decl *FD = Gen->GetDeclForMangledName(D.getFunction().getName()))
571       Loc = FD->getASTContext().getFullLoc(FD->getLocation());
572 
573   if (DILoc.isInvalid() && D.isLocationAvailable())
574     // If we were not able to translate the file:line:col information
575     // back to a SourceLocation, at least emit a note stating that
576     // we could not translate this location. This can happen in the
577     // case of #line directives.
578     Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
579         << Filename << Line << Column;
580 
581   return Loc;
582 }
583 
584 void BackendConsumer::UnsupportedDiagHandler(
585     const llvm::DiagnosticInfoUnsupported &D) {
586   // We only support errors.
587   assert(D.getSeverity() == llvm::DS_Error);
588 
589   StringRef Filename;
590   unsigned Line, Column;
591   bool BadDebugInfo = false;
592   FullSourceLoc Loc =
593       getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column);
594 
595   Diags.Report(Loc, diag::err_fe_backend_unsupported) << D.getMessage().str();
596 
597   if (BadDebugInfo)
598     // If we were not able to translate the file:line:col information
599     // back to a SourceLocation, at least emit a note stating that
600     // we could not translate this location. This can happen in the
601     // case of #line directives.
602     Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
603         << Filename << Line << Column;
604 }
605 
606 void BackendConsumer::EmitOptimizationMessage(
607     const llvm::DiagnosticInfoOptimizationBase &D, unsigned DiagID) {
608   // We only support warnings and remarks.
609   assert(D.getSeverity() == llvm::DS_Remark ||
610          D.getSeverity() == llvm::DS_Warning);
611 
612   StringRef Filename;
613   unsigned Line, Column;
614   bool BadDebugInfo = false;
615   FullSourceLoc Loc =
616       getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column);
617 
618   std::string Msg;
619   raw_string_ostream MsgStream(Msg);
620   MsgStream << D.getMsg();
621 
622   if (D.getHotness())
623     MsgStream << " (hotness: " << *D.getHotness() << ")";
624 
625   Diags.Report(Loc, DiagID)
626       << AddFlagValue(D.getPassName())
627       << MsgStream.str();
628 
629   if (BadDebugInfo)
630     // If we were not able to translate the file:line:col information
631     // back to a SourceLocation, at least emit a note stating that
632     // we could not translate this location. This can happen in the
633     // case of #line directives.
634     Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
635         << Filename << Line << Column;
636 }
637 
638 void BackendConsumer::OptimizationRemarkHandler(
639     const llvm::DiagnosticInfoOptimizationBase &D) {
640   // Without hotness information, don't show noisy remarks.
641   if (D.isVerbose() && !D.getHotness())
642     return;
643 
644   if (D.isPassed()) {
645     // Optimization remarks are active only if the -Rpass flag has a regular
646     // expression that matches the name of the pass name in \p D.
647     if (CodeGenOpts.OptimizationRemarkPattern &&
648         CodeGenOpts.OptimizationRemarkPattern->match(D.getPassName()))
649       EmitOptimizationMessage(D, diag::remark_fe_backend_optimization_remark);
650   } else if (D.isMissed()) {
651     // Missed optimization remarks are active only if the -Rpass-missed
652     // flag has a regular expression that matches the name of the pass
653     // name in \p D.
654     if (CodeGenOpts.OptimizationRemarkMissedPattern &&
655         CodeGenOpts.OptimizationRemarkMissedPattern->match(D.getPassName()))
656       EmitOptimizationMessage(
657           D, diag::remark_fe_backend_optimization_remark_missed);
658   } else {
659     assert(D.isAnalysis() && "Unknown remark type");
660 
661     bool ShouldAlwaysPrint = false;
662     if (auto *ORA = dyn_cast<llvm::OptimizationRemarkAnalysis>(&D))
663       ShouldAlwaysPrint = ORA->shouldAlwaysPrint();
664 
665     if (ShouldAlwaysPrint ||
666         (CodeGenOpts.OptimizationRemarkAnalysisPattern &&
667          CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName())))
668       EmitOptimizationMessage(
669           D, diag::remark_fe_backend_optimization_remark_analysis);
670   }
671 }
672 
673 void BackendConsumer::OptimizationRemarkHandler(
674     const llvm::OptimizationRemarkAnalysisFPCommute &D) {
675   // Optimization analysis remarks are active if the pass name is set to
676   // llvm::DiagnosticInfo::AlwasyPrint or if the -Rpass-analysis flag has a
677   // regular expression that matches the name of the pass name in \p D.
678 
679   if (D.shouldAlwaysPrint() ||
680       (CodeGenOpts.OptimizationRemarkAnalysisPattern &&
681        CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName())))
682     EmitOptimizationMessage(
683         D, diag::remark_fe_backend_optimization_remark_analysis_fpcommute);
684 }
685 
686 void BackendConsumer::OptimizationRemarkHandler(
687     const llvm::OptimizationRemarkAnalysisAliasing &D) {
688   // Optimization analysis remarks are active if the pass name is set to
689   // llvm::DiagnosticInfo::AlwasyPrint or if the -Rpass-analysis flag has a
690   // regular expression that matches the name of the pass name in \p D.
691 
692   if (D.shouldAlwaysPrint() ||
693       (CodeGenOpts.OptimizationRemarkAnalysisPattern &&
694        CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName())))
695     EmitOptimizationMessage(
696         D, diag::remark_fe_backend_optimization_remark_analysis_aliasing);
697 }
698 
699 void BackendConsumer::OptimizationFailureHandler(
700     const llvm::DiagnosticInfoOptimizationFailure &D) {
701   EmitOptimizationMessage(D, diag::warn_fe_backend_optimization_failure);
702 }
703 
704 /// This function is invoked when the backend needs
705 /// to report something to the user.
706 void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) {
707   unsigned DiagID = diag::err_fe_inline_asm;
708   llvm::DiagnosticSeverity Severity = DI.getSeverity();
709   // Get the diagnostic ID based.
710   switch (DI.getKind()) {
711   case llvm::DK_InlineAsm:
712     if (InlineAsmDiagHandler(cast<DiagnosticInfoInlineAsm>(DI)))
713       return;
714     ComputeDiagID(Severity, inline_asm, DiagID);
715     break;
716   case llvm::DK_StackSize:
717     if (StackSizeDiagHandler(cast<DiagnosticInfoStackSize>(DI)))
718       return;
719     ComputeDiagID(Severity, backend_frame_larger_than, DiagID);
720     break;
721   case DK_Linker:
722     assert(CurLinkModule);
723     // FIXME: stop eating the warnings and notes.
724     if (Severity != DS_Error)
725       return;
726     DiagID = diag::err_fe_cannot_link_module;
727     break;
728   case llvm::DK_OptimizationRemark:
729     // Optimization remarks are always handled completely by this
730     // handler. There is no generic way of emitting them.
731     OptimizationRemarkHandler(cast<OptimizationRemark>(DI));
732     return;
733   case llvm::DK_OptimizationRemarkMissed:
734     // Optimization remarks are always handled completely by this
735     // handler. There is no generic way of emitting them.
736     OptimizationRemarkHandler(cast<OptimizationRemarkMissed>(DI));
737     return;
738   case llvm::DK_OptimizationRemarkAnalysis:
739     // Optimization remarks are always handled completely by this
740     // handler. There is no generic way of emitting them.
741     OptimizationRemarkHandler(cast<OptimizationRemarkAnalysis>(DI));
742     return;
743   case llvm::DK_OptimizationRemarkAnalysisFPCommute:
744     // Optimization remarks are always handled completely by this
745     // handler. There is no generic way of emitting them.
746     OptimizationRemarkHandler(cast<OptimizationRemarkAnalysisFPCommute>(DI));
747     return;
748   case llvm::DK_OptimizationRemarkAnalysisAliasing:
749     // Optimization remarks are always handled completely by this
750     // handler. There is no generic way of emitting them.
751     OptimizationRemarkHandler(cast<OptimizationRemarkAnalysisAliasing>(DI));
752     return;
753   case llvm::DK_MachineOptimizationRemark:
754     // Optimization remarks are always handled completely by this
755     // handler. There is no generic way of emitting them.
756     OptimizationRemarkHandler(cast<MachineOptimizationRemark>(DI));
757     return;
758   case llvm::DK_MachineOptimizationRemarkMissed:
759     // Optimization remarks are always handled completely by this
760     // handler. There is no generic way of emitting them.
761     OptimizationRemarkHandler(cast<MachineOptimizationRemarkMissed>(DI));
762     return;
763   case llvm::DK_MachineOptimizationRemarkAnalysis:
764     // Optimization remarks are always handled completely by this
765     // handler. There is no generic way of emitting them.
766     OptimizationRemarkHandler(cast<MachineOptimizationRemarkAnalysis>(DI));
767     return;
768   case llvm::DK_OptimizationFailure:
769     // Optimization failures are always handled completely by this
770     // handler.
771     OptimizationFailureHandler(cast<DiagnosticInfoOptimizationFailure>(DI));
772     return;
773   case llvm::DK_Unsupported:
774     UnsupportedDiagHandler(cast<DiagnosticInfoUnsupported>(DI));
775     return;
776   default:
777     // Plugin IDs are not bound to any value as they are set dynamically.
778     ComputeDiagRemarkID(Severity, backend_plugin, DiagID);
779     break;
780   }
781   std::string MsgStorage;
782   {
783     raw_string_ostream Stream(MsgStorage);
784     DiagnosticPrinterRawOStream DP(Stream);
785     DI.print(DP);
786   }
787 
788   if (DiagID == diag::err_fe_cannot_link_module) {
789     Diags.Report(diag::err_fe_cannot_link_module)
790         << CurLinkModule->getModuleIdentifier() << MsgStorage;
791     return;
792   }
793 
794   // Report the backend message using the usual diagnostic mechanism.
795   FullSourceLoc Loc;
796   Diags.Report(Loc, DiagID).AddString(MsgStorage);
797 }
798 #undef ComputeDiagID
799 
800 CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
801     : Act(_Act), VMContext(_VMContext ? _VMContext : new LLVMContext),
802       OwnsVMContext(!_VMContext) {}
803 
804 CodeGenAction::~CodeGenAction() {
805   TheModule.reset();
806   if (OwnsVMContext)
807     delete VMContext;
808 }
809 
810 bool CodeGenAction::hasIRSupport() const { return true; }
811 
812 void CodeGenAction::EndSourceFileAction() {
813   // If the consumer creation failed, do nothing.
814   if (!getCompilerInstance().hasASTConsumer())
815     return;
816 
817   // Steal the module from the consumer.
818   TheModule = BEConsumer->takeModule();
819 }
820 
821 std::unique_ptr<llvm::Module> CodeGenAction::takeModule() {
822   return std::move(TheModule);
823 }
824 
825 llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
826   OwnsVMContext = false;
827   return VMContext;
828 }
829 
830 static std::unique_ptr<raw_pwrite_stream>
831 GetOutputStream(CompilerInstance &CI, StringRef InFile, BackendAction Action) {
832   switch (Action) {
833   case Backend_EmitAssembly:
834     return CI.createDefaultOutputFile(false, InFile, "s");
835   case Backend_EmitLL:
836     return CI.createDefaultOutputFile(false, InFile, "ll");
837   case Backend_EmitBC:
838     return CI.createDefaultOutputFile(true, InFile, "bc");
839   case Backend_EmitNothing:
840     return nullptr;
841   case Backend_EmitMCNull:
842     return CI.createNullOutputFile();
843   case Backend_EmitObj:
844     return CI.createDefaultOutputFile(true, InFile, "o");
845   }
846 
847   llvm_unreachable("Invalid action!");
848 }
849 
850 std::unique_ptr<ASTConsumer>
851 CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
852   BackendAction BA = static_cast<BackendAction>(Act);
853   std::unique_ptr<raw_pwrite_stream> OS = CI.takeOutputStream();
854   if (!OS)
855     OS = GetOutputStream(CI, InFile, BA);
856 
857   if (BA != Backend_EmitNothing && !OS)
858     return nullptr;
859 
860   // Load bitcode modules to link with, if we need to.
861   if (LinkModules.empty())
862     for (const CodeGenOptions::BitcodeFileToLink &F :
863          CI.getCodeGenOpts().LinkBitcodeFiles) {
864       auto BCBuf = CI.getFileManager().getBufferForFile(F.Filename);
865       if (!BCBuf) {
866         CI.getDiagnostics().Report(diag::err_cannot_open_file)
867             << F.Filename << BCBuf.getError().message();
868         LinkModules.clear();
869         return nullptr;
870       }
871 
872       Expected<std::unique_ptr<llvm::Module>> ModuleOrErr =
873           getOwningLazyBitcodeModule(std::move(*BCBuf), *VMContext);
874       if (!ModuleOrErr) {
875         handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
876           CI.getDiagnostics().Report(diag::err_cannot_open_file)
877               << F.Filename << EIB.message();
878         });
879         LinkModules.clear();
880         return nullptr;
881       }
882       LinkModules.push_back({std::move(ModuleOrErr.get()), F.PropagateAttrs,
883                              F.Internalize, F.LinkFlags});
884     }
885 
886   CoverageSourceInfo *CoverageInfo = nullptr;
887   // Add the preprocessor callback only when the coverage mapping is generated.
888   if (CI.getCodeGenOpts().CoverageMapping) {
889     CoverageInfo = new CoverageSourceInfo;
890     CI.getPreprocessor().addPPCallbacks(
891                                     std::unique_ptr<PPCallbacks>(CoverageInfo));
892   }
893 
894   std::unique_ptr<BackendConsumer> Result(new BackendConsumer(
895       BA, CI.getDiagnostics(), CI.getHeaderSearchOpts(),
896       CI.getPreprocessorOpts(), CI.getCodeGenOpts(), CI.getTargetOpts(),
897       CI.getLangOpts(), CI.getFrontendOpts().ShowTimers, InFile,
898       std::move(LinkModules), std::move(OS), *VMContext, CoverageInfo));
899   BEConsumer = Result.get();
900 
901   // Enable generating macro debug info only when debug info is not disabled and
902   // also macro debug info is enabled.
903   if (CI.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo &&
904       CI.getCodeGenOpts().MacroDebugInfo) {
905     std::unique_ptr<PPCallbacks> Callbacks =
906         llvm::make_unique<MacroPPCallbacks>(BEConsumer->getCodeGenerator(),
907                                             CI.getPreprocessor());
908     CI.getPreprocessor().addPPCallbacks(std::move(Callbacks));
909   }
910 
911   return std::move(Result);
912 }
913 
914 static void BitcodeInlineAsmDiagHandler(const llvm::SMDiagnostic &SM,
915                                          void *Context,
916                                          unsigned LocCookie) {
917   SM.print(nullptr, llvm::errs());
918 
919   auto Diags = static_cast<DiagnosticsEngine *>(Context);
920   unsigned DiagID;
921   switch (SM.getKind()) {
922   case llvm::SourceMgr::DK_Error:
923     DiagID = diag::err_fe_inline_asm;
924     break;
925   case llvm::SourceMgr::DK_Warning:
926     DiagID = diag::warn_fe_inline_asm;
927     break;
928   case llvm::SourceMgr::DK_Note:
929     DiagID = diag::note_fe_inline_asm;
930     break;
931   case llvm::SourceMgr::DK_Remark:
932     llvm_unreachable("remarks unexpected");
933   }
934 
935   Diags->Report(DiagID).AddString("cannot compile inline asm");
936 }
937 
938 std::unique_ptr<llvm::Module> CodeGenAction::loadModule(MemoryBufferRef MBRef) {
939   CompilerInstance &CI = getCompilerInstance();
940   SourceManager &SM = CI.getSourceManager();
941 
942   // For ThinLTO backend invocations, ensure that the context
943   // merges types based on ODR identifiers. We also need to read
944   // the correct module out of a multi-module bitcode file.
945   if (!CI.getCodeGenOpts().ThinLTOIndexFile.empty()) {
946     VMContext->enableDebugTypeODRUniquing();
947 
948     auto DiagErrors = [&](Error E) -> std::unique_ptr<llvm::Module> {
949       unsigned DiagID =
950           CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0");
951       handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
952         CI.getDiagnostics().Report(DiagID) << EIB.message();
953       });
954       return {};
955     };
956 
957     Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
958     if (!BMsOrErr)
959       return DiagErrors(BMsOrErr.takeError());
960     BitcodeModule *Bm = FindThinLTOModule(*BMsOrErr);
961     // We have nothing to do if the file contains no ThinLTO module. This is
962     // possible if ThinLTO compilation was not able to split module. Content of
963     // the file was already processed by indexing and will be passed to the
964     // linker using merged object file.
965     if (!Bm) {
966       auto M = llvm::make_unique<llvm::Module>("empty", *VMContext);
967       M->setTargetTriple(CI.getTargetOpts().Triple);
968       return M;
969     }
970     Expected<std::unique_ptr<llvm::Module>> MOrErr =
971         Bm->parseModule(*VMContext);
972     if (!MOrErr)
973       return DiagErrors(MOrErr.takeError());
974     return std::move(*MOrErr);
975   }
976 
977   llvm::SMDiagnostic Err;
978   if (std::unique_ptr<llvm::Module> M = parseIR(MBRef, Err, *VMContext))
979     return M;
980 
981   // Translate from the diagnostic info to the SourceManager location if
982   // available.
983   // TODO: Unify this with ConvertBackendLocation()
984   SourceLocation Loc;
985   if (Err.getLineNo() > 0) {
986     assert(Err.getColumnNo() >= 0);
987     Loc = SM.translateFileLineCol(SM.getFileEntryForID(SM.getMainFileID()),
988                                   Err.getLineNo(), Err.getColumnNo() + 1);
989   }
990 
991   // Strip off a leading diagnostic code if there is one.
992   StringRef Msg = Err.getMessage();
993   if (Msg.startswith("error: "))
994     Msg = Msg.substr(7);
995 
996   unsigned DiagID =
997       CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0");
998 
999   CI.getDiagnostics().Report(Loc, DiagID) << Msg;
1000   return {};
1001 }
1002 
1003 void CodeGenAction::ExecuteAction() {
1004   // If this is an IR file, we have to treat it specially.
1005   if (getCurrentFileKind().getLanguage() == InputKind::LLVM_IR) {
1006     BackendAction BA = static_cast<BackendAction>(Act);
1007     CompilerInstance &CI = getCompilerInstance();
1008     std::unique_ptr<raw_pwrite_stream> OS =
1009         GetOutputStream(CI, getCurrentFile(), BA);
1010     if (BA != Backend_EmitNothing && !OS)
1011       return;
1012 
1013     bool Invalid;
1014     SourceManager &SM = CI.getSourceManager();
1015     FileID FID = SM.getMainFileID();
1016     llvm::MemoryBuffer *MainFile = SM.getBuffer(FID, &Invalid);
1017     if (Invalid)
1018       return;
1019 
1020     TheModule = loadModule(*MainFile);
1021     if (!TheModule)
1022       return;
1023 
1024     const TargetOptions &TargetOpts = CI.getTargetOpts();
1025     if (TheModule->getTargetTriple() != TargetOpts.Triple) {
1026       CI.getDiagnostics().Report(SourceLocation(),
1027                                  diag::warn_fe_override_module)
1028           << TargetOpts.Triple;
1029       TheModule->setTargetTriple(TargetOpts.Triple);
1030     }
1031 
1032     EmbedBitcode(TheModule.get(), CI.getCodeGenOpts(),
1033                  MainFile->getMemBufferRef());
1034 
1035     LLVMContext &Ctx = TheModule->getContext();
1036     Ctx.setInlineAsmDiagnosticHandler(BitcodeInlineAsmDiagHandler,
1037                                       &CI.getDiagnostics());
1038 
1039     EmitBackendOutput(CI.getDiagnostics(), CI.getHeaderSearchOpts(),
1040                       CI.getCodeGenOpts(), TargetOpts, CI.getLangOpts(),
1041                       CI.getTarget().getDataLayout(), TheModule.get(), BA,
1042                       std::move(OS));
1043     return;
1044   }
1045 
1046   // Otherwise follow the normal AST path.
1047   this->ASTFrontendAction::ExecuteAction();
1048 }
1049 
1050 //
1051 
1052 void EmitAssemblyAction::anchor() { }
1053 EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
1054   : CodeGenAction(Backend_EmitAssembly, _VMContext) {}
1055 
1056 void EmitBCAction::anchor() { }
1057 EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
1058   : CodeGenAction(Backend_EmitBC, _VMContext) {}
1059 
1060 void EmitLLVMAction::anchor() { }
1061 EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
1062   : CodeGenAction(Backend_EmitLL, _VMContext) {}
1063 
1064 void EmitLLVMOnlyAction::anchor() { }
1065 EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
1066   : CodeGenAction(Backend_EmitNothing, _VMContext) {}
1067 
1068 void EmitCodeGenOnlyAction::anchor() { }
1069 EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
1070   : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
1071 
1072 void EmitObjAction::anchor() { }
1073 EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
1074   : CodeGenAction(Backend_EmitObj, _VMContext) {}
1075