import_cpp.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "toolchain/check/import_cpp.h"
  5. #include <memory>
  6. #include <string>
  7. #include "clang/Frontend/TextDiagnosticPrinter.h"
  8. #include "clang/Tooling/Tooling.h"
  9. #include "common/raw_string_ostream.h"
  10. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. #include "toolchain/check/context.h"
  14. #include "toolchain/check/diagnostic_helpers.h"
  15. #include "toolchain/diagnostics/diagnostic.h"
  16. #include "toolchain/diagnostics/format_providers.h"
  17. namespace Carbon::Check {
  18. // Generates C++ file contents to #include all requested imports.
  19. static auto GenerateCppIncludesHeaderCode(
  20. llvm::ArrayRef<std::pair<llvm::StringRef, SemIRLoc>> imports)
  21. -> std::string {
  22. std::string code;
  23. llvm::raw_string_ostream code_stream(code);
  24. for (const auto& [path, _] : imports) {
  25. code_stream << "#include \"" << FormatEscaped(path) << "\"\n";
  26. }
  27. return code;
  28. }
  29. auto ImportCppFiles(
  30. Context& context, llvm::StringRef importing_file_path,
  31. llvm::ArrayRef<std::pair<llvm::StringRef, SemIRLoc>> imports,
  32. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs) -> void {
  33. size_t num_imports = imports.size();
  34. if (num_imports == 0) {
  35. return;
  36. }
  37. // TODO: Use all import locations by referring each Clang diagnostic to the
  38. // relevant import.
  39. SemIRLoc loc = imports.back().second;
  40. std::string diagnostics_str;
  41. llvm::raw_string_ostream diagnostics_stream(diagnostics_str);
  42. llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagnostic_options(
  43. new clang::DiagnosticOptions());
  44. clang::TextDiagnosticPrinter diagnostics_consumer(diagnostics_stream,
  45. diagnostic_options.get());
  46. // TODO: Share compilation flags with ClangRunner.
  47. auto ast = clang::tooling::buildASTFromCodeWithArgs(
  48. GenerateCppIncludesHeaderCode(imports), {},
  49. (importing_file_path + ".generated.cpp_imports.h").str(), "clang-tool",
  50. std::make_shared<clang::PCHContainerOperations>(),
  51. clang::tooling::getClangStripDependencyFileAdjuster(),
  52. clang::tooling::FileContentMappings(), &diagnostics_consumer, fs);
  53. // TODO: Implement and use a DynamicRecursiveASTVisitor to traverse the AST.
  54. int num_errors = diagnostics_consumer.getNumErrors();
  55. int num_warnings = diagnostics_consumer.getNumWarnings();
  56. if (num_errors > 0) {
  57. // TODO: Remove the warnings part when there are no warnings.
  58. CARBON_DIAGNOSTIC(
  59. CppInteropParseError, Error,
  60. "{0} error{0:s} and {1} warning{1:s} in {2} `Cpp` import{2:s}:\n{3}",
  61. IntAsSelect, IntAsSelect, IntAsSelect, std::string);
  62. context.emitter().Emit(loc, CppInteropParseError, num_errors, num_warnings,
  63. num_imports, diagnostics_str);
  64. } else if (num_warnings > 0) {
  65. CARBON_DIAGNOSTIC(CppInteropParseWarning, Warning,
  66. "{0} warning{0:s} in `Cpp` {1} import{1:s}:\n{2}",
  67. IntAsSelect, IntAsSelect, std::string);
  68. context.emitter().Emit(loc, CppInteropParseWarning, num_warnings,
  69. num_imports, diagnostics_str);
  70. }
  71. }
  72. } // namespace Carbon::Check