[clang] 21401a7 - [clang] Introduce SourceLocation::[U]IntTy typedefs.

Simon Tatham via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 21 02:46:04 PDT 2021


Author: Simon Tatham
Date: 2021-07-21T10:45:46+01:00
New Revision: 21401a72629cc591bab7ec6816f03e6c550f3fb3

URL: https://github.com/llvm/llvm-project/commit/21401a72629cc591bab7ec6816f03e6c550f3fb3
DIFF: https://github.com/llvm/llvm-project/commit/21401a72629cc591bab7ec6816f03e6c550f3fb3.diff

LOG: [clang] Introduce SourceLocation::[U]IntTy typedefs.

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

NFC: this patch introduces typedefs for the integer type used by
SourceLocation and makes all the boring changes to use the typedefs
everywhere, but for the moment, they are unconditionally defined to
uint32_t.

Patch originally by Mikhail Maltsev.

Reviewed By: tmatheson

Differential Revision: https://reviews.llvm.org/D105492

Added: 
    

Modified: 
    clang/include/clang/AST/DeclarationName.h
    clang/include/clang/Basic/SourceLocation.h
    clang/include/clang/Basic/SourceManager.h
    clang/include/clang/Lex/Token.h
    clang/include/clang/Serialization/ASTBitCodes.h
    clang/include/clang/Serialization/ASTReader.h
    clang/include/clang/Serialization/ASTWriter.h
    clang/include/clang/Serialization/ModuleFile.h
    clang/lib/ARCMigrate/TransEmptyStatementsAndDealloc.cpp
    clang/lib/AST/NestedNameSpecifier.cpp
    clang/lib/Basic/SourceLocation.cpp
    clang/lib/Basic/SourceManager.cpp
    clang/lib/CodeGen/CGOpenMPRuntime.cpp
    clang/lib/Lex/Lexer.cpp
    clang/lib/Lex/ModuleMap.cpp
    clang/lib/Lex/PPCaching.cpp
    clang/lib/Lex/TokenLexer.cpp
    clang/lib/Serialization/ASTReader.cpp
    clang/lib/Serialization/ASTWriter.cpp
    clang/tools/libclang/CIndex.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/DeclarationName.h b/clang/include/clang/AST/DeclarationName.h
