Repository: carbon-language/carbon-lang
File: toolchain/check/import_cpp.cpp (C++)

Code before:
#include "toolchain/sem_ir/name_scope.h"

namespace Carbon::Check {

// Generates C++ file contents to #include all requested imports.
static auto GenerateCppIncludesHeaderCode(
    Context& context, llvm::ArrayRef<Parse::Tree::PackagingNames> imports)
    -> std::string {
  std::string code;
  llvm::raw_string_ostream code_stream(code);
  for (const Parse::Tree::PackagingNames& import : imports) {
    code_stream << "#include \""
                << FormatEscaped(
                       context.string_literal_values().Get(import.library_id))
                << "\"\n";
  }
  return code;
}

namespace {

// Used to convert Clang diagnostics to Carbon diagnostics.
class CarbonClangDiagnosticConsumer : public clang::DiagnosticConsumer {
 public:
  // Creates an instance with the location that triggers calling Clang.
  CarbonClangDiagnosticConsumer(Context& context, SemIRLoc loc)
      : context_(context), loc_(loc) {}

  // Generates a Carbon warning for each Clang warning and a Carbon error for
  // each Clang error or fatal.
  auto HandleDiagnostic(clang::DiagnosticsEngine::Level diag_level,
                        const clang::Diagnostic& info) -> void override {
    DiagnosticConsumer::HandleDiagnostic(diag_level, info);

    llvm::SmallString<256> message;
    info.FormatDiagnostic(message);

    RawStringOstream diagnostics_stream;
    clang::TextDiagnostic text_diagnostic(
        diagnostics_stream,
        // TODO: Consider allowing setting `LangOptions` or use
        // `ASTContext::getLangOptions()`.
        clang::LangOptions(),
        // TODO: Consider allowing setting `DiagnosticOptions` or use
        // `ASTUnit::getDiagnostics().::getLangOptions().getDiagnosticOptions()`.
        new clang::DiagnosticOptions());
    text_diagnostic.emitDiagnostic(
        clang::FullSourceLoc(info.getLocation(), info.getSourceManager()),
        diag_level, message, info.getRanges(), info.getFixItHints());

    std::string diagnostics_str = diagnostics_stream.TakeStr();

Review comment (suggestion):
```suggestion
  // Creates an instance with the location that triggers calling Clang. `context` is required.
  explicit CarbonClangDiagnosticConsumer(Context* context, SemIRLoc loc)
```

Style, per https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md:

- explicit constructors: "Always mark constructors explicit unless there's a specific reason to support implicit or {} initialization."
- Pointer: "If it is captured and must outlive the call expression itself, use a pointer and document that it must not be null"

Code after:
#include "toolchain/sem_ir/name_scope.h"

namespace Carbon::Check {

// Generates C++ file contents to #include all requested imports.
static auto GenerateCppIncludesHeaderCode(
    Context& context, llvm::ArrayRef<Parse::Tree::PackagingNames> imports)
    -> std::string {
  std::string code;
  llvm::raw_string_ostream code_stream(code);
  for (const Parse::Tree::PackagingNames& import : imports) {
    code_stream << "#include \""
                << FormatEscaped(
                       context.string_literal_values().Get(import.library_id))
                << "\"\n";
  }
  return code;
}

namespace {

// Used to convert Clang diagnostics to Carbon diagnostics.
class CarbonClangDiagnosticConsumer : public clang::DiagnosticConsumer {
 public:
  // Creates an instance with the location that triggers calling Clang.
  // `context` must not be null.
  explicit CarbonClangDiagnosticConsumer(Context* context, SemIRLoc loc)
      : context_(context), loc_(loc) {}

  // Generates a Carbon warning for each Clang warning and a Carbon error for
  // each Clang error or fatal.
  auto HandleDiagnostic(clang::DiagnosticsEngine::Level diag_level,
                        const clang::Diagnostic& info) -> void override {
    DiagnosticConsumer::HandleDiagnostic(diag_level, info);

    llvm::SmallString<256> message;
    info.FormatDiagnostic(message);

    RawStringOstream diagnostics_stream;
    clang::TextDiagnostic text_diagnostic(
        diagnostics_stream,
        // TODO: Consider allowing setting `LangOptions` or use
        // `ASTContext::getLangOptions()`.
        clang::LangOptions(),
        // TODO: Consider allowing setting `DiagnosticOptions` or use
        // `ASTUnit::getDiagnostics().::getLangOptions().getDiagnosticOptions()`.
        new clang::DiagnosticOptions());
    text_diagnostic.emitDiagnostic(
        clang::FullSourceLoc(info.getLocation(), info.getSourceManager()),
        diag_level, message, info.getRanges(), info.getFixItHints());