sorting_consumer.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_DIAGNOSTICS_SORTING_CONSUMER_H_
  5. #define CARBON_TOOLCHAIN_DIAGNOSTICS_SORTING_CONSUMER_H_
  6. #include "common/check.h"
  7. #include "llvm/ADT/STLExtras.h"
  8. #include "toolchain/diagnostics/emitter.h"
  9. namespace Carbon::Diagnostics {
  10. // Buffers incoming diagnostics for printing and sorting.
  11. //
  12. // Sorting is based on `last_byte_offset` without taking the filename into
  13. // account. When processing multiple files, it's expected that separate
  14. // consumers will be used in order to keep diagnostics distinct. Typically
  15. // `Diagnostic::messages[0]` will always be a location in the consumer's primary
  16. // file, but if it needs to correspond to a different file, the
  17. // `last_byte_offset` must still indicate an offset within the primary file.
  18. class SortingConsumer : public Consumer {
  19. public:
  20. explicit SortingConsumer(Consumer& next_consumer)
  21. : next_consumer_(&next_consumer) {}
  22. ~SortingConsumer() override {
  23. // We choose not to automatically flush diagnostics here, because they are
  24. // likely to refer to data that gets destroyed before the diagnostics
  25. // consumer is destroyed, because the diagnostics consumer is typically
  26. // created before the objects that diagnostics refer into are created.
  27. CARBON_CHECK(diagnostics_.empty(),
  28. "Must flush diagnostics consumer before destroying it");
  29. }
  30. // Buffers the diagnostic.
  31. auto HandleDiagnostic(Diagnostic diagnostic) -> void override {
  32. diagnostics_.push_back(std::move(diagnostic));
  33. }
  34. // Sorts and flushes buffered diagnostics.
  35. auto Flush() -> void override {
  36. llvm::stable_sort(
  37. diagnostics_, [](const Diagnostic& lhs, const Diagnostic& rhs) {
  38. if (lhs.last_byte_offset != rhs.last_byte_offset) {
  39. return lhs.last_byte_offset < rhs.last_byte_offset;
  40. }
  41. if (lhs.is_on_scope && rhs.is_on_scope) {
  42. // When both are on-scope, we need to compare the locations.
  43. const auto& lhs_loc = lhs.messages[0].loc;
  44. const auto& rhs_loc = rhs.messages[0].loc;
  45. return std::tie(lhs_loc.line_number, lhs_loc.column_number) <
  46. std::tie(rhs_loc.line_number, rhs_loc.column_number);
  47. } else {
  48. // Order non-on-scope before on-scope diagnostics.
  49. return !lhs.is_on_scope && rhs.is_on_scope;
  50. }
  51. });
  52. for (auto& diag : diagnostics_) {
  53. next_consumer_->HandleDiagnostic(std::move(diag));
  54. }
  55. diagnostics_.clear();
  56. }
  57. private:
  58. // A Diagnostic is undesirably large for inline storage by SmallVector, so we
  59. // specify 0.
  60. llvm::SmallVector<Diagnostic, 0> diagnostics_;
  61. Consumer* next_consumer_;
  62. };
  63. } // namespace Carbon::Diagnostics
  64. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_SORTING_CONSUMER_H_