Files
carbon-lang/toolchain/parse/tree_node_diagnostic_converter.h
T
Jon Ross-Perkins 6b5eb1a101 Id::Invalid -> Id::None (#4834)
High level, replacing `Id::Invalid` with `Id::None` and `Id::is_valid`
with `Id::has_value` for clarity, as discussed
[here](https://discord.com/channels/655572317891461132/655578254970716160/1331664574545395794).
The `IntId` refactoring is needed together with `AnyIdBase` because it's
also used with `ValueStore`.

Note, trying to be careful not to rewrite `EnumBase::InvalidIndex`, or
`is_valid` in general (e.g., `IdKind::is_valid`).

I've tried to sequence commits here:

1. Automatic replacements:

- `((?:Id|Index)(?: |::|\(|Base(?:\(|::)))Invalid((?:Index)?\W)` ->
`$1None$2`
  - `<invalid>` -> `<none>`
  - `InvalidNodeId` -> `NoneNodeId`
  - `/\*invalid\*/` -> `/*none*/`
  - `id((?:_|\(\))(?:\.|->))is_valid` -> `id$1has_value`

2. Manual edits:

  - In `int.h` and `int_test.cpp`
    - `IntT` has `is_value`, which I'm renaming to `is_embedded_value`.
    - Manual edits to comments in this file.
  - `AnyIdBase` and `IdBase`
- Declaration of `is_valid` -> `has_value`, `InvalidIndex` ->
`NoneIndex`.
  - In `ids.h` and `ids.cpp`
    - `is_valid` -> `has_value`
- `// An explicitly invalid ID.` -> `// An ID with no value.`; similar
for index
    - Various math on `InvalidIndex` -> `NoneIndex`
    - Various mentions of "valid" in comments
  - In `value_store.h`, for `IdT::Invalid`, plus one comment
- In `impl.h` and `tokenized_buffer.h`, we had different initialization
of `::None` values (versus `ids.h` syntax) that I fixed manually.
  - Spot checks to compile
- Particularly where `is_valid` replacements didn't catch spots due to
different naming.

3. Autoupdate tests

4. verbose.carbon (NOAUTOUPDATE)

5. Comment spot checks

Note there are probably other mentions of "Invalid" that should be swept
up, but I'd like to argue for merging and separating out remaining
cleanup since this is so sweeping (and likely to hit merge conflicts
from churn). We'll probably have lingering mentions of "invalid" for a
bit regardless, just because there are uses of "invalid" in non-Id APIs.
2025-01-22 23:15:00 +00:00

107 lines
3.7 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_TOOLCHAIN_PARSE_TREE_NODE_DIAGNOSTIC_CONVERTER_H_
#define CARBON_TOOLCHAIN_PARSE_TREE_NODE_DIAGNOSTIC_CONVERTER_H_
#include <utility>
#include "toolchain/diagnostics/diagnostic_emitter.h"
#include "toolchain/lex/tokenized_buffer.h"
#include "toolchain/parse/tree.h"
#include "toolchain/parse/tree_and_subtrees.h"
namespace Carbon::Parse {
class NodeLoc {
public:
// NOLINTNEXTLINE(google-explicit-constructor)
NodeLoc(NodeId node_id) : NodeLoc(node_id, false) {}
NodeLoc(NodeId node_id, bool token_only)
: node_id_(node_id), token_only_(token_only) {}
// TODO: Have some other way of representing diagnostic that applies to a file
// as a whole.
// NOLINTNEXTLINE(google-explicit-constructor)
NodeLoc(NoneNodeId node_id) : NodeLoc(node_id, false) {}
auto node_id() const -> NodeId { return node_id_; }
auto token_only() const -> bool { return token_only_; }
private:
NodeId node_id_;
bool token_only_;
};
class NodeLocConverter : public DiagnosticConverter<NodeLoc> {
public:
explicit NodeLocConverter(
const Lex::TokenizedBuffer* tokens, llvm::StringRef filename,
llvm::function_ref<const Parse::TreeAndSubtrees&()> get_tree_and_subtrees)
: token_converter_(tokens),
filename_(filename),
get_tree_and_subtrees_(get_tree_and_subtrees) {}
// Implements `DiagnosticConverter::ConvertLoc`.
auto ConvertLoc(NodeLoc node_loc, ContextFnT context_fn) const
-> ConvertedDiagnosticLoc override {
// Support the invalid token as a way to emit only the filename, when there
// is no line association.
if (!node_loc.node_id().has_value()) {
return {{.filename = filename_}, -1};
}
const auto& tree = get_tree_and_subtrees_();
if (node_loc.token_only()) {
return token_converter_.ConvertLoc(
tree.tree().node_token(node_loc.node_id()), context_fn);
}
// Construct a location that encompasses all tokens that descend from this
// node (including the root).
Lex::TokenIndex start_token = tree.tree().node_token(node_loc.node_id());
Lex::TokenIndex end_token = start_token;
for (NodeId desc : tree.postorder(node_loc.node_id())) {
Lex::TokenIndex desc_token = tree.tree().node_token(desc);
if (!desc_token.has_value()) {
continue;
}
if (desc_token < start_token) {
start_token = desc_token;
} else if (desc_token > end_token) {
end_token = desc_token;
}
}
auto start_loc = token_converter_.ConvertLoc(start_token, context_fn);
if (start_token == end_token) {
return start_loc;
}
auto end_loc = token_converter_.ConvertLoc(end_token, context_fn);
start_loc.last_byte_offset = end_loc.last_byte_offset;
// For multiline locations we simply return the rest of the line for now
// since true multiline locations are not yet supported.
if (start_loc.loc.line_number != end_loc.loc.line_number) {
start_loc.loc.length =
start_loc.loc.line.size() - start_loc.loc.column_number + 1;
} else {
if (start_loc.loc.column_number != end_loc.loc.column_number) {
start_loc.loc.length = end_loc.loc.column_number + end_loc.loc.length -
start_loc.loc.column_number;
}
}
return start_loc;
}
private:
Lex::TokenDiagnosticConverter token_converter_;
llvm::StringRef filename_;
// Returns a lazily constructed TreeAndSubtrees.
llvm::function_ref<const Parse::TreeAndSubtrees&()> get_tree_and_subtrees_;
};
} // namespace Carbon::Parse
#endif // CARBON_TOOLCHAIN_PARSE_TREE_NODE_DIAGNOSTIC_CONVERTER_H_