index acf7e243da46b..38da6fc727fbd 100644
--- a/clang/include/clang/AST/DeclarationName.h
+++ b/clang/include/clang/AST/DeclarationName.h
@@ -660,13 +660,13 @@ class DeclarationNameLoc {
 
   // The location (if any) of the operator keyword is stored elsewhere.
   struct CXXOpName {
-    unsigned BeginOpNameLoc;
-    unsigned EndOpNameLoc;
+    SourceLocation::UIntTy BeginOpNameLoc;
+    SourceLocation::UIntTy EndOpNameLoc;
   };
 
   // The location (if any) of the operator keyword is stored elsewhere.
   struct CXXLitOpName {
-    unsigned OpNameLoc;
+    SourceLocation::UIntTy OpNameLoc;
   };
 
   // struct {} CXXUsingDirective;

diff  --git a/clang/include/clang/Basic/SourceLocation.h b/clang/include/clang/Basic/SourceLocation.h
index 0ba0f9bd3ddf2..540de23b9f55e 100644
--- a/clang/include/clang/Basic/SourceLocation.h
+++ b/clang/include/clang/Basic/SourceLocation.h
@@ -91,11 +91,14 @@ class SourceLocation {
   friend class SourceManager;
   friend struct llvm::FoldingSetTrait<SourceLocation>;
 
-  unsigned ID = 0;
+public:
+  using UIntTy = uint32_t;
+  using IntTy = int32_t;
 
-  enum : unsigned {
-    MacroIDBit = 1U << 31
-  };
+private:
+  UIntTy ID = 0;
+
+  enum : UIntTy { MacroIDBit = 1ULL << (8 * sizeof(UIntTy) - 1) };
 
 public:
   bool isFileID() const  { return (ID & MacroIDBit) == 0; }
@@ -111,18 +114,16 @@ class SourceLocation {
 
 private:
   /// Return the offset into the manager's global input view.
-  unsigned getOffset() const {
-    return ID & ~MacroIDBit;
-  }
+  UIntTy getOffset() const { return ID & ~MacroIDBit; }
 
-  static SourceLocation getFileLoc(unsigned ID) {
+  static SourceLocation getFileLoc(UIntTy ID) {
     assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
     SourceLocation L;
     L.ID = ID;
     return L;
   }
 
-  static SourceLocation getMacroLoc(unsigned ID) {
+  static SourceLocation getMacroLoc(UIntTy ID) {
     assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
     SourceLocation L;
     L.ID = MacroIDBit | ID;
@@ -132,7 +133,7 @@ class SourceLocation {
 public:
   /// Return a source location with the specified offset from this
   /// SourceLocation.
-  SourceLocation getLocWithOffset(int Offset) const {
+  SourceLocation getLocWithOffset(IntTy Offset) const {
     assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow");
     SourceLocation L;
     L.ID = ID+Offset;
@@ -144,13 +145,13 @@ class SourceLocation {
   ///
   /// This should only be passed to SourceLocation::getFromRawEncoding, it
   /// should not be inspected directly.
-  unsigned getRawEncoding() const { return ID; }
+  UIntTy getRawEncoding() const { return ID; }
 
   /// Turn a raw encoding of a SourceLocation object into
   /// a real SourceLocation.
   ///
   /// \see getRawEncoding.
-  static SourceLocation getFromRawEncoding(unsigned Encoding) {
+  static SourceLocation getFromRawEncoding(UIntTy Encoding) {
     SourceLocation X;
     X.ID = Encoding;
     return X;
@@ -170,7 +171,7 @@ class SourceLocation {
   /// Turn a pointer encoding of a SourceLocation object back
   /// into a real SourceLocation.
   static SourceLocation getFromPtrEncoding(const void *Encoding) {
-    return getFromRawEncoding((unsigned)(uintptr_t)Encoding);
+    return getFromRawEncoding((SourceLocation::UIntTy)(uintptr_t)Encoding);
   }
 
   static bool isPairOfFileLocations(SourceLocation Start, SourceLocation End) {
@@ -488,11 +489,13 @@ namespace llvm {
   /// DenseMapInfo<unsigned> which uses SourceLocation::ID is used as a key.
   template <> struct DenseMapInfo<clang::SourceLocation> {
     static clang::SourceLocation getEmptyKey() {
-      return clang::SourceLocation::getFromRawEncoding(~0U);
+      constexpr clang::SourceLocation::UIntTy Zero = 0;
+      return clang::SourceLocation::getFromRawEncoding(~Zero);
     }
 
     static clang::SourceLocation getTombstoneKey() {
-      return clang::SourceLocation::getFromRawEncoding(~0U - 1);
+      constexpr clang::SourceLocation::UIntTy Zero = 0;
+      return clang::SourceLocation::getFromRawEncoding(~Zero - 1);
     }
 
     static unsigned getHashValue(clang::SourceLocation Loc) {

diff  --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h
index 8cd37756d8f19..cc29c24f5a35f 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -465,8 +465,9 @@ static_assert(sizeof(FileInfo) <= sizeof(ExpansionInfo),
 /// SourceManager keeps an array of these objects, and they are uniquely
 /// identified by the FileID datatype.
 class SLocEntry {
-  unsigned Offset : 31;
-  unsigned IsExpansion : 1;
+  static constexpr int OffsetBits = 8 * sizeof(SourceLocation::UIntTy) - 1;
+  SourceLocation::UIntTy Offset : OffsetBits;
+  SourceLocation::UIntTy IsExpansion : 1;
   union {
     FileInfo File;
     ExpansionInfo Expansion;
@@ -475,7 +476,7 @@ class SLocEntry {
 public:
   SLocEntry() : Offset(), IsExpansion(), File() {}
 
-  unsigned getOffset() const { return Offset; }
+  SourceLocation::UIntTy getOffset() const { return Offset; }
 
   bool isExpansion() const { return IsExpansion; }
   bool isFile() const { return !isExpansion(); }
@@ -490,8 +491,8 @@ class SLocEntry {
     return Expansion;
   }
 
-  static SLocEntry get(unsigned Offset, const FileInfo &FI) {
-    assert(!(Offset & (1u << 31)) && "Offset is too large");
+  static SLocEntry get(SourceLocation::UIntTy Offset, const FileInfo &FI) {
+    assert(!(Offset & (1ULL << OffsetBits)) && "Offset is too large");
     SLocEntry E;
     E.Offset = Offset;
     E.IsExpansion = false;
@@ -499,8 +500,9 @@ class SLocEntry {
     return E;
   }
 
-  static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
-    assert(!(Offset & (1u << 31)) && "Offset is too large");
+  static SLocEntry get(SourceLocation::UIntTy Offset,
+                       const ExpansionInfo &Expansion) {
+    assert(!(Offset & (1ULL << OffsetBits)) && "Offset is too large");
     SLocEntry E;
     E.Offset = Offset;
     E.IsExpansion = true;
@@ -690,17 +692,18 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// The starting offset of the next local SLocEntry.
   ///
   /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
-  unsigned NextLocalOffset;
+  SourceLocation::UIntTy NextLocalOffset;
 
   /// The starting offset of the latest batch of loaded SLocEntries.
   ///
   /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
   /// not have been loaded, so that value would be unknown.
-  unsigned CurrentLoadedOffset;
+  SourceLocation::UIntTy CurrentLoadedOffset;
 
-  /// The highest possible offset is 2^31-1, so CurrentLoadedOffset
-  /// starts at 2^31.
-  static const unsigned MaxLoadedOffset = 1U << 31U;
+  /// The highest possible offset is 2^32-1 (2^63-1 for 64-bit source
+  /// locations), so CurrentLoadedOffset starts at 2^31 (2^63 resp.).
+  static const SourceLocation::UIntTy MaxLoadedOffset =
+      1ULL << (8 * sizeof(SourceLocation::UIntTy) - 1);
 
   /// A bitmap that indicates whether the entries of LoadedSLocEntryTable
   /// have already been loaded from the external source.
@@ -865,11 +868,13 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// This translates NULL into standard input.
   FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
                       SrcMgr::CharacteristicKind FileCharacter,
-                      int LoadedID = 0, unsigned LoadedOffset = 0);
+                      int LoadedID = 0,
+                      SourceLocation::UIntTy LoadedOffset = 0);
 
   FileID createFileID(FileEntryRef SourceFile, SourceLocation IncludePos,
                       SrcMgr::CharacteristicKind FileCharacter,
-                      int LoadedID = 0, unsigned LoadedOffset = 0);
+                      int LoadedID = 0,
+                      SourceLocation::UIntTy LoadedOffset = 0);
 
   /// Create a new FileID that represents the specified memory buffer.
   ///
@@ -877,7 +882,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// MemoryBuffer, so only pass a MemoryBuffer to this once.
   FileID createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer,
                       SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
-                      int LoadedID = 0, unsigned LoadedOffset = 0,
+                      int LoadedID = 0, SourceLocation::UIntTy LoadedOffset = 0,
                       SourceLocation IncludeLoc = SourceLocation());
 
   /// Create a new FileID that represents the specified memory buffer.
@@ -886,7 +891,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// outlive the SourceManager.
   FileID createFileID(const llvm::MemoryBufferRef &Buffer,
                       SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
-                      int LoadedID = 0, unsigned LoadedOffset = 0,
+                      int LoadedID = 0, SourceLocation::UIntTy LoadedOffset = 0,
                       SourceLocation IncludeLoc = SourceLocation());
 
   /// Get the FileID for \p SourceFile if it exists. Otherwise, create a
@@ -905,13 +910,11 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// Return a new SourceLocation that encodes the fact
   /// that a token from SpellingLoc should actually be referenced from
   /// ExpansionLoc.
-  SourceLocation createExpansionLoc(SourceLocation Loc,
-                                    SourceLocation ExpansionLocStart,
-                                    SourceLocation ExpansionLocEnd,
-                                    unsigned TokLength,
-                                    bool ExpansionIsTokenRange = true,
-                                    int LoadedID = 0,
-                                    unsigned LoadedOffset = 0);
+  SourceLocation
+  createExpansionLoc(SourceLocation Loc, SourceLocation ExpansionLocStart,
+                     SourceLocation ExpansionLocEnd, unsigned TokLength,
+                     bool ExpansionIsTokenRange = true, int LoadedID = 0,
+                     SourceLocation::UIntTy LoadedOffset = 0);
 
   /// Return a new SourceLocation that encodes that the token starting
   /// at \p TokenStart ends prematurely at \p TokenEnd.
@@ -1098,7 +1101,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// the entry in SLocEntryTable which contains the specified location.
   ///
   FileID getFileID(SourceLocation SpellingLoc) const {
-    unsigned SLocOffset = SpellingLoc.getOffset();
+    SourceLocation::UIntTy SLocOffset = SpellingLoc.getOffset();
 
     // If our one-entry cache covers this offset, just return it.
     if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
@@ -1221,7 +1224,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
     if (!Entry)
       return SourceLocation();
 
-    unsigned GlobalOffset = Entry->getOffset() + Offset;
+    SourceLocation::UIntTy GlobalOffset = Entry->getOffset() + Offset;
     return Entry->isFile() ? SourceLocation::getFileLoc(GlobalOffset)
                            : SourceLocation::getMacroLoc(GlobalOffset);
   }
@@ -1326,17 +1329,17 @@ class SourceManager : public RefCountedBase<SourceManager> {
   ///
   /// If it's true and \p RelativeOffset is non-null, it will be set to the
   /// relative offset of \p Loc inside the chunk.
-  bool isInSLocAddrSpace(SourceLocation Loc,
-                         SourceLocation Start, unsigned Length,
-                         unsigned *RelativeOffset = nullptr) const {
+  bool
+  isInSLocAddrSpace(SourceLocation Loc, SourceLocation Start, unsigned Length,
+                    SourceLocation::UIntTy *RelativeOffset = nullptr) const {
     assert(((Start.getOffset() < NextLocalOffset &&
                Start.getOffset()+Length <= NextLocalOffset) ||
             (Start.getOffset() >= CurrentLoadedOffset &&
                 Start.getOffset()+Length < MaxLoadedOffset)) &&
            "Chunk is not valid SLoc address space");
-    unsigned LocOffs = Loc.getOffset();
-    unsigned BeginOffs = Start.getOffset();
-    unsigned EndOffs = BeginOffs + Length;
+    SourceLocation::UIntTy LocOffs = Loc.getOffset();
+    SourceLocation::UIntTy BeginOffs = Start.getOffset();
+    SourceLocation::UIntTy EndOffs = BeginOffs + Length;
     if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
       if (RelativeOffset)
         *RelativeOffset = LocOffs - BeginOffs;
@@ -1352,8 +1355,8 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// If it's true and \p RelativeOffset is non-null, it will be set to the
   /// offset of \p RHS relative to \p LHS.
   bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
-                             int *RelativeOffset) const {
-    unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
+                             SourceLocation::IntTy *RelativeOffset) const {
+    SourceLocation::UIntTy LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
     bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
     bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
 
@@ -1517,7 +1520,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// of FileID) to \p relativeOffset.
   bool isInFileID(SourceLocation Loc, FileID FID,
                   unsigned *RelativeOffset = nullptr) const {
-    unsigned Offs = Loc.getOffset();
+    SourceLocation::UIntTy Offs = Loc.getOffset();
     if (isOffsetInFileID(FID, Offs)) {
       if (RelativeOffset)
         *RelativeOffset = Offs - getSLocEntry(FID).getOffset();
@@ -1636,8 +1639,9 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// offset in the "source location address space".
   ///
   /// Note that we always consider source locations loaded from
-  bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const {
-    unsigned LHSOffset = LHS.getOffset();
+  bool isBeforeInSLocAddrSpace(SourceLocation LHS,
+                               SourceLocation::UIntTy RHS) const {
+    SourceLocation::UIntTy LHSOffset = LHS.getOffset();
     bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
     bool RHSLoaded = RHS >= CurrentLoadedOffset;
     if (LHSLoaded == RHSLoaded)
@@ -1699,7 +1703,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
     return getSLocEntryByID(FID.ID, Invalid);
   }
 
-  unsigned getNextLocalOffset() const { return NextLocalOffset; }
+  SourceLocation::UIntTy getNextLocalOffset() const { return NextLocalOffset; }
 
   void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
     assert(LoadedSLocEntryTable.empty() &&
@@ -1713,8 +1717,9 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
   /// in the global source view. The lowest ID and the base offset of the
   /// entries will be returned.
-  std::pair<int, unsigned>
-  AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
+  std::pair<int, SourceLocation::UIntTy>
+  AllocateLoadedSLocEntries(unsigned NumSLocEntries,
+                            SourceLocation::UIntTy TotalSize);
 
   /// Returns true if \p Loc came from a PCH/Module.
   bool isLoadedSourceLocation(SourceLocation Loc) const {
@@ -1795,14 +1800,15 @@ class SourceManager : public RefCountedBase<SourceManager> {
 
   /// Implements the common elements of storing an expansion info struct into
   /// the SLocEntry table and producing a source location that refers to it.
-  SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
-                                        unsigned TokLength,
-                                        int LoadedID = 0,
-                                        unsigned LoadedOffset = 0);
+  SourceLocation
+  createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
+                         unsigned TokLength, int LoadedID = 0,
+                         SourceLocation::UIntTy LoadedOffset = 0);
 
   /// Return true if the specified FileID contains the
   /// specified SourceLocation offset.  This is a very hot method.
-  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
+  inline bool isOffsetInFileID(FileID FID,
+                               SourceLocation::UIntTy SLocOffset) const {
     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
     // If the entry is after the offset, it can't contain it.
     if (SLocOffset < Entry.getOffset()) return false;
@@ -1836,7 +1842,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
   FileID createFileIDImpl(SrcMgr::ContentCache &File, StringRef Filename,
                           SourceLocation IncludePos,
                           SrcMgr::CharacteristicKind DirCharacter, int LoadedID,
-                          unsigned LoadedOffset);
+                          SourceLocation::UIntTy LoadedOffset);
 
   SrcMgr::ContentCache &getOrCreateContentCache(FileEntryRef SourceFile,
                                                 bool isSystemFile = false);
@@ -1845,9 +1851,9 @@ class SourceManager : public RefCountedBase<SourceManager> {
   SrcMgr::ContentCache &
   createMemBufferContentCache(std::unique_ptr<llvm::MemoryBuffer> Buf);
 
-  FileID getFileIDSlow(unsigned SLocOffset) const;
-  FileID getFileIDLocal(unsigned SLocOffset) const;
-  FileID getFileIDLoaded(unsigned SLocOffset) const;
+  FileID getFileIDSlow(SourceLocation::UIntTy SLocOffset) const;
+  FileID getFileIDLocal(SourceLocation::UIntTy SLocOffset) const;
+  FileID getFileIDLoaded(SourceLocation::UIntTy SLocOffset) const;
 
   SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
   SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;

diff  --git a/clang/include/clang/Lex/Token.h b/clang/include/clang/Lex/Token.h
index 89042a674fec3..00fbe6d18f721 100644
--- a/clang/include/clang/Lex/Token.h
+++ b/clang/include/clang/Lex/Token.h
@@ -33,7 +33,7 @@ class IdentifierInfo;
 /// information about the SourceRange of the tokens and the type object.
 class Token {
   /// The location of the token. This is actually a SourceLocation.
-  unsigned Loc;
+  SourceLocation::UIntTy Loc;
 
   // Conceptually these next two fields could be in a union.  However, this
   // causes gcc 4.2 to pessimize LexTokenInternal, a very performance critical
@@ -43,7 +43,7 @@ class Token {
   /// UintData - This holds either the length of the token text, when
   /// a normal token, or the end of the SourceRange when an annotation
   /// token.
-  unsigned UintData;
+  SourceLocation::UIntTy UintData;
 
   /// PtrData - This is a union of four 
diff erent pointer types, which depends
   /// on what type of token this is:

diff  --git a/clang/include/clang/Serialization/ASTBitCodes.h b/clang/include/clang/Serialization/ASTBitCodes.h
index ca8ffc746612d..027a981df22ca 100644
--- a/clang/include/clang/Serialization/ASTBitCodes.h
+++ b/clang/include/clang/Serialization/ASTBitCodes.h
@@ -176,10 +176,10 @@ const unsigned int NUM_PREDEF_SUBMODULE_IDS = 1;
 /// Source range/offset of a preprocessed entity.
 struct PPEntityOffset {
   /// Raw source location of beginning of range.
-  unsigned Begin;
+  SourceLocation::UIntTy Begin;
 
   /// Raw source location of end of range.
-  unsigned End;
+  SourceLocation::UIntTy End;
 
   /// Offset in the AST file relative to ModuleFile::MacroOffsetsBase.
   uint32_t BitOffset;
@@ -200,9 +200,9 @@ struct PPEntityOffset {
 /// Source range of a skipped preprocessor region
 struct PPSkippedRange {
   /// Raw source location of beginning of range.
-  unsigned Begin;
+  SourceLocation::UIntTy Begin;
   /// Raw source location of end of range.
-  unsigned End;
+  SourceLocation::UIntTy End;
 
   PPSkippedRange(SourceRange R)
       : Begin(R.getBegin().getRawEncoding()), End(R.getEnd().getRawEncoding()) {
@@ -240,7 +240,7 @@ struct UnderalignedInt64 {
 /// Source location and bit offset of a declaration.
 struct DeclOffset {
   /// Raw source location.
-  unsigned Loc = 0;
+  SourceLocation::UIntTy Loc = 0;
 
   /// Offset relative to the start of the DECLTYPES_BLOCK block. Keep
   /// structure alignment 32-bit and avoid padding gap because undefined

diff  --git a/clang/include/clang/Serialization/ASTReader.h b/clang/include/clang/Serialization/ASTReader.h
index 4819a5aecefaf..242b75baca6c9 100644
--- a/clang/include/clang/Serialization/ASTReader.h
+++ b/clang/include/clang/Serialization/ASTReader.h
@@ -2139,12 +2139,15 @@ class ASTReader
 
   /// Read a source location from raw form and return it in its
   /// originating module file's source location space.
-  SourceLocation ReadUntranslatedSourceLocation(uint32_t Raw) const {
-    return SourceLocation::getFromRawEncoding((Raw >> 1) | (Raw << 31));
+  SourceLocation
+  ReadUntranslatedSourceLocation(SourceLocation::UIntTy Raw) const {
+    return SourceLocation::getFromRawEncoding((Raw >> 1) |
+                                              (Raw << (8 * sizeof(Raw) - 1)));
   }
 
   /// Read a source location from raw form.
-  SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, uint32_t Raw) const {
+  SourceLocation ReadSourceLocation(ModuleFile &ModuleFile,
+                                    SourceLocation::UIntTy Raw) const {
     SourceLocation Loc = ReadUntranslatedSourceLocation(Raw);
     return TranslateSourceLocation(ModuleFile, Loc);
   }
@@ -2158,7 +2161,8 @@ class ASTReader
     assert(ModuleFile.SLocRemap.find(Loc.getOffset()) !=
                ModuleFile.SLocRemap.end() &&
            "Cannot find offset to remap.");
-    int Remap = ModuleFile.SLocRemap.find(Loc.getOffset())->second;
+    SourceLocation::IntTy Remap =
+        ModuleFile.SLocRemap.find(Loc.getOffset())->second;
     return Loc.getLocWithOffset(Remap);
   }
 

diff  --git a/clang/include/clang/Serialization/ASTWriter.h b/clang/include/clang/Serialization/ASTWriter.h
index e4d92f0b0515c..ac88cb0a31773 100644
--- a/clang/include/clang/Serialization/ASTWriter.h
+++ b/clang/include/clang/Serialization/ASTWriter.h
@@ -347,7 +347,7 @@ class ASTWriter : public ASTDeserializationListener,
     union {
       const Decl *Dcl;
       void *Type;
-      unsigned Loc;
+      SourceLocation::UIntTy Loc;
       unsigned Val;
       Module *Mod;
       const Attr *Attribute;

diff  --git a/clang/include/clang/Serialization/ModuleFile.h b/clang/include/clang/Serialization/ModuleFile.h
index 51d03fcca3442..b1c8a8c8e72b6 100644
--- a/clang/include/clang/Serialization/ModuleFile.h
+++ b/clang/include/clang/Serialization/ModuleFile.h
@@ -260,7 +260,7 @@ class ModuleFile {
   int SLocEntryBaseID = 0;
 
   /// The base offset in the source manager's view of this module.
-  unsigned SLocEntryBaseOffset = 0;
+  SourceLocation::UIntTy SLocEntryBaseOffset = 0;
 
   /// Base file offset for the offsets in SLocEntryOffsets. Real file offset
   /// for the entry is SLocEntryOffsetsBase + SLocEntryOffsets[i].
@@ -274,7 +274,8 @@ class ModuleFile {
   SmallVector<uint64_t, 4> PreloadSLocEntries;
 
   /// Remapping table for source locations in this module.
-  ContinuousRangeMap<uint32_t, int, 2> SLocRemap;
+  ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy, 2>
+      SLocRemap;
 
   // === Identifiers ===
 

diff  --git a/clang/lib/ARCMigrate/TransEmptyStatementsAndDealloc.cpp b/clang/lib/ARCMigrate/TransEmptyStatementsAndDealloc.cpp
index 5d0cfb8a8b9c2..e9c21b8106d7d 100644
--- a/clang/lib/ARCMigrate/TransEmptyStatementsAndDealloc.cpp
+++ b/clang/lib/ARCMigrate/TransEmptyStatementsAndDealloc.cpp
@@ -52,7 +52,7 @@ static bool isEmptyARCMTMacroStatement(NullStmt *S,
   if (AfterMacroLoc == SemiLoc)
     return true;
 
-  int RelOffs = 0;
+  SourceLocation::IntTy RelOffs = 0;
   if (!SM.isInSameSLocAddrSpace(AfterMacroLoc, SemiLoc, &RelOffs))
     return false;
   if (RelOffs < 0)

diff  --git a/clang/lib/AST/NestedNameSpecifier.cpp b/clang/lib/AST/NestedNameSpecifier.cpp
index 3f8fb7aa85537..21afdd1570f4b 100644
--- a/clang/lib/AST/NestedNameSpecifier.cpp
+++ b/clang/lib/AST/NestedNameSpecifier.cpp
@@ -356,7 +356,7 @@ NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
   assert(Qualifier && "Expected a non-NULL qualifier");
 
   // Location of the trailing '::'.
-  unsigned Length = sizeof(unsigned);
+  unsigned Length = sizeof(SourceLocation::UIntTy);
 
   switch (Qualifier->getKind()) {
   case NestedNameSpecifier::Global:
@@ -368,7 +368,7 @@ NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
   case NestedNameSpecifier::NamespaceAlias:
   case NestedNameSpecifier::Super:
     // The location of the identifier or namespace name.
-    Length += sizeof(unsigned);
+    Length += sizeof(SourceLocation::UIntTy);
     break;
 
   case NestedNameSpecifier::TypeSpecWithTemplate:
@@ -393,8 +393,8 @@ NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
 /// Load a (possibly unaligned) source location from a given address
 /// and offset.
 static SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
-  unsigned Raw;
-  memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(unsigned));
+  SourceLocation::UIntTy Raw;
+  memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(Raw));
   return SourceLocation::getFromRawEncoding(Raw);
 }
 
@@ -431,8 +431,9 @@ SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
   case NestedNameSpecifier::Namespace:
   case NestedNameSpecifier::NamespaceAlias:
   case NestedNameSpecifier::Super:
-    return SourceRange(LoadSourceLocation(Data, Offset),
-                       LoadSourceLocation(Data, Offset + sizeof(unsigned)));
+    return SourceRange(
+        LoadSourceLocation(Data, Offset),
+        LoadSourceLocation(Data, Offset + sizeof(SourceLocation::UIntTy)));
 
   case NestedNameSpecifier::TypeSpecWithTemplate:
   case NestedNameSpecifier::TypeSpec: {
@@ -487,10 +488,10 @@ static void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
 /// Save a source location to the given buffer.
 static void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
                                unsigned &BufferSize, unsigned &BufferCapacity) {
-  unsigned Raw = Loc.getRawEncoding();
+  SourceLocation::UIntTy Raw = Loc.getRawEncoding();
   Append(reinterpret_cast<char *>(&Raw),
-         reinterpret_cast<char *>(&Raw) + sizeof(unsigned),
-         Buffer, BufferSize, BufferCapacity);
+         reinterpret_cast<char *>(&Raw) + sizeof(Raw), Buffer, BufferSize,
+         BufferCapacity);
 }
 
 /// Save a pointer to the given buffer.

diff  --git a/clang/lib/Basic/SourceLocation.cpp b/clang/lib/Basic/SourceLocation.cpp
index 6f6412028d776..6986fcd322f26 100644
--- a/clang/lib/Basic/SourceLocation.cpp
+++ b/clang/lib/Basic/SourceLocation.cpp
@@ -51,7 +51,7 @@ static_assert(std::is_trivially_destructible<SourceRange>::value,
               "used in unions");
 
 unsigned SourceLocation::getHashValue() const {
-  return llvm::DenseMapInfo<unsigned>::getHashValue(ID);
+  return llvm::DenseMapInfo<UIntTy>::getHashValue(ID);
 }
 
 void llvm::FoldingSetTrait<SourceLocation>::Profile(

diff  --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 2c8036aaf36af..8cba379aa0f80 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -450,9 +450,9 @@ const SrcMgr::SLocEntry &SourceManager::loadSLocEntry(unsigned Index,
   return LoadedSLocEntryTable[Index];
 }
 
-std::pair<int, unsigned>
+std::pair<int, SourceLocation::UIntTy>
 SourceManager::AllocateLoadedSLocEntries(unsigned NumSLocEntries,
-                                         unsigned TotalSize) {
+                                         SourceLocation::UIntTy TotalSize) {
   assert(ExternalSLocEntries && "Don't have an external sloc source");
   // Make sure we're not about to run out of source locations.
   if (CurrentLoadedOffset - TotalSize < NextLocalOffset)
@@ -532,7 +532,8 @@ FileID SourceManager::getNextFileID(FileID FID) const {
 FileID SourceManager::createFileID(const FileEntry *SourceFile,
                                    SourceLocation IncludePos,
                                    SrcMgr::CharacteristicKind FileCharacter,
-                                   int LoadedID, unsigned LoadedOffset) {
+                                   int LoadedID,
+                                   SourceLocation::UIntTy LoadedOffset) {
   return createFileID(SourceFile->getLastRef(), IncludePos, FileCharacter,
                       LoadedID, LoadedOffset);
 }
@@ -540,7 +541,8 @@ FileID SourceManager::createFileID(const FileEntry *SourceFile,
 FileID SourceManager::createFileID(FileEntryRef SourceFile,
                                    SourceLocation IncludePos,
                                    SrcMgr::CharacteristicKind FileCharacter,
-                                   int LoadedID, unsigned LoadedOffset) {
+                                   int LoadedID,
+                                   SourceLocation::UIntTy LoadedOffset) {
   SrcMgr::ContentCache &IR = getOrCreateContentCache(SourceFile,
                                                      isSystem(FileCharacter));
 
@@ -559,7 +561,8 @@ FileID SourceManager::createFileID(FileEntryRef SourceFile,
 /// MemoryBuffer, so only pass a MemoryBuffer to this once.
 FileID SourceManager::createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer,
                                    SrcMgr::CharacteristicKind FileCharacter,
-                                   int LoadedID, unsigned LoadedOffset,
+                                   int LoadedID,
+                                   SourceLocation::UIntTy LoadedOffset,
                                    SourceLocation IncludeLoc) {
   StringRef Name = Buffer->getBufferIdentifier();
   return createFileIDImpl(createMemBufferContentCache(std::move(Buffer)), Name,
@@ -572,7 +575,8 @@ FileID SourceManager::createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer,
 /// outlive the SourceManager.
 FileID SourceManager::createFileID(const llvm::MemoryBufferRef &Buffer,
                                    SrcMgr::CharacteristicKind FileCharacter,
-                                   int LoadedID, unsigned LoadedOffset,
+                                   int LoadedID,
+                                   SourceLocation::UIntTy LoadedOffset,
                                    SourceLocation IncludeLoc) {
   return createFileID(llvm::MemoryBuffer::getMemBuffer(Buffer), FileCharacter,
                       LoadedID, LoadedOffset, IncludeLoc);
@@ -594,7 +598,8 @@ SourceManager::getOrCreateFileID(const FileEntry *SourceFile,
 FileID SourceManager::createFileIDImpl(ContentCache &File, StringRef Filename,
                                        SourceLocation IncludePos,
                                        SrcMgr::CharacteristicKind FileCharacter,
-                                       int LoadedID, unsigned LoadedOffset) {
+                                       int LoadedID,
+                                       SourceLocation::UIntTy LoadedOffset) {
   if (LoadedID < 0) {
     assert(LoadedID != -1 && "Loading sentinel FileID");
     unsigned Index = unsigned(-LoadedID) - 2;
@@ -633,14 +638,11 @@ SourceManager::createMacroArgExpansionLoc(SourceLocation SpellingLoc,
   return createExpansionLocImpl(Info, TokLength);
 }
 
-SourceLocation
-SourceManager::createExpansionLoc(SourceLocation SpellingLoc,
-                                  SourceLocation ExpansionLocStart,
-                                  SourceLocation ExpansionLocEnd,
-                                  unsigned TokLength,
-                                  bool ExpansionIsTokenRange,
-                                  int LoadedID,
-                                  unsigned LoadedOffset) {
+SourceLocation SourceManager::createExpansionLoc(
+    SourceLocation SpellingLoc, SourceLocation ExpansionLocStart,
+    SourceLocation ExpansionLocEnd, unsigned TokLength,
+    bool ExpansionIsTokenRange, int LoadedID,
+    SourceLocation::UIntTy LoadedOffset) {
   ExpansionInfo Info = ExpansionInfo::create(
       SpellingLoc, ExpansionLocStart, ExpansionLocEnd, ExpansionIsTokenRange);
   return createExpansionLocImpl(Info, TokLength, LoadedID, LoadedOffset);
@@ -658,9 +660,8 @@ SourceLocation SourceManager::createTokenSplitLoc(SourceLocation Spelling,
 
 SourceLocation
 SourceManager::createExpansionLocImpl(const ExpansionInfo &Info,
-                                      unsigned TokLength,
-                                      int LoadedID,
-                                      unsigned LoadedOffset) {
+                                      unsigned TokLength, int LoadedID,
+                                      SourceLocation::UIntTy LoadedOffset) {
   if (LoadedID < 0) {
     assert(LoadedID != -1 && "Loading sentinel FileID");
     unsigned Index = unsigned(-LoadedID) - 2;
@@ -762,7 +763,7 @@ llvm::Optional<StringRef> SourceManager::getBufferDataOrNone(FileID FID) const {
 /// This is the cache-miss path of getFileID. Not as hot as that function, but
 /// still very important. It is responsible for finding the entry in the
 /// SLocEntry tables that contains the specified location.
-FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
+FileID SourceManager::getFileIDSlow(SourceLocation::UIntTy SLocOffset) const {
   if (!SLocOffset)
     return FileID::get(0);
 
@@ -777,7 +778,7 @@ FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
 ///
 /// This function knows that the SourceLocation is in a local buffer, not a
 /// loaded one.
-FileID SourceManager::getFileIDLocal(unsigned SLocOffset) const {
+FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const {
   assert(SLocOffset < NextLocalOffset && "Bad function choice");
 
   // After the first and second level caches, I see two common sorts of
@@ -828,7 +829,8 @@ FileID SourceManager::getFileIDLocal(unsigned SLocOffset) const {
   NumProbes = 0;
   while (true) {
     unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
-    unsigned MidOffset = getLocalSLocEntry(MiddleIndex).getOffset();
+    SourceLocation::UIntTy MidOffset =
+        getLocalSLocEntry(MiddleIndex).getOffset();
 
     ++NumProbes;
 
@@ -859,7 +861,7 @@ FileID SourceManager::getFileIDLocal(unsigned SLocOffset) const {
 ///
 /// This function knows that the SourceLocation is in a loaded buffer, not a
 /// local one.
-FileID SourceManager::getFileIDLoaded(unsigned SLocOffset) const {
+FileID SourceManager::getFileIDLoaded(SourceLocation::UIntTy SLocOffset) const {
   // Sanity checking, otherwise a bug may lead to hanging in release build.
   if (SLocOffset < CurrentLoadedOffset) {
     assert(0 && "Invalid SLocOffset or bad function choice");
@@ -1619,7 +1621,7 @@ unsigned SourceManager::getFileIDSize(FileID FID) const {
     return 0;
 
   int ID = FID.ID;
-  unsigned NextOffset;
+  SourceLocation::UIntTy NextOffset;
   if ((ID > 0 && unsigned(ID+1) == local_sloc_entry_size()))
     NextOffset = getNextLocalOffset();
   else if (ID+1 == -1)
@@ -1827,8 +1829,8 @@ void SourceManager::associateFileChunkWithMacroArgExp(
                                          SourceLocation ExpansionLoc,
                                          unsigned ExpansionLength) const {
   if (!SpellLoc.isFileID()) {
-    unsigned SpellBeginOffs = SpellLoc.getOffset();
-    unsigned SpellEndOffs = SpellBeginOffs + ExpansionLength;
+    SourceLocation::UIntTy SpellBeginOffs = SpellLoc.getOffset();
+    SourceLocation::UIntTy SpellEndOffs = SpellBeginOffs + ExpansionLength;
 
     // The spelling range for this macro argument expansion can span multiple
     // consecutive FileID entries. Go through each entry contained in the
@@ -1840,9 +1842,9 @@ void SourceManager::associateFileChunkWithMacroArgExp(
     std::tie(SpellFID, SpellRelativeOffs) = getDecomposedLoc(SpellLoc);
     while (true) {
       const SLocEntry &Entry = getSLocEntry(SpellFID);
-      unsigned SpellFIDBeginOffs = Entry.getOffset();
+      SourceLocation::UIntTy SpellFIDBeginOffs = Entry.getOffset();
       unsigned SpellFIDSize = getFileIDSize(SpellFID);
-      unsigned SpellFIDEndOffs = SpellFIDBeginOffs + SpellFIDSize;
+      SourceLocation::UIntTy SpellFIDEndOffs = SpellFIDBeginOffs + SpellFIDSize;
       const ExpansionInfo &Info = Entry.getExpansion();
       if (Info.isMacroArgExpansion()) {
         unsigned CurrSpellLength;
@@ -1934,7 +1936,7 @@ SourceManager::getMacroArgExpandedLocation(SourceLocation Loc) const {
 
   --I;
 
-  unsigned MacroArgBeginOffs = I->first;
+  SourceLocation::UIntTy MacroArgBeginOffs = I->first;
   SourceLocation MacroArgExpandedLoc = I->second;
   if (MacroArgExpandedLoc.isValid())
     return MacroArgExpandedLoc.getLocWithOffset(Offset - MacroArgBeginOffs);
@@ -2154,7 +2156,7 @@ LLVM_DUMP_METHOD void SourceManager::dump() const {
   llvm::raw_ostream &out = llvm::errs();
 
   auto DumpSLocEntry = [&](int ID, const SrcMgr::SLocEntry &Entry,
-                           llvm::Optional<unsigned> NextStart) {
+                           llvm::Optional<SourceLocation::UIntTy> NextStart) {
     out << "SLocEntry <FileID " << ID << "> " << (Entry.isFile() ? "file" : "expansion")
         << " <SourceLocation " << Entry.getOffset() << ":";
     if (NextStart)
@@ -2194,7 +2196,7 @@ LLVM_DUMP_METHOD void SourceManager::dump() const {
                                    : LocalSLocEntryTable[ID + 1].getOffset());
   }
   // Dump loaded SLocEntries.
-  llvm::Optional<unsigned> NextStart;
+  llvm::Optional<SourceLocation::UIntTy> NextStart;
   for (unsigned Index = 0; Index != LoadedSLocEntryTable.size(); ++Index) {
     int ID = -(int)Index - 2;
     if (SLocEntryLoaded[Index]) {

diff  --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index a9f7b0906f65a..7310b5c3f8d03 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -12000,13 +12000,14 @@ Address CGOpenMPRuntime::getAddressOfLocalVariable(CodeGenFunction &CGF,
     // Cleanup action for allocate support.
     class OMPAllocateCleanupTy final : public EHScopeStack::Cleanup {
       llvm::FunctionCallee RTLFn;
-      unsigned LocEncoding;
+      SourceLocation::UIntTy LocEncoding;
       Address Addr;
       const Expr *Allocator;
 
     public:
-      OMPAllocateCleanupTy(llvm::FunctionCallee RTLFn, unsigned LocEncoding,
-                           Address Addr, const Expr *Allocator)
+      OMPAllocateCleanupTy(llvm::FunctionCallee RTLFn,
+                           SourceLocation::UIntTy LocEncoding, Address Addr,
+                           const Expr *Allocator)
           : RTLFn(RTLFn), LocEncoding(LocEncoding), Addr(Addr),
             Allocator(Allocator) {}
       void Emit(CodeGenFunction &CGF, Flags /*flags*/) override {

diff  --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index ac0489e58407f..3034af231e0ee 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -588,7 +588,7 @@ PreambleBounds Lexer::ComputePreamble(StringRef Buffer,
   // Create a lexer starting at the beginning of the file. Note that we use a
   // "fake" file source location at offset 1 so that the lexer will track our
   // position within the file.
-  const unsigned StartOffset = 1;
+  const SourceLocation::UIntTy StartOffset = 1;
   SourceLocation FileLoc = SourceLocation::getFromRawEncoding(StartOffset);
   Lexer TheLexer(FileLoc, LangOpts, Buffer.begin(), Buffer.begin(),
                  Buffer.end());

diff  --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index 2c0038da36c9e..f9af7c2a24fb9 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -1378,7 +1378,7 @@ namespace clang {
       RSquare
     } Kind;
 
-    unsigned Location;
+    SourceLocation::UIntTy Location;
     unsigned StringLength;
     union {
       // If Kind != IntegerLiteral.

diff  --git a/clang/lib/Lex/PPCaching.cpp b/clang/lib/Lex/PPCaching.cpp
index 31548d246d5a5..e05e52ba9bb53 100644
--- a/clang/lib/Lex/PPCaching.cpp
+++ b/clang/lib/Lex/PPCaching.cpp
@@ -145,7 +145,7 @@ bool Preprocessor::IsPreviousCachedToken(const Token &Tok) const {
   if (LastCachedTok.getKind() != Tok.getKind())
     return false;
 
-  int RelOffset = 0;
+  SourceLocation::IntTy RelOffset = 0;
   if ((!getSourceManager().isInSameSLocAddrSpace(
           Tok.getLocation(), getLastCachedTokenLocation(), &RelOffset)) ||
       RelOffset)

diff  --git a/clang/lib/Lex/TokenLexer.cpp b/clang/lib/Lex/TokenLexer.cpp
index da5681aaf4784..41e7f3f1dccb8 100644
--- a/clang/lib/Lex/TokenLexer.cpp
+++ b/clang/lib/Lex/TokenLexer.cpp
@@ -971,7 +971,7 @@ TokenLexer::getExpansionLocForMacroDefLoc(SourceLocation loc) const {
   assert(SM.isInSLocAddrSpace(loc, MacroDefStart, MacroDefLength) &&
          "Expected loc to come from the macro definition");
 
-  unsigned relativeOffset = 0;
+  SourceLocation::UIntTy relativeOffset = 0;
   SM.isInSLocAddrSpace(loc, MacroDefStart, MacroDefLength, &relativeOffset);
   return MacroExpansionStart.getLocWithOffset(relativeOffset);
 }
@@ -1010,7 +1010,7 @@ static void updateConsecutiveMacroArgTokens(SourceManager &SM,
     if (CurLoc.isFileID() != NextLoc.isFileID())
       break; // Token from 
diff erent kind of FileID.
 
-    int RelOffs;
+    SourceLocation::IntTy RelOffs;
     if (!SM.isInSameSLocAddrSpace(CurLoc, NextLoc, &RelOffs))
       break; // Token from 
diff erent local/loaded location.
     // Check that token is not before the previous token or more than 50
@@ -1027,10 +1027,11 @@ static void updateConsecutiveMacroArgTokens(SourceManager &SM,
   // For the consecutive tokens, find the length of the SLocEntry to contain
   // all of them.
   Token &LastConsecutiveTok = *(NextTok-1);
-  int LastRelOffs = 0;
+  SourceLocation::IntTy LastRelOffs = 0;
   SM.isInSameSLocAddrSpace(FirstLoc, LastConsecutiveTok.getLocation(),
                            &LastRelOffs);
-  unsigned FullLength = LastRelOffs + LastConsecutiveTok.getLength();
+  SourceLocation::UIntTy FullLength =
+      LastRelOffs + LastConsecutiveTok.getLength();
 
   // Create a macro expansion SLocEntry that will "contain" all of the tokens.
   SourceLocation Expansion =
@@ -1040,7 +1041,7 @@ static void updateConsecutiveMacroArgTokens(SourceManager &SM,
   // expanded location.
   for (; begin_tokens < NextTok; ++begin_tokens) {
     Token &Tok = *begin_tokens;
-    int RelOffs = 0;
+    SourceLocation::IntTy RelOffs = 0;
     SM.isInSameSLocAddrSpace(FirstLoc, Tok.getLocation(), &RelOffs);
     Tok.setLocation(Expansion.getLocWithOffset(RelOffs));
   }

diff  --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp
index faf6f3dc5d953..83bade9941b3d 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -1482,7 +1482,7 @@ bool ASTReader::ReadSLocEntry(int ID) {
   }
 
   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
-  unsigned BaseOffset = F->SLocEntryBaseOffset;
+  SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
 
   ++NumSLocEntriesRead;
   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
@@ -3409,7 +3409,7 @@ ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
     case SOURCE_LOCATION_OFFSETS: {
       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
       F.LocalNumSLocEntries = Record[0];
-      unsigned SLocSpaceSize = Record[1];
+      SourceLocation::UIntTy SLocSpaceSize = Record[1];
       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
@@ -3427,7 +3427,7 @@ ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
 
       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
-      assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
+      assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
       GlobalSLocOffsetMap.insert(
           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
                            - SLocSpaceSize,&F));
@@ -3436,8 +3436,8 @@ ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
       // Invalid stays invalid.
       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
       // This module. Base was 2 when being compiled.
-      F.SLocRemap.insertOrReplace(std::make_pair(2U,
-                                  static_cast<int>(F.SLocEntryBaseOffset - 2)));
+      F.SLocRemap.insertOrReplace(std::make_pair(
+          2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2)));
 
       TotalNumSLocEntries += F.LocalNumSLocEntries;
       break;
@@ -3838,8 +3838,11 @@ void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
   }
 
   // Continuous range maps we may be updating in our module.
+  using SLocRemapBuilder =
+      ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy,
+                         2>::Builder;
   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
-  RemapBuilder SLocRemap(F.SLocRemap);
+  SLocRemapBuilder SLocRemap(F.SLocRemap);
   RemapBuilder IdentifierRemap(F.IdentifierRemap);
   RemapBuilder MacroRemap(F.MacroRemap);
   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
@@ -3870,7 +3873,7 @@ void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
       return;
     }
 
-    uint32_t SLocOffset =
+    SourceLocation::UIntTy SLocOffset =
         endian::readNext<uint32_t, little, unaligned>(Data);
     uint32_t IdentifierIDOffset =
         endian::readNext<uint32_t, little, unaligned>(Data);
@@ -3887,15 +3890,21 @@ void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
     uint32_t TypeIndexOffset =
         endian::readNext<uint32_t, little, unaligned>(Data);
 
-    uint32_t None = std::numeric_limits<uint32_t>::max();
-
     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
                          RemapBuilder &Remap) {
+      constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
       if (Offset != None)
         Remap.insert(std::make_pair(Offset,
                                     static_cast<int>(BaseOffset - Offset)));
     };
-    mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
+
+    constexpr SourceLocation::UIntTy SLocNone =
+        std::numeric_limits<SourceLocation::UIntTy>::max();
+    if (SLocOffset != SLocNone)
+      SLocRemap.insert(std::make_pair(
+          SLocOffset, static_cast<SourceLocation::IntTy>(
+                          OM->SLocEntryBaseOffset - SLocOffset)));
+
     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,

diff  --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index ca169c010555c..66c207ad9243d 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -2041,7 +2041,7 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
       Record.push_back(Expansion.isExpansionTokenRange());
 
       // Compute the token length for this macro expansion.
-      unsigned NextOffset = SourceMgr.getNextLocalOffset();
+      SourceLocation::UIntTy NextOffset = SourceMgr.getNextLocalOffset();
       if (I + 1 != N)
         NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
       Record.push_back(NextOffset - SLoc->getOffset() - 1);
@@ -4640,7 +4640,7 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
         // another module after it or have more than one entity inside it.
         uint32_t None = std::numeric_limits<uint32_t>::max();
 
-        auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
+        auto writeBaseIDOrNone = [&](auto BaseID, bool ShouldWrite) {
           assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
           if (ShouldWrite)
             LE.write<uint32_t>(BaseID);
@@ -5027,8 +5027,8 @@ void ASTWriter::AddAlignPackInfo(const Sema::AlignPackInfo &Info,
 }
 
 void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
-  uint32_t Raw = Loc.getRawEncoding();
-  Record.push_back((Raw << 1) | (Raw >> 31));
+  SourceLocation::UIntTy Raw = Loc.getRawEncoding();
+  Record.push_back((Raw << 1) | (Raw >> (8 * sizeof(Raw) - 1)));
 }
 
 void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {

diff  --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp
index 72eae1e2cd9ca..7b93164ccaa21 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -1983,7 +1983,8 @@ class MemberRefVisit : public VisitorJob {
     return static_cast<const FieldDecl *>(data[0]);
   }
   SourceLocation getLoc() const {
-    return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)data[1]);
+    return SourceLocation::getFromRawEncoding(
+        (SourceLocation::UIntTy)(uintptr_t)data[1]);
   }
 };
 class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> {


        


More information about the cfe-commits mailing list