Skip to content

Commit 21401a7

Browse files
committedJul 21, 2021
[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
1 parent 91670f5 commit 21401a7

20 files changed

+176
-147
lines changed
 

‎clang/include/clang/AST/DeclarationName.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -660,13 +660,13 @@ class DeclarationNameLoc {
660660

661661
// The location (if any) of the operator keyword is stored elsewhere.
662662
struct CXXOpName {
663-
unsigned BeginOpNameLoc;
664-
unsigned EndOpNameLoc;
663+
SourceLocation::UIntTy BeginOpNameLoc;
664+
SourceLocation::UIntTy EndOpNameLoc;
665665
};
666666

667667
// The location (if any) of the operator keyword is stored elsewhere.
668668
struct CXXLitOpName {
669-
unsigned OpNameLoc;
669+
SourceLocation::UIntTy OpNameLoc;
670670
};
671671

672672
// struct {} CXXUsingDirective;

‎clang/include/clang/Basic/SourceLocation.h

+18-15
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,14 @@ class SourceLocation {
9191
friend class SourceManager;
9292
friend struct llvm::FoldingSetTrait<SourceLocation>;
9393

94-
unsigned ID = 0;
94+
public:
95+
using UIntTy = uint32_t;
96+
using IntTy = int32_t;
9597

96-
enum : unsigned {
97-
MacroIDBit = 1U << 31
98-
};
98+
private:
99+
UIntTy ID = 0;
100+
101+
enum : UIntTy { MacroIDBit = 1ULL << (8 * sizeof(UIntTy) - 1) };
99102

100103
public:
101104
bool isFileID() const { return (ID & MacroIDBit) == 0; }
@@ -111,18 +114,16 @@ class SourceLocation {
111114

112115
private:
113116
/// Return the offset into the manager's global input view.
114-
unsigned getOffset() const {
115-
return ID & ~MacroIDBit;
116-
}
117+
UIntTy getOffset() const { return ID & ~MacroIDBit; }
117118

118-
static SourceLocation getFileLoc(unsigned ID) {
119+
static SourceLocation getFileLoc(UIntTy ID) {
119120
assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
120121
SourceLocation L;
121122
L.ID = ID;
122123
return L;
123124
}
124125

125-
static SourceLocation getMacroLoc(unsigned ID) {
126+
static SourceLocation getMacroLoc(UIntTy ID) {
126127
assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
127128
SourceLocation L;
128129
L.ID = MacroIDBit | ID;
@@ -132,7 +133,7 @@ class SourceLocation {
132133
public:
133134
/// Return a source location with the specified offset from this
134135
/// SourceLocation.
135-
SourceLocation getLocWithOffset(int Offset) const {
136+
SourceLocation getLocWithOffset(IntTy Offset) const {
136137
assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow");
137138
SourceLocation L;
138139
L.ID = ID+Offset;
@@ -144,13 +145,13 @@ class SourceLocation {
144145
///
145146
/// This should only be passed to SourceLocation::getFromRawEncoding, it
146147
/// should not be inspected directly.
147-
unsigned getRawEncoding() const { return ID; }
148+
UIntTy getRawEncoding() const { return ID; }
148149

149150
/// Turn a raw encoding of a SourceLocation object into
150151
/// a real SourceLocation.
151152
///
152153
/// \see getRawEncoding.
153-
static SourceLocation getFromRawEncoding(unsigned Encoding) {
154+
static SourceLocation getFromRawEncoding(UIntTy Encoding) {
154155
SourceLocation X;
155156
X.ID = Encoding;
156157
return X;
@@ -170,7 +171,7 @@ class SourceLocation {
170171
/// Turn a pointer encoding of a SourceLocation object back
171172
/// into a real SourceLocation.
172173
static SourceLocation getFromPtrEncoding(const void *Encoding) {
173-
return getFromRawEncoding((unsigned)(uintptr_t)Encoding);
174+
return getFromRawEncoding((SourceLocation::UIntTy)(uintptr_t)Encoding);
174175
}
175176

176177
static bool isPairOfFileLocations(SourceLocation Start, SourceLocation End) {
@@ -488,11 +489,13 @@ namespace llvm {
488489
/// DenseMapInfo<unsigned> which uses SourceLocation::ID is used as a key.
489490
template <> struct DenseMapInfo<clang::SourceLocation> {
490491
static clang::SourceLocation getEmptyKey() {
491-
return clang::SourceLocation::getFromRawEncoding(~0U);
492+
constexpr clang::SourceLocation::UIntTy Zero = 0;
493+
return clang::SourceLocation::getFromRawEncoding(~Zero);
492494
}
493495

494496
static clang::SourceLocation getTombstoneKey() {
495-
return clang::SourceLocation::getFromRawEncoding(~0U - 1);
497+
constexpr clang::SourceLocation::UIntTy Zero = 0;
498+
return clang::SourceLocation::getFromRawEncoding(~Zero - 1);
496499
}
497500

498501
static unsigned getHashValue(clang::SourceLocation Loc) {

‎clang/include/clang/Basic/SourceManager.h

+54-48
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,9 @@ static_assert(sizeof(FileInfo) <= sizeof(ExpansionInfo),
465465
/// SourceManager keeps an array of these objects, and they are uniquely
466466
/// identified by the FileID datatype.
467467
class SLocEntry {
468-
unsigned Offset : 31;
469-
unsigned IsExpansion : 1;
468+
static constexpr int OffsetBits = 8 * sizeof(SourceLocation::UIntTy) - 1;
469+
SourceLocation::UIntTy Offset : OffsetBits;
470+
SourceLocation::UIntTy IsExpansion : 1;
470471
union {
471472
FileInfo File;
472473
ExpansionInfo Expansion;
@@ -475,7 +476,7 @@ class SLocEntry {
475476
public:
476477
SLocEntry() : Offset(), IsExpansion(), File() {}
477478

478-
unsigned getOffset() const { return Offset; }
479+
SourceLocation::UIntTy getOffset() const { return Offset; }
479480

480481
bool isExpansion() const { return IsExpansion; }
481482
bool isFile() const { return !isExpansion(); }
@@ -490,17 +491,18 @@ class SLocEntry {
490491
return Expansion;
491492
}
492493

493-
static SLocEntry get(unsigned Offset, const FileInfo &FI) {
494-
assert(!(Offset & (1u << 31)) && "Offset is too large");
494+
static SLocEntry get(SourceLocation::UIntTy Offset, const FileInfo &FI) {
495+
assert(!(Offset & (1ULL << OffsetBits)) && "Offset is too large");
495496
SLocEntry E;
496497
E.Offset = Offset;
497498
E.IsExpansion = false;
498499
E.File = FI;
499500
return E;
500501
}
501502

502-
static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
503-
assert(!(Offset & (1u << 31)) && "Offset is too large");
503+
static SLocEntry get(SourceLocation::UIntTy Offset,
504+
const ExpansionInfo &Expansion) {
505+
assert(!(Offset & (1ULL << OffsetBits)) && "Offset is too large");
504506
SLocEntry E;
505507
E.Offset = Offset;
506508
E.IsExpansion = true;
@@ -690,17 +692,18 @@ class SourceManager : public RefCountedBase<SourceManager> {
690692
/// The starting offset of the next local SLocEntry.
691693
///
692694
/// This is LocalSLocEntryTable.back().Offset + the size of that entry.
693-
unsigned NextLocalOffset;
695+
SourceLocation::UIntTy NextLocalOffset;
694696

695697
/// The starting offset of the latest batch of loaded SLocEntries.
696698
///
697699
/// This is LoadedSLocEntryTable.back().Offset, except that that entry might
698700
/// not have been loaded, so that value would be unknown.
699-
unsigned CurrentLoadedOffset;
701+
SourceLocation::UIntTy CurrentLoadedOffset;
700702

701-
/// The highest possible offset is 2^31-1, so CurrentLoadedOffset
702-
/// starts at 2^31.
703-
static const unsigned MaxLoadedOffset = 1U << 31U;
703+
/// The highest possible offset is 2^32-1 (2^63-1 for 64-bit source
704+
/// locations), so CurrentLoadedOffset starts at 2^31 (2^63 resp.).
705+
static const SourceLocation::UIntTy MaxLoadedOffset =
706+
1ULL << (8 * sizeof(SourceLocation::UIntTy) - 1);
704707

705708
/// A bitmap that indicates whether the entries of LoadedSLocEntryTable
706709
/// have already been loaded from the external source.
@@ -865,19 +868,21 @@ class SourceManager : public RefCountedBase<SourceManager> {
865868
/// This translates NULL into standard input.
866869
FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
867870
SrcMgr::CharacteristicKind FileCharacter,
868-
int LoadedID = 0, unsigned LoadedOffset = 0);
871+
int LoadedID = 0,
872+
SourceLocation::UIntTy LoadedOffset = 0);
869873

870874
FileID createFileID(FileEntryRef SourceFile, SourceLocation IncludePos,
871875
SrcMgr::CharacteristicKind FileCharacter,
872-
int LoadedID = 0, unsigned LoadedOffset = 0);
876+
int LoadedID = 0,
877+
SourceLocation::UIntTy LoadedOffset = 0);
873878

874879
/// Create a new FileID that represents the specified memory buffer.
875880
///
876881
/// This does no caching of the buffer and takes ownership of the
877882
/// MemoryBuffer, so only pass a MemoryBuffer to this once.
878883
FileID createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer,
879884
SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
880-
int LoadedID = 0, unsigned LoadedOffset = 0,
885+
int LoadedID = 0, SourceLocation::UIntTy LoadedOffset = 0,
881886
SourceLocation IncludeLoc = SourceLocation());
882887

883888
/// Create a new FileID that represents the specified memory buffer.
@@ -886,7 +891,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
886891
/// outlive the SourceManager.
887892
FileID createFileID(const llvm::MemoryBufferRef &Buffer,
888893
SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
889-
int LoadedID = 0, unsigned LoadedOffset = 0,
894+
int LoadedID = 0, SourceLocation::UIntTy LoadedOffset = 0,
890895
SourceLocation IncludeLoc = SourceLocation());
891896

892897
/// Get the FileID for \p SourceFile if it exists. Otherwise, create a
@@ -905,13 +910,11 @@ class SourceManager : public RefCountedBase<SourceManager> {
905910
/// Return a new SourceLocation that encodes the fact
906911
/// that a token from SpellingLoc should actually be referenced from
907912
/// ExpansionLoc.
908-
SourceLocation createExpansionLoc(SourceLocation Loc,
909-
SourceLocation ExpansionLocStart,
910-
SourceLocation ExpansionLocEnd,
911-
unsigned TokLength,
912-
bool ExpansionIsTokenRange = true,
913-
int LoadedID = 0,
914-
unsigned LoadedOffset = 0);
913+
SourceLocation
914+
createExpansionLoc(SourceLocation Loc, SourceLocation ExpansionLocStart,
915+
SourceLocation ExpansionLocEnd, unsigned TokLength,
916+
bool ExpansionIsTokenRange = true, int LoadedID = 0,
917+
SourceLocation::UIntTy LoadedOffset = 0);
915918

916919
/// Return a new SourceLocation that encodes that the token starting
917920
/// at \p TokenStart ends prematurely at \p TokenEnd.
@@ -1098,7 +1101,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
10981101
/// the entry in SLocEntryTable which contains the specified location.
10991102
///
11001103
FileID getFileID(SourceLocation SpellingLoc) const {
1101-
unsigned SLocOffset = SpellingLoc.getOffset();
1104+
SourceLocation::UIntTy SLocOffset = SpellingLoc.getOffset();
11021105

11031106
// If our one-entry cache covers this offset, just return it.
11041107
if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
@@ -1221,7 +1224,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
12211224
if (!Entry)
12221225
return SourceLocation();
12231226

1224-
unsigned GlobalOffset = Entry->getOffset() + Offset;
1227+
SourceLocation::UIntTy GlobalOffset = Entry->getOffset() + Offset;
12251228
return Entry->isFile() ? SourceLocation::getFileLoc(GlobalOffset)
12261229
: SourceLocation::getMacroLoc(GlobalOffset);
12271230
}
@@ -1326,17 +1329,17 @@ class SourceManager : public RefCountedBase<SourceManager> {
13261329
///
13271330
/// If it's true and \p RelativeOffset is non-null, it will be set to the
13281331
/// relative offset of \p Loc inside the chunk.
1329-
bool isInSLocAddrSpace(SourceLocation Loc,
1330-
SourceLocation Start, unsigned Length,
1331-
unsigned *RelativeOffset = nullptr) const {
1332+
bool
1333+
isInSLocAddrSpace(SourceLocation Loc, SourceLocation Start, unsigned Length,
1334+
SourceLocation::UIntTy *RelativeOffset = nullptr) const {
13321335
assert(((Start.getOffset() < NextLocalOffset &&
13331336
Start.getOffset()+Length <= NextLocalOffset) ||
13341337
(Start.getOffset() >= CurrentLoadedOffset &&
13351338
Start.getOffset()+Length < MaxLoadedOffset)) &&
13361339
"Chunk is not valid SLoc address space");
1337-
unsigned LocOffs = Loc.getOffset();
1338-
unsigned BeginOffs = Start.getOffset();
1339-
unsigned EndOffs = BeginOffs + Length;
1340+
SourceLocation::UIntTy LocOffs = Loc.getOffset();
1341+
SourceLocation::UIntTy BeginOffs = Start.getOffset();
1342+
SourceLocation::UIntTy EndOffs = BeginOffs + Length;
13401343
if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
13411344
if (RelativeOffset)
13421345
*RelativeOffset = LocOffs - BeginOffs;
@@ -1352,8 +1355,8 @@ class SourceManager : public RefCountedBase<SourceManager> {
13521355
/// If it's true and \p RelativeOffset is non-null, it will be set to the
13531356
/// offset of \p RHS relative to \p LHS.
13541357
bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
1355-
int *RelativeOffset) const {
1356-
unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
1358+
SourceLocation::IntTy *RelativeOffset) const {
1359+
SourceLocation::UIntTy LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
13571360
bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
13581361
bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
13591362

@@ -1517,7 +1520,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
15171520
/// of FileID) to \p relativeOffset.
15181521
bool isInFileID(SourceLocation Loc, FileID FID,
15191522
unsigned *RelativeOffset = nullptr) const {
1520-
unsigned Offs = Loc.getOffset();
1523+
SourceLocation::UIntTy Offs = Loc.getOffset();
15211524
if (isOffsetInFileID(FID, Offs)) {
15221525
if (RelativeOffset)
15231526
*RelativeOffset = Offs - getSLocEntry(FID).getOffset();
@@ -1636,8 +1639,9 @@ class SourceManager : public RefCountedBase<SourceManager> {
16361639
/// offset in the "source location address space".
16371640
///
16381641
/// Note that we always consider source locations loaded from
1639-
bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const {
1640-
unsigned LHSOffset = LHS.getOffset();
1642+
bool isBeforeInSLocAddrSpace(SourceLocation LHS,
1643+
SourceLocation::UIntTy RHS) const {
1644+
SourceLocation::UIntTy LHSOffset = LHS.getOffset();
16411645
bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
16421646
bool RHSLoaded = RHS >= CurrentLoadedOffset;
16431647
if (LHSLoaded == RHSLoaded)
@@ -1699,7 +1703,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
16991703
return getSLocEntryByID(FID.ID, Invalid);
17001704
}
17011705

1702-
unsigned getNextLocalOffset() const { return NextLocalOffset; }
1706+
SourceLocation::UIntTy getNextLocalOffset() const { return NextLocalOffset; }
17031707

17041708
void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
17051709
assert(LoadedSLocEntryTable.empty() &&
@@ -1713,8 +1717,9 @@ class SourceManager : public RefCountedBase<SourceManager> {
17131717
/// NumSLocEntries will be allocated, which occupy a total of TotalSize space
17141718
/// in the global source view. The lowest ID and the base offset of the
17151719
/// entries will be returned.
1716-
std::pair<int, unsigned>
1717-
AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
1720+
std::pair<int, SourceLocation::UIntTy>
1721+
AllocateLoadedSLocEntries(unsigned NumSLocEntries,
1722+
SourceLocation::UIntTy TotalSize);
17181723

17191724
/// Returns true if \p Loc came from a PCH/Module.
17201725
bool isLoadedSourceLocation(SourceLocation Loc) const {
@@ -1795,14 +1800,15 @@ class SourceManager : public RefCountedBase<SourceManager> {
17951800

17961801
/// Implements the common elements of storing an expansion info struct into
17971802
/// the SLocEntry table and producing a source location that refers to it.
1798-
SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1799-
unsigned TokLength,
1800-
int LoadedID = 0,
1801-
unsigned LoadedOffset = 0);
1803+
SourceLocation
1804+
createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1805+
unsigned TokLength, int LoadedID = 0,
1806+
SourceLocation::UIntTy LoadedOffset = 0);
18021807

18031808
/// Return true if the specified FileID contains the
18041809
/// specified SourceLocation offset. This is a very hot method.
1805-
inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1810+
inline bool isOffsetInFileID(FileID FID,
1811+
SourceLocation::UIntTy SLocOffset) const {
18061812
const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
18071813
// If the entry is after the offset, it can't contain it.
18081814
if (SLocOffset < Entry.getOffset()) return false;
@@ -1836,7 +1842,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
18361842
FileID createFileIDImpl(SrcMgr::ContentCache &File, StringRef Filename,
18371843
SourceLocation IncludePos,
18381844
SrcMgr::CharacteristicKind DirCharacter, int LoadedID,
1839-
unsigned LoadedOffset);
1845+
SourceLocation::UIntTy LoadedOffset);
18401846

18411847
SrcMgr::ContentCache &getOrCreateContentCache(FileEntryRef SourceFile,
18421848
bool isSystemFile = false);
@@ -1845,9 +1851,9 @@ class SourceManager : public RefCountedBase<SourceManager> {
18451851
SrcMgr::ContentCache &
18461852
createMemBufferContentCache(std::unique_ptr<llvm::MemoryBuffer> Buf);
18471853

1848-
FileID getFileIDSlow(unsigned SLocOffset) const;
1849-
FileID getFileIDLocal(unsigned SLocOffset) const;
1850-
FileID getFileIDLoaded(unsigned SLocOffset) const;
1854+
FileID getFileIDSlow(SourceLocation::UIntTy SLocOffset) const;
1855+
FileID getFileIDLocal(SourceLocation::UIntTy SLocOffset) const;
1856+
FileID getFileIDLoaded(SourceLocation::UIntTy SLocOffset) const;
18511857

18521858
SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
18531859
SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;

‎clang/include/clang/Lex/Token.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class IdentifierInfo;
3333
/// information about the SourceRange of the tokens and the type object.
3434
class Token {
3535
/// The location of the token. This is actually a SourceLocation.
36-
unsigned Loc;
36+
SourceLocation::UIntTy Loc;
3737

3838
// Conceptually these next two fields could be in a union. However, this
3939
// causes gcc 4.2 to pessimize LexTokenInternal, a very performance critical
@@ -43,7 +43,7 @@ class Token {
4343
/// UintData - This holds either the length of the token text, when
4444
/// a normal token, or the end of the SourceRange when an annotation
4545
/// token.
46-
unsigned UintData;
46+
SourceLocation::UIntTy UintData;
4747

4848
/// PtrData - This is a union of four different pointer types, which depends
4949
/// on what type of token this is:

0 commit comments

Comments
 (0)
Please sign in to comment.