cpp_file.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/ASTUnit.h"
  8. #include "clang/Frontend/CompilerInvocation.h"
  9. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  10. #include "llvm/Support/FileSystem.h"
  11. namespace Carbon::SemIR {
  12. // The result of compiling the C++ portion of a `File`, including both any
  13. // imported C++ headers and any inline C++ fragments.
  14. class CppFile {
  15. public:
  16. explicit CppFile(std::unique_ptr<clang::ASTUnit> ast_unit)
  17. : ast_unit_(std::move(ast_unit)) {}
  18. // Access to compilation options.
  19. auto diagnostic_options() const -> const clang::DiagnosticOptions& {
  20. return ast_unit_->getDiagnostics().getDiagnosticOptions();
  21. }
  22. auto lang_options() const -> const clang::LangOptions& {
  23. return ast_unit_->getLangOpts();
  24. }
  25. // Access to Clang's compilation environment.
  26. auto source_manager() -> clang::SourceManager& {
  27. return ast_unit_->getSourceManager();
  28. }
  29. auto source_manager() const -> const clang::SourceManager& {
  30. return ast_unit_->getSourceManager();
  31. }
  32. auto diagnostics() const -> clang::DiagnosticsEngine& {
  33. return ast_unit_->getDiagnostics();
  34. }
  35. // Access to layers of Clang's C++ representation.
  36. auto ast_context() -> clang::ASTContext& {
  37. return ast_unit_->getASTContext();
  38. }
  39. auto ast_context() const -> const clang::ASTContext& {
  40. return ast_unit_->getASTContext();
  41. }
  42. auto sema() -> clang::Sema& { return ast_unit_->getSema(); }
  43. // Visit all top-level declarations in the file.
  44. auto VisitLocalTopLevelDecls(
  45. llvm::function_ref<auto(const clang::Decl*)->void> visitor) const -> void;
  46. private:
  47. std::unique_ptr<clang::ASTUnit> ast_unit_;
  48. };
  49. } // namespace Carbon::SemIR
  50. #endif // CARBON_TOOLCHAIN_SEM_IR_CPP_FILE_H_