[clang] [clang-tools-extra] [clang] Extend SourceLocation to 64 bits. (PR #147292)

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 7 23:43:37 PDT 2025


================
@@ -43,17 +46,24 @@ namespace clang {
 // Macro locations have the top bit set, we rotate by one so it is the low bit.
 class SourceLocationEncoding {
   using UIntTy = SourceLocation::UIntTy;
-  constexpr static unsigned UIntBits = CHAR_BIT * sizeof(UIntTy);
 
   static UIntTy encodeRaw(UIntTy Raw) {
-    return (Raw << 1) | (Raw >> (UIntBits - 1));
+    return ((Raw & llvm::maskTrailingOnes<uint64_t>(SourceLocation::Bits - 1))
+            << 1) |
+           (Raw >> (SourceLocation::Bits - 1));
   }
   static UIntTy decodeRaw(UIntTy Raw) {
-    return (Raw >> 1) | (Raw << (UIntBits - 1));
+    return (Raw >> 1) | ((Raw & 1) << (SourceLocation::Bits - 1));
   }
 
 public:
   using RawLocEncoding = uint64_t;
+  // 16 bits should be sufficient to store the module file index.
+  constexpr static unsigned ModuleFileIndexBits = 16;
----------------
hokein wrote:

This reflects the "only the lower 16 bits of A are used to store the module file index" comment.

I’m fine with using 20 bits for the module file index (which would still leave us with 4 spare bits that could potentially be used for the offset). However, I’d prefer to make that change in a follow-up patch to keep this one as minimal as possible.


https://github.com/llvm/llvm-project/pull/147292


More information about the cfe-commits mailing list