mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-16 19:08:12 +00:00
This shifts explorer's file_test to use ExplorerMain in order to be able to pass arguments similar to how the command line would. It also means that various output wrapping done by the command line is shared, no longer copied by file_test. In order to help make this work, I plugged vfs::FileSystem into explorer, similar to how we're doing this on the toolchain side (might try to share more code later). I was having trouble getting an overlay working, I think due to how paths are specified -- but due to the issues in fixing this, I'm just loading the prelude into the InMemoryFilesystem for now. Under this approach, I'm unifying more of the file versus string handling. I think we should shift towards an approach where, like toolchain code, anyone trying to use Explorer should use a vfs implementation to supply code. This should reduce the number of distinct code paths which we're maintaining/testing. Short-term, this allows the trace testdata to be merged into file_test with less special casing, using ARGS:. Long-term, it means that the file_test knows the ARGS to pass to generate checks to match against, which is the direction I'm planning for autoupdate.
70 lines
2.3 KiB
C++
70 lines
2.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_EXPLORER_COMMON_SOURCE_LOCATION_H_
|
|
#define CARBON_EXPLORER_COMMON_SOURCE_LOCATION_H_
|
|
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
#include "common/ostream.h"
|
|
#include "explorer/common/nonnull.h"
|
|
|
|
namespace Carbon {
|
|
|
|
// Describes the kind of file that the source location is within.
|
|
enum class FileKind { Main, Prelude, Import, Unknown, Last = Unknown };
|
|
|
|
class SourceLocation {
|
|
public:
|
|
// Produce a source location that is known to not be used, because it is fed
|
|
// into an operation that creates no AST nodes and whose diagnostics are
|
|
// discarded.
|
|
static auto DiagnosticsIgnored() -> SourceLocation {
|
|
return SourceLocation("", 0, FileKind::Unknown);
|
|
}
|
|
|
|
// The filename should be eternal or arena-allocated to eliminate copies.
|
|
explicit constexpr SourceLocation(std::string_view filename, int line_num,
|
|
FileKind file_kind)
|
|
: filename_(filename), line_num_(line_num), file_kind_(file_kind) {}
|
|
explicit SourceLocation(Nonnull<const std::string*> filename, int line_num,
|
|
FileKind file_kind)
|
|
: filename_(*filename), line_num_(line_num), file_kind_(file_kind) {}
|
|
|
|
SourceLocation(const SourceLocation&) = default;
|
|
SourceLocation(SourceLocation&&) = default;
|
|
auto operator=(const SourceLocation&) -> SourceLocation& = default;
|
|
auto operator=(SourceLocation&&) -> SourceLocation& = default;
|
|
|
|
auto operator==(SourceLocation other) const -> bool {
|
|
return filename_ == other.filename_ && line_num_ == other.line_num_ &&
|
|
file_kind_ == other.file_kind_;
|
|
}
|
|
|
|
auto filename() const -> std::string_view { return filename_; }
|
|
|
|
auto file_kind() const -> FileKind { return file_kind_; }
|
|
|
|
void Print(llvm::raw_ostream& out) const {
|
|
out << filename_ << ":" << line_num_;
|
|
}
|
|
auto ToString() const -> std::string {
|
|
std::string result;
|
|
llvm::raw_string_ostream out(result);
|
|
Print(out);
|
|
return result;
|
|
}
|
|
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
|
|
|
|
private:
|
|
std::string_view filename_;
|
|
int line_num_;
|
|
FileKind file_kind_;
|
|
};
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // CARBON_EXPLORER_COMMON_SOURCE_LOCATION_H_
|