mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:10:12 +01:00
As discussed [on discord](https://discord.com/channels/655572317891461132/655578254970716160/1178878128714678282) and today's toolchain discussion.
40 lines
1.3 KiB
C++
40 lines
1.3 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_LOCATION_TRANSLATOR_H_
|
|
#define CARBON_TOOLCHAIN_PARSE_TREE_NODE_LOCATION_TRANSLATOR_H_
|
|
|
|
#include "toolchain/parse/tree.h"
|
|
|
|
namespace Carbon::Parse {
|
|
|
|
class NodeLocationTranslator : public DiagnosticLocationTranslator<NodeId> {
|
|
public:
|
|
explicit NodeLocationTranslator(const Lex::TokenizedBuffer* tokens,
|
|
llvm::StringRef filename,
|
|
const Tree* parse_tree)
|
|
: token_translator_(tokens),
|
|
filename_(filename),
|
|
parse_tree_(parse_tree) {}
|
|
|
|
// Map the given token into a diagnostic location.
|
|
auto GetLocation(NodeId node) -> DiagnosticLocation override {
|
|
// Support the invalid token as a way to emit only the filename, when there
|
|
// is no line association.
|
|
if (!node.is_valid()) {
|
|
return {.file_name = filename_};
|
|
}
|
|
return token_translator_.GetLocation(parse_tree_->node_token(node));
|
|
}
|
|
|
|
private:
|
|
Lex::TokenLocationTranslator token_translator_;
|
|
llvm::StringRef filename_;
|
|
const Tree* parse_tree_;
|
|
};
|
|
|
|
} // namespace Carbon::Parse
|
|
|
|
#endif // CARBON_TOOLCHAIN_PARSE_TREE_NODE_LOCATION_TRANSLATOR_H_
|