blob: 68dd5c94dc01fa5ce758e4283d24597b9fd8c8d9 [file] [log] [blame]
Daniel Dunbarcea0c702010-02-25 04:37:45 +00001//===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===//
Daniel Dunbarc13935e2008-10-21 23:49:24 +00002//
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 Dunbarc1b17292010-06-15 17:48:49 +000010#include "clang/CodeGen/CodeGenAction.h"
Chris Lattner5ec32e72010-04-06 18:38:50 +000011#include "clang/Basic/SourceManager.h"
12#include "clang/Basic/TargetInfo.h"
Chris Lattner5bbb3c82009-03-29 16:50:03 +000013#include "clang/AST/ASTConsumer.h"
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000014#include "clang/AST/ASTContext.h"
Chris Lattner5bbb3c82009-03-29 16:50:03 +000015#include "clang/AST/DeclGroup.h"
Daniel Dunbarc1b17292010-06-15 17:48:49 +000016#include "clang/CodeGen/BackendUtil.h"
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000017#include "clang/CodeGen/ModuleBuilder.h"
Daniel Dunbarcea0c702010-02-25 04:37:45 +000018#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbaracadc552009-12-03 09:12:54 +000019#include "clang/Frontend/FrontendDiagnostic.h"
Chris Lattner6d672132010-04-06 17:52:14 +000020#include "llvm/LLVMContext.h"
Daniel Dunbarc13935e2008-10-21 23:49:24 +000021#include "llvm/Module.h"
Daniel Dunbar3e111522010-06-07 23:21:04 +000022#include "llvm/Pass.h"
Daniel Dunbarc13935e2008-10-21 23:49:24 +000023#include "llvm/ADT/OwningPtr.h"
Daniel Dunbar6f8362c2010-06-07 23:27:59 +000024#include "llvm/Support/IRReader.h"
Chris Lattner5ec32e72010-04-06 18:38:50 +000025#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/Support/SourceMgr.h"
Chris Lattner263d64c2009-02-18 01:37:30 +000027#include "llvm/Support/Timer.h"
Daniel Dunbarc13935e2008-10-21 23:49:24 +000028using namespace clang;
29using namespace llvm;
30
Nico Weber2992efa2011-01-25 20:34:14 +000031namespace clang {
Benjamin Kramer16634c22009-11-28 10:07:24 +000032 class BackendConsumer : public ASTConsumer {
David Blaikie9c902b52011-09-25 23:23:43 +000033 DiagnosticsEngine &Diags;
Daniel Dunbarc13935e2008-10-21 23:49:24 +000034 BackendAction Action;
Daniel Dunbarde182242009-11-30 08:39:32 +000035 const CodeGenOptions &CodeGenOpts;
Daniel Dunbarde182242009-11-30 08:39:32 +000036 const TargetOptions &TargetOpts;
Dan Gohmanfec0ff82011-07-05 22:02:36 +000037 const LangOptions &LangOpts;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000038 raw_ostream *AsmOutStream;
Chris Lattnereae6cb62009-03-05 08:00:35 +000039 ASTContext *Context;
Daniel Dunbarb3a36cf2008-10-29 08:50:02 +000040
Chris Lattner263d64c2009-02-18 01:37:30 +000041 Timer LLVMIRGeneration;
Mike Stump11289f42009-09-09 15:08:12 +000042
Daniel Dunbarc13935e2008-10-21 23:49:24 +000043 llvm::OwningPtr<CodeGenerator> Gen;
Mike Stump11289f42009-09-09 15:08:12 +000044
Daniel Dunbar400a6932010-02-25 04:37:50 +000045 llvm::OwningPtr<llvm::Module> TheModule;
Daniel Dunbarc13935e2008-10-21 23:49:24 +000046
Mike Stump11289f42009-09-09 15:08:12 +000047 public:
David Blaikie9c902b52011-09-25 23:23:43 +000048 BackendConsumer(BackendAction action, DiagnosticsEngine &_Diags,
Daniel Dunbar6d5824f2010-06-07 23:19:17 +000049 const CodeGenOptions &compopts,
Dan Gohmanfec0ff82011-07-05 22:02:36 +000050 const TargetOptions &targetopts,
51 const LangOptions &langopts,
52 bool TimePasses,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000053 const std::string &infile, raw_ostream *OS,
Chris Lattner6d672132010-04-06 17:52:14 +000054 LLVMContext &C) :
Daniel Dunbaracadc552009-12-03 09:12:54 +000055 Diags(_Diags),
Mike Stump11289f42009-09-09 15:08:12 +000056 Action(action),
Chandler Carruthbc55fe22009-11-12 17:24:48 +000057 CodeGenOpts(compopts),
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000058 TargetOpts(targetopts),
Dan Gohmanfec0ff82011-07-05 22:02:36 +000059 LangOpts(langopts),
Chris Lattner15681772009-07-14 20:39:15 +000060 AsmOutStream(OS),
Chris Lattner263d64c2009-02-18 01:37:30 +000061 LLVMIRGeneration("LLVM IR Generation Time"),
Daniel Dunbar3e111522010-06-07 23:21:04 +000062 Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)) {
Daniel Dunbar8e705052009-11-30 08:39:52 +000063 llvm::TimePassesIsEnabled = TimePasses;
Chris Lattnerdeffa132009-02-18 01:23:44 +000064 }
Daniel Dunbarc13935e2008-10-21 23:49:24 +000065
Daniel Dunbar400a6932010-02-25 04:37:50 +000066 llvm::Module *takeModule() { return TheModule.take(); }
67
Chris Lattner5cf49fe2009-03-28 02:18:25 +000068 virtual void Initialize(ASTContext &Ctx) {
69 Context = &Ctx;
Mike Stump11289f42009-09-09 15:08:12 +000070
Daniel Dunbar8e705052009-11-30 08:39:52 +000071 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +000072 LLVMIRGeneration.startTimer();
Mike Stump11289f42009-09-09 15:08:12 +000073
Chris Lattner5cf49fe2009-03-28 02:18:25 +000074 Gen->Initialize(Ctx);
Daniel Dunbarc13935e2008-10-21 23:49:24 +000075
Daniel Dunbar400a6932010-02-25 04:37:50 +000076 TheModule.reset(Gen->GetModule());
Mike Stump11289f42009-09-09 15:08:12 +000077
Daniel Dunbar8e705052009-11-30 08:39:52 +000078 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +000079 LLVMIRGeneration.stopTimer();
Daniel Dunbarc13935e2008-10-21 23:49:24 +000080 }
Mike Stump11289f42009-09-09 15:08:12 +000081
Chris Lattner5bbb3c82009-03-29 16:50:03 +000082 virtual void HandleTopLevelDecl(DeclGroupRef D) {
83 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattnereae6cb62009-03-05 08:00:35 +000084 Context->getSourceManager(),
85 "LLVM IR generation of declaration");
Mike Stump11289f42009-09-09 15:08:12 +000086
Daniel Dunbar8e705052009-11-30 08:39:52 +000087 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +000088 LLVMIRGeneration.startTimer();
Chris Lattner5bbb3c82009-03-29 16:50:03 +000089
Daniel Dunbarc13935e2008-10-21 23:49:24 +000090 Gen->HandleTopLevelDecl(D);
Chris Lattner263d64c2009-02-18 01:37:30 +000091
Daniel Dunbar8e705052009-11-30 08:39:52 +000092 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +000093 LLVMIRGeneration.stopTimer();
Daniel Dunbarc13935e2008-10-21 23:49:24 +000094 }
Mike Stump11289f42009-09-09 15:08:12 +000095
Chris Lattnercf169832009-03-28 04:11:33 +000096 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattnereae6cb62009-03-05 08:00:35 +000097 {
Chris Lattnere46de752009-03-06 06:46:31 +000098 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Daniel Dunbar8e705052009-11-30 08:39:52 +000099 if (llvm::TimePassesIsEnabled)
Chris Lattnereae6cb62009-03-05 08:00:35 +0000100 LLVMIRGeneration.startTimer();
Chris Lattner263d64c2009-02-18 01:37:30 +0000101
Chris Lattnercf169832009-03-28 04:11:33 +0000102 Gen->HandleTranslationUnit(C);
Daniel Dunbara94d8732008-11-11 06:35:39 +0000103
Daniel Dunbar8e705052009-11-30 08:39:52 +0000104 if (llvm::TimePassesIsEnabled)
Chris Lattnereae6cb62009-03-05 08:00:35 +0000105 LLVMIRGeneration.stopTimer();
106 }
Chris Lattner263d64c2009-02-18 01:37:30 +0000107
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000108 // Silently ignore if we weren't initialized for some reason.
Daniel Dunbar3e111522010-06-07 23:21:04 +0000109 if (!TheModule)
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000110 return;
Mike Stump11289f42009-09-09 15:08:12 +0000111
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000112 // Make sure IR generation is happy with the module. This is released by
113 // the module provider.
114 Module *M = Gen->ReleaseModule();
115 if (!M) {
116 // The module has been released by IR gen on failures, do not double
117 // free.
118 TheModule.take();
119 return;
120 }
121
122 assert(TheModule.get() == M &&
123 "Unexpected module change during IR generation");
124
125 // Install an inline asm handler so that diagnostics get printed through
126 // our diagnostics hooks.
127 LLVMContext &Ctx = TheModule->getContext();
Chris Lattner068f2ab2010-11-17 08:13:04 +0000128 LLVMContext::InlineAsmDiagHandlerTy OldHandler =
129 Ctx.getInlineAsmDiagnosticHandler();
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000130 void *OldContext = Ctx.getInlineAsmDiagnosticContext();
Chris Lattner068f2ab2010-11-17 08:13:04 +0000131 Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000132
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000133 EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
Daniel Dunbar3e111522010-06-07 23:21:04 +0000134 TheModule.get(), Action, AsmOutStream);
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000135
136 Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000137 }
Mike Stump11289f42009-09-09 15:08:12 +0000138
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000139 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000140 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
141 Context->getSourceManager(),
142 "LLVM IR generation of declaration");
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000143 Gen->HandleTagDeclDefinition(D);
144 }
Douglas Gregorbeecd582009-04-21 17:11:58 +0000145
146 virtual void CompleteTentativeDefinition(VarDecl *D) {
147 Gen->CompleteTentativeDefinition(D);
148 }
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000149
Douglas Gregor88d292c2010-05-13 16:44:06 +0000150 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
151 Gen->HandleVTable(RD, DefinitionRequired);
152 }
153
Chris Lattner5ec32e72010-04-06 18:38:50 +0000154 static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
155 unsigned LocCookie) {
156 SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie);
157 ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
158 }
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000159
Chris Lattner5ec32e72010-04-06 18:38:50 +0000160 void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
161 SourceLocation LocCookie);
Mike Stump11289f42009-09-09 15:08:12 +0000162 };
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000163}
164
Chris Lattner79f67a72010-04-08 00:23:06 +0000165/// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
166/// buffer to be a valid FullSourceLoc.
167static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
168 SourceManager &CSM) {
169 // Get both the clang and llvm source managers. The location is relative to
170 // a memory buffer that the LLVM Source Manager is handling, we need to add
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000171 // a copy to the Clang source manager.
Chris Lattner79f67a72010-04-08 00:23:06 +0000172 const llvm::SourceMgr &LSM = *D.getSourceMgr();
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000173
Chris Lattner79f67a72010-04-08 00:23:06 +0000174 // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
175 // already owns its one and clang::SourceManager wants to own its one.
176 const MemoryBuffer *LBuf =
177 LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000178
Chris Lattner79f67a72010-04-08 00:23:06 +0000179 // Create the copy and transfer ownership to clang::SourceManager.
180 llvm::MemoryBuffer *CBuf =
181 llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
182 LBuf->getBufferIdentifier());
183 FileID FID = CSM.createFileIDForMemBuffer(CBuf);
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000184
Chris Lattner79f67a72010-04-08 00:23:06 +0000185 // Translate the offset into the file.
186 unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000187 SourceLocation NewLoc =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000188 CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
Chris Lattner79f67a72010-04-08 00:23:06 +0000189 return FullSourceLoc(NewLoc, CSM);
190}
191
Chris Lattner6d672132010-04-06 17:52:14 +0000192
Chris Lattner5ec32e72010-04-06 18:38:50 +0000193/// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
194/// error parsing inline asm. The SMDiagnostic indicates the error relative to
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000195/// the temporary memory buffer that the inline asm parser has set up.
Chris Lattner5ec32e72010-04-06 18:38:50 +0000196void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
197 SourceLocation LocCookie) {
198 // There are a couple of different kinds of errors we could get here. First,
199 // we re-format the SMDiagnostic in terms of a clang diagnostic.
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000200
Chris Lattner5ec32e72010-04-06 18:38:50 +0000201 // Strip "error: " off the start of the message string.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000202 StringRef Message = D.getMessage();
Chris Lattner5ec32e72010-04-06 18:38:50 +0000203 if (Message.startswith("error: "))
204 Message = Message.substr(7);
205
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000206 // If the SMDiagnostic has an inline asm source location, translate it.
Chris Lattner5ec32e72010-04-06 18:38:50 +0000207 FullSourceLoc Loc;
Chris Lattner79f67a72010-04-08 00:23:06 +0000208 if (D.getLoc() != SMLoc())
209 Loc = ConvertBackendLocation(D, Context->getSourceManager());
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000210
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000211
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000212 // If this problem has clang-level source location information, report the
213 // issue as being an error in the source with a note showing the instantiated
214 // code.
215 if (LocCookie.isValid()) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000216 Diags.Report(LocCookie, diag::err_fe_inline_asm).AddString(Message);
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000217
218 if (D.getLoc().isValid())
219 Diags.Report(Loc, diag::note_fe_inline_asm_here);
220 return;
221 }
222
Chris Lattner57540c52011-04-15 05:22:18 +0000223 // Otherwise, report the backend error as occurring in the generated .s file.
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000224 // If Loc is invalid, we still need to report the error, it just gets no
225 // location info.
226 Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message);
Chris Lattner5ec32e72010-04-06 18:38:50 +0000227}
228
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000229//
230
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000231CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
232 : Act(_Act), VMContext(_VMContext ? _VMContext : new LLVMContext),
233 OwnsVMContext(!_VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000234
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000235CodeGenAction::~CodeGenAction() {
236 TheModule.reset();
237 if (OwnsVMContext)
238 delete VMContext;
239}
Daniel Dunbare8ecf9a2010-02-25 20:37:44 +0000240
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000241bool CodeGenAction::hasIRSupport() const { return true; }
242
Daniel Dunbar400a6932010-02-25 04:37:50 +0000243void CodeGenAction::EndSourceFileAction() {
244 // If the consumer creation failed, do nothing.
245 if (!getCompilerInstance().hasASTConsumer())
246 return;
247
248 // Steal the module from the consumer.
Nico Weber2992efa2011-01-25 20:34:14 +0000249 TheModule.reset(BEConsumer->takeModule());
Daniel Dunbar400a6932010-02-25 04:37:50 +0000250}
251
252llvm::Module *CodeGenAction::takeModule() {
253 return TheModule.take();
254}
255
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000256llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
257 OwnsVMContext = false;
258 return VMContext;
259}
260
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000261static raw_ostream *GetOutputStream(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000262 StringRef InFile,
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000263 BackendAction Action) {
264 switch (Action) {
265 case Backend_EmitAssembly:
266 return CI.createDefaultOutputFile(false, InFile, "s");
267 case Backend_EmitLL:
268 return CI.createDefaultOutputFile(false, InFile, "ll");
269 case Backend_EmitBC:
270 return CI.createDefaultOutputFile(true, InFile, "bc");
271 case Backend_EmitNothing:
272 return 0;
273 case Backend_EmitMCNull:
274 case Backend_EmitObj:
275 return CI.createDefaultOutputFile(true, InFile, "o");
276 }
277
David Blaikie83d382b2011-09-23 05:06:16 +0000278 llvm_unreachable("Invalid action!");
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000279}
280
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000281ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000282 StringRef InFile) {
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000283 BackendAction BA = static_cast<BackendAction>(Act);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000284 llvm::OwningPtr<raw_ostream> OS(GetOutputStream(CI, InFile, BA));
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000285 if (BA != Backend_EmitNothing && !OS)
286 return 0;
287
Nico Weber2992efa2011-01-25 20:34:14 +0000288 BEConsumer =
289 new BackendConsumer(BA, CI.getDiagnostics(),
290 CI.getCodeGenOpts(), CI.getTargetOpts(),
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000291 CI.getLangOpts(),
Nico Weber2992efa2011-01-25 20:34:14 +0000292 CI.getFrontendOpts().ShowTimers, InFile, OS.take(),
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000293 *VMContext);
Nico Weber2992efa2011-01-25 20:34:14 +0000294 return BEConsumer;
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000295}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000296
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000297void CodeGenAction::ExecuteAction() {
298 // If this is an IR file, we have to treat it specially.
299 if (getCurrentFileKind() == IK_LLVM_IR) {
300 BackendAction BA = static_cast<BackendAction>(Act);
301 CompilerInstance &CI = getCompilerInstance();
302 raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA);
303 if (BA != Backend_EmitNothing && !OS)
304 return;
305
306 bool Invalid;
307 SourceManager &SM = CI.getSourceManager();
308 const llvm::MemoryBuffer *MainFile = SM.getBuffer(SM.getMainFileID(),
309 &Invalid);
310 if (Invalid)
311 return;
312
313 // FIXME: This is stupid, IRReader shouldn't take ownership.
314 llvm::MemoryBuffer *MainFileCopy =
315 llvm::MemoryBuffer::getMemBufferCopy(MainFile->getBuffer(),
316 getCurrentFile().c_str());
317
318 llvm::SMDiagnostic Err;
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000319 TheModule.reset(ParseIR(MainFileCopy, Err, *VMContext));
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000320 if (!TheModule) {
321 // Translate from the diagnostic info to the SourceManager location.
Argyrios Kyrtzidis27bf76d2011-09-19 20:40:38 +0000322 SourceLocation Loc = SM.translateFileLineCol(
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000323 SM.getFileEntryForID(SM.getMainFileID()), Err.getLineNo(),
324 Err.getColumnNo() + 1);
325
326 // Get a custom diagnostic for the error. We strip off a leading
327 // diagnostic code if there is one.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000328 StringRef Msg = Err.getMessage();
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000329 if (Msg.startswith("error: "))
330 Msg = Msg.substr(7);
David Blaikie9c902b52011-09-25 23:23:43 +0000331 unsigned DiagID = CI.getDiagnostics().getCustomDiagID(
332 DiagnosticsEngine::Error, Msg);
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000333
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000334 CI.getDiagnostics().Report(Loc, DiagID);
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000335 return;
336 }
337
338 EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(),
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000339 CI.getTargetOpts(), CI.getLangOpts(),
340 TheModule.get(),
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000341 BA, OS);
342 return;
343 }
344
345 // Otherwise follow the normal AST path.
346 this->ASTFrontendAction::ExecuteAction();
347}
348
349//
350
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000351EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
352 : CodeGenAction(Backend_EmitAssembly, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000353
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000354EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
355 : CodeGenAction(Backend_EmitBC, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000356
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000357EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
358 : CodeGenAction(Backend_EmitLL, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000359
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000360EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
361 : CodeGenAction(Backend_EmitNothing, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000362
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000363EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
364 : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
Daniel Dunbar4c77a642010-05-25 18:41:01 +0000365
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000366EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
367 : CodeGenAction(Backend_EmitObj, _VMContext) {}