| 1 | //===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This is the entry point to the clang -cc1 functionality, which implements the |
| 10 | // core compiler functionality along with a number of additional tools for |
| 11 | // demonstration and testing purposes. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Basic/Stack.h" |
| 16 | #include "clang/Basic/TargetOptions.h" |
| 17 | #include "clang/CodeGen/ObjectFilePCHContainerWriter.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 "clang/Serialization/ObjectFilePCHContainerReader.h" |
| 29 | #include "llvm/ADT/Statistic.h" |
| 30 | #include "llvm/ADT/StringExtras.h" |
| 31 | #include "llvm/Config/llvm-config.h" |
| 32 | #include "llvm/LinkAllPasses.h" |
| 33 | #include "llvm/MC/MCSubtargetInfo.h" |
| 34 | #include "llvm/MC/TargetRegistry.h" |
| 35 | #include "llvm/Option/Arg.h" |
| 36 | #include "llvm/Option/ArgList.h" |
| 37 | #include "llvm/Option/OptTable.h" |
| 38 | #include "llvm/Support/BuryPointer.h" |
| 39 | #include "llvm/Support/Compiler.h" |
| 40 | #include "llvm/Support/ErrorHandling.h" |
| 41 | #include "llvm/Support/ManagedStatic.h" |
| 42 | #include "llvm/Support/Path.h" |
| 43 | #include "llvm/Support/Process.h" |
| 44 | #include "llvm/Support/Signals.h" |
| 45 | #include "llvm/Support/TargetSelect.h" |
| 46 | #include "llvm/Support/TimeProfiler.h" |
| 47 | #include "llvm/Support/Timer.h" |
| 48 | #include "llvm/Support/VirtualFileSystem.h" |
| 49 | #include "llvm/Support/raw_ostream.h" |
| 50 | #include "llvm/Target/TargetMachine.h" |
| 51 | #include "llvm/TargetParser/AArch64TargetParser.h" |
| 52 | #include "llvm/TargetParser/ARMTargetParser.h" |
| 53 | #include "llvm/TargetParser/RISCVISAInfo.h" |
| 54 | #include <cstdio> |
| 55 | |
| 56 | #ifdef CLANG_HAVE_RLIMITS |
| 57 | #include <sys/resource.h> |
| 58 | #endif |
| 59 | |
| 60 | using namespace clang; |
| 61 | using namespace llvm::opt; |
| 62 | |
| 63 | //===----------------------------------------------------------------------===// |
| 64 | // Main driver |
| 65 | //===----------------------------------------------------------------------===// |
| 66 | |
| 67 | static void LLVMErrorHandler(void *UserData, const char *Message, |
| 68 | bool GenCrashDiag) { |
| 69 | DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData); |
| 70 | |
| 71 | Diags.Report(DiagID: diag::err_fe_error_backend) << Message; |
| 72 | |
| 73 | // Run the interrupt handlers to make sure any special cleanups get done, in |
| 74 | // particular that we remove files registered with RemoveFileOnSignal. |
| 75 | llvm::sys::RunInterruptHandlers(); |
| 76 | |
| 77 | // We cannot recover from llvm errors. When reporting a fatal error, exit |
| 78 | // with status 70 to generate crash diagnostics. For BSD systems this is |
| 79 | // defined as an internal software error. Otherwise, exit with status 1. |
| 80 | llvm::sys::Process::Exit(RetCode: GenCrashDiag ? 70 : 1); |
| 81 | } |
| 82 | |
| 83 | #ifdef CLANG_HAVE_RLIMITS |
| 84 | /// Attempt to ensure that we have at least 8MiB of usable stack space. |
| 85 | static void ensureSufficientStack() { |
| 86 | struct rlimit rlim; |
| 87 | if (getrlimit(RLIMIT_STACK, rlimits: &rlim) != 0) |
| 88 | return; |
| 89 | |
| 90 | // Increase the soft stack limit to our desired level, if necessary and |
| 91 | // possible. |
| 92 | if (rlim.rlim_cur != RLIM_INFINITY && |
| 93 | rlim.rlim_cur < rlim_t(DesiredStackSize)) { |
| 94 | // Try to allocate sufficient stack. |
| 95 | if (rlim.rlim_max == RLIM_INFINITY || |
| 96 | rlim.rlim_max >= rlim_t(DesiredStackSize)) |
| 97 | rlim.rlim_cur = DesiredStackSize; |
| 98 | else if (rlim.rlim_cur == rlim.rlim_max) |
| 99 | return; |
| 100 | else |
| 101 | rlim.rlim_cur = rlim.rlim_max; |
| 102 | |
| 103 | if (setrlimit(RLIMIT_STACK, rlimits: &rlim) != 0 || |
| 104 | rlim.rlim_cur != DesiredStackSize) |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | #else |
| 109 | static void ensureSufficientStack() {} |
| 110 | #endif |
| 111 | |
| 112 | /// Print supported cpus of the given target. |
| 113 | static int PrintSupportedCPUs(std::string TargetStr) { |
| 114 | llvm::Triple Triple(TargetStr); |
| 115 | std::string Error; |
| 116 | const llvm::Target *TheTarget = |
| 117 | llvm::TargetRegistry::lookupTarget(TheTriple: Triple, Error); |
| 118 | if (!TheTarget) { |
| 119 | llvm::errs() << Error; |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | // the target machine will handle the mcpu printing |
| 124 | llvm::TargetOptions Options; |
| 125 | std::unique_ptr<llvm::TargetMachine> TheTargetMachine( |
| 126 | TheTarget->createTargetMachine(TT: Triple, CPU: "" , Features: "+cpuhelp" , Options, |
| 127 | RM: std::nullopt)); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int PrintSupportedExtensions(std::string TargetStr) { |
| 132 | llvm::Triple Triple(TargetStr); |
| 133 | std::string Error; |
| 134 | const llvm::Target *TheTarget = |
| 135 | llvm::TargetRegistry::lookupTarget(TheTriple: Triple, Error); |
| 136 | if (!TheTarget) { |
| 137 | llvm::errs() << Error; |
| 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | llvm::TargetOptions Options; |
| 142 | std::unique_ptr<llvm::TargetMachine> TheTargetMachine( |
| 143 | TheTarget->createTargetMachine(TT: Triple, CPU: "" , Features: "" , Options, RM: std::nullopt)); |
| 144 | const llvm::Triple &MachineTriple = TheTargetMachine->getTargetTriple(); |
| 145 | const llvm::MCSubtargetInfo *MCInfo = TheTargetMachine->getMCSubtargetInfo(); |
| 146 | const llvm::ArrayRef<llvm::SubtargetFeatureKV> Features = |
| 147 | MCInfo->getAllProcessorFeatures(); |
| 148 | |
| 149 | llvm::StringMap<llvm::StringRef> DescMap; |
| 150 | for (const llvm::SubtargetFeatureKV &feature : Features) |
| 151 | DescMap.insert(KV: {feature.Key, feature.Desc}); |
| 152 | |
| 153 | if (MachineTriple.isRISCV()) |
| 154 | llvm::RISCVISAInfo::printSupportedExtensions(DescMap); |
| 155 | else if (MachineTriple.isAArch64()) |
| 156 | llvm::AArch64::PrintSupportedExtensions(); |
| 157 | else if (MachineTriple.isARM()) |
| 158 | llvm::ARM::PrintSupportedExtensions(DescMap); |
| 159 | else { |
| 160 | // The option was already checked in Driver::HandleImmediateArgs, |
| 161 | // so we do not expect to get here if we are not a supported architecture. |
| 162 | assert(0 && "Unhandled triple for --print-supported-extensions option." ); |
| 163 | return 1; |
| 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static int PrintEnabledExtensions(const TargetOptions& TargetOpts) { |
| 170 | llvm::Triple Triple(TargetOpts.Triple); |
| 171 | std::string Error; |
| 172 | const llvm::Target *TheTarget = |
| 173 | llvm::TargetRegistry::lookupTarget(TheTriple: Triple, Error); |
| 174 | if (!TheTarget) { |
| 175 | llvm::errs() << Error; |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | // Create a target machine using the input features, the triple information |
| 180 | // and a dummy instance of llvm::TargetOptions. Note that this is _not_ the |
| 181 | // same as the `clang::TargetOptions` instance we have access to here. |
| 182 | llvm::TargetOptions BackendOptions; |
| 183 | std::string FeaturesStr = llvm::join(R: TargetOpts.FeaturesAsWritten, Separator: "," ); |
| 184 | std::unique_ptr<llvm::TargetMachine> TheTargetMachine( |
| 185 | TheTarget->createTargetMachine(TT: Triple, CPU: TargetOpts.CPU, Features: FeaturesStr, |
| 186 | Options: BackendOptions, RM: std::nullopt)); |
| 187 | const llvm::Triple &MachineTriple = TheTargetMachine->getTargetTriple(); |
| 188 | const llvm::MCSubtargetInfo *MCInfo = TheTargetMachine->getMCSubtargetInfo(); |
| 189 | |
| 190 | // Extract the feature names that are enabled for the given target. |
| 191 | // We do that by capturing the key from the set of SubtargetFeatureKV entries |
| 192 | // provided by MCSubtargetInfo, which match the '-target-feature' values. |
| 193 | const std::vector<llvm::SubtargetFeatureKV> Features = |
| 194 | MCInfo->getEnabledProcessorFeatures(); |
| 195 | std::set<llvm::StringRef> EnabledFeatureNames; |
| 196 | for (const llvm::SubtargetFeatureKV &feature : Features) |
| 197 | EnabledFeatureNames.insert(x: feature.Key); |
| 198 | |
| 199 | if (MachineTriple.isAArch64()) |
| 200 | llvm::AArch64::printEnabledExtensions(EnabledFeatureNames); |
| 201 | else if (MachineTriple.isRISCV()) { |
| 202 | llvm::StringMap<llvm::StringRef> DescMap; |
| 203 | for (const llvm::SubtargetFeatureKV &feature : Features) |
| 204 | DescMap.insert(KV: {feature.Key, feature.Desc}); |
| 205 | llvm::RISCVISAInfo::printEnabledExtensions(IsRV64: MachineTriple.isArch64Bit(), |
| 206 | EnabledFeatureNames, DescMap); |
| 207 | } else { |
| 208 | // The option was already checked in Driver::HandleImmediateArgs, |
| 209 | // so we do not expect to get here if we are not a supported architecture. |
| 210 | assert(0 && "Unhandled triple for --print-enabled-extensions option." ); |
| 211 | return 1; |
| 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { |
| 218 | ensureSufficientStack(); |
| 219 | |
| 220 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 221 | |
| 222 | // Register the support for object-file-wrapped Clang modules. |
| 223 | auto PCHOps = std::make_shared<PCHContainerOperations>(); |
| 224 | PCHOps->registerWriter(Writer: std::make_unique<ObjectFilePCHContainerWriter>()); |
| 225 | PCHOps->registerReader(Reader: std::make_unique<ObjectFilePCHContainerReader>()); |
| 226 | |
| 227 | // Initialize targets first, so that --version shows registered targets. |
| 228 | llvm::InitializeAllTargets(); |
| 229 | llvm::InitializeAllTargetMCs(); |
| 230 | llvm::InitializeAllAsmPrinters(); |
| 231 | llvm::InitializeAllAsmParsers(); |
| 232 | |
| 233 | // Buffer diagnostics from argument parsing so that we can output them using a |
| 234 | // well formed diagnostic object. |
| 235 | DiagnosticOptions DiagOpts; |
| 236 | TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer; |
| 237 | DiagnosticsEngine Diags(DiagID, DiagOpts, DiagsBuffer); |
| 238 | |
| 239 | // Setup round-trip remarks for the DiagnosticsEngine used in CreateFromArgs. |
| 240 | if (find(Range&: Argv, Val: StringRef("-Rround-trip-cc1-args" )) != Argv.end()) |
| 241 | Diags.setSeverity(Diag: diag::remark_cc1_round_trip_generated, |
| 242 | Map: diag::Severity::Remark, Loc: {}); |
| 243 | |
| 244 | auto Invocation = std::make_shared<CompilerInvocation>(); |
| 245 | bool Success = |
| 246 | CompilerInvocation::CreateFromArgs(Res&: *Invocation, CommandLineArgs: Argv, Diags, Argv0); |
| 247 | |
| 248 | auto Clang = std::make_unique<CompilerInstance>(args: std::move(Invocation), |
| 249 | args: std::move(PCHOps)); |
| 250 | |
| 251 | if (!Clang->getFrontendOpts().TimeTracePath.empty()) { |
| 252 | llvm::timeTraceProfilerInitialize( |
| 253 | TimeTraceGranularity: Clang->getFrontendOpts().TimeTraceGranularity, ProcName: Argv0, |
| 254 | TimeTraceVerbose: Clang->getFrontendOpts().TimeTraceVerbose); |
| 255 | } |
| 256 | // --print-supported-cpus takes priority over the actual compilation. |
| 257 | if (Clang->getFrontendOpts().PrintSupportedCPUs) |
| 258 | return PrintSupportedCPUs(TargetStr: Clang->getTargetOpts().Triple); |
| 259 | |
| 260 | // --print-supported-extensions takes priority over the actual compilation. |
| 261 | if (Clang->getFrontendOpts().PrintSupportedExtensions) |
| 262 | return PrintSupportedExtensions(TargetStr: Clang->getTargetOpts().Triple); |
| 263 | |
| 264 | // --print-enabled-extensions takes priority over the actual compilation. |
| 265 | if (Clang->getFrontendOpts().PrintEnabledExtensions) |
| 266 | return PrintEnabledExtensions(TargetOpts: Clang->getTargetOpts()); |
| 267 | |
| 268 | // Infer the builtin include path if unspecified. |
| 269 | if (Clang->getHeaderSearchOpts().UseBuiltinIncludes && |
| 270 | Clang->getHeaderSearchOpts().ResourceDir.empty()) |
| 271 | Clang->getHeaderSearchOpts().ResourceDir = |
| 272 | CompilerInvocation::GetResourcesPath(Argv0, MainAddr); |
| 273 | |
| 274 | // Create the actual diagnostics engine. |
| 275 | Clang->createDiagnostics(VFS&: *llvm::vfs::getRealFileSystem()); |
| 276 | if (!Clang->hasDiagnostics()) |
| 277 | return 1; |
| 278 | |
| 279 | // Set an error handler, so that any LLVM backend diagnostics go through our |
| 280 | // error handler. |
| 281 | llvm::install_fatal_error_handler(handler: LLVMErrorHandler, |
| 282 | user_data: static_cast<void*>(&Clang->getDiagnostics())); |
| 283 | |
| 284 | DiagsBuffer->FlushDiagnostics(Diags&: Clang->getDiagnostics()); |
| 285 | if (!Success) { |
| 286 | Clang->getDiagnosticClient().finish(); |
| 287 | return 1; |
| 288 | } |
| 289 | |
| 290 | // Execute the frontend actions. |
| 291 | { |
| 292 | llvm::TimeTraceScope TimeScope("ExecuteCompiler" ); |
| 293 | bool TimePasses = Clang->getCodeGenOpts().TimePasses; |
| 294 | if (TimePasses) |
| 295 | Clang->createFrontendTimer(); |
| 296 | llvm::TimeRegion Timer(TimePasses ? &Clang->getFrontendTimer() : nullptr); |
| 297 | Success = ExecuteCompilerInvocation(Clang: Clang.get()); |
| 298 | } |
| 299 | |
| 300 | // If any timers were active but haven't been destroyed yet, print their |
| 301 | // results now. This happens in -disable-free mode. |
| 302 | std::unique_ptr<raw_ostream> IOFile = llvm::CreateInfoOutputFile(); |
| 303 | if (Clang->getCodeGenOpts().TimePassesJson) { |
| 304 | *IOFile << "{\n" ; |
| 305 | llvm::TimerGroup::printAllJSONValues(OS&: *IOFile, delim: "" ); |
| 306 | *IOFile << "\n}\n" ; |
| 307 | } else { |
| 308 | llvm::TimerGroup::printAll(OS&: *IOFile); |
| 309 | } |
| 310 | llvm::TimerGroup::clearAll(); |
| 311 | |
| 312 | if (llvm::timeTraceProfilerEnabled()) { |
| 313 | // It is possible that the compiler instance doesn't own a file manager here |
| 314 | // if we're compiling a module unit. Since the file manager are owned by AST |
| 315 | // when we're compiling a module unit. So the file manager may be invalid |
| 316 | // here. |
| 317 | // |
| 318 | // It should be fine to create file manager here since the file system |
| 319 | // options are stored in the compiler invocation and we can recreate the VFS |
| 320 | // from the compiler invocation. |
| 321 | if (!Clang->hasFileManager()) |
| 322 | Clang->createFileManager(VFS: createVFSFromCompilerInvocation( |
| 323 | CI: Clang->getInvocation(), Diags&: Clang->getDiagnostics())); |
| 324 | |
| 325 | if (auto profilerOutput = Clang->createOutputFile( |
| 326 | OutputPath: Clang->getFrontendOpts().TimeTracePath, /*Binary=*/false, |
| 327 | /*RemoveFileOnSignal=*/false, |
| 328 | /*useTemporary=*/UseTemporary: false)) { |
| 329 | llvm::timeTraceProfilerWrite(OS&: *profilerOutput); |
| 330 | profilerOutput.reset(); |
| 331 | llvm::timeTraceProfilerCleanup(); |
| 332 | Clang->clearOutputFiles(EraseFiles: false); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // Our error handler depends on the Diagnostics object, which we're |
| 337 | // potentially about to delete. Uninstall the handler now so that any |
| 338 | // later errors use the default handling behavior instead. |
| 339 | llvm::remove_fatal_error_handler(); |
| 340 | |
| 341 | // When running with -disable-free, don't do any destruction or shutdown. |
| 342 | if (Clang->getFrontendOpts().DisableFree) { |
| 343 | llvm::BuryPointer(Ptr: std::move(Clang)); |
| 344 | return !Success; |
| 345 | } |
| 346 | |
| 347 | return !Success; |
| 348 | } |
| 349 | |