init_llvm.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "common/init_llvm.h"
  5. #include "llvm/Support/TargetSelect.h"
  6. #include "llvm/Support/raw_ostream.h"
  7. namespace Carbon {
  8. InitLLVM::InitLLVM(int& argc, char**& argv)
  9. : init_llvm_(argc, argv),
  10. // LLVM assumes that argc and argv won't change, and registers them with
  11. // an `llvm::PrettyStackTraceProgram` that will crash if an argv element
  12. // gets nulled out, which for example `testing::InitGoogleTest` does. So
  13. // make a copy of the argv that LLVM produces in order to support
  14. // mutation.
  15. args_(argv, argv + argc) {
  16. // Return our mutable copy of argv for the program to use.
  17. argc = args_.size();
  18. argv = args_.data();
  19. // `argv[argc]` is expected to be a null pointer.
  20. args_.push_back(nullptr);
  21. llvm::setBugReportMsg(
  22. "Please report issues to "
  23. "https://github.com/carbon-language/carbon-lang/issues and include the "
  24. "crash backtrace.\n");
  25. // Initialize LLVM targets if //common:all_llvm_targets was linked in.
  26. if (InitializeTargets) {
  27. InitializeTargets();
  28. }
  29. // Printing to stderr should flush stdout. This is most noticeable when stderr
  30. // is piped to stdout.
  31. llvm::errs().tie(&llvm::outs());
  32. }
  33. InitLLVM::InitializeTargetsFn* InitLLVM::InitializeTargets = nullptr;
  34. } // namespace Carbon