15 #include "clang/Basic/Stack.h"
16 #include "clang/Basic/TargetOptions.h"
17 #include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
18 #include "clang/Config/config.h"
19 #include "clang/Driver/DriverDiagnostic.h"
20 #include "clang/Driver/Options.h"
21 #include "clang/Frontend/CompilerInstance.h"
22 #include "clang/Frontend/CompilerInvocation.h"
23 #include "clang/Frontend/FrontendDiagnostic.h"
24 #include "clang/Frontend/TextDiagnosticBuffer.h"
25 #include "clang/Frontend/TextDiagnosticPrinter.h"
26 #include "clang/Frontend/Utils.h"
27 #include "clang/FrontendTool/Utils.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/Config/llvm-config.h"
30 #include "llvm/LinkAllPasses.h"
31 #include "llvm/MC/TargetRegistry.h"
32 #include "llvm/Option/Arg.h"
33 #include "llvm/Option/ArgList.h"
34 #include "llvm/Option/OptTable.h"
35 #include "llvm/Support/BuryPointer.h"
36 #include "llvm/Support/Compiler.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/ManagedStatic.h"
39 #include "llvm/Support/Path.h"
40 #include "llvm/Support/Process.h"
41 #include "llvm/Support/Signals.h"
42 #include "llvm/Support/TargetSelect.h"
43 #include "llvm/Support/TimeProfiler.h"
44 #include "llvm/Support/Timer.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include "llvm/Target/TargetMachine.h"
49 #ifdef CLANG_HAVE_RLIMITS
50 #include <sys/resource.h>
53 using namespace clang;
54 using namespace llvm::opt;
60 static void LLVMErrorHandler(
void *UserData,
const char *Message,
bool GenCrashDiag) {
61 DiagnosticsEngine &Diags = *
static_cast<DiagnosticsEngine *
>(UserData);
63 Diags.Report(diag::err_fe_error_backend) << Message;
67 llvm::sys::RunInterruptHandlers();
72 llvm::sys::Process::Exit(GenCrashDiag ? 70 : 1);
75 #ifdef CLANG_HAVE_RLIMITS
76 #if defined(__linux__) && defined(__PIE__)
77 static size_t getCurrentStackAllocation() {
80 size_t Usage = 512 * 1024;
81 if (FILE *StatFile = fopen(
"/proc/self/stat",
"r")) {
86 unsigned long StackPtr, EnvEnd;
90 #if defined(__GNUC__) && !defined(__clang__)
91 #pragma GCC diagnostic push
92 #pragma GCC diagnostic ignored "-Wformat"
95 "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*lu %*lu %*lu %*lu %*lu "
96 "%*lu %*ld %*ld %*ld %*ld %*ld %*ld %*llu %*lu %*ld %*lu %*lu "
97 "%*lu %*lu %lu %*lu %*lu %*lu %*lu %*lu %*llu %*lu %*lu %*d %*d "
98 "%*u %*u %*llu %*lu %*ld %*lu %*lu %*lu %*lu %*lu %*lu %lu %*d",
101 #if defined(__GNUC__) && !defined(__clang__)
102 #pragma GCC diagnostic pop
104 Usage = StackPtr < EnvEnd ? EnvEnd - StackPtr : StackPtr - EnvEnd;
113 LLVM_ATTRIBUTE_NOINLINE
114 static void ensureStackAddressSpace() {
122 size_t Curr = getCurrentStackAllocation();
123 const int kTargetStack = DesiredStackSize - 256 * 1024;
124 if (Curr < kTargetStack) {
125 volatile char *
volatile Alloc =
static_cast<volatile char *
>(alloca(kTargetStack - Curr));
127 Alloc[kTargetStack - Curr - 1] = 0;
131 static void ensureStackAddressSpace() {}
135 static void ensureSufficientStack() {
137 if (getrlimit(RLIMIT_STACK, &rlim) != 0)
142 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < rlim_t(DesiredStackSize)) {
144 if (rlim.rlim_max == RLIM_INFINITY || rlim.rlim_max >= rlim_t(DesiredStackSize))
145 rlim.rlim_cur = DesiredStackSize;
146 else if (rlim.rlim_cur == rlim.rlim_max)
149 rlim.rlim_cur = rlim.rlim_max;
151 if (setrlimit(RLIMIT_STACK, &rlim) != 0 || rlim.rlim_cur != DesiredStackSize)
157 ensureStackAddressSpace();
160 static void ensureSufficientStack() {}
164 static int PrintSupportedCPUs(std::string TargetStr) {
166 const llvm::Target *TheTarget = llvm::TargetRegistry::lookupTarget(TargetStr, Error);
168 llvm::errs() << Error;
173 llvm::TargetOptions Options;
174 std::unique_ptr<llvm::TargetMachine> TheTargetMachine(
175 TheTarget->createTargetMachine(TargetStr,
"",
"+cpuhelp", Options, std::nullopt));
179 int cc1_main(ArrayRef<const char *> Argv,
const char *Argv0,
void *MainAddr) {
180 ensureSufficientStack();
182 std::unique_ptr<CompilerInstance> Clang(
new CompilerInstance());
183 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(
new DiagnosticIDs());
186 auto PCHOps = Clang->getPCHContainerOperations();
187 PCHOps->registerWriter(std::make_unique<ObjectFilePCHContainerWriter>());
188 PCHOps->registerReader(std::make_unique<ObjectFilePCHContainerReader>());
191 llvm::InitializeAllTargets();
192 llvm::InitializeAllTargetMCs();
193 llvm::InitializeAllAsmPrinters();
194 llvm::InitializeAllAsmParsers();
198 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
new DiagnosticOptions();
199 TextDiagnosticBuffer *DiagsBuffer =
new TextDiagnosticBuffer;
200 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
203 if (find(Argv, StringRef(
"-Rround-trip-cc1-args")) != Argv.end())
204 Diags.setSeverity(diag::remark_cc1_round_trip_generated, diag::Severity::Remark, {});
206 bool Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(), Argv, Diags, Argv0);
208 if (Clang->getFrontendOpts().TimeTrace || !Clang->getFrontendOpts().TimeTracePath.empty()) {
209 Clang->getFrontendOpts().TimeTrace = 1;
210 llvm::timeTraceProfilerInitialize(Clang->getFrontendOpts().TimeTraceGranularity, Argv0);
213 if (Clang->getFrontendOpts().PrintSupportedCPUs)
214 return PrintSupportedCPUs(Clang->getTargetOpts().Triple);
217 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes && Clang->getHeaderSearchOpts().ResourceDir.empty())
218 Clang->getHeaderSearchOpts().ResourceDir = CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
221 Clang->createDiagnostics();
222 if (!Clang->hasDiagnostics())
227 llvm::install_fatal_error_handler(LLVMErrorHandler,
static_cast<void *
>(&Clang->getDiagnostics()));
229 DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
231 Clang->getDiagnosticClient().finish();
237 llvm::TimeTraceScope TimeScope(
"ExecuteCompiler");
238 Success = ExecuteCompilerInvocation(Clang.get());
243 llvm::TimerGroup::printAll(llvm::errs());
244 llvm::TimerGroup::clearAll();
246 if (llvm::timeTraceProfilerEnabled()) {
247 SmallString<128> Path(Clang->getFrontendOpts().OutputFile);
248 llvm::sys::path::replace_extension(Path,
"json");
249 if (!Clang->getFrontendOpts().TimeTracePath.empty()) {
251 SmallString<128> TracePath(Clang->getFrontendOpts().TimeTracePath);
252 if (llvm::sys::fs::is_directory(TracePath))
253 llvm::sys::path::append(TracePath, llvm::sys::path::filename(Path));
254 Path.assign(TracePath);
256 if (
auto profilerOutput = Clang->createOutputFile(Path.str(),
260 llvm::timeTraceProfilerWrite(*profilerOutput);
261 profilerOutput.reset();
262 llvm::timeTraceProfilerCleanup();
263 Clang->clearOutputFiles(
false);
270 llvm::remove_fatal_error_handler();
273 if (Clang->getFrontendOpts().DisableFree) {
274 llvm::BuryPointer(std::move(Clang));