Add Location (#88)

This commit is contained in:
ykiko
2025-02-21 12:51:05 +08:00
committed by GitHub
parent bcb4c3895a
commit 828d20b8c2
2 changed files with 27 additions and 1 deletions

View File

@@ -22,6 +22,24 @@ struct LocalSourceRange {
constexpr bool intersects(const LocalSourceRange& other) const {
return begin <= other.end && end >= other.begin;
}
constexpr bool valid() const {
return begin != -1 && end != -1;
}
};
struct Location {
/// The file path.
std::string file;
/// The range in the file.
LocalSourceRange range;
constexpr bool operator== (const Location& other) const = default;
constexpr bool valid() const {
return !file.empty() && range.valid();
}
};
/// Get the content of the file with the given file ID.

View File

@@ -124,6 +124,14 @@ public:
/// files, we cut off the range at the end of the first file.
std::pair<clang::FileID, LocalSourceRange> toLocalRange(clang::SourceRange range);
Location toLocation(clang::SourceRange range) {
auto [fid, localRange] = toLocalRange(range);
return Location{
.file = getFilePath(fid).str(),
.range = localRange,
};
}
private:
/// The interested file ID.
clang::FileID interested;
@@ -150,7 +158,7 @@ private:
/// Cache for file path. It is used to avoid multiple file path lookup.
llvm::DenseMap<clang::FileID, llvm::StringRef> pathCache;
llvm::BumpPtrAllocator pathStorage;
};