[PATCH] D105495: [clang] Make negative getLocWithOffset widening-safe.

Simon Tatham via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 6 09:27:30 PDT 2021


simon_tatham created this revision.
simon_tatham added reviewers: rsmith, lebedev.ri, akyrtzi.
Herald added subscribers: dexonsmith, martong.
simon_tatham requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This is part of a patch series working towards the ability to make
SourceLocation into a 64-bit type to handle larger translation units.

In many places in the code, SourceLocation::getLocWithOffset is used
with an argument of type `unsigned`, holding a value that's
conceptually negative (to find a location shortly before the current
one). If SourceLocation becomes a 64-bit type, this won't work any
more, because the negation will happen before widening to 64 bits, so
that instead of an offset of (say) -3, you'll get 2^32-3.

One answer would be to add casts to SourceLocation::IntType at all the
call sites, but that's quite cumbersome. In this patch I've taken a
different approach: getLocWithOffset now takes an optional second
parameter, which is subtracted from the location in addition to adding
the first parameter. That happens after widening to SourceLocation's
native integer type, so it will avoid this problem.

So you can compute an offset of -N for known-positive N by saying
`Loc.getLocWithOffset(0, N)`. And if you know the file offsets of two
features A,B and want to translate a SourceLocation for A into one for
B, you can say `Loc.getLocWithOffset(B, A)` to add (B-A) to the
position, without having to worry about integer types.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105495

Files:
  clang/include/clang/Basic/SourceLocation.h
  clang/lib/AST/SelectorLocationsKind.cpp
  clang/lib/CodeGen/CoverageMappingGen.cpp
  clang/lib/Format/FormatTokenLexer.cpp
  clang/lib/Lex/Lexer.cpp
  clang/lib/Parse/ParseStmtAsm.cpp
  clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105495.356760.patch
Type: text/x-patch
Size: 4970 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210706/75f55b29/attachment-0001.bin>


More information about the cfe-commits mailing list