Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 1 | //===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===// |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 10 | #include "clang/CodeGen/CodeGenAction.h" |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | b9bbd54 | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 13 | #include "clang/AST/DeclGroup.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/Basic/FileManager.h" |
| 15 | #include "clang/Basic/SourceManager.h" |
| 16 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 17 | #include "clang/CodeGen/BackendUtil.h" |
Daniel Dunbar | b9bbd54 | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 18 | #include "clang/CodeGen/ModuleBuilder.h" |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | acadc55 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/FrontendDiagnostic.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/OwningPtr.h" |
| 22 | #include "llvm/ADT/SmallString.h" |
| 23 | #include "llvm/Bitcode/ReaderWriter.h" |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DiagnosticInfo.h" |
| 25 | #include "llvm/IR/DiagnosticPrinter.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 26 | #include "llvm/IR/LLVMContext.h" |
| 27 | #include "llvm/IR/Module.h" |
Chandler Carruth | b45836a | 2013-03-26 02:25:54 +0000 | [diff] [blame] | 28 | #include "llvm/IRReader/IRReader.h" |
Chandler Carruth | 00fa3f7 | 2014-03-06 03:46:44 +0000 | [diff] [blame] | 29 | #include "llvm/Linker/Linker.h" |
Daniel Dunbar | 3e11152 | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 30 | #include "llvm/Pass.h" |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MemoryBuffer.h" |
| 32 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Timer.h" |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | using namespace llvm; |
| 36 | |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 37 | namespace clang { |
Benjamin Kramer | 16634c2 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 38 | class BackendConsumer : public ASTConsumer { |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 39 | virtual void anchor(); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 40 | DiagnosticsEngine &Diags; |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 41 | BackendAction Action; |
Daniel Dunbar | de18224 | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 42 | const CodeGenOptions &CodeGenOpts; |
Daniel Dunbar | de18224 | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 43 | const TargetOptions &TargetOpts; |
Dan Gohman | fec0ff8 | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 44 | const LangOptions &LangOpts; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 45 | raw_ostream *AsmOutStream; |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 46 | ASTContext *Context; |
Daniel Dunbar | b3a36cf | 2008-10-29 08:50:02 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 48 | Timer LLVMIRGeneration; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 50 | OwningPtr<CodeGenerator> Gen; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 52 | OwningPtr<llvm::Module> TheModule, LinkModule; |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 53 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | public: |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 55 | BackendConsumer(BackendAction action, DiagnosticsEngine &_Diags, |
Daniel Dunbar | 6d5824f | 2010-06-07 23:19:17 +0000 | [diff] [blame] | 56 | const CodeGenOptions &compopts, |
Dan Gohman | fec0ff8 | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 57 | const TargetOptions &targetopts, |
Rafael Espindola | 666a2ab | 2013-12-18 16:38:48 +0000 | [diff] [blame] | 58 | const LangOptions &langopts, bool TimePasses, |
| 59 | const std::string &infile, llvm::Module *LinkModule, |
| 60 | raw_ostream *OS, LLVMContext &C) |
| 61 | : Diags(_Diags), Action(action), CodeGenOpts(compopts), |
| 62 | TargetOpts(targetopts), LangOpts(langopts), AsmOutStream(OS), |
| 63 | Context(), LLVMIRGeneration("LLVM IR Generation Time"), |
| 64 | Gen(CreateLLVMCodeGen(Diags, infile, compopts, targetopts, C)), |
| 65 | LinkModule(LinkModule) { |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 66 | llvm::TimePassesIsEnabled = TimePasses; |
Chris Lattner | deffa13 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 67 | } |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 68 | |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame^] | 69 | llvm::Module *takeModule() { return TheModule.release(); } |
| 70 | llvm::Module *takeLinkModule() { return LinkModule.release(); } |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 71 | |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 72 | virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) { |
| 73 | Gen->HandleCXXStaticMemberVarInstantiation(VD); |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Chris Lattner | 5cf49fe | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 76 | virtual void Initialize(ASTContext &Ctx) { |
| 77 | Context = &Ctx; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 79 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 80 | LLVMIRGeneration.startTimer(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 5cf49fe | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 82 | Gen->Initialize(Ctx); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 83 | |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 84 | TheModule.reset(Gen->GetModule()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 86 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 87 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 88 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Argyrios Kyrtzidis | 841dd88 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 90 | virtual bool HandleTopLevelDecl(DeclGroupRef D) { |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 91 | PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(), |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 92 | Context->getSourceManager(), |
| 93 | "LLVM IR generation of declaration"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 95 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 96 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 97 | |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 98 | Gen->HandleTopLevelDecl(D); |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 99 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 100 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 101 | LLVMIRGeneration.stopTimer(); |
Argyrios Kyrtzidis | 841dd88 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 102 | |
| 103 | return true; |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 104 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 106 | virtual void HandleTranslationUnit(ASTContext &C) { |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 107 | { |
Chris Lattner | e46de75 | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 108 | PrettyStackTraceString CrashInfo("Per-file LLVM IR generation"); |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 109 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 110 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 111 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 112 | Gen->HandleTranslationUnit(C); |
Daniel Dunbar | a94d873 | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 113 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 114 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 115 | LLVMIRGeneration.stopTimer(); |
| 116 | } |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 117 | |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 118 | // Silently ignore if we weren't initialized for some reason. |
Daniel Dunbar | 3e11152 | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 119 | if (!TheModule) |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 120 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 122 | // Make sure IR generation is happy with the module. This is released by |
| 123 | // the module provider. |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 124 | llvm::Module *M = Gen->ReleaseModule(); |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 125 | if (!M) { |
| 126 | // The module has been released by IR gen on failures, do not double |
| 127 | // free. |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame^] | 128 | TheModule.release(); |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | |
| 132 | assert(TheModule.get() == M && |
| 133 | "Unexpected module change during IR generation"); |
| 134 | |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 135 | // Link LinkModule into this module if present, preserving its validity. |
| 136 | if (LinkModule) { |
| 137 | std::string ErrorMsg; |
| 138 | if (Linker::LinkModules(M, LinkModule.get(), Linker::PreserveSource, |
| 139 | &ErrorMsg)) { |
| 140 | Diags.Report(diag::err_fe_cannot_link_module) |
| 141 | << LinkModule->getModuleIdentifier() << ErrorMsg; |
| 142 | return; |
| 143 | } |
| 144 | } |
| 145 | |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 146 | // Install an inline asm handler so that diagnostics get printed through |
| 147 | // our diagnostics hooks. |
| 148 | LLVMContext &Ctx = TheModule->getContext(); |
Chris Lattner | 068f2ab | 2010-11-17 08:13:04 +0000 | [diff] [blame] | 149 | LLVMContext::InlineAsmDiagHandlerTy OldHandler = |
| 150 | Ctx.getInlineAsmDiagnosticHandler(); |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 151 | void *OldContext = Ctx.getInlineAsmDiagnosticContext(); |
Chris Lattner | 068f2ab | 2010-11-17 08:13:04 +0000 | [diff] [blame] | 152 | Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this); |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 153 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 154 | LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler = |
| 155 | Ctx.getDiagnosticHandler(); |
| 156 | void *OldDiagnosticContext = Ctx.getDiagnosticContext(); |
| 157 | Ctx.setDiagnosticHandler(DiagnosticHandler, this); |
| 158 | |
Dan Gohman | fec0ff8 | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 159 | EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts, |
Alp Toker | e83b906 | 2014-01-02 15:08:04 +0000 | [diff] [blame] | 160 | C.getTargetInfo().getTargetDescription(), |
Daniel Dunbar | 3e11152 | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 161 | TheModule.get(), Action, AsmOutStream); |
Rafael Espindola | 666a2ab | 2013-12-18 16:38:48 +0000 | [diff] [blame] | 162 | |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 163 | Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext); |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 164 | |
| 165 | Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 166 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 168 | virtual void HandleTagDeclDefinition(TagDecl *D) { |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 169 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 170 | Context->getSourceManager(), |
| 171 | "LLVM IR generation of declaration"); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 172 | Gen->HandleTagDeclDefinition(D); |
| 173 | } |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 174 | |
David Blaikie | 48ad6dc | 2013-07-13 21:08:14 +0000 | [diff] [blame] | 175 | virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) { |
| 176 | Gen->HandleTagDeclRequiredDefinition(D); |
| 177 | } |
| 178 | |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 179 | virtual void CompleteTentativeDefinition(VarDecl *D) { |
| 180 | Gen->CompleteTentativeDefinition(D); |
| 181 | } |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 182 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 183 | virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) { |
| 184 | Gen->HandleVTable(RD, DefinitionRequired); |
| 185 | } |
| 186 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 187 | virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) { |
| 188 | Gen->HandleLinkerOptionPragma(Opts); |
| 189 | } |
| 190 | |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 191 | virtual void HandleDetectMismatch(llvm::StringRef Name, |
| 192 | llvm::StringRef Value) { |
| 193 | Gen->HandleDetectMismatch(Name, Value); |
| 194 | } |
| 195 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 196 | virtual void HandleDependentLibrary(llvm::StringRef Opts) { |
| 197 | Gen->HandleDependentLibrary(Opts); |
| 198 | } |
| 199 | |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 200 | static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context, |
| 201 | unsigned LocCookie) { |
| 202 | SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie); |
| 203 | ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc); |
| 204 | } |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 205 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 206 | static void DiagnosticHandler(const llvm::DiagnosticInfo &DI, |
| 207 | void *Context) { |
| 208 | ((BackendConsumer *)Context)->DiagnosticHandlerImpl(DI); |
| 209 | } |
| 210 | |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 211 | void InlineAsmDiagHandler2(const llvm::SMDiagnostic &, |
| 212 | SourceLocation LocCookie); |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 213 | |
| 214 | void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI); |
| 215 | /// \brief Specialized handler for InlineAsm diagnostic. |
| 216 | /// \return True if the diagnostic has been successfully reported, false |
| 217 | /// otherwise. |
| 218 | bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D); |
| 219 | /// \brief Specialized handler for StackSize diagnostic. |
| 220 | /// \return True if the diagnostic has been successfully reported, false |
| 221 | /// otherwise. |
| 222 | bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | }; |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 224 | |
| 225 | void BackendConsumer::anchor() {} |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 228 | /// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr |
| 229 | /// buffer to be a valid FullSourceLoc. |
| 230 | static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D, |
| 231 | SourceManager &CSM) { |
| 232 | // Get both the clang and llvm source managers. The location is relative to |
| 233 | // a memory buffer that the LLVM Source Manager is handling, we need to add |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 234 | // a copy to the Clang source manager. |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 235 | const llvm::SourceMgr &LSM = *D.getSourceMgr(); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 236 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 237 | // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr |
| 238 | // already owns its one and clang::SourceManager wants to own its one. |
| 239 | const MemoryBuffer *LBuf = |
| 240 | LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc())); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 242 | // Create the copy and transfer ownership to clang::SourceManager. |
| 243 | llvm::MemoryBuffer *CBuf = |
| 244 | llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(), |
| 245 | LBuf->getBufferIdentifier()); |
| 246 | FileID FID = CSM.createFileIDForMemBuffer(CBuf); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 248 | // Translate the offset into the file. |
| 249 | unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart(); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 250 | SourceLocation NewLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 251 | CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset); |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 252 | return FullSourceLoc(NewLoc, CSM); |
| 253 | } |
| 254 | |
Chris Lattner | 6d67213 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 256 | /// InlineAsmDiagHandler2 - This function is invoked when the backend hits an |
| 257 | /// error parsing inline asm. The SMDiagnostic indicates the error relative to |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 258 | /// the temporary memory buffer that the inline asm parser has set up. |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 259 | void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D, |
| 260 | SourceLocation LocCookie) { |
| 261 | // There are a couple of different kinds of errors we could get here. First, |
| 262 | // we re-format the SMDiagnostic in terms of a clang diagnostic. |
Argyrios Kyrtzidis | a11b35a | 2012-02-01 06:36:49 +0000 | [diff] [blame] | 263 | |
| 264 | // Strip "error: " off the start of the message string. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 265 | StringRef Message = D.getMessage(); |
Argyrios Kyrtzidis | a11b35a | 2012-02-01 06:36:49 +0000 | [diff] [blame] | 266 | if (Message.startswith("error: ")) |
| 267 | Message = Message.substr(7); |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 268 | |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 269 | // If the SMDiagnostic has an inline asm source location, translate it. |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 270 | FullSourceLoc Loc; |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 271 | if (D.getLoc() != SMLoc()) |
| 272 | Loc = ConvertBackendLocation(D, Context->getSourceManager()); |
Chris Lattner | f4a4bec | 2012-01-31 06:13:55 +0000 | [diff] [blame] | 273 | |
Argyrios Kyrtzidis | a11b35a | 2012-02-01 06:36:49 +0000 | [diff] [blame] | 274 | |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 275 | // If this problem has clang-level source location information, report the |
| 276 | // issue as being an error in the source with a note showing the instantiated |
| 277 | // code. |
| 278 | if (LocCookie.isValid()) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 279 | Diags.Report(LocCookie, diag::err_fe_inline_asm).AddString(Message); |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 280 | |
Benjamin Kramer | e06b2b7 | 2011-10-16 10:48:28 +0000 | [diff] [blame] | 281 | if (D.getLoc().isValid()) { |
| 282 | DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here); |
| 283 | // Convert the SMDiagnostic ranges into SourceRange and attach them |
| 284 | // to the diagnostic. |
| 285 | for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) { |
| 286 | std::pair<unsigned, unsigned> Range = D.getRanges()[i]; |
| 287 | unsigned Column = D.getColumnNo(); |
| 288 | B << SourceRange(Loc.getLocWithOffset(Range.first - Column), |
| 289 | Loc.getLocWithOffset(Range.second - Column)); |
| 290 | } |
| 291 | } |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 295 | // Otherwise, report the backend error as occurring in the generated .s file. |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 296 | // If Loc is invalid, we still need to report the error, it just gets no |
| 297 | // location info. |
| 298 | Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message); |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 301 | #define ComputeDiagID(Severity, GroupName, DiagID) \ |
| 302 | do { \ |
| 303 | switch (Severity) { \ |
| 304 | case llvm::DS_Error: \ |
| 305 | DiagID = diag::err_fe_##GroupName; \ |
| 306 | break; \ |
| 307 | case llvm::DS_Warning: \ |
| 308 | DiagID = diag::warn_fe_##GroupName; \ |
| 309 | break; \ |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 310 | case llvm::DS_Remark: \ |
| 311 | llvm_unreachable("'remark' severity not expected"); \ |
| 312 | break; \ |
| 313 | case llvm::DS_Note: \ |
| 314 | DiagID = diag::note_fe_##GroupName; \ |
| 315 | break; \ |
| 316 | } \ |
| 317 | } while (false) |
| 318 | |
| 319 | #define ComputeDiagRemarkID(Severity, GroupName, DiagID) \ |
| 320 | do { \ |
| 321 | switch (Severity) { \ |
| 322 | case llvm::DS_Error: \ |
| 323 | DiagID = diag::err_fe_##GroupName; \ |
| 324 | break; \ |
| 325 | case llvm::DS_Warning: \ |
| 326 | DiagID = diag::warn_fe_##GroupName; \ |
| 327 | break; \ |
| 328 | case llvm::DS_Remark: \ |
| 329 | DiagID = diag::remark_fe_##GroupName; \ |
| 330 | break; \ |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 331 | case llvm::DS_Note: \ |
| 332 | DiagID = diag::note_fe_##GroupName; \ |
| 333 | break; \ |
| 334 | } \ |
| 335 | } while (false) |
| 336 | |
| 337 | bool |
| 338 | BackendConsumer::InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D) { |
| 339 | unsigned DiagID; |
| 340 | ComputeDiagID(D.getSeverity(), inline_asm, DiagID); |
| 341 | std::string Message = D.getMsgStr().str(); |
| 342 | |
| 343 | // If this problem has clang-level source location information, report the |
Tobias Grosser | bd25beb | 2014-02-26 10:21:56 +0000 | [diff] [blame] | 344 | // issue as being a problem in the source with a note showing the instantiated |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 345 | // code. |
| 346 | SourceLocation LocCookie = |
| 347 | SourceLocation::getFromRawEncoding(D.getLocCookie()); |
| 348 | if (LocCookie.isValid()) |
| 349 | Diags.Report(LocCookie, DiagID).AddString(Message); |
| 350 | else { |
| 351 | // Otherwise, report the backend diagnostic as occurring in the generated |
| 352 | // .s file. |
| 353 | // If Loc is invalid, we still need to report the diagnostic, it just gets |
| 354 | // no location info. |
| 355 | FullSourceLoc Loc; |
| 356 | Diags.Report(Loc, DiagID).AddString(Message); |
| 357 | } |
| 358 | // We handled all the possible severities. |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | bool |
| 363 | BackendConsumer::StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D) { |
| 364 | if (D.getSeverity() != llvm::DS_Warning) |
| 365 | // For now, the only support we have for StackSize diagnostic is warning. |
| 366 | // We do not know how to format other severities. |
| 367 | return false; |
| 368 | |
| 369 | // FIXME: We should demangle the function name. |
| 370 | // FIXME: Is there a way to get a location for that function? |
| 371 | FullSourceLoc Loc; |
| 372 | Diags.Report(Loc, diag::warn_fe_backend_frame_larger_than) |
| 373 | << D.getStackSize() << D.getFunction().getName(); |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | /// \brief This function is invoked when the backend needs |
| 378 | /// to report something to the user. |
| 379 | void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) { |
| 380 | unsigned DiagID = diag::err_fe_inline_asm; |
| 381 | llvm::DiagnosticSeverity Severity = DI.getSeverity(); |
| 382 | // Get the diagnostic ID based. |
| 383 | switch (DI.getKind()) { |
| 384 | case llvm::DK_InlineAsm: |
| 385 | if (InlineAsmDiagHandler(cast<DiagnosticInfoInlineAsm>(DI))) |
| 386 | return; |
| 387 | ComputeDiagID(Severity, inline_asm, DiagID); |
| 388 | break; |
| 389 | case llvm::DK_StackSize: |
| 390 | if (StackSizeDiagHandler(cast<DiagnosticInfoStackSize>(DI))) |
| 391 | return; |
| 392 | ComputeDiagID(Severity, backend_frame_larger_than, DiagID); |
| 393 | break; |
| 394 | default: |
| 395 | // Plugin IDs are not bound to any value as they are set dynamically. |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 396 | ComputeDiagRemarkID(Severity, backend_plugin, DiagID); |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 397 | break; |
| 398 | } |
| 399 | std::string MsgStorage; |
| 400 | { |
| 401 | raw_string_ostream Stream(MsgStorage); |
| 402 | DiagnosticPrinterRawOStream DP(Stream); |
| 403 | DI.print(DP); |
| 404 | } |
| 405 | |
| 406 | // Report the backend message using the usual diagnostic mechanism. |
| 407 | FullSourceLoc Loc; |
| 408 | Diags.Report(Loc, DiagID).AddString(MsgStorage); |
| 409 | } |
| 410 | #undef ComputeDiagID |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 411 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 412 | CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext) |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 413 | : Act(_Act), LinkModule(0), |
| 414 | VMContext(_VMContext ? _VMContext : new LLVMContext), |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 415 | OwnsVMContext(!_VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 416 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 417 | CodeGenAction::~CodeGenAction() { |
| 418 | TheModule.reset(); |
| 419 | if (OwnsVMContext) |
| 420 | delete VMContext; |
| 421 | } |
Daniel Dunbar | e8ecf9a | 2010-02-25 20:37:44 +0000 | [diff] [blame] | 422 | |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 423 | bool CodeGenAction::hasIRSupport() const { return true; } |
| 424 | |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 425 | void CodeGenAction::EndSourceFileAction() { |
| 426 | // If the consumer creation failed, do nothing. |
| 427 | if (!getCompilerInstance().hasASTConsumer()) |
| 428 | return; |
| 429 | |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 430 | // If we were given a link module, release consumer's ownership of it. |
| 431 | if (LinkModule) |
| 432 | BEConsumer->takeLinkModule(); |
| 433 | |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 434 | // Steal the module from the consumer. |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 435 | TheModule.reset(BEConsumer->takeModule()); |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame^] | 438 | llvm::Module *CodeGenAction::takeModule() { return TheModule.release(); } |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 439 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 440 | llvm::LLVMContext *CodeGenAction::takeLLVMContext() { |
| 441 | OwnsVMContext = false; |
| 442 | return VMContext; |
| 443 | } |
| 444 | |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 445 | static raw_ostream *GetOutputStream(CompilerInstance &CI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 446 | StringRef InFile, |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 447 | BackendAction Action) { |
| 448 | switch (Action) { |
| 449 | case Backend_EmitAssembly: |
| 450 | return CI.createDefaultOutputFile(false, InFile, "s"); |
| 451 | case Backend_EmitLL: |
| 452 | return CI.createDefaultOutputFile(false, InFile, "ll"); |
| 453 | case Backend_EmitBC: |
| 454 | return CI.createDefaultOutputFile(true, InFile, "bc"); |
| 455 | case Backend_EmitNothing: |
| 456 | return 0; |
| 457 | case Backend_EmitMCNull: |
| 458 | case Backend_EmitObj: |
| 459 | return CI.createDefaultOutputFile(true, InFile, "o"); |
| 460 | } |
| 461 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 462 | llvm_unreachable("Invalid action!"); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 465 | ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 466 | StringRef InFile) { |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 467 | BackendAction BA = static_cast<BackendAction>(Act); |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 468 | OwningPtr<raw_ostream> OS(GetOutputStream(CI, InFile, BA)); |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 469 | if (BA != Backend_EmitNothing && !OS) |
| 470 | return 0; |
| 471 | |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 472 | llvm::Module *LinkModuleToUse = LinkModule; |
| 473 | |
| 474 | // If we were not given a link module, and the user requested that one be |
| 475 | // loaded from bitcode, do so now. |
| 476 | const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile; |
| 477 | if (!LinkModuleToUse && !LinkBCFile.empty()) { |
| 478 | std::string ErrorStr; |
| 479 | |
| 480 | llvm::MemoryBuffer *BCBuf = |
| 481 | CI.getFileManager().getBufferForFile(LinkBCFile, &ErrorStr); |
| 482 | if (!BCBuf) { |
| 483 | CI.getDiagnostics().Report(diag::err_cannot_open_file) |
| 484 | << LinkBCFile << ErrorStr; |
| 485 | return 0; |
| 486 | } |
| 487 | |
Rafael Espindola | 6b6004a | 2014-01-13 18:31:09 +0000 | [diff] [blame] | 488 | ErrorOr<llvm::Module *> ModuleOrErr = |
| 489 | getLazyBitcodeModule(BCBuf, *VMContext); |
| 490 | if (error_code EC = ModuleOrErr.getError()) { |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 491 | CI.getDiagnostics().Report(diag::err_cannot_open_file) |
Rafael Espindola | 6b6004a | 2014-01-13 18:31:09 +0000 | [diff] [blame] | 492 | << LinkBCFile << EC.message(); |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 493 | return 0; |
| 494 | } |
Rafael Espindola | 6b6004a | 2014-01-13 18:31:09 +0000 | [diff] [blame] | 495 | LinkModuleToUse = ModuleOrErr.get(); |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame^] | 498 | BEConsumer = new BackendConsumer(BA, CI.getDiagnostics(), CI.getCodeGenOpts(), |
| 499 | CI.getTargetOpts(), CI.getLangOpts(), |
| 500 | CI.getFrontendOpts().ShowTimers, InFile, |
| 501 | LinkModuleToUse, OS.release(), *VMContext); |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 502 | return BEConsumer; |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 503 | } |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 504 | |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 505 | void CodeGenAction::ExecuteAction() { |
| 506 | // If this is an IR file, we have to treat it specially. |
| 507 | if (getCurrentFileKind() == IK_LLVM_IR) { |
| 508 | BackendAction BA = static_cast<BackendAction>(Act); |
| 509 | CompilerInstance &CI = getCompilerInstance(); |
| 510 | raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA); |
| 511 | if (BA != Backend_EmitNothing && !OS) |
| 512 | return; |
| 513 | |
| 514 | bool Invalid; |
| 515 | SourceManager &SM = CI.getSourceManager(); |
| 516 | const llvm::MemoryBuffer *MainFile = SM.getBuffer(SM.getMainFileID(), |
| 517 | &Invalid); |
| 518 | if (Invalid) |
| 519 | return; |
| 520 | |
| 521 | // FIXME: This is stupid, IRReader shouldn't take ownership. |
| 522 | llvm::MemoryBuffer *MainFileCopy = |
| 523 | llvm::MemoryBuffer::getMemBufferCopy(MainFile->getBuffer(), |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 524 | getCurrentFile()); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 525 | |
| 526 | llvm::SMDiagnostic Err; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 527 | TheModule.reset(ParseIR(MainFileCopy, Err, *VMContext)); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 528 | if (!TheModule) { |
| 529 | // Translate from the diagnostic info to the SourceManager location. |
Argyrios Kyrtzidis | 27bf76d | 2011-09-19 20:40:38 +0000 | [diff] [blame] | 530 | SourceLocation Loc = SM.translateFileLineCol( |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 531 | SM.getFileEntryForID(SM.getMainFileID()), Err.getLineNo(), |
| 532 | Err.getColumnNo() + 1); |
| 533 | |
Alp Toker | bc043f2 | 2013-12-21 05:20:03 +0000 | [diff] [blame] | 534 | // Strip off a leading diagnostic code if there is one. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 535 | StringRef Msg = Err.getMessage(); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 536 | if (Msg.startswith("error: ")) |
| 537 | Msg = Msg.substr(7); |
Benjamin Kramer | 778b8b8 | 2012-03-16 22:31:42 +0000 | [diff] [blame] | 538 | |
Alp Toker | bc043f2 | 2013-12-21 05:20:03 +0000 | [diff] [blame] | 539 | unsigned DiagID = |
| 540 | CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0"); |
Benjamin Kramer | 778b8b8 | 2012-03-16 22:31:42 +0000 | [diff] [blame] | 541 | |
Alp Toker | bc043f2 | 2013-12-21 05:20:03 +0000 | [diff] [blame] | 542 | CI.getDiagnostics().Report(Loc, DiagID) << Msg; |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 543 | return; |
| 544 | } |
Rafael Espindola | d5e81e5 | 2013-12-20 22:01:25 +0000 | [diff] [blame] | 545 | const TargetOptions &TargetOpts = CI.getTargetOpts(); |
| 546 | if (TheModule->getTargetTriple() != TargetOpts.Triple) { |
| 547 | unsigned DiagID = CI.getDiagnostics().getCustomDiagID( |
| 548 | DiagnosticsEngine::Warning, |
| 549 | "overriding the module target triple with %0"); |
| 550 | |
| 551 | CI.getDiagnostics().Report(SourceLocation(), DiagID) << TargetOpts.Triple; |
| 552 | TheModule->setTargetTriple(TargetOpts.Triple); |
| 553 | } |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 554 | |
Alp Toker | e83b906 | 2014-01-02 15:08:04 +0000 | [diff] [blame] | 555 | EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(), TargetOpts, |
| 556 | CI.getLangOpts(), CI.getTarget().getTargetDescription(), |
| 557 | TheModule.get(), BA, OS); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 558 | return; |
| 559 | } |
| 560 | |
| 561 | // Otherwise follow the normal AST path. |
| 562 | this->ASTFrontendAction::ExecuteAction(); |
| 563 | } |
| 564 | |
| 565 | // |
| 566 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 567 | void EmitAssemblyAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 568 | EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext) |
| 569 | : CodeGenAction(Backend_EmitAssembly, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 570 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 571 | void EmitBCAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 572 | EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext) |
| 573 | : CodeGenAction(Backend_EmitBC, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 574 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 575 | void EmitLLVMAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 576 | EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext) |
| 577 | : CodeGenAction(Backend_EmitLL, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 578 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 579 | void EmitLLVMOnlyAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 580 | EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext) |
| 581 | : CodeGenAction(Backend_EmitNothing, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 582 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 583 | void EmitCodeGenOnlyAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 584 | EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext) |
| 585 | : CodeGenAction(Backend_EmitMCNull, _VMContext) {} |
Daniel Dunbar | 4c77a64 | 2010-05-25 18:41:01 +0000 | [diff] [blame] | 586 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 587 | void EmitObjAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 588 | EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext) |
| 589 | : CodeGenAction(Backend_EmitObj, _VMContext) {} |