diff --git a/include/Basic/SourceCode.h b/include/Basic/SourceCode.h index 02804688..99e040ac 100644 --- a/include/Basic/SourceCode.h +++ b/include/Basic/SourceCode.h @@ -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. diff --git a/include/Compiler/AST.h b/include/Compiler/AST.h index c9119702..67b1e875 100644 --- a/include/Compiler/AST.h +++ b/include/Compiler/AST.h @@ -124,6 +124,14 @@ public: /// files, we cut off the range at the end of the first file. std::pair 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 pathCache; - + llvm::BumpPtrAllocator pathStorage; };