blob: 11d6075582fe8b630f14f147bac8dae5e0fd3cc4 [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
Benjamin Kramer96fda0c2011-10-16 10:48:28 +0000218 if (D.getLoc().isValid()) {
219 DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
220 // Convert the SMDiagnostic ranges into SourceRange and attach them
221 // to the diagnostic.
222 for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) {
223 std::pair<unsigned, unsigned> Range = D.getRanges()[i];
224 unsigned Column = D.getColumnNo();
225 B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
226 Loc.getLocWithOffset(Range.second - Column));
227 }
228 }
Chris Lattner99e14a02010-06-15 00:03:12 +0000229 return;
230 }
231
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000232 // Otherwise, report the backend error as occurring in the generated .s file.
Chris Lattner99e14a02010-06-15 00:03:12 +0000233 // If Loc is invalid, we still need to report the error, it just gets no
234 // location info.
235 Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message);
Chris Lattner6da9eb62010-04-06 18:38:50 +0000236}
237
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000238//
239
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000240CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
241 : Act(_Act), VMContext(_VMContext ? _VMContext : new LLVMContext),
242 OwnsVMContext(!_VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000243
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000244CodeGenAction::~CodeGenAction() {
245 TheModule.reset();
246 if (OwnsVMContext)
247 delete VMContext;
248}
Daniel Dunbar9ad1c022010-02-25 20:37:44 +0000249
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000250bool CodeGenAction::hasIRSupport() const { return true; }
251
Daniel Dunbarb954e982010-02-25 04:37:50 +0000252void CodeGenAction::EndSourceFileAction() {
253 // If the consumer creation failed, do nothing.
254 if (!getCompilerInstance().hasASTConsumer())
255 return;
256
257 // Steal the module from the consumer.
Nico Weber5aa74af2011-01-25 20:34:14 +0000258 TheModule.reset(BEConsumer->takeModule());
Daniel Dunbarb954e982010-02-25 04:37:50 +0000259}
260
261llvm::Module *CodeGenAction::takeModule() {
262 return TheModule.take();
263}
264
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000265llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
266 OwnsVMContext = false;
267 return VMContext;
268}
269
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000270static raw_ostream *GetOutputStream(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000271 StringRef InFile,
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000272 BackendAction Action) {
273 switch (Action) {
274 case Backend_EmitAssembly:
275 return CI.createDefaultOutputFile(false, InFile, "s");
276 case Backend_EmitLL:
277 return CI.createDefaultOutputFile(false, InFile, "ll");
278 case Backend_EmitBC:
279 return CI.createDefaultOutputFile(true, InFile, "bc");
280 case Backend_EmitNothing:
281 return 0;
282 case Backend_EmitMCNull:
283 case Backend_EmitObj:
284 return CI.createDefaultOutputFile(true, InFile, "o");
285 }
286
David Blaikieb219cfc2011-09-23 05:06:16 +0000287 llvm_unreachable("Invalid action!");
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000288}
289
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000290ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000291 StringRef InFile) {
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000292 BackendAction BA = static_cast<BackendAction>(Act);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000293 llvm::OwningPtr<raw_ostream> OS(GetOutputStream(CI, InFile, BA));
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000294 if (BA != Backend_EmitNothing && !OS)
295 return 0;
296
Nico Weber5aa74af2011-01-25 20:34:14 +0000297 BEConsumer =
298 new BackendConsumer(BA, CI.getDiagnostics(),
299 CI.getCodeGenOpts(), CI.getTargetOpts(),
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000300 CI.getLangOpts(),
Nico Weber5aa74af2011-01-25 20:34:14 +0000301 CI.getFrontendOpts().ShowTimers, InFile, OS.take(),
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000302 *VMContext);
Nico Weber5aa74af2011-01-25 20:34:14 +0000303 return BEConsumer;
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000304}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000305
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000306void CodeGenAction::ExecuteAction() {
307 // If this is an IR file, we have to treat it specially.
308 if (getCurrentFileKind() == IK_LLVM_IR) {
309 BackendAction BA = static_cast<BackendAction>(Act);
310 CompilerInstance &CI = getCompilerInstance();
311 raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA);
312 if (BA != Backend_EmitNothing && !OS)
313 return;
314
315 bool Invalid;
316 SourceManager &SM = CI.getSourceManager();
317 const llvm::MemoryBuffer *MainFile = SM.getBuffer(SM.getMainFileID(),
318 &Invalid);
319 if (Invalid)
320 return;
321
322 // FIXME: This is stupid, IRReader shouldn't take ownership.
323 llvm::MemoryBuffer *MainFileCopy =
324 llvm::MemoryBuffer::getMemBufferCopy(MainFile->getBuffer(),
325 getCurrentFile().c_str());
326
327 llvm::SMDiagnostic Err;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000328 TheModule.reset(ParseIR(MainFileCopy, Err, *VMContext));
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000329 if (!TheModule) {
330 // Translate from the diagnostic info to the SourceManager location.
Argyrios Kyrtzidis5a9ee202011-09-19 20:40:38 +0000331 SourceLocation Loc = SM.translateFileLineCol(
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000332 SM.getFileEntryForID(SM.getMainFileID()), Err.getLineNo(),
333 Err.getColumnNo() + 1);
334
335 // Get a custom diagnostic for the error. We strip off a leading
336 // diagnostic code if there is one.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000337 StringRef Msg = Err.getMessage();
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000338 if (Msg.startswith("error: "))
339 Msg = Msg.substr(7);
David Blaikied6471f72011-09-25 23:23:43 +0000340 unsigned DiagID = CI.getDiagnostics().getCustomDiagID(
341 DiagnosticsEngine::Error, Msg);
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000342
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000343 CI.getDiagnostics().Report(Loc, DiagID);
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000344 return;
345 }
346
347 EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(),
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000348 CI.getTargetOpts(), CI.getLangOpts(),
349 TheModule.get(),
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000350 BA, OS);
351 return;
352 }
353
354 // Otherwise follow the normal AST path.
355 this->ASTFrontendAction::ExecuteAction();
356}
357
358//
359
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000360EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
361 : CodeGenAction(Backend_EmitAssembly, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000362
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000363EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
364 : CodeGenAction(Backend_EmitBC, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000365
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000366EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
367 : CodeGenAction(Backend_EmitLL, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000368
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000369EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
370 : CodeGenAction(Backend_EmitNothing, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000371
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000372EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
373 : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
Daniel Dunbar32148ce2010-05-25 18:41:01 +0000374
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000375EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
376 : CodeGenAction(Backend_EmitObj, _VMContext) {}