Repository: carbon-language/carbon-lang
File: toolchain/sem_ir/diagnostic_loc_converter.h (C/C++)

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

namespace Carbon::SemIR {

class DiagnosticLocConverter {
 public:
  // Information about an import within which the location was found.
  struct ImportLoc {
    Diagnostics::Loc loc;
    // TODO: Include the name of the imported library in this information so it
    // can be included in the diagnostic.
  };

  // Information about a location that has been converted from a LocId to a
  // diagnostic location.
  struct LocAndImports {
    llvm::SmallVector<ImportLoc> imports;
    Diagnostics::ConvertedLoc loc;
  };

  explicit DiagnosticLocConverter(
      llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> tree_and_subtrees_getters,
      const File* sem_ir)
      : tree_and_subtrees_getters_(tree_and_subtrees_getters),
        sem_ir_(sem_ir) {}

  // Converts the given location into a sequence of import locations and a final
  // diagnostic location.
  auto Convert(LocId loc_id, bool is_token_only) const -> LocAndImports;

  // Converts an `absolute_node_id` in either a Carbon file or C++ import to a
  // diagnostic location.
  auto Convert(AbsoluteNodeId absolute_node_id, bool token_only) const
      -> Diagnostics::ConvertedLoc;

  // Converts a `node_id` corresponding to a specific check IR to a diagnostic
  // location.
  auto Convert(CheckIRId check_ir_id, Parse::NodeId node_id,
               bool token_only) const -> Diagnostics::ConvertedLoc;

  // Converts a `node_id` corresponding to a specific sem_ir to a diagnostic
  // location.
  auto Convert(ClangSourceLocId clang_source_loc_id) const
      -> Diagnostics::ConvertedLoc;

 private:
  // Converters for each SemIR.
  llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> tree_and_subtrees_getters_;

  // The current SemIR being processed.
  const File* sem_ir_;

Review comment (suggestion):
```suggestion
  // sem_ir must be non-null.
  explicit DiagnosticLocConverter(
```

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

namespace Carbon::SemIR {

// Converter from compact location information into a diagnostic location
// describing a filename, line location, and potentially a sequence of imports.
// Such diagnostics locations are used to render user-facing diagnostics and
// also locations for stack trace in crash diagnostics.
class DiagnosticLocConverter {
 public:
  // Information about an import within which the location was found.
  struct ImportLoc {
    Diagnostics::Loc loc;
    // TODO: Include the name of the imported library in this information so it
    // can be included in the diagnostic.
  };

  // Information about a location that has been converted from a LocId to a
  // diagnostic location.
  struct LocAndImports {
    llvm::SmallVector<ImportLoc> imports;
    Diagnostics::ConvertedLoc loc;
  };

  // `sem_ir` must not be null.
  explicit DiagnosticLocConverter(
      llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> tree_and_subtrees_getters,
      const File* sem_ir)
      : tree_and_subtrees_getters_(tree_and_subtrees_getters),
        sem_ir_(sem_ir) {}

  // Converts the given location into a sequence of import locations and a final
  // diagnostic location.
  auto ConvertWithImports(LocId loc_id, bool token_only) const -> LocAndImports;

  // Converts the given location into a diagnostic location.
  auto Convert(LocId loc_id, bool token_only) const
      -> Diagnostics::ConvertedLoc;

  // Converts an `absolute_node_id` in either a Carbon file or C++ import to a
  // diagnostic location.
  auto Convert(AbsoluteNodeId absolute_node_id, bool token_only) const
      -> Diagnostics::ConvertedLoc;

  // Converts a `node_id` corresponding to a specific check IR to a diagnostic
  // location.
  auto Convert(CheckIRId check_ir_id, Parse::NodeId node_id,
               bool token_only) const -> Diagnostics::ConvertedLoc;

  // Converts a location pointing into C++ code to a diagnostic location.
  auto Convert(ClangSourceLocId clang_source_loc_id) const