blob: 1f8d1129dad184f0b35158cd416efb5009b180bb [file] [log] [blame]
Daniel Dunbar41b5b172010-05-20 17:49:16 +00001//===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
Daniel Dunbar217acbf2009-11-19 07:37:51 +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 Dunbar21085772009-12-11 22:20:12 +000010// This is the entry point to the clang -cc1 functionality, which implements the
11// core compiler functionality along with a number of additional tools for
12// demonstration and testing purposes.
Daniel Dunbar217acbf2009-11-19 07:37:51 +000013//
14//===----------------------------------------------------------------------===//
15
Daniel Dunbar21085772009-12-11 22:20:12 +000016#include "clang/Driver/Arg.h"
17#include "clang/Driver/ArgList.h"
18#include "clang/Driver/CC1Options.h"
19#include "clang/Driver/DriverDiagnostic.h"
20#include "clang/Driver/OptTable.h"
21#include "clang/Frontend/CompilerInstance.h"
22#include "clang/Frontend/CompilerInvocation.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000023#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000024#include "clang/Frontend/TextDiagnosticBuffer.h"
25#include "clang/Frontend/TextDiagnosticPrinter.h"
Peter Collingbourne1b7255d2010-08-24 00:31:22 +000026#include "clang/FrontendTool/Utils.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000027#include "llvm/LLVMContext.h"
Douglas Gregor95dd5582010-03-30 17:33:59 +000028#include "llvm/ADT/Statistic.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000029#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/ManagedStatic.h"
Chris Lattner30bc7e82010-03-30 05:39:52 +000031#include "llvm/Support/Timer.h"
Daniel Dunbar217acbf2009-11-19 07:37:51 +000032#include "llvm/Support/raw_ostream.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000033#include "llvm/Target/TargetSelect.h"
34#include <cstdio>
35using namespace clang;
Daniel Dunbar217acbf2009-11-19 07:37:51 +000036
Daniel Dunbar21085772009-12-11 22:20:12 +000037//===----------------------------------------------------------------------===//
38// Main driver
39//===----------------------------------------------------------------------===//
40
Daniel Dunbar41b5b172010-05-20 17:49:16 +000041static void LLVMErrorHandler(void *UserData, const std::string &Message) {
Daniel Dunbar21085772009-12-11 22:20:12 +000042 Diagnostic &Diags = *static_cast<Diagnostic*>(UserData);
43
44 Diags.Report(diag::err_fe_error_backend) << Message;
45
46 // We cannot recover from llvm errors.
47 exit(1);
48}
49
Daniel Dunbar21085772009-12-11 22:20:12 +000050// FIXME: Define the need for this testing away.
51static int cc1_test(Diagnostic &Diags,
Daniel Dunbar1e69fe32009-12-13 03:45:58 +000052 const char **ArgBegin, const char **ArgEnd) {
Daniel Dunbar21085772009-12-11 22:20:12 +000053 using namespace clang::driver;
54
Daniel Dunbar217acbf2009-11-19 07:37:51 +000055 llvm::errs() << "cc1 argv:";
56 for (const char **i = ArgBegin; i != ArgEnd; ++i)
57 llvm::errs() << " \"" << *i << '"';
58 llvm::errs() << "\n";
59
Daniel Dunbar21085772009-12-11 22:20:12 +000060 // Parse the arguments.
61 OptTable *Opts = createCC1OptTable();
62 unsigned MissingArgIndex, MissingArgCount;
63 InputArgList *Args = Opts->ParseArgs(ArgBegin, ArgEnd,
64 MissingArgIndex, MissingArgCount);
65
66 // Check for missing argument error.
67 if (MissingArgCount)
68 Diags.Report(clang::diag::err_drv_missing_argument)
69 << Args->getArgString(MissingArgIndex) << MissingArgCount;
70
71 // Dump the parsed arguments.
72 llvm::errs() << "cc1 parsed options:\n";
73 for (ArgList::const_iterator it = Args->begin(), ie = Args->end();
74 it != ie; ++it)
75 (*it)->dump();
76
77 // Create a compiler invocation.
78 llvm::errs() << "cc1 creating invocation.\n";
79 CompilerInvocation Invocation;
Daniel Dunbar1e69fe32009-12-13 03:45:58 +000080 CompilerInvocation::CreateFromArgs(Invocation, ArgBegin, ArgEnd, Diags);
Daniel Dunbar21085772009-12-11 22:20:12 +000081
82 // Convert the invocation back to argument strings.
83 std::vector<std::string> InvocationArgs;
84 Invocation.toArgs(InvocationArgs);
85
86 // Dump the converted arguments.
87 llvm::SmallVector<const char*, 32> Invocation2Args;
88 llvm::errs() << "invocation argv :";
89 for (unsigned i = 0, e = InvocationArgs.size(); i != e; ++i) {
90 Invocation2Args.push_back(InvocationArgs[i].c_str());
91 llvm::errs() << " \"" << InvocationArgs[i] << '"';
92 }
93 llvm::errs() << "\n";
94
95 // Convert those arguments to another invocation, and check that we got the
96 // same thing.
97 CompilerInvocation Invocation2;
98 CompilerInvocation::CreateFromArgs(Invocation2, Invocation2Args.begin(),
Daniel Dunbar1e69fe32009-12-13 03:45:58 +000099 Invocation2Args.end(), Diags);
Daniel Dunbar21085772009-12-11 22:20:12 +0000100
101 // FIXME: Implement CompilerInvocation comparison.
102 if (true) {
103 //llvm::errs() << "warning: Invocations differ!\n";
104
105 std::vector<std::string> Invocation2Args;
106 Invocation2.toArgs(Invocation2Args);
107 llvm::errs() << "invocation2 argv:";
108 for (unsigned i = 0, e = Invocation2Args.size(); i != e; ++i)
109 llvm::errs() << " \"" << Invocation2Args[i] << '"';
110 llvm::errs() << "\n";
111 }
112
Daniel Dunbar217acbf2009-11-19 07:37:51 +0000113 return 0;
114}
Daniel Dunbar21085772009-12-11 22:20:12 +0000115
116int cc1_main(const char **ArgBegin, const char **ArgEnd,
117 const char *Argv0, void *MainAddr) {
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000118 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000119 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
Daniel Dunbar42e9f8e42010-02-16 01:54:47 +0000120
Chris Lattnercabae682010-04-06 17:52:14 +0000121 Clang->setLLVMContext(new llvm::LLVMContext());
Daniel Dunbar21085772009-12-11 22:20:12 +0000122
123 // Run clang -cc1 test.
124 if (ArgBegin != ArgEnd && llvm::StringRef(ArgBegin[0]) == "-cc1test") {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000125 Diagnostic Diags(DiagID, new TextDiagnosticPrinter(llvm::errs(),
126 DiagnosticOptions()));
Daniel Dunbar1e69fe32009-12-13 03:45:58 +0000127 return cc1_test(Diags, ArgBegin + 1, ArgEnd);
Daniel Dunbar21085772009-12-11 22:20:12 +0000128 }
129
130 // Initialize targets first, so that --version shows registered targets.
131 llvm::InitializeAllTargets();
132 llvm::InitializeAllAsmPrinters();
Chris Lattner01ae93f2010-04-05 23:33:20 +0000133 llvm::InitializeAllAsmParsers();
Daniel Dunbar21085772009-12-11 22:20:12 +0000134
135 // Buffer diagnostics from argument parsing so that we can output them using a
136 // well formed diagnostic object.
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000137 TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000138 Diagnostic Diags(DiagID, DiagsBuffer);
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000139 CompilerInvocation::CreateFromArgs(Clang->getInvocation(), ArgBegin, ArgEnd,
Daniel Dunbar1e69fe32009-12-13 03:45:58 +0000140 Diags);
141
142 // Infer the builtin include path if unspecified.
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000143 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
144 Clang->getHeaderSearchOpts().ResourceDir.empty())
145 Clang->getHeaderSearchOpts().ResourceDir =
Daniel Dunbar8b9adfe2009-12-15 00:06:45 +0000146 CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
Daniel Dunbar21085772009-12-11 22:20:12 +0000147
Daniel Dunbara6bf47f2010-08-12 02:53:07 +0000148 // Create the actual diagnostics engine.
149 Clang->createDiagnostics(ArgEnd - ArgBegin, const_cast<char**>(ArgBegin));
150 if (!Clang->hasDiagnostics())
151 return 1;
152
153 // Set an error handler, so that any LLVM backend diagnostics go through our
154 // error handler.
155 llvm::install_fatal_error_handler(LLVMErrorHandler,
156 static_cast<void*>(&Clang->getDiagnostics()));
157
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000158 DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
Daniel Dunbara6bf47f2010-08-12 02:53:07 +0000159
Daniel Dunbar8eb2b012010-08-12 02:53:12 +0000160 // Execute the frontend actions.
161 bool Success = ExecuteCompilerInvocation(Clang.get());
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000162
Chris Lattner30bc7e82010-03-30 05:39:52 +0000163 // If any timers were active but haven't been destroyed yet, print their
164 // results now. This happens in -disable-free mode.
165 llvm::TimerGroup::printAll(llvm::errs());
Daniel Dunbarf56a4882010-08-02 15:31:28 +0000166
Dan Gohman726578c2010-08-18 21:23:17 +0000167 // Our error handler depends on the Diagnostics object, which we're
168 // potentially about to delete. Uninstall the handler now so that any
169 // later errors use the default handling behavior instead.
170 llvm::remove_fatal_error_handler();
171
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000172 // When running with -disable-free, don't do any destruction or shutdown.
173 if (Clang->getFrontendOpts().DisableFree) {
Douglas Gregor95dd5582010-03-30 17:33:59 +0000174 if (Clang->getFrontendOpts().ShowStats)
175 llvm::PrintStatistics();
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000176 Clang.take();
177 return !Success;
Daniel Dunbar21085772009-12-11 22:20:12 +0000178 }
179
Daniel Dunbar21085772009-12-11 22:20:12 +0000180 // Managed static deconstruction. Useful for making things like
181 // -time-passes usable.
182 llvm::llvm_shutdown();
183
Daniel Dunbar0397af22010-01-13 00:48:06 +0000184 return !Success;
Daniel Dunbar21085772009-12-11 22:20:12 +0000185}