Coverage Report

Created: 2018-07-22 10:17

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/tools/driver/cc1_main.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
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
//
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.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#include "llvm/Option/Arg.h"
17
#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
18
#include "clang/Config/config.h"
19
#include "clang/Basic/Stack.h"
20
#include "clang/Driver/DriverDiagnostic.h"
21
#include "clang/Driver/Options.h"
22
#include "clang/Frontend/CompilerInstance.h"
23
#include "clang/Frontend/CompilerInvocation.h"
24
#include "clang/Frontend/FrontendDiagnostic.h"
25
#include "clang/Frontend/TextDiagnosticBuffer.h"
26
#include "clang/Frontend/TextDiagnosticPrinter.h"
27
#include "clang/Frontend/Utils.h"
28
#include "clang/FrontendTool/Utils.h"
29
#include "llvm/ADT/Statistic.h"
30
#include "llvm/Config/llvm-config.h"
31
#include "llvm/LinkAllPasses.h"
32
#include "llvm/Option/ArgList.h"
33
#include "llvm/Option/OptTable.h"
34
#include "llvm/Support/Compiler.h"
35
#include "llvm/Support/ErrorHandling.h"
36
#include "llvm/Support/ManagedStatic.h"
37
#include "llvm/Support/Signals.h"
38
#include "llvm/Support/TargetSelect.h"
39
#include "llvm/Support/Timer.h"
40
#include "llvm/Support/raw_ostream.h"
41
#include <cstdio>
42
43
#ifdef CLANG_HAVE_RLIMITS
44
#include <sys/resource.h>
45
#endif
46
47
using namespace clang;
48
using namespace llvm::opt;
49
50
//===----------------------------------------------------------------------===//
51
// Main driver
52
//===----------------------------------------------------------------------===//
53
54
static void LLVMErrorHandler(void *UserData, const std::string &Message,
55
1
                             bool GenCrashDiag) {
56
1
  DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
57
1
58
1
  Diags.Report(diag::err_fe_error_backend) << Message;
59
1
60
1
  // Run the interrupt handlers to make sure any special cleanups get done, in
61
1
  // particular that we remove files registered with RemoveFileOnSignal.
62
1
  llvm::sys::RunInterruptHandlers();
63
1
64
1
  // We cannot recover from llvm errors.  When reporting a fatal error, exit
65
1
  // with status 70 to generate crash diagnostics.  For BSD systems this is
66
1
  // defined as an internal software error.  Otherwise, exit with status 1.
67
1
  exit(GenCrashDiag ? 
700
: 1);
68
1
}
69
70
#ifdef LINK_POLLY_INTO_TOOLS
71
namespace polly {
72
void initializePollyPasses(llvm::PassRegistry &Registry);
73
}
74
#endif
75
76
#ifdef CLANG_HAVE_RLIMITS
77
#if defined(__linux__) && defined(__PIE__)
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;
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
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) {
101
#if defined(__GNUC__) && !defined(__clang__)
102
#pragma GCC diagnostic pop
103
#endif
104
      Usage = StackPtr < EnvEnd ? EnvEnd - StackPtr : StackPtr - EnvEnd;
105
    }
106
    fclose(StatFile);
107
  }
108
  return Usage;
109
}
110
111
#include <alloca.h>
112
113
LLVM_ATTRIBUTE_NOINLINE
114
static void ensureStackAddressSpace() {
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
121
  // pages allocated before we start running.
122
  size_t Curr = getCurrentStackAllocation();
123
  const int kTargetStack = DesiredStackSize - 256 * 1024;
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
  }
130
}
131
#else
132
34.9k
static void ensureStackAddressSpace() {}
133
#endif
134
135
/// Attempt to ensure that we have at least 8MiB of usable stack space.
136
34.9k
static void ensureSufficientStack() {
137
34.9k
  struct rlimit rlim;
138
34.9k
  if (getrlimit(RLIMIT_STACK, &rlim) != 0)
139
0
    return;
140
34.9k
141
34.9k
  // Increase the soft stack limit to our desired level, if necessary and
142
34.9k
  // possible.
143
34.9k
  if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < DesiredStackSize) {
144
0
    // Try to allocate sufficient stack.
145
0
    if (rlim.rlim_max == RLIM_INFINITY || rlim.rlim_max >= DesiredStackSize)
146
0
      rlim.rlim_cur = DesiredStackSize;
147
0
    else if (rlim.rlim_cur == rlim.rlim_max)
148
0
      return;
149
0
    else
150
0
      rlim.rlim_cur = rlim.rlim_max;
151
0
152
0
    if (setrlimit(RLIMIT_STACK, &rlim) != 0 ||
153
0
        rlim.rlim_cur != DesiredStackSize)
154
0
      return;
155
34.9k
  }
156
34.9k
157
34.9k
  // We should now have a stack of size at least DesiredStackSize. Ensure
158
34.9k
  // that we can actually use that much, if necessary.
159
34.9k
  ensureStackAddressSpace();
160
34.9k
}
161
#else
162
static void ensureSufficientStack() {}
163
#endif
164
165
34.9k
int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
166
34.9k
  ensureSufficientStack();
167
34.9k
168
34.9k
  std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
169
34.9k
  IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
170
34.9k
171
34.9k
  // Register the support for object-file-wrapped Clang modules.
172
34.9k
  auto PCHOps = Clang->getPCHContainerOperations();
173
34.9k
  PCHOps->registerWriter(llvm::make_unique<ObjectFilePCHContainerWriter>());
174
34.9k
  PCHOps->registerReader(llvm::make_unique<ObjectFilePCHContainerReader>());
175
34.9k
176
34.9k
  // Initialize targets first, so that --version shows registered targets.
177
34.9k
  llvm::InitializeAllTargets();
178
34.9k
  llvm::InitializeAllTargetMCs();
179
34.9k
  llvm::InitializeAllAsmPrinters();
180
34.9k
  llvm::InitializeAllAsmParsers();
181
34.9k
182
34.9k
#ifdef LINK_POLLY_INTO_TOOLS
183
34.9k
  llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry();
184
34.9k
  polly::initializePollyPasses(Registry);
185
34.9k
#endif
186
34.9k
187
34.9k
  // Buffer diagnostics from argument parsing so that we can output them using a
188
34.9k
  // well formed diagnostic object.
189
34.9k
  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
190
34.9k
  TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
191
34.9k
  DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
192
34.9k
  bool Success = CompilerInvocation::CreateFromArgs(
193
34.9k
      Clang->getInvocation(), Argv.begin(), Argv.end(), Diags);
194
34.9k
195
34.9k
  // Infer the builtin include path if unspecified.
196
34.9k
  if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
197
34.9k
      
Clang->getHeaderSearchOpts().ResourceDir.empty()34.9k
)
198
20.6k
    Clang->getHeaderSearchOpts().ResourceDir =
199
20.6k
      CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
200
34.9k
201
34.9k
  // Create the actual diagnostics engine.
202
34.9k
  Clang->createDiagnostics();
203
34.9k
  if (!Clang->hasDiagnostics())
204
0
    return 1;
205
34.9k
206
34.9k
  // Set an error handler, so that any LLVM backend diagnostics go through our
207
34.9k
  // error handler.
208
34.9k
  llvm::install_fatal_error_handler(LLVMErrorHandler,
209
34.9k
                                  static_cast<void*>(&Clang->getDiagnostics()));
210
34.9k
211
34.9k
  DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
212
34.9k
  if (!Success)
213
12
    return 1;
214
34.9k
215
34.9k
  // Execute the frontend actions.
216
34.9k
  Success = ExecuteCompilerInvocation(Clang.get());
217
34.9k
218
34.9k
  // If any timers were active but haven't been destroyed yet, print their
219
34.9k
  // results now.  This happens in -disable-free mode.
220
34.9k
  llvm::TimerGroup::printAll(llvm::errs());
221
34.9k
222
34.9k
  // Our error handler depends on the Diagnostics object, which we're
223
34.9k
  // potentially about to delete. Uninstall the handler now so that any
224
34.9k
  // later errors use the default handling behavior instead.
225
34.9k
  llvm::remove_fatal_error_handler();
226
34.9k
227
34.9k
  // When running with -disable-free, don't do any destruction or shutdown.
228
34.9k
  if (Clang->getFrontendOpts().DisableFree) {
229
14.2k
    BuryPointer(std::move(Clang));
230
14.2k
    return !Success;
231
14.2k
  }
232
20.7k
233
20.7k
  return !Success;
234
20.7k
}