blob: 4d200bfadaf239eaf6f1768eed83e8680b6ec0af [file] [log] [blame]
Daniel Dunbar2fcaa542010-05-20 17:49:16 +00001//===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
Daniel Dunbar51cd8f02009-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 Dunbarf72bdf72009-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 Dunbar51cd8f02009-11-19 07:37:51 +000013//
14//===----------------------------------------------------------------------===//
15
Reid Kleckner898229a2013-06-14 17:17:23 +000016#include "llvm/Option/Arg.h"
Adrian Prantlbc068582015-07-08 01:00:30 +000017#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
Chris Bienemana6b39ab2016-08-23 20:07:07 +000018#include "clang/Config/config.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000019#include "clang/Driver/DriverDiagnostic.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000020#include "clang/Driver/Options.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000021#include "clang/Frontend/CompilerInstance.h"
22#include "clang/Frontend/CompilerInvocation.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000023#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000024#include "clang/Frontend/TextDiagnosticBuffer.h"
25#include "clang/Frontend/TextDiagnosticPrinter.h"
Kostya Serebryanyce2c7262013-12-27 08:11:08 +000026#include "clang/Frontend/Utils.h"
Peter Collingbourne85dd0bd2010-08-24 00:31:22 +000027#include "clang/FrontendTool/Utils.h"
Douglas Gregor171b7802010-03-30 17:33:59 +000028#include "llvm/ADT/Statistic.h"
Nico Weberd637c052018-04-30 13:52:15 +000029#include "llvm/Config/llvm-config.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000030#include "llvm/LinkAllPasses.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000031#include "llvm/Option/ArgList.h"
32#include "llvm/Option/OptTable.h"
Richard Smith194b6a32016-08-17 01:05:07 +000033#include "llvm/Support/Compiler.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/ManagedStatic.h"
Chad Rosier7ea73972012-11-12 19:39:37 +000036#include "llvm/Support/Signals.h"
Evan Cheng494eb062011-08-24 18:09:14 +000037#include "llvm/Support/TargetSelect.h"
Chris Lattner09f8cc82010-03-30 05:39:52 +000038#include "llvm/Support/Timer.h"
Daniel Dunbar51cd8f02009-11-19 07:37:51 +000039#include "llvm/Support/raw_ostream.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000040#include <cstdio>
Richard Smithc33b8372016-08-18 18:22:22 +000041
Chris Bienemana6b39ab2016-08-23 20:07:07 +000042#ifdef CLANG_HAVE_RLIMITS
Richard Smith194b6a32016-08-17 01:05:07 +000043#include <sys/resource.h>
44#endif
Richard Smithc33b8372016-08-18 18:22:22 +000045
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000046using namespace clang;
Reid Kleckner898229a2013-06-14 17:17:23 +000047using namespace llvm::opt;
Daniel Dunbar51cd8f02009-11-19 07:37:51 +000048
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000049//===----------------------------------------------------------------------===//
50// Main driver
51//===----------------------------------------------------------------------===//
52
Chad Rosier05c71aa2013-03-27 18:28:23 +000053static void LLVMErrorHandler(void *UserData, const std::string &Message,
54 bool GenCrashDiag) {
David Blaikie9c902b52011-09-25 23:23:43 +000055 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000056
57 Diags.Report(diag::err_fe_error_backend) << Message;
58
Chad Rosier7ea73972012-11-12 19:39:37 +000059 // Run the interrupt handlers to make sure any special cleanups get done, in
60 // particular that we remove files registered with RemoveFileOnSignal.
61 llvm::sys::RunInterruptHandlers();
62
Chad Rosierad6e96d2012-11-12 21:32:24 +000063 // We cannot recover from llvm errors. When reporting a fatal error, exit
Chad Rosier05c71aa2013-03-27 18:28:23 +000064 // with status 70 to generate crash diagnostics. For BSD systems this is
65 // defined as an internal software error. Otherwise, exit with status 1.
66 exit(GenCrashDiag ? 70 : 1);
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000067}
68
Sebastian Pop17fac042014-03-14 04:04:27 +000069#ifdef LINK_POLLY_INTO_TOOLS
70namespace polly {
71void initializePollyPasses(llvm::PassRegistry &Registry);
72}
73#endif
74
Chris Bienemana6b39ab2016-08-23 20:07:07 +000075#ifdef CLANG_HAVE_RLIMITS
Richard Smith194b6a32016-08-17 01:05:07 +000076// The amount of stack we think is "sufficient". If less than this much is
77// available, we may be unable to reach our template instantiation depth
78// limit and other similar limits.
79// FIXME: Unify this with the stack we request when spawning a thread to build
80// a module.
81static const int kSufficientStack = 8 << 20;
82
83#if defined(__linux__) && defined(__PIE__)
Richard Smithf80a27e2016-10-14 19:51:36 +000084static size_t getCurrentStackAllocation() {
85 // If we can't compute the current stack usage, allow for 512K of command
86 // line arguments and environment.
87 size_t Usage = 512 * 1024;
88 if (FILE *StatFile = fopen("/proc/self/stat", "r")) {
89 // We assume that the stack extends from its current address to the end of
90 // the environment space. In reality, there is another string literal (the
91 // program name) after the environment, but this is close enough (we only
92 // need to be within 100K or so).
93 unsigned long StackPtr, EnvEnd;
Richard Smith49db68d2016-10-15 01:59:52 +000094 // Disable silly GCC -Wformat warning that complains about length
95 // modifiers on ignored format specifiers. We want to retain these
96 // for documentation purposes even though they have no effect.
97#if defined(__GNUC__) && !defined(__clang__)
98#pragma GCC diagnostic push
99#pragma GCC diagnostic ignored "-Wformat"
100#endif
Richard Smithf80a27e2016-10-14 19:51:36 +0000101 if (fscanf(StatFile,
102 "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*lu %*lu %*lu %*lu %*lu "
103 "%*lu %*ld %*ld %*ld %*ld %*ld %*ld %*llu %*lu %*ld %*lu %*lu "
104 "%*lu %*lu %lu %*lu %*lu %*lu %*lu %*lu %*llu %*lu %*lu %*d %*d "
105 "%*u %*u %*llu %*lu %*ld %*lu %*lu %*lu %*lu %*lu %*lu %lu %*d",
106 &StackPtr, &EnvEnd) == 2) {
Richard Smith49db68d2016-10-15 01:59:52 +0000107#if defined(__GNUC__) && !defined(__clang__)
108#pragma GCC diagnostic pop
109#endif
Richard Smithf80a27e2016-10-14 19:51:36 +0000110 Usage = StackPtr < EnvEnd ? EnvEnd - StackPtr : StackPtr - EnvEnd;
111 }
112 fclose(StatFile);
113 }
114 return Usage;
115}
116
117#include <alloca.h>
118
Richard Smith194b6a32016-08-17 01:05:07 +0000119LLVM_ATTRIBUTE_NOINLINE
Richard Smithf80a27e2016-10-14 19:51:36 +0000120static void ensureStackAddressSpace(int ExtraChunks = 0) {
Richard Smith194b6a32016-08-17 01:05:07 +0000121 // Linux kernels prior to 4.1 will sometimes locate the heap of a PIE binary
122 // relatively close to the stack (they are only guaranteed to be 128MiB
123 // apart). This results in crashes if we happen to heap-allocate more than
124 // 128MiB before we reach our stack high-water mark.
125 //
126 // To avoid these crashes, ensure that we have sufficient virtual memory
Richard Smithf80a27e2016-10-14 19:51:36 +0000127 // pages allocated before we start running.
128 size_t Curr = getCurrentStackAllocation();
129 const int kTargetStack = kSufficientStack - 256 * 1024;
130 if (Curr < kTargetStack) {
131 volatile char *volatile Alloc =
132 static_cast<volatile char *>(alloca(kTargetStack - Curr));
133 Alloc[0] = 0;
134 Alloc[kTargetStack - Curr - 1] = 0;
135 }
Richard Smith194b6a32016-08-17 01:05:07 +0000136}
137#else
138static void ensureStackAddressSpace() {}
139#endif
140
141/// Attempt to ensure that we have at least 8MiB of usable stack space.
142static void ensureSufficientStack() {
143 struct rlimit rlim;
144 if (getrlimit(RLIMIT_STACK, &rlim) != 0)
145 return;
146
147 // Increase the soft stack limit to our desired level, if necessary and
148 // possible.
149 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < kSufficientStack) {
150 // Try to allocate sufficient stack.
151 if (rlim.rlim_max == RLIM_INFINITY || rlim.rlim_max >= kSufficientStack)
152 rlim.rlim_cur = kSufficientStack;
153 else if (rlim.rlim_cur == rlim.rlim_max)
154 return;
155 else
156 rlim.rlim_cur = rlim.rlim_max;
157
158 if (setrlimit(RLIMIT_STACK, &rlim) != 0 ||
159 rlim.rlim_cur != kSufficientStack)
160 return;
161 }
162
163 // We should now have a stack of size at least kSufficientStack. Ensure
164 // that we can actually use that much, if necessary.
165 ensureStackAddressSpace();
166}
167#else
Richard Smith525ce252016-08-17 02:22:39 +0000168static void ensureSufficientStack() {}
Richard Smith194b6a32016-08-17 01:05:07 +0000169#endif
170
Sean Silva070cd2d2014-08-15 21:38:36 +0000171int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
Richard Smith194b6a32016-08-17 01:05:07 +0000172 ensureSufficientStack();
173
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000174 std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000175 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
Daniel Dunbare922d9b2010-02-16 01:54:47 +0000176
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000177 // Register the support for object-file-wrapped Clang modules.
178 auto PCHOps = Clang->getPCHContainerOperations();
179 PCHOps->registerWriter(llvm::make_unique<ObjectFilePCHContainerWriter>());
180 PCHOps->registerReader(llvm::make_unique<ObjectFilePCHContainerReader>());
181
Daniel Dunbarf72bdf72009-12-11 22:20:12 +0000182 // Initialize targets first, so that --version shows registered targets.
183 llvm::InitializeAllTargets();
Evan Chengc391a582011-07-22 21:59:11 +0000184 llvm::InitializeAllTargetMCs();
Daniel Dunbarf72bdf72009-12-11 22:20:12 +0000185 llvm::InitializeAllAsmPrinters();
Chris Lattner61955ab2010-04-05 23:33:20 +0000186 llvm::InitializeAllAsmParsers();
Daniel Dunbarf72bdf72009-12-11 22:20:12 +0000187
Sebastian Pop17fac042014-03-14 04:04:27 +0000188#ifdef LINK_POLLY_INTO_TOOLS
189 llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry();
190 polly::initializePollyPasses(Registry);
191#endif
192
Daniel Dunbarf72bdf72009-12-11 22:20:12 +0000193 // Buffer diagnostics from argument parsing so that we can output them using a
194 // well formed diagnostic object.
Douglas Gregor811db4e2012-10-23 22:26:28 +0000195 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000196 TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
Douglas Gregor811db4e2012-10-23 22:26:28 +0000197 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
Sean Silva070cd2d2014-08-15 21:38:36 +0000198 bool Success = CompilerInvocation::CreateFromArgs(
199 Clang->getInvocation(), Argv.begin(), Argv.end(), Diags);
Daniel Dunbard6136772009-12-13 03:45:58 +0000200
201 // Infer the builtin include path if unspecified.
Daniel Dunbar30b24e92010-03-23 05:09:16 +0000202 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
203 Clang->getHeaderSearchOpts().ResourceDir.empty())
204 Clang->getHeaderSearchOpts().ResourceDir =
Daniel Dunbara5a166d2009-12-15 00:06:45 +0000205 CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
Daniel Dunbarf72bdf72009-12-11 22:20:12 +0000206
Daniel Dunbar1cdf04c2010-08-12 02:53:07 +0000207 // Create the actual diagnostics engine.
Sean Silvaf1b49e22013-01-20 01:58:28 +0000208 Clang->createDiagnostics();
Daniel Dunbar1cdf04c2010-08-12 02:53:07 +0000209 if (!Clang->hasDiagnostics())
210 return 1;
211
212 // Set an error handler, so that any LLVM backend diagnostics go through our
213 // error handler.
214 llvm::install_fatal_error_handler(LLVMErrorHandler,
215 static_cast<void*>(&Clang->getDiagnostics()));
216
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000217 DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
Argyrios Kyrtzidisbe6d89d2012-01-25 20:00:43 +0000218 if (!Success)
219 return 1;
Daniel Dunbar1cdf04c2010-08-12 02:53:07 +0000220
Daniel Dunbare9e91b02010-08-12 02:53:12 +0000221 // Execute the frontend actions.
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000222 Success = ExecuteCompilerInvocation(Clang.get());
Daniel Dunbar30b24e92010-03-23 05:09:16 +0000223
Chris Lattner09f8cc82010-03-30 05:39:52 +0000224 // If any timers were active but haven't been destroyed yet, print their
225 // results now. This happens in -disable-free mode.
226 llvm::TimerGroup::printAll(llvm::errs());
Daniel Dunbar2be96742010-08-02 15:31:28 +0000227
Dan Gohmanb37af7d2010-08-18 21:23:17 +0000228 // Our error handler depends on the Diagnostics object, which we're
229 // potentially about to delete. Uninstall the handler now so that any
230 // later errors use the default handling behavior instead.
231 llvm::remove_fatal_error_handler();
232
Daniel Dunbar30b24e92010-03-23 05:09:16 +0000233 // When running with -disable-free, don't do any destruction or shutdown.
234 if (Clang->getFrontendOpts().DisableFree) {
David Blaikie3c13a7f2014-08-29 17:02:26 +0000235 BuryPointer(std::move(Clang));
Daniel Dunbar30b24e92010-03-23 05:09:16 +0000236 return !Success;
Daniel Dunbarf72bdf72009-12-11 22:20:12 +0000237 }
238
Daniel Dunbar4f2bc552010-01-13 00:48:06 +0000239 return !Success;
Daniel Dunbarf72bdf72009-12-11 22:20:12 +0000240}