cpp_file.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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_TOOLCHAIN_SEM_IR_CPP_FILE_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_CPP_FILE_H_
  6. #include "clang/Basic/Diagnostic.h"
  7. #include "clang/Frontend/CompilerInstance.h"
  8. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  9. #include "llvm/Support/FileSystem.h"
  10. namespace Carbon::SemIR {
  11. // The result of compiling the C++ portion of a `File`, including both any
  12. // imported C++ headers and any inline C++ fragments.
  13. class CppFile {
  14. public:
  15. explicit CppFile(std::unique_ptr<clang::CompilerInstance> clang)
  16. : clang_(std::move(clang)) {}
  17. // Access to compilation options.
  18. auto diagnostic_options() const -> const clang::DiagnosticOptions& {
  19. return clang_->getDiagnostics().getDiagnosticOptions();
  20. }
  21. auto lang_options() const -> const clang::LangOptions& {
  22. return clang_->getLangOpts();
  23. }
  24. // Access to Clang's compilation environment.
  25. auto source_manager() -> clang::SourceManager& {
  26. return clang_->getSourceManager();
  27. }
  28. auto source_manager() const -> const clang::SourceManager& {
  29. return clang_->getSourceManager();
  30. }
  31. // TODO: This doesn't really belong here, but is currently used by lowering
  32. // because Clang's code generation may produce diagnostics.
  33. auto diagnostics() const -> clang::DiagnosticsEngine& {
  34. return clang_->getDiagnostics();
  35. }
  36. // Access to layers of Clang's C++ representation.
  37. auto ast_context() -> clang::ASTContext& { return clang_->getASTContext(); }
  38. auto ast_context() const -> const clang::ASTContext& {
  39. return clang_->getASTContext();
  40. }
  41. // A list of all the top-level decl groups produced in this compilation.
  42. auto decl_groups() -> llvm::SmallVector<clang::DeclGroupRef>& {
  43. return decl_groups_;
  44. }
  45. auto decl_groups() const -> const llvm::SmallVector<clang::DeclGroupRef>& {
  46. return decl_groups_;
  47. }
  48. private:
  49. std::unique_ptr<clang::CompilerInstance> clang_;
  50. llvm::SmallVector<clang::DeclGroupRef> decl_groups_;
  51. };
  52. } // namespace Carbon::SemIR
  53. #endif // CARBON_TOOLCHAIN_SEM_IR_CPP_FILE_H_