refactor(lint): derive CommentStripped from tokens, drop the char scanner
StripComments hand-scanned five states over raw characters and could not see
a raw string literal, which was documented as a v1 limitation. It was worse
than "does not recognise them": an odd number of quotes inside a raw string
desynchronised the scanner for the rest of the file. Simulated on the new
fixture, the old output was
auto banner = R" "hi)" \n \n ... "MARKER text"
— the code after the raw string blanked away, and the *contents* of a later
string literal left standing as if it were code. Every rule reading
CommentStripped() saw that. There are 33 raw strings across 6 files here,
including the two largest.
The replacement projects the token stream onto a copy of the buffer, blanking
comment tokens whole and the bodies of string/character literals. Editing a
copy in place makes the length-preserving contract structural rather than
something each branch has to remember, and the token extents make raw
strings, escapes, encoding prefixes and '"' all fall out for free. A raw
string reduces to R"…" so rules that bracket a literal by counting quotes
keep working. Digit separators (1'000) are excluded by requiring the text
before the quote to be an encoding prefix.
The public contract is unchanged, so no rule needed editing, and `lint` over
this repo produces byte-identical output to the previous implementation.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
5888af29ed
commit
04bc58b2fa
3 changed files with 81 additions and 72 deletions
|
|
@ -161,10 +161,16 @@ export namespace Crafter {
|
|||
|
||||
CRAFTER_API std::string Extension() const; // ".cppm", ".cpp", ".h", ...
|
||||
CRAFTER_API std::string_view Line(std::size_t n) const; // 1-based; empty if out of range
|
||||
// `content` with //-comments, /*...*/ comments and string/char literal
|
||||
// bodies blanked to spaces, newlines preserved — offsets and line
|
||||
// numbers stay valid. Built on first call, cached per file. Raw string
|
||||
// literals are not recognized (v1 limitation).
|
||||
// `content` with comments and string/char literal bodies blanked to
|
||||
// spaces, newlines preserved — offsets and line numbers stay valid.
|
||||
// Built from Tokens() on first call, cached per file, so raw strings,
|
||||
// escapes, encoding prefixes and literals like '"' all come out right.
|
||||
// A raw string is reduced to R"…" with the body and the delimiter
|
||||
// scaffolding blanked, leaving exactly two quote characters.
|
||||
//
|
||||
// Convenient for a quick scan, but Tokens() is the better tool for
|
||||
// anything structural: this view cannot tell an identifier from a
|
||||
// keyword, and it has already thrown away where the literals were.
|
||||
CRAFTER_API const std::string& CommentStripped();
|
||||
// Record a finding at `line` (1-based; pass 0 for a whole-file finding).
|
||||
CRAFTER_API void Report(std::size_t line, std::string message);
|
||||
|
|
|
|||
Loading…
Reference in a new issue