[PATCH] Add debug method to visualize complex source locations

Richard Smith richard at metafoo.co.uk
Wed May 22 14:41:36 PDT 2013


On Wed, May 22, 2013 at 9:20 AM, Jordan Rose <jordan_rose at apple.com> wrote:

>
>   You can tokenize without a lexer, but the line/caret combo might
> actually be better. Nice work!
>
>
> ================
> Comment at: lib/Basic/SourceLocation.cpp:70-71
> @@ +69,4 @@
> +  StringRef Buffer = SM.getBufferData(LocInfo.first);
> +  int First = std::max<int>(Buffer.rfind('\n', LocInfo.second) + 1, 0);
> +  int Last = std::min<int>(Buffer.find('\n', LocInfo.second),
> Buffer.size());
> +  OS << "|" << Buffer.substr(First, Last - First) << "|\n" << IndentStr
> ----------------
> Not super-happy about relying on the value of StringRef::npos here, but
> okay.
>

Aren't these min/max calls both redundant? Assuming we're not dealing with
buffers longer than 2^31 characters, they're equivalent to:

int First = Buffer.rfind('\n', LocInfo.second) + 1; // because this is
never negative
int Last = Buffer.find('\n', LocInfo.second); // because this is never >=
Buffer.size() after conversion to int

These are both exactly what was wanted, but should be of type size_t (==
StringRef::size_type).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130522/ad534e97/attachment.html>


More information about the cfe-commits mailing list