Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 1 | //===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===// |
Daniel Dunbar | 51cd8f0 | 2009-11-19 07:37:51 +0000 | [diff] [blame] | 2 | // |
| 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 Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 10 | // 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 Dunbar | 51cd8f0 | 2009-11-19 07:37:51 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 16 | #include "llvm/Option/Arg.h" |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 17 | #include "clang/CodeGen/ObjectFilePCHContainerOperations.h" |
Chris Bieneman | a6b39ab | 2016-08-23 20:07:07 +0000 | [diff] [blame] | 18 | #include "clang/Config/config.h" |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Stack.h" |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 20 | #include "clang/Driver/DriverDiagnostic.h" |
Chandler Carruth | cc0694c | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 21 | #include "clang/Driver/Options.h" |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CompilerInstance.h" |
| 23 | #include "clang/Frontend/CompilerInvocation.h" |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| 26 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Kostya Serebryany | ce2c726 | 2013-12-27 08:11:08 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/Utils.h" |
Peter Collingbourne | 85dd0bd | 2010-08-24 00:31:22 +0000 | [diff] [blame] | 28 | #include "clang/FrontendTool/Utils.h" |
Douglas Gregor | 171b780 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Statistic.h" |
Nico Weber | d637c05 | 2018-04-30 13:52:15 +0000 | [diff] [blame] | 30 | #include "llvm/Config/llvm-config.h" |
Chandler Carruth | cc0694c | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 31 | #include "llvm/LinkAllPasses.h" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 32 | #include "llvm/Option/ArgList.h" |
| 33 | #include "llvm/Option/OptTable.h" |
Richard Smith | 194b6a3 | 2016-08-17 01:05:07 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Compiler.h" |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
| 36 | #include "llvm/Support/ManagedStatic.h" |
Chad Rosier | 7ea7397 | 2012-11-12 19:39:37 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Signals.h" |
Evan Cheng | 494eb06 | 2011-08-24 18:09:14 +0000 | [diff] [blame] | 38 | #include "llvm/Support/TargetSelect.h" |
Chris Lattner | 09f8cc8 | 2010-03-30 05:39:52 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Timer.h" |
Daniel Dunbar | 51cd8f0 | 2009-11-19 07:37:51 +0000 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 41 | #include <cstdio> |
Richard Smith | c33b837 | 2016-08-18 18:22:22 +0000 | [diff] [blame] | 42 | |
Chris Bieneman | a6b39ab | 2016-08-23 20:07:07 +0000 | [diff] [blame] | 43 | #ifdef CLANG_HAVE_RLIMITS |
Richard Smith | 194b6a3 | 2016-08-17 01:05:07 +0000 | [diff] [blame] | 44 | #include <sys/resource.h> |
| 45 | #endif |
Richard Smith | c33b837 | 2016-08-18 18:22:22 +0000 | [diff] [blame] | 46 | |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 47 | using namespace clang; |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 48 | using namespace llvm::opt; |
Daniel Dunbar | 51cd8f0 | 2009-11-19 07:37:51 +0000 | [diff] [blame] | 49 | |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// |
| 51 | // Main driver |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | |
Chad Rosier | 05c71aa | 2013-03-27 18:28:23 +0000 | [diff] [blame] | 54 | static void LLVMErrorHandler(void *UserData, const std::string &Message, |
| 55 | bool GenCrashDiag) { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 56 | DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData); |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 57 | |
| 58 | Diags.Report(diag::err_fe_error_backend) << Message; |
| 59 | |
Chad Rosier | 7ea7397 | 2012-11-12 19:39:37 +0000 | [diff] [blame] | 60 | // Run the interrupt handlers to make sure any special cleanups get done, in |
| 61 | // particular that we remove files registered with RemoveFileOnSignal. |
| 62 | llvm::sys::RunInterruptHandlers(); |
| 63 | |
Chad Rosier | ad6e96d | 2012-11-12 21:32:24 +0000 | [diff] [blame] | 64 | // We cannot recover from llvm errors. When reporting a fatal error, exit |
Chad Rosier | 05c71aa | 2013-03-27 18:28:23 +0000 | [diff] [blame] | 65 | // with status 70 to generate crash diagnostics. For BSD systems this is |
| 66 | // defined as an internal software error. Otherwise, exit with status 1. |
| 67 | exit(GenCrashDiag ? 70 : 1); |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Sebastian Pop | 17fac04 | 2014-03-14 04:04:27 +0000 | [diff] [blame] | 70 | #ifdef LINK_POLLY_INTO_TOOLS |
| 71 | namespace polly { |
| 72 | void initializePollyPasses(llvm::PassRegistry &Registry); |
| 73 | } |
| 74 | #endif |
| 75 | |
Chris Bieneman | a6b39ab | 2016-08-23 20:07:07 +0000 | [diff] [blame] | 76 | #ifdef CLANG_HAVE_RLIMITS |
Richard Smith | 194b6a3 | 2016-08-17 01:05:07 +0000 | [diff] [blame] | 77 | #if defined(__linux__) && defined(__PIE__) |
Richard Smith | f80a27e | 2016-10-14 19:51:36 +0000 | [diff] [blame] | 78 | static size_t getCurrentStackAllocation() { |
| 79 | // If we can't compute the current stack usage, allow for 512K of command |
| 80 | // line arguments and environment. |
| 81 | size_t Usage = 512 * 1024; |
| 82 | if (FILE *StatFile = fopen("/proc/self/stat", "r")) { |
| 83 | // We assume that the stack extends from its current address to the end of |
| 84 | // the environment space. In reality, there is another string literal (the |
| 85 | // program name) after the environment, but this is close enough (we only |
| 86 | // need to be within 100K or so). |
| 87 | unsigned long StackPtr, EnvEnd; |
Richard Smith | 49db68d | 2016-10-15 01:59:52 +0000 | [diff] [blame] | 88 | // Disable silly GCC -Wformat warning that complains about length |
| 89 | // modifiers on ignored format specifiers. We want to retain these |
| 90 | // for documentation purposes even though they have no effect. |
| 91 | #if defined(__GNUC__) && !defined(__clang__) |
| 92 | #pragma GCC diagnostic push |
| 93 | #pragma GCC diagnostic ignored "-Wformat" |
| 94 | #endif |
Richard Smith | f80a27e | 2016-10-14 19:51:36 +0000 | [diff] [blame] | 95 | if (fscanf(StatFile, |
| 96 | "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*lu %*lu %*lu %*lu %*lu " |
| 97 | "%*lu %*ld %*ld %*ld %*ld %*ld %*ld %*llu %*lu %*ld %*lu %*lu " |
| 98 | "%*lu %*lu %lu %*lu %*lu %*lu %*lu %*lu %*llu %*lu %*lu %*d %*d " |
| 99 | "%*u %*u %*llu %*lu %*ld %*lu %*lu %*lu %*lu %*lu %*lu %lu %*d", |
| 100 | &StackPtr, &EnvEnd) == 2) { |
Richard Smith | 49db68d | 2016-10-15 01:59:52 +0000 | [diff] [blame] | 101 | #if defined(__GNUC__) && !defined(__clang__) |
| 102 | #pragma GCC diagnostic pop |
| 103 | #endif |
Richard Smith | f80a27e | 2016-10-14 19:51:36 +0000 | [diff] [blame] | 104 | Usage = StackPtr < EnvEnd ? EnvEnd - StackPtr : StackPtr - EnvEnd; |
| 105 | } |
| 106 | fclose(StatFile); |
| 107 | } |
| 108 | return Usage; |
| 109 | } |
| 110 | |
| 111 | #include <alloca.h> |
| 112 | |
Richard Smith | 194b6a3 | 2016-08-17 01:05:07 +0000 | [diff] [blame] | 113 | LLVM_ATTRIBUTE_NOINLINE |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 114 | static void ensureStackAddressSpace() { |
Richard Smith | 194b6a3 | 2016-08-17 01:05:07 +0000 | [diff] [blame] | 115 | // Linux kernels prior to 4.1 will sometimes locate the heap of a PIE binary |
| 116 | // relatively close to the stack (they are only guaranteed to be 128MiB |
| 117 | // apart). This results in crashes if we happen to heap-allocate more than |
| 118 | // 128MiB before we reach our stack high-water mark. |
| 119 | // |
| 120 | // To avoid these crashes, ensure that we have sufficient virtual memory |
Richard Smith | f80a27e | 2016-10-14 19:51:36 +0000 | [diff] [blame] | 121 | // pages allocated before we start running. |
| 122 | size_t Curr = getCurrentStackAllocation(); |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 123 | const int kTargetStack = DesiredStackSize - 256 * 1024; |
Richard Smith | f80a27e | 2016-10-14 19:51:36 +0000 | [diff] [blame] | 124 | if (Curr < kTargetStack) { |
| 125 | volatile char *volatile Alloc = |
| 126 | static_cast<volatile char *>(alloca(kTargetStack - Curr)); |
| 127 | Alloc[0] = 0; |
| 128 | Alloc[kTargetStack - Curr - 1] = 0; |
| 129 | } |
Richard Smith | 194b6a3 | 2016-08-17 01:05:07 +0000 | [diff] [blame] | 130 | } |
| 131 | #else |
| 132 | static void ensureStackAddressSpace() {} |
| 133 | #endif |
| 134 | |
| 135 | /// Attempt to ensure that we have at least 8MiB of usable stack space. |
| 136 | static void ensureSufficientStack() { |
| 137 | struct rlimit rlim; |
| 138 | if (getrlimit(RLIMIT_STACK, &rlim) != 0) |
| 139 | return; |
| 140 | |
| 141 | // Increase the soft stack limit to our desired level, if necessary and |
| 142 | // possible. |
Fangrui Song | 4eab1ba | 2018-07-25 06:57:31 +0000 | [diff] [blame] | 143 | if (rlim.rlim_cur != RLIM_INFINITY && |
| 144 | rlim.rlim_cur < rlim_t(DesiredStackSize)) { |
Richard Smith | 194b6a3 | 2016-08-17 01:05:07 +0000 | [diff] [blame] | 145 | // Try to allocate sufficient stack. |
Fangrui Song | 4eab1ba | 2018-07-25 06:57:31 +0000 | [diff] [blame] | 146 | if (rlim.rlim_max == RLIM_INFINITY || |
| 147 | rlim.rlim_max >= rlim_t(DesiredStackSize)) |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 148 | rlim.rlim_cur = DesiredStackSize; |
Richard Smith | 194b6a3 | 2016-08-17 01:05:07 +0000 | [diff] [blame] | 149 | else if (rlim.rlim_cur == rlim.rlim_max) |
| 150 | return; |
| 151 | else |
| 152 | rlim.rlim_cur = rlim.rlim_max; |
| 153 | |
| 154 | if (setrlimit(RLIMIT_STACK, &rlim) != 0 || |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 155 | rlim.rlim_cur != DesiredStackSize) |
Richard Smith | 194b6a3 | 2016-08-17 01:05:07 +0000 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 159 | // We should now have a stack of size at least DesiredStackSize. Ensure |
Richard Smith | 194b6a3 | 2016-08-17 01:05:07 +0000 | [diff] [blame] | 160 | // that we can actually use that much, if necessary. |
| 161 | ensureStackAddressSpace(); |
| 162 | } |
| 163 | #else |
Richard Smith | 525ce25 | 2016-08-17 02:22:39 +0000 | [diff] [blame] | 164 | static void ensureSufficientStack() {} |
Richard Smith | 194b6a3 | 2016-08-17 01:05:07 +0000 | [diff] [blame] | 165 | #endif |
| 166 | |
Sean Silva | 070cd2d | 2014-08-15 21:38:36 +0000 | [diff] [blame] | 167 | int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { |
Richard Smith | 194b6a3 | 2016-08-17 01:05:07 +0000 | [diff] [blame] | 168 | ensureSufficientStack(); |
| 169 | |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 170 | std::unique_ptr<CompilerInstance> Clang(new CompilerInstance()); |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 171 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
Daniel Dunbar | e922d9b | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 172 | |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 173 | // Register the support for object-file-wrapped Clang modules. |
| 174 | auto PCHOps = Clang->getPCHContainerOperations(); |
| 175 | PCHOps->registerWriter(llvm::make_unique<ObjectFilePCHContainerWriter>()); |
| 176 | PCHOps->registerReader(llvm::make_unique<ObjectFilePCHContainerReader>()); |
| 177 | |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 178 | // Initialize targets first, so that --version shows registered targets. |
| 179 | llvm::InitializeAllTargets(); |
Evan Cheng | c391a58 | 2011-07-22 21:59:11 +0000 | [diff] [blame] | 180 | llvm::InitializeAllTargetMCs(); |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 181 | llvm::InitializeAllAsmPrinters(); |
Chris Lattner | 61955ab | 2010-04-05 23:33:20 +0000 | [diff] [blame] | 182 | llvm::InitializeAllAsmParsers(); |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 183 | |
Sebastian Pop | 17fac04 | 2014-03-14 04:04:27 +0000 | [diff] [blame] | 184 | #ifdef LINK_POLLY_INTO_TOOLS |
| 185 | llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry(); |
| 186 | polly::initializePollyPasses(Registry); |
| 187 | #endif |
| 188 | |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 189 | // Buffer diagnostics from argument parsing so that we can output them using a |
| 190 | // well formed diagnostic object. |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 191 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 192 | TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer; |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 193 | DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer); |
Sean Silva | 070cd2d | 2014-08-15 21:38:36 +0000 | [diff] [blame] | 194 | bool Success = CompilerInvocation::CreateFromArgs( |
| 195 | Clang->getInvocation(), Argv.begin(), Argv.end(), Diags); |
Daniel Dunbar | d613677 | 2009-12-13 03:45:58 +0000 | [diff] [blame] | 196 | |
| 197 | // Infer the builtin include path if unspecified. |
Daniel Dunbar | 30b24e9 | 2010-03-23 05:09:16 +0000 | [diff] [blame] | 198 | if (Clang->getHeaderSearchOpts().UseBuiltinIncludes && |
| 199 | Clang->getHeaderSearchOpts().ResourceDir.empty()) |
| 200 | Clang->getHeaderSearchOpts().ResourceDir = |
Daniel Dunbar | a5a166d | 2009-12-15 00:06:45 +0000 | [diff] [blame] | 201 | CompilerInvocation::GetResourcesPath(Argv0, MainAddr); |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 202 | |
Daniel Dunbar | 1cdf04c | 2010-08-12 02:53:07 +0000 | [diff] [blame] | 203 | // Create the actual diagnostics engine. |
Sean Silva | f1b49e2 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 204 | Clang->createDiagnostics(); |
Daniel Dunbar | 1cdf04c | 2010-08-12 02:53:07 +0000 | [diff] [blame] | 205 | if (!Clang->hasDiagnostics()) |
| 206 | return 1; |
| 207 | |
| 208 | // Set an error handler, so that any LLVM backend diagnostics go through our |
| 209 | // error handler. |
| 210 | llvm::install_fatal_error_handler(LLVMErrorHandler, |
| 211 | static_cast<void*>(&Clang->getDiagnostics())); |
| 212 | |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 213 | DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics()); |
Argyrios Kyrtzidis | be6d89d | 2012-01-25 20:00:43 +0000 | [diff] [blame] | 214 | if (!Success) |
| 215 | return 1; |
Daniel Dunbar | 1cdf04c | 2010-08-12 02:53:07 +0000 | [diff] [blame] | 216 | |
Daniel Dunbar | e9e91b0 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 217 | // Execute the frontend actions. |
Dylan Noblesmith | e99b27f | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 218 | Success = ExecuteCompilerInvocation(Clang.get()); |
Daniel Dunbar | 30b24e9 | 2010-03-23 05:09:16 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 09f8cc8 | 2010-03-30 05:39:52 +0000 | [diff] [blame] | 220 | // If any timers were active but haven't been destroyed yet, print their |
| 221 | // results now. This happens in -disable-free mode. |
| 222 | llvm::TimerGroup::printAll(llvm::errs()); |
Daniel Dunbar | 2be9674 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 223 | |
Dan Gohman | b37af7d | 2010-08-18 21:23:17 +0000 | [diff] [blame] | 224 | // Our error handler depends on the Diagnostics object, which we're |
| 225 | // potentially about to delete. Uninstall the handler now so that any |
| 226 | // later errors use the default handling behavior instead. |
| 227 | llvm::remove_fatal_error_handler(); |
| 228 | |
Daniel Dunbar | 30b24e9 | 2010-03-23 05:09:16 +0000 | [diff] [blame] | 229 | // When running with -disable-free, don't do any destruction or shutdown. |
| 230 | if (Clang->getFrontendOpts().DisableFree) { |
David Blaikie | 3c13a7f | 2014-08-29 17:02:26 +0000 | [diff] [blame] | 231 | BuryPointer(std::move(Clang)); |
Daniel Dunbar | 30b24e9 | 2010-03-23 05:09:16 +0000 | [diff] [blame] | 232 | return !Success; |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 235 | return !Success; |
Daniel Dunbar | f72bdf7 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 236 | } |