15#include <clang/Basic/DiagnosticSema.h>
17#include "clang/Basic/FileManager.h"
18#include "clang/Basic/Stack.h"
19#include "clang/Basic/TargetOptions.h"
20#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
21#include "clang/Config/config.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/FrontendTool/Utils.h"
28#include "llvm/Config/llvm-config.h"
29#include "llvm/LinkAllPasses.h"
30#include "llvm/Option/Arg.h"
31#include "llvm/Option/ArgList.h"
32#include "llvm/Option/OptTable.h"
33#include "llvm/Support/BuryPointer.h"
34#include "llvm/Support/Compiler.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/FileSystem.h"
37#include "llvm/Support/ManagedStatic.h"
38#include "llvm/Support/Process.h"
39#include "llvm/Support/Signals.h"
41#include "llvm/Support/TargetSelect.h"
42#include "llvm/Support/TimeProfiler.h"
43#include "llvm/Support/Timer.h"
44#include "llvm/Support/raw_ostream.h"
45#include "llvm/Target/TargetMachine.h"
50#ifdef CLANG_HAVE_RLIMITS
51#include <sys/resource.h>
55using namespace llvm::opt;
62 void finish()
override { std::cerr <<
" == finish == \n"; }
64 HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
const Diagnostic& Info)
override
66 SmallVector<char, 1024> vec;
67 Info.FormatDiagnostic(vec);
68 std::cerr <<
" == diagnostic == " << vec.data() <<
"\n";
75#if LLVM_VERSION_MAJOR < 14
77LLVMErrorHandler(
void* UserData,
const std::string& Message,
bool GenCrashDiag)
79static void LLVMErrorHandler(
void* UserData,
const char* Message,
bool GenCrashDiag)
82 DiagnosticsEngine& Diags = *
static_cast<DiagnosticsEngine*
>(UserData);
84 Diags.Report(diag::err_fe_error_backend) << Message;
88 llvm::sys::RunInterruptHandlers();
93 llvm::sys::Process::Exit(GenCrashDiag ? 70 : 1);
96#ifdef CLANG_HAVE_RLIMITS
97#if defined(__linux__) && defined(__PIE__)
98static size_t getCurrentStackAllocation()
102 size_t Usage = 512 * 1024;
103 if(FILE* StatFile = fopen(
"/proc/self/stat",
"r"))
109 unsigned long StackPtr, EnvEnd;
113#if defined(__GNUC__) && !defined(__clang__)
114#pragma GCC diagnostic push
115#pragma GCC diagnostic ignored "-Wformat"
119 "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*lu %*lu %*lu %*lu %*lu "
120 "%*lu %*ld %*ld %*ld %*ld %*ld %*ld %*llu %*lu %*ld %*lu %*lu "
121 "%*lu %*lu %lu %*lu %*lu %*lu %*lu %*lu %*llu %*lu %*lu %*d %*d "
122 "%*u %*u %*llu %*lu %*ld %*lu %*lu %*lu %*lu %*lu %*lu %lu %*d",
126#if defined(__GNUC__) && !defined(__clang__)
127#pragma GCC diagnostic pop
129 Usage = StackPtr < EnvEnd ? EnvEnd - StackPtr : StackPtr - EnvEnd;
138LLVM_ATTRIBUTE_NOINLINE
139static void ensureStackAddressSpace()
148 size_t Curr = getCurrentStackAllocation();
149 const int kTargetStack = DesiredStackSize - 256 * 1024;
150 if(Curr < kTargetStack)
152 volatile char*
volatile Alloc
153 =
static_cast<volatile char*
>(alloca(kTargetStack - Curr));
155 Alloc[kTargetStack - Curr - 1] = 0;
159static void ensureStackAddressSpace() { }
163static void ensureSufficientStack()
166 if(getrlimit(RLIMIT_STACK, &rlim) != 0)
171 if(rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < rlim_t(DesiredStackSize))
174 if(rlim.rlim_max == RLIM_INFINITY || rlim.rlim_max >= rlim_t(DesiredStackSize))
175 rlim.rlim_cur = DesiredStackSize;
176 else if(rlim.rlim_cur == rlim.rlim_max)
179 rlim.rlim_cur = rlim.rlim_max;
181 if(setrlimit(RLIMIT_STACK, &rlim) != 0 || rlim.rlim_cur != DesiredStackSize)
187 ensureStackAddressSpace();
190static void ensureSufficientStack() { }
193auto printErrors(TextDiagnosticBuffer& buf,
const SourceManager& mgr)
195 std::stringstream ss;
196 for(
auto it = buf.err_begin(); it != buf.err_end(); ++it)
198 auto& loc = it->first;
199 ss << loc.printToString(mgr) <<
":\n" << it->second <<
"\n\n";
202 std::cerr << ss.str();
205llvm::Error cc1_main(ArrayRef<const char*> Argv,
const char* Argv0,
void* MainAddr)
207 ensureSufficientStack();
210 llvm::InitializeAllTargets();
211 llvm::InitializeAllTargetMCs();
212 llvm::InitializeAllAsmPrinters();
213 llvm::InitializeAllAsmParsers();
215 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(
new DiagnosticIDs());
216#ifdef LINK_POLLY_INTO_TOOLS
217 llvm::PassRegistry& Registry = *llvm::PassRegistry::getPassRegistry();
218 polly::initializePollyPasses(Registry);
223 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
new DiagnosticOptions();
224 TextDiagnosticBuffer* DiagsBuffer =
new TextDiagnosticBuffer;
225 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
226 new DiagnosticsEngine(DiagID, &*DiagOpts, DiagsBuffer));
228#if LLVM_VERSION_MAJOR >= 13
229 llvm::IntrusiveRefCntPtr<FileManager> Files(
230 new FileManager(FileSystemOptions(), llvm::vfs::getRealFileSystem()));
231 llvm::IntrusiveRefCntPtr<SourceManager> SrcMgr(
new SourceManager(*Diags, *Files));
235 std::unique_ptr<CompilerInstance> Clang(
new CompilerInstance());
238 auto PCHOps = Clang->getPCHContainerOperations();
239 PCHOps->registerWriter(std::make_unique<ObjectFilePCHContainerWriter>());
240 PCHOps->registerReader(std::make_unique<ObjectFilePCHContainerReader>());
243 = CompilerInvocation::CreateFromArgs(Clang->getInvocation(), Argv, *Diags);
245#if LLVM_VERSION_MAJOR >= 13
246 if(!Clang->hasSourceManager())
248 Diags->setSourceManager(SrcMgr.get());
250 Clang->setFileManager(Files.get());
251 Clang->setSourceManager(SrcMgr.get());
255 if(Clang->getHeaderSearchOpts().UseBuiltinIncludes
256 && Clang->getHeaderSearchOpts().ResourceDir.empty())
257 Clang->getHeaderSearchOpts().ResourceDir
258 = CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
260 Clang->setDiagnostics(Diags.get());
261 if(!Clang->hasDiagnostics())
263 return llvm::make_error<llvm::StringError>(
264 "No diagnostics", std::error_code(1, std::system_category()));
269 llvm::install_fatal_error_handler(
270 LLVMErrorHandler,
static_cast<void*
>(&Clang->getDiagnostics()));
274 Clang->getDiagnostics().setSeverity(clang::diag::ext_constexpr_function_never_constant_expr, diag::Severity::Ignored, {});
277 return llvm::make_error<llvm::StringError>(
278 printErrors(*DiagsBuffer, Clang->getSourceManager()),
279 std::error_code(1, std::system_category()));
284 llvm::TimeTraceScope TimeScope(
"ExecuteCompiler");
285 Success = ExecuteCompilerInvocation(Clang.get());
290 llvm::TimerGroup::printAll(llvm::errs());
291 llvm::TimerGroup::clearAll();
293 auto res = Success ? llvm::Error::success()
294 : llvm::make_error<llvm::StringError>(
295 printErrors(*DiagsBuffer, Clang->getSourceManager()),
296 std::error_code(1,
std::system_category()));
301 llvm::remove_fatal_error_handler();
304 if(Clang->getFrontendOpts().DisableFree)
306 llvm::BuryPointer(std::move(Clang));
Definition cc1_main.cpp:58