Skip to content

Commit d0ddd1d

Browse files
committed
[Serialization] Fix source location data loss during decoding.
The delta encoding can produce values up to 33 bits, but the current decoding logic only preserves the lower 32 bits, potentially causing data loss. This patch fixes the issue by preserving the lower 33 bits for the encode.
1 parent 12ba751 commit d0ddd1d

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

clang/include/clang/Serialization/SourceLocationEncoding.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class SourceLocationEncoding {
4848
using UIntTy = SourceLocation::UIntTy;
4949
constexpr static unsigned UIntBits = CHAR_BIT * sizeof(UIntTy);
5050

51+
// The maximum number of bits we use for the encoding.
52+
constexpr static unsigned EncodingBits = UIntBits + 1;
53+
5154
static UIntTy encodeRaw(UIntTy Raw) {
5255
return (Raw << 1) | (Raw >> (UIntBits - 1));
5356
}
@@ -179,20 +182,20 @@ SourceLocationEncoding::encode(SourceLocation Loc, UIntTy BaseOffset,
179182

180183
// 16 bits should be sufficient to store the module file index.
181184
assert(BaseModuleFileIndex < (1 << 16));
182-
Encoded |= (RawLocEncoding)BaseModuleFileIndex << 32;
185+
Encoded |= (RawLocEncoding)BaseModuleFileIndex << EncodingBits;
183186
return Encoded;
184187
}
185188
inline std::pair<SourceLocation, unsigned>
186189
SourceLocationEncoding::decode(RawLocEncoding Encoded,
187190
SourceLocationSequence *Seq) {
188-
unsigned ModuleFileIndex = Encoded >> 32;
191+
unsigned ModuleFileIndex = Encoded >> EncodingBits;
189192

190193
if (!ModuleFileIndex)
191194
return {Seq ? Seq->decode(Encoded)
192195
: SourceLocation::getFromRawEncoding(decodeRaw(Encoded)),
193196
ModuleFileIndex};
194197

195-
Encoded &= llvm::maskTrailingOnes<RawLocEncoding>(32);
198+
Encoded &= llvm::maskTrailingOnes<RawLocEncoding>(EncodingBits);
196199
SourceLocation Loc = SourceLocation::getFromRawEncoding(decodeRaw(Encoded));
197200

198201
return {Loc, ModuleFileIndex};

clang/unittests/Serialization/SourceLocationEncodingTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ TEST(SourceLocationEncoding, Sequence) {
104104

105105
roundTrip(
106106
{123 | MacroBit, 1, 9, Biggest, Big, Big + 1, 0, MacroBit | Big, 0});
107+
108+
roundTrip({1, (1u << 30) + 1});
107109
}
108110

109111
} // namespace

0 commit comments

Comments
 (0)