Skip to content

Commit bf12aed

Browse files
committed
libutil: Document hacks and problems around Pos class
This should provide context for follow-up commits in the patch series.
1 parent a53b184 commit bf12aed

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/libutil/error.hh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ struct LinesOfCode {
5050
std::optional<std::string> nextLineOfCode;
5151
};
5252

53+
/* NOTE: position.hh recursively depends on source-path.hh -> source-accessor.hh
54+
-> hash.hh -> config.hh -> experimental-features.hh -> error.hh -> Pos.
55+
There are other such cycles.
56+
Thus, Pos has to be an incomplete type in this header. But since ErrorInfo/Trace
57+
have to refer to Pos, they have to use pointer indirection via std::shared_ptr
58+
to break the recursive header dependency.
59+
FIXME: Untangle this mess. Should there be AbstractPos as there used to be before
60+
4feb7d9f71? */
5361
struct Pos;
5462

5563
void printCodeLines(std::ostream & out,

src/libutil/pos-table.hh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ public:
7676
return PosIdx(1 + origin.offset + offset);
7777
}
7878

79+
/**
80+
* Convert a byte-offset PosIdx into a Pos with line/column information.
81+
*
82+
* @param p Byte offset into the virtual concatenation of all parsed contents
83+
* @return Position
84+
*
85+
* @warning Very expensive to call, as this has to read the entire source
86+
* into memory each time. Call this only if absolutely necessary. Prefer
87+
* to keep PosIdx around instead of needlessly converting it into Pos by
88+
* using this lookup method.
89+
*/
7990
Pos operator[](PosIdx p) const;
8091

8192
Pos::Origin originOf(PosIdx p) const

src/libutil/position.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct Pos
5050

5151
explicit operator bool() const { return line > 0; }
5252

53+
/* TODO: Why std::shared_ptr<Pos> and not std::shared_ptr<const Pos>? */
5354
operator std::shared_ptr<Pos>() const;
5455

5556
/**

0 commit comments

Comments
 (0)