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

Code before:
      switch (info.level) {
        case clang::DiagnosticsEngine::Ignored:
        case clang::DiagnosticsEngine::Note:
        case clang::DiagnosticsEngine::Remark: {
          context_->TODO(
              SemIR::LocId(info.import_ir_inst_id),
              llvm::formatv(
                  "Unsupported: C++ diagnostic level for diagnostic\n{0}",
                  info.message));
          break;
        }
        case clang::DiagnosticsEngine::Warning:
        case clang::DiagnosticsEngine::Error:
        case clang::DiagnosticsEngine::Fatal: {
          CARBON_DIAGNOSTIC(CppInteropParseWarning, Warning, "{0}",
                            std::string);
          CARBON_DIAGNOSTIC(CppInteropParseError, Error, "{0}", std::string);
          auto builder = context_->emitter().Build(
              SemIR::LocId(info.import_ir_inst_id),
              info.level == clang::DiagnosticsEngine::Warning
                  ? CppInteropParseWarning
                  : CppInteropParseError,
              info.message);
          for (;
               i + 1 < diagnostic_infos_.size() &&
               diagnostic_infos_[i + 1].level == clang::DiagnosticsEngine::Note;
               ++i) {
            const ClangDiagnosticInfo& note_info = diagnostic_infos_[i + 1];
            CARBON_DIAGNOSTIC(CppInteropParseNote, Note, "{0}", std::string);
            builder.Note(SemIR::LocId(note_info.import_ir_inst_id),
                         CppInteropParseNote, note_info.message);
          }
          builder.Emit();
          break;
        }
      }
    }
    diagnostic_infos_.clear();
  }

 private:
  // Callback to flush Clang diagnostics into the Carbon diagnostics engine.
  struct DiagnosticFlusher {
    CarbonClangDiagnosticConsumer* consumer_;
    void operator()() const { consumer_->EmitDiagnostics(); }
  };

  // The type-checking context in which we're running Clang.
  Context* context_;

  // The compiler invocation that is producing the diagnostics.

Review comment (suggestion):
```suggestion
          for (const auto& note_info : llvm::ArrayRef(diagnostic_infos_).drop_front(i)) {
            if (note_info.level != clang::DiagnosticsEngine::Note) {
              break;
            }
```

Code after:
        case clang::DiagnosticsEngine::Note:
        case clang::DiagnosticsEngine::Remark: {
          context_->TODO(
              SemIR::LocId(info.import_ir_inst_id),
              llvm::formatv(
                  "Unsupported: C++ diagnostic level for diagnostic\n{0}",
                  info.message));
          break;
        }
        case clang::DiagnosticsEngine::Warning:
        case clang::DiagnosticsEngine::Error:
        case clang::DiagnosticsEngine::Fatal: {
          CARBON_DIAGNOSTIC(CppInteropParseWarning, Warning, "{0}",
                            std::string);
          CARBON_DIAGNOSTIC(CppInteropParseError, Error, "{0}", std::string);
          auto builder = context_->emitter().Build(
              SemIR::LocId(info.import_ir_inst_id),
              info.level == clang::DiagnosticsEngine::Warning
                  ? CppInteropParseWarning
                  : CppInteropParseError,
              info.message);
          for (;
               i + 1 < diagnostic_infos_.size() &&
               diagnostic_infos_[i + 1].level == clang::DiagnosticsEngine::Note;
               ++i) {
            const ClangDiagnosticInfo& note_info = diagnostic_infos_[i + 1];
            CARBON_DIAGNOSTIC(CppInteropParseNote, Note, "{0}", std::string);
            builder.Note(SemIR::LocId(note_info.import_ir_inst_id),
                         CppInteropParseNote, note_info.message);
          }
          builder.Emit();
          break;
        }
      }
    }
    diagnostic_infos_.clear();
  }

 private:
  // The type-checking context in which we're running Clang.
  Context* context_;

  // The compiler invocation that is producing the diagnostics.
  std::shared_ptr<clang::CompilerInvocation> invocation_;

  // Information on a Clang diagnostic that can be converted to a Carbon
  // diagnostic.
  struct ClangDiagnosticInfo {
    // The Clang diagnostic level.
    clang::DiagnosticsEngine::Level level;