CodeGenAction.cpp 41 KB

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