Skip to content

[Serialization] Fix source location data loss during decoding. #145529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions clang/include/clang/Serialization/SourceLocationEncoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class SourceLocationEncoding {
using UIntTy = SourceLocation::UIntTy;
constexpr static unsigned UIntBits = CHAR_BIT * sizeof(UIntTy);

// The maximum number of bits we use for the encoding.
constexpr static unsigned EncodingBits = UIntBits + 1;

static UIntTy encodeRaw(UIntTy Raw) {
return (Raw << 1) | (Raw >> (UIntBits - 1));
}
Expand Down Expand Up @@ -179,20 +182,20 @@ SourceLocationEncoding::encode(SourceLocation Loc, UIntTy BaseOffset,

// 16 bits should be sufficient to store the module file index.
assert(BaseModuleFileIndex < (1 << 16));
Encoded |= (RawLocEncoding)BaseModuleFileIndex << 32;
Encoded |= (RawLocEncoding)BaseModuleFileIndex << EncodingBits;
return Encoded;
}
inline std::pair<SourceLocation, unsigned>
SourceLocationEncoding::decode(RawLocEncoding Encoded,
SourceLocationSequence *Seq) {
unsigned ModuleFileIndex = Encoded >> 32;
unsigned ModuleFileIndex = Encoded >> EncodingBits;

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

Encoded &= llvm::maskTrailingOnes<RawLocEncoding>(32);
Encoded &= llvm::maskTrailingOnes<RawLocEncoding>(EncodingBits);
SourceLocation Loc = SourceLocation::getFromRawEncoding(decodeRaw(Encoded));

return {Loc, ModuleFileIndex};
Expand Down
2 changes: 2 additions & 0 deletions clang/unittests/Serialization/SourceLocationEncodingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ TEST(SourceLocationEncoding, Sequence) {

roundTrip(
{123 | MacroBit, 1, 9, Biggest, Big, Big + 1, 0, MacroBit | Big, 0});

roundTrip({1, (1u << 30) + 1});
}

} // namespace
Loading