blob: 68dd5c94dc01fa5ce758e4283d24597b9fd8c8d9 [file] [log] [blame]
Daniel Dunbar4ee34612010-02-25 04:37:45 +00001//===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===//
Daniel Dunbard69bacc2008-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 Dunbar9b414d32010-06-15 17:48:49 +000010#include "clang/CodeGen/CodeGenAction.h"
Chris Lattner6da9eb62010-04-06 18:38:50 +000011#include "clang/Basic/SourceManager.h"
12#include "clang/Basic/TargetInfo.h"
Chris Lattner682bf922009-03-29 16:50:03 +000013#include "clang/AST/ASTConsumer.h"
Daniel Dunbard58c03f2009-11-15 06:48:46 +000014#include "clang/AST/ASTContext.h"
Chris Lattner682bf922009-03-29 16:50:03 +000015#include "clang/AST/DeclGroup.h"
Daniel Dunbar9b414d32010-06-15 17:48:49 +000016#include "clang/CodeGen/BackendUtil.h"
Daniel Dunbard58c03f2009-11-15 06:48:46 +000017#include "clang/CodeGen/ModuleBuilder.h"
Daniel Dunbar4ee34612010-02-25 04:37:45 +000018#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbar3be0d192009-12-03 09:12:54 +000019#include "clang/Frontend/FrontendDiagnostic.h"
Chris Lattnercabae682010-04-06 17:52:14 +000020#include "llvm/LLVMContext.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000021#include "llvm/Module.h"
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +000022#include "llvm/Pass.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000023#include "llvm/ADT/OwningPtr.h"
Daniel Dunbar4cbbd942010-06-07 23:27:59 +000024#include "llvm/Support/IRReader.h"
Chris Lattner6da9eb62010-04-06 18:38:50 +000025#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/Support/SourceMgr.h"
Chris Lattner6f114eb2009-02-18 01:37:30 +000027#include "llvm/Support/Timer.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000028using namespace clang;
29using namespace llvm;
30
Nico Weber5aa74af2011-01-25 20:34:14 +000031namespace clang {
Benjamin Kramerbd218282009-11-28 10:07:24 +000032 class BackendConsumer : public ASTConsumer {
David Blaikied6471f72011-09-25 23:23:43 +000033 DiagnosticsEngine &Diags;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000034 BackendAction Action;
Daniel Dunbar3636e1d2009-11-30 08:39:32 +000035 const CodeGenOptions &CodeGenOpts;
Daniel Dunbar3636e1d2009-11-30 08:39:32 +000036 const TargetOptions &TargetOpts;
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000037 const LangOptions &LangOpts;
Chris Lattner5f9e2722011-07-23 10:55:15 +000038 raw_ostream *AsmOutStream;
Chris Lattner49f28ca2009-03-05 08:00:35 +000039 ASTContext *Context;
Daniel Dunbar90f41302008-10-29 08:50:02 +000040
Chris Lattner6f114eb2009-02-18 01:37:30 +000041 Timer LLVMIRGeneration;
Mike Stump1eb44332009-09-09 15:08:12 +000042
Daniel Dunbard69bacc2008-10-21 23:49:24 +000043 llvm::OwningPtr<CodeGenerator> Gen;
Mike Stump1eb44332009-09-09 15:08:12 +000044
Daniel Dunbarb954e982010-02-25 04:37:50 +000045 llvm::OwningPtr<llvm::Module> TheModule;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000046
Mike Stump1eb44332009-09-09 15:08:12 +000047 public:
David Blaikied6471f72011-09-25 23:23:43 +000048 BackendConsumer(BackendAction action, DiagnosticsEngine &_Diags,
Daniel Dunbar6b0cf672010-06-07 23:19:17 +000049 const CodeGenOptions &compopts,
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000050 const TargetOptions &targetopts,
51 const LangOptions &langopts,
52 bool TimePasses,
Chris Lattner5f9e2722011-07-23 10:55:15 +000053 const std::string &infile, raw_ostream *OS,
Chris Lattnercabae682010-04-06 17:52:14 +000054 LLVMContext &C) :
Daniel Dunbar3be0d192009-12-03 09:12:54 +000055 Diags(_Diags),
Mike Stump1eb44332009-09-09 15:08:12 +000056 Action(action),
Chandler Carruth2811ccf2009-11-12 17:24:48 +000057 CodeGenOpts(compopts),
Daniel Dunbard58c03f2009-11-15 06:48:46 +000058 TargetOpts(targetopts),
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000059 LangOpts(langopts),
Chris Lattner03eacc72009-07-14 20:39:15 +000060 AsmOutStream(OS),
Chris Lattner6f114eb2009-02-18 01:37:30 +000061 LLVMIRGeneration("LLVM IR Generation Time"),
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +000062 Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)) {
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000063 llvm::TimePassesIsEnabled = TimePasses;
Chris Lattner44502662009-02-18 01:23:44 +000064 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +000065
Daniel Dunbarb954e982010-02-25 04:37:50 +000066 llvm::Module *takeModule() { return TheModule.take(); }
67
Chris Lattner7bb0da02009-03-28 02:18:25 +000068 virtual void Initialize(ASTContext &Ctx) {
69 Context = &Ctx;
Mike Stump1eb44332009-09-09 15:08:12 +000070
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000071 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +000072 LLVMIRGeneration.startTimer();
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattner7bb0da02009-03-28 02:18:25 +000074 Gen->Initialize(Ctx);
Daniel Dunbard69bacc2008-10-21 23:49:24 +000075
Daniel Dunbarb954e982010-02-25 04:37:50 +000076 TheModule.reset(Gen->GetModule());
Mike Stump1eb44332009-09-09 15:08:12 +000077
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000078 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +000079 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +000080 }
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattner682bf922009-03-29 16:50:03 +000082 virtual void HandleTopLevelDecl(DeclGroupRef D) {
83 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattner49f28ca2009-03-05 08:00:35 +000084 Context->getSourceManager(),
85 "LLVM IR generation of declaration");
Mike Stump1eb44332009-09-09 15:08:12 +000086
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000087 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +000088 LLVMIRGeneration.startTimer();
Chris Lattner682bf922009-03-29 16:50:03 +000089
Daniel Dunbard69bacc2008-10-21 23:49:24 +000090 Gen->HandleTopLevelDecl(D);
Chris Lattner6f114eb2009-02-18 01:37:30 +000091
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000092 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +000093 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +000094 }
Mike Stump1eb44332009-09-09 15:08:12 +000095
Chris Lattnerdacbc5d2009-03-28 04:11:33 +000096 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattner49f28ca2009-03-05 08:00:35 +000097 {
Chris Lattner14f234e2009-03-06 06:46:31 +000098 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000099 if (llvm::TimePassesIsEnabled)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000100 LLVMIRGeneration.startTimer();
Chris Lattner6f114eb2009-02-18 01:37:30 +0000101
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000102 Gen->HandleTranslationUnit(C);
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000103
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000104 if (llvm::TimePassesIsEnabled)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000105 LLVMIRGeneration.stopTimer();
106 }
Chris Lattner6f114eb2009-02-18 01:37:30 +0000107
Daniel Dunbar897c6762010-06-07 23:20:08 +0000108 // Silently ignore if we weren't initialized for some reason.
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +0000109 if (!TheModule)
Daniel Dunbar897c6762010-06-07 23:20:08 +0000110 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Daniel Dunbar897c6762010-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 Lattner063e4762010-11-17 08:13:04 +0000128 LLVMContext::InlineAsmDiagHandlerTy OldHandler =
129 Ctx.getInlineAsmDiagnosticHandler();
Daniel Dunbar897c6762010-06-07 23:20:08 +0000130 void *OldContext = Ctx.getInlineAsmDiagnosticContext();
Chris Lattner063e4762010-11-17 08:13:04 +0000131 Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
Daniel Dunbar897c6762010-06-07 23:20:08 +0000132
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000133 EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +0000134 TheModule.get(), Action, AsmOutStream);
Daniel Dunbar897c6762010-06-07 23:20:08 +0000135
136 Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000139 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000140 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
141 Context->getSourceManager(),
142 "LLVM IR generation of declaration");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000143 Gen->HandleTagDeclDefinition(D);
144 }
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000145
146 virtual void CompleteTentativeDefinition(VarDecl *D) {
147 Gen->CompleteTentativeDefinition(D);
148 }
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000149
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000150 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
151 Gen->HandleVTable(RD, DefinitionRequired);
152 }
153
Chris Lattner6da9eb62010-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 Dunbarf80cb752010-04-29 16:29:09 +0000159
Chris Lattner6da9eb62010-04-06 18:38:50 +0000160 void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
161 SourceLocation LocCookie);
Mike Stump1eb44332009-09-09 15:08:12 +0000162 };
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000163}
164
Chris Lattnerd6f19062010-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 Dunbarf80cb752010-04-29 16:29:09 +0000171 // a copy to the Clang source manager.
Chris Lattnerd6f19062010-04-08 00:23:06 +0000172 const llvm::SourceMgr &LSM = *D.getSourceMgr();
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000173
Chris Lattnerd6f19062010-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 Dunbarf80cb752010-04-29 16:29:09 +0000178
Chris Lattnerd6f19062010-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 Dunbarf80cb752010-04-29 16:29:09 +0000184
Chris Lattnerd6f19062010-04-08 00:23:06 +0000185 // Translate the offset into the file.
186 unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000187 SourceLocation NewLoc =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000188 CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
Chris Lattnerd6f19062010-04-08 00:23:06 +0000189 return FullSourceLoc(NewLoc, CSM);
190}
191
Chris Lattnercabae682010-04-06 17:52:14 +0000192
Chris Lattner6da9eb62010-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 Dunbarf80cb752010-04-29 16:29:09 +0000195/// the temporary memory buffer that the inline asm parser has set up.
Chris Lattner6da9eb62010-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 Dunbarf80cb752010-04-29 16:29:09 +0000200
Chris Lattner6da9eb62010-04-06 18:38:50 +0000201 // Strip "error: " off the start of the message string.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000202 StringRef Message = D.getMessage();
Chris Lattner6da9eb62010-04-06 18:38:50 +0000203 if (Message.startswith("error: "))
204 Message = Message.substr(7);
205
Chris Lattner99e14a02010-06-15 00:03:12 +0000206 // If the SMDiagnostic has an inline asm source location, translate it.
Chris Lattner6da9eb62010-04-06 18:38:50 +0000207 FullSourceLoc Loc;
Chris Lattnerd6f19062010-04-08 00:23:06 +0000208 if (D.getLoc() != SMLoc())
209 Loc = ConvertBackendLocation(D, Context->getSourceManager());
Chris Lattner99e14a02010-06-15 00:03:12 +0000210
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000211
Chris Lattner99e14a02010-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 Kyrtzidis33e4e702010-11-18 20:06:41 +0000216 Diags.Report(LocCookie, diag::err_fe_inline_asm).AddString(Message);
Chris Lattner99e14a02010-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 Lattnerfc8f0e12011-04-15 05:22:18 +0000223 // Otherwise, report the backend error as occurring in the generated .s file.
Chris Lattner99e14a02010-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 Lattner6da9eb62010-04-06 18:38:50 +0000227}
228
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000229//
230
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000231CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
232 : Act(_Act), VMContext(_VMContext ? _VMContext : new LLVMContext),
233 OwnsVMContext(!_VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000234
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000235CodeGenAction::~CodeGenAction() {
236 TheModule.reset();
237 if (OwnsVMContext)
238 delete VMContext;
239}
Daniel Dunbar9ad1c022010-02-25 20:37:44 +0000240
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000241bool CodeGenAction::hasIRSupport() const { return true; }
242
Daniel Dunbarb954e982010-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 Weber5aa74af2011-01-25 20:34:14 +0000249 TheModule.reset(BEConsumer->takeModule());
Daniel Dunbarb954e982010-02-25 04:37:50 +0000250}
251
252llvm::Module *CodeGenAction::takeModule() {
253 return TheModule.take();
254}
255
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000256llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
257 OwnsVMContext = false;
258 return VMContext;
259}
260
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000261static raw_ostream *GetOutputStream(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000262 StringRef InFile,
Daniel Dunbar4cbbd942010-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 Blaikieb219cfc2011-09-23 05:06:16 +0000278 llvm_unreachable("Invalid action!");
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000279}
280
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000281ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000282 StringRef InFile) {
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000283 BackendAction BA = static_cast<BackendAction>(Act);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000284 llvm::OwningPtr<raw_ostream> OS(GetOutputStream(CI, InFile, BA));
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000285 if (BA != Backend_EmitNothing && !OS)
286 return 0;
287
Nico Weber5aa74af2011-01-25 20:34:14 +0000288 BEConsumer =
289 new BackendConsumer(BA, CI.getDiagnostics(),
290 CI.getCodeGenOpts(), CI.getTargetOpts(),
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000291 CI.getLangOpts(),
Nico Weber5aa74af2011-01-25 20:34:14 +0000292 CI.getFrontendOpts().ShowTimers, InFile, OS.take(),
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000293 *VMContext);
Nico Weber5aa74af2011-01-25 20:34:14 +0000294 return BEConsumer;
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000295}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000296
Daniel Dunbar4cbbd942010-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 Collingbourne4b93d662011-02-19 23:03:58 +0000319 TheModule.reset(ParseIR(MainFileCopy, Err, *VMContext));
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000320 if (!TheModule) {
321 // Translate from the diagnostic info to the SourceManager location.
Argyrios Kyrtzidis5a9ee202011-09-19 20:40:38 +0000322 SourceLocation Loc = SM.translateFileLineCol(
Daniel Dunbar4cbbd942010-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 Lattner5f9e2722011-07-23 10:55:15 +0000328 StringRef Msg = Err.getMessage();
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000329 if (Msg.startswith("error: "))
330 Msg = Msg.substr(7);
David Blaikied6471f72011-09-25 23:23:43 +0000331 unsigned DiagID = CI.getDiagnostics().getCustomDiagID(
332 DiagnosticsEngine::Error, Msg);
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000333
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000334 CI.getDiagnostics().Report(Loc, DiagID);
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000335 return;
336 }
337
338 EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(),
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000339 CI.getTargetOpts(), CI.getLangOpts(),
340 TheModule.get(),
Daniel Dunbar4cbbd942010-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 Collingbourne4b93d662011-02-19 23:03:58 +0000351EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
352 : CodeGenAction(Backend_EmitAssembly, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000353
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000354EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
355 : CodeGenAction(Backend_EmitBC, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000356
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000357EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
358 : CodeGenAction(Backend_EmitLL, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000359
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000360EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
361 : CodeGenAction(Backend_EmitNothing, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000362
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000363EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
364 : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
Daniel Dunbar32148ce2010-05-25 18:41:01 +0000365
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000366EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
367 : CodeGenAction(Backend_EmitObj, _VMContext) {}