init_llvm.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. #ifndef CARBON_COMMON_INIT_LLVM_H_
  5. #define CARBON_COMMON_INIT_LLVM_H_
  6. #include "llvm/ADT/SmallVector.h"
  7. #include "llvm/Support/InitLLVM.h"
  8. namespace Carbon {
  9. // A RAII class to handle initializing LLVM and shutting it down. An instance of
  10. // this class should be created in the `main` function of each Carbon binary
  11. // that interacts with LLVM, before `argc` and `argv` are first inspected.
  12. class InitLLVM {
  13. public:
  14. // Initializes LLVM for use by a Carbon binary. On Windows, `argc` and `argv`
  15. // are updated to refer to properly-encoded UTF-8 versions of the command line
  16. // arguments.
  17. explicit InitLLVM(int& argc, char**& argv);
  18. // Shuts down LLVM.
  19. ~InitLLVM() = default;
  20. private:
  21. using InitializeTargetsFn = auto() -> void;
  22. llvm::InitLLVM init_llvm_;
  23. llvm::SmallVector<char*> args_;
  24. // A pointer to the LLVM target initialization function, if :all_llvm_targets
  25. // is linked in. Otherwise nullptr.
  26. // NOLINTNEXTLINE(readability-identifier-naming): Constant after static init.
  27. static InitializeTargetsFn* InitializeTargets;
  28. // The initializer of this static data member populates `InitializeTargets`.
  29. // Defined only if :all_llvm_targets is linked in. This is a member so that
  30. // it has access to `InitializeTargets`.
  31. static const char RegisterTargets;
  32. };
  33. } // namespace Carbon
  34. #endif // CARBON_COMMON_INIT_LLVM_H_