Skip to content

Commit 8f336eb

Browse files
hokeinAnthony Tran
authored andcommitted
[clang] NFC: Add alias for std::pair<FileID, unsigned> used in SourceLocation (llvm#145711)
Introduce a type alias for the commonly used `std::pair<FileID, unsigned>` to improve code readability, and make it easier for future updates (64-bit source locations).
1 parent ee247df commit 8f336eb

39 files changed

+125
-147
lines changed

clang/include/clang/Basic/SourceLocation.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class FileID {
7070
int getOpaqueValue() const { return ID; }
7171
};
7272

73+
using FileIDAndOffset = std::pair<FileID, unsigned>;
74+
7375
/// Encodes a location in the source. The SourceManager can decode this
7476
/// to get at the full include stack, line and column information.
7577
///
@@ -403,7 +405,7 @@ class FullSourceLoc : public SourceLocation {
403405
/// pair, after walking through all expansion records.
404406
///
405407
/// \see SourceManager::getDecomposedExpansionLoc
406-
std::pair<FileID, unsigned> getDecomposedExpansionLoc() const;
408+
FileIDAndOffset getDecomposedExpansionLoc() const;
407409

408410
unsigned getSpellingLineNumber(bool *Invalid = nullptr) const;
409411
unsigned getSpellingColumnNumber(bool *Invalid = nullptr) const;
@@ -424,7 +426,7 @@ class FullSourceLoc : public SourceLocation {
424426
///
425427
/// The first element is the FileID, the second is the offset from the
426428
/// start of the buffer of the location.
427-
std::pair<FileID, unsigned> getDecomposedLoc() const;
429+
FileIDAndOffset getDecomposedLoc() const;
428430

429431
bool isInSystemHeader() const;
430432

clang/include/clang/Basic/SourceManager.h

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
795795
///
796796
/// Used to cache results from and speed-up \c getDecomposedIncludedLoc
797797
/// function.
798-
mutable llvm::DenseMap<FileID, std::pair<FileID, unsigned>> IncludedLocMap;
798+
mutable llvm::DenseMap<FileID, FileIDAndOffset> IncludedLocMap;
799799

800800
/// The key value into the IsBeforeInTUCache table.
801801
using IsBeforeInTUCacheKey = std::pair<FileID, FileID>;
@@ -1269,7 +1269,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
12691269
///
12701270
/// The first element is the FileID, the second is the offset from the
12711271
/// start of the buffer of the location.
1272-
std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
1272+
FileIDAndOffset getDecomposedLoc(SourceLocation Loc) const {
12731273
FileID FID = getFileID(Loc);
12741274
auto *Entry = getSLocEntryOrNull(FID);
12751275
if (!Entry)
@@ -1281,8 +1281,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
12811281
///
12821282
/// If the location is an expansion record, walk through it until we find
12831283
/// the final location expanded.
1284-
std::pair<FileID, unsigned>
1285-
getDecomposedExpansionLoc(SourceLocation Loc) const {
1284+
FileIDAndOffset getDecomposedExpansionLoc(SourceLocation Loc) const {
12861285
FileID FID = getFileID(Loc);
12871286
auto *E = getSLocEntryOrNull(FID);
12881287
if (!E)
@@ -1299,8 +1298,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
12991298
///
13001299
/// If the location is an expansion record, walk through it until we find
13011300
/// its spelling record.
1302-
std::pair<FileID, unsigned>
1303-
getDecomposedSpellingLoc(SourceLocation Loc) const {
1301+
FileIDAndOffset getDecomposedSpellingLoc(SourceLocation Loc) const {
13041302
FileID FID = getFileID(Loc);
13051303
auto *E = getSLocEntryOrNull(FID);
13061304
if (!E)
@@ -1314,7 +1312,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
13141312

13151313
/// Returns the "included/expanded in" decomposed location of the given
13161314
/// FileID.
1317-
std::pair<FileID, unsigned> getDecomposedIncludedLoc(FileID FID) const;
1315+
FileIDAndOffset getDecomposedIncludedLoc(FileID FID) const;
13181316

13191317
/// Returns the offset from the start of the file that the
13201318
/// specified SourceLocation represents.
@@ -1682,18 +1680,17 @@ class SourceManager : public RefCountedBase<SourceManager> {
16821680
/// are in the same TU. The second bool is true if the first is true
16831681
/// and \p LOffs is before \p ROffs.
16841682
std::pair<bool, bool>
1685-
isInTheSameTranslationUnit(std::pair<FileID, unsigned> &LOffs,
1686-
std::pair<FileID, unsigned> &ROffs) const;
1683+
isInTheSameTranslationUnit(FileIDAndOffset &LOffs,
1684+
FileIDAndOffset &ROffs) const;
16871685

16881686
/// \param Loc a source location in a loaded AST (of a PCH/Module file).
16891687
/// \returns a FileID uniquely identifies the AST of a loaded
16901688
/// module/PCH where `Loc` is at.
16911689
FileID getUniqueLoadedASTFileID(SourceLocation Loc) const;
16921690

16931691
/// Determines whether the two decomposed source location is in the same TU.
1694-
bool isInTheSameTranslationUnitImpl(
1695-
const std::pair<FileID, unsigned> &LOffs,
1696-
const std::pair<FileID, unsigned> &ROffs) const;
1692+
bool isInTheSameTranslationUnitImpl(const FileIDAndOffset &LOffs,
1693+
const FileIDAndOffset &ROffs) const;
16971694

16981695
/// Determines the order of 2 source locations in the "source location
16991696
/// address space".
@@ -1979,11 +1976,10 @@ class SourceManager : public RefCountedBase<SourceManager> {
19791976
SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
19801977
SourceLocation getFileLocSlowCase(SourceLocation Loc) const;
19811978

1982-
std::pair<FileID, unsigned>
1979+
FileIDAndOffset
19831980
getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1984-
std::pair<FileID, unsigned>
1985-
getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1986-
unsigned Offset) const;
1981+
FileIDAndOffset getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1982+
unsigned Offset) const;
19871983
void computeMacroArgsCache(MacroArgsMap &MacroArgsCache, FileID FID) const;
19881984
void associateFileChunkWithMacroArgExp(MacroArgsMap &MacroArgsCache,
19891985
FileID FID,

clang/lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ RawComment *ASTContext::getRawCommentForDeclNoCacheImpl(
248248

249249
// Decompose the location for the declaration and find the beginning of the
250250
// file buffer.
251-
const std::pair<FileID, unsigned> DeclLocDecomp =
251+
const FileIDAndOffset DeclLocDecomp =
252252
SourceMgr.getDecomposedLoc(RepresentativeLocForDecl);
253253

254254
// Slow path.

clang/lib/AST/ASTImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9989,7 +9989,7 @@ Expected<SourceLocation> ASTImporter::Import(SourceLocation FromLoc) {
99899989
SourceManager &FromSM = FromContext.getSourceManager();
99909990
bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc);
99919991

9992-
std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
9992+
FileIDAndOffset Decomposed = FromSM.getDecomposedLoc(FromLoc);
99939993
Expected<FileID> ToFileIDOrErr = Import(Decomposed.first, IsBuiltin);
99949994
if (!ToFileIDOrErr)
99959995
return ToFileIDOrErr.takeError();

clang/lib/AST/CommentLexer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ void Lexer::lex(Token &T) {
904904
StringRef Lexer::getSpelling(const Token &Tok,
905905
const SourceManager &SourceMgr) const {
906906
SourceLocation Loc = Tok.getLocation();
907-
std::pair<FileID, unsigned> LocInfo = SourceMgr.getDecomposedLoc(Loc);
907+
FileIDAndOffset LocInfo = SourceMgr.getDecomposedLoc(Loc);
908908

909909
bool InvalidTemp = false;
910910
StringRef File = SourceMgr.getBufferData(LocInfo.first, &InvalidTemp);

clang/lib/AST/Expr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,8 +1356,7 @@ StringLiteral::getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
13561356
SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
13571357

13581358
// Re-lex the token to get its length and original spelling.
1359-
std::pair<FileID, unsigned> LocInfo =
1360-
SM.getDecomposedLoc(StrTokSpellingLoc);
1359+
FileIDAndOffset LocInfo = SM.getDecomposedLoc(StrTokSpellingLoc);
13611360
bool Invalid = false;
13621361
StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
13631362
if (Invalid) {

clang/lib/AST/RawCommentList.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ comments::FullComment *RawComment::parse(const ASTContext &Context,
224224
static bool onlyWhitespaceBetween(SourceManager &SM,
225225
SourceLocation Loc1, SourceLocation Loc2,
226226
unsigned MaxNewlinesAllowed) {
227-
std::pair<FileID, unsigned> Loc1Info = SM.getDecomposedLoc(Loc1);
228-
std::pair<FileID, unsigned> Loc2Info = SM.getDecomposedLoc(Loc2);
227+
FileIDAndOffset Loc1Info = SM.getDecomposedLoc(Loc1);
228+
FileIDAndOffset Loc2Info = SM.getDecomposedLoc(Loc2);
229229

230230
// Question does not make sense if locations are in different files.
231231
if (Loc1Info.first != Loc2Info.first)
@@ -279,8 +279,7 @@ void RawCommentList::addComment(const RawComment &RC,
279279
if (RC.isOrdinary() && !CommentOpts.ParseAllComments)
280280
return;
281281

282-
std::pair<FileID, unsigned> Loc =
283-
SourceMgr.getDecomposedLoc(RC.getBeginLoc());
282+
FileIDAndOffset Loc = SourceMgr.getDecomposedLoc(RC.getBeginLoc());
284283

285284
const FileID CommentFile = Loc.first;
286285
const unsigned CommentOffset = Loc.second;

clang/lib/Analysis/PathDiagnostic.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ static bool compareCrossTUSourceLocs(FullSourceLoc XL, FullSourceLoc YL) {
323323
return true;
324324
if (XL.isValid() && YL.isInvalid())
325325
return false;
326-
std::pair<FileID, unsigned> XOffs = XL.getDecomposedLoc();
327-
std::pair<FileID, unsigned> YOffs = YL.getDecomposedLoc();
326+
FileIDAndOffset XOffs = XL.getDecomposedLoc();
327+
FileIDAndOffset YOffs = YL.getDecomposedLoc();
328328
const SourceManager &SM = XL.getManager();
329329
std::pair<bool, bool> InSameTU = SM.isInTheSameTranslationUnit(XOffs, YOffs);
330330
if (InSameTU.first)

clang/lib/Basic/Diagnostic.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ void DiagnosticsEngine::DiagStateMap::append(SourceManager &SrcMgr,
173173
CurDiagState = State;
174174
CurDiagStateLoc = Loc;
175175

176-
std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedLoc(Loc);
176+
FileIDAndOffset Decomp = SrcMgr.getDecomposedLoc(Loc);
177177
unsigned Offset = Decomp.second;
178178
for (File *F = getFile(SrcMgr, Decomp.first); F;
179179
Offset = F->ParentOffset, F = F->Parent) {
@@ -199,7 +199,7 @@ DiagnosticsEngine::DiagStateMap::lookup(SourceManager &SrcMgr,
199199
if (Files.empty())
200200
return FirstDiagState;
201201

202-
std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedLoc(Loc);
202+
FileIDAndOffset Decomp = SrcMgr.getDecomposedLoc(Loc);
203203
const File *F = getFile(SrcMgr, Decomp.first);
204204
return F->lookup(Decomp.second);
205205
}
@@ -226,7 +226,7 @@ DiagnosticsEngine::DiagStateMap::getFile(SourceManager &SrcMgr,
226226
// We created a new File; look up the diagnostic state at the start of it and
227227
// initialize it.
228228
if (ID.isValid()) {
229-
std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedIncludedLoc(ID);
229+
FileIDAndOffset Decomp = SrcMgr.getDecomposedIncludedLoc(ID);
230230
F.Parent = getFile(SrcMgr, Decomp.first);
231231
F.ParentOffset = Decomp.second;
232232
F.StateTransitions.push_back({F.Parent->lookup(Decomp.second), 0});
@@ -263,8 +263,7 @@ void DiagnosticsEngine::DiagStateMap::dump(SourceManager &SrcMgr,
263263
<< ">: " << SrcMgr.getBufferOrFake(ID).getBufferIdentifier();
264264

265265
if (F.second.Parent) {
266-
std::pair<FileID, unsigned> Decomp =
267-
SrcMgr.getDecomposedIncludedLoc(ID);
266+
FileIDAndOffset Decomp = SrcMgr.getDecomposedIncludedLoc(ID);
268267
assert(File.ParentOffset == Decomp.second);
269268
llvm::errs() << " parent " << File.Parent << " <FileID "
270269
<< Decomp.first.getHashValue() << "> ";

clang/lib/Basic/Sarif.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static unsigned int adjustColumnPos(FullSourceLoc Loc,
117117
unsigned int TokenLen = 0) {
118118
assert(!Loc.isInvalid() && "invalid Loc when adjusting column position");
119119

120-
std::pair<FileID, unsigned> LocInfo = Loc.getDecomposedExpansionLoc();
120+
FileIDAndOffset LocInfo = Loc.getDecomposedExpansionLoc();
121121
std::optional<MemoryBufferRef> Buf =
122122
Loc.getManager().getBufferOrNone(LocInfo.first);
123123
assert(Buf && "got an invalid buffer for the location's file");

clang/lib/Basic/SourceLocation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ FullSourceLoc FullSourceLoc::getExpansionLoc() const {
164164
return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
165165
}
166166

167-
std::pair<FileID, unsigned> FullSourceLoc::getDecomposedExpansionLoc() const {
167+
FileIDAndOffset FullSourceLoc::getDecomposedExpansionLoc() const {
168168
return SrcMgr->getDecomposedExpansionLoc(*this);
169169
}
170170

@@ -274,6 +274,6 @@ StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
274274
return SrcMgr->getBufferData(SrcMgr->getFileID(*this), Invalid);
275275
}
276276

277-
std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
277+
FileIDAndOffset FullSourceLoc::getDecomposedLoc() const {
278278
return SrcMgr->getDecomposedLoc(*this);
279279
}

0 commit comments

Comments
 (0)