Skip to content

[clang] NFC: Add alias for std::pair<FileID, unsigned> used in SourceLocation #145711

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

Merged
merged 2 commits into from
Jun 26, 2025
Merged
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
6 changes: 4 additions & 2 deletions clang/include/clang/Basic/SourceLocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class FileID {
int getOpaqueValue() const { return ID; }
};

using FileIDAndOffset = std::pair<FileID, unsigned>;

/// Encodes a location in the source. The SourceManager can decode this
/// to get at the full include stack, line and column information.
///
Expand Down Expand Up @@ -403,7 +405,7 @@ class FullSourceLoc : public SourceLocation {
/// pair, after walking through all expansion records.
///
/// \see SourceManager::getDecomposedExpansionLoc
std::pair<FileID, unsigned> getDecomposedExpansionLoc() const;
FileIDAndOffset getDecomposedExpansionLoc() const;

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

bool isInSystemHeader() const;

Expand Down
28 changes: 12 additions & 16 deletions clang/include/clang/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
///
/// Used to cache results from and speed-up \c getDecomposedIncludedLoc
/// function.
mutable llvm::DenseMap<FileID, std::pair<FileID, unsigned>> IncludedLocMap;
mutable llvm::DenseMap<FileID, FileIDAndOffset> IncludedLocMap;

/// The key value into the IsBeforeInTUCache table.
using IsBeforeInTUCacheKey = std::pair<FileID, FileID>;
Expand Down Expand Up @@ -1269,7 +1269,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
///
/// The first element is the FileID, the second is the offset from the
/// start of the buffer of the location.
std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
FileIDAndOffset getDecomposedLoc(SourceLocation Loc) const {
FileID FID = getFileID(Loc);
auto *Entry = getSLocEntryOrNull(FID);
if (!Entry)
Expand All @@ -1281,8 +1281,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
///
/// If the location is an expansion record, walk through it until we find
/// the final location expanded.
std::pair<FileID, unsigned>
getDecomposedExpansionLoc(SourceLocation Loc) const {
FileIDAndOffset getDecomposedExpansionLoc(SourceLocation Loc) const {
FileID FID = getFileID(Loc);
auto *E = getSLocEntryOrNull(FID);
if (!E)
Expand All @@ -1299,8 +1298,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
///
/// If the location is an expansion record, walk through it until we find
/// its spelling record.
std::pair<FileID, unsigned>
getDecomposedSpellingLoc(SourceLocation Loc) const {
FileIDAndOffset getDecomposedSpellingLoc(SourceLocation Loc) const {
FileID FID = getFileID(Loc);
auto *E = getSLocEntryOrNull(FID);
if (!E)
Expand All @@ -1314,7 +1312,7 @@ class SourceManager : public RefCountedBase<SourceManager> {

/// Returns the "included/expanded in" decomposed location of the given
/// FileID.
std::pair<FileID, unsigned> getDecomposedIncludedLoc(FileID FID) const;
FileIDAndOffset getDecomposedIncludedLoc(FileID FID) const;

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

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

/// Determines whether the two decomposed source location is in the same TU.
bool isInTheSameTranslationUnitImpl(
const std::pair<FileID, unsigned> &LOffs,
const std::pair<FileID, unsigned> &ROffs) const;
bool isInTheSameTranslationUnitImpl(const FileIDAndOffset &LOffs,
const FileIDAndOffset &ROffs) const;

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

std::pair<FileID, unsigned>
FileIDAndOffset
getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
std::pair<FileID, unsigned>
getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
unsigned Offset) const;
FileIDAndOffset getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
unsigned Offset) const;
void computeMacroArgsCache(MacroArgsMap &MacroArgsCache, FileID FID) const;
void associateFileChunkWithMacroArgExp(MacroArgsMap &MacroArgsCache,
FileID FID,
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ RawComment *ASTContext::getRawCommentForDeclNoCacheImpl(

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

// Slow path.
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9989,7 +9989,7 @@ Expected<SourceLocation> ASTImporter::Import(SourceLocation FromLoc) {
SourceManager &FromSM = FromContext.getSourceManager();
bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc);

std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
FileIDAndOffset Decomposed = FromSM.getDecomposedLoc(FromLoc);
Expected<FileID> ToFileIDOrErr = Import(Decomposed.first, IsBuiltin);
if (!ToFileIDOrErr)
return ToFileIDOrErr.takeError();
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/CommentLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ void Lexer::lex(Token &T) {
StringRef Lexer::getSpelling(const Token &Tok,
const SourceManager &SourceMgr) const {
SourceLocation Loc = Tok.getLocation();
std::pair<FileID, unsigned> LocInfo = SourceMgr.getDecomposedLoc(Loc);
FileIDAndOffset LocInfo = SourceMgr.getDecomposedLoc(Loc);

bool InvalidTemp = false;
StringRef File = SourceMgr.getBufferData(LocInfo.first, &InvalidTemp);
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1356,8 +1356,7 @@ StringLiteral::getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);

// Re-lex the token to get its length and original spelling.
std::pair<FileID, unsigned> LocInfo =
SM.getDecomposedLoc(StrTokSpellingLoc);
FileIDAndOffset LocInfo = SM.getDecomposedLoc(StrTokSpellingLoc);
bool Invalid = false;
StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
if (Invalid) {
Expand Down
7 changes: 3 additions & 4 deletions clang/lib/AST/RawCommentList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ comments::FullComment *RawComment::parse(const ASTContext &Context,
static bool onlyWhitespaceBetween(SourceManager &SM,
SourceLocation Loc1, SourceLocation Loc2,
unsigned MaxNewlinesAllowed) {
std::pair<FileID, unsigned> Loc1Info = SM.getDecomposedLoc(Loc1);
std::pair<FileID, unsigned> Loc2Info = SM.getDecomposedLoc(Loc2);
FileIDAndOffset Loc1Info = SM.getDecomposedLoc(Loc1);
FileIDAndOffset Loc2Info = SM.getDecomposedLoc(Loc2);

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

std::pair<FileID, unsigned> Loc =
SourceMgr.getDecomposedLoc(RC.getBeginLoc());
FileIDAndOffset Loc = SourceMgr.getDecomposedLoc(RC.getBeginLoc());

const FileID CommentFile = Loc.first;
const unsigned CommentOffset = Loc.second;
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Analysis/PathDiagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ static bool compareCrossTUSourceLocs(FullSourceLoc XL, FullSourceLoc YL) {
return true;
if (XL.isValid() && YL.isInvalid())
return false;
std::pair<FileID, unsigned> XOffs = XL.getDecomposedLoc();
std::pair<FileID, unsigned> YOffs = YL.getDecomposedLoc();
FileIDAndOffset XOffs = XL.getDecomposedLoc();
FileIDAndOffset YOffs = YL.getDecomposedLoc();
const SourceManager &SM = XL.getManager();
std::pair<bool, bool> InSameTU = SM.isInTheSameTranslationUnit(XOffs, YOffs);
if (InSameTU.first)
Expand Down
9 changes: 4 additions & 5 deletions clang/lib/Basic/Diagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void DiagnosticsEngine::DiagStateMap::append(SourceManager &SrcMgr,
CurDiagState = State;
CurDiagStateLoc = Loc;

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

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

if (F.second.Parent) {
std::pair<FileID, unsigned> Decomp =
SrcMgr.getDecomposedIncludedLoc(ID);
FileIDAndOffset Decomp = SrcMgr.getDecomposedIncludedLoc(ID);
assert(File.ParentOffset == Decomp.second);
llvm::errs() << " parent " << File.Parent << " <FileID "
<< Decomp.first.getHashValue() << "> ";
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Sarif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ static unsigned int adjustColumnPos(FullSourceLoc Loc,
unsigned int TokenLen = 0) {
assert(!Loc.isInvalid() && "invalid Loc when adjusting column position");

std::pair<FileID, unsigned> LocInfo = Loc.getDecomposedExpansionLoc();
FileIDAndOffset LocInfo = Loc.getDecomposedExpansionLoc();
std::optional<MemoryBufferRef> Buf =
Loc.getManager().getBufferOrNone(LocInfo.first);
assert(Buf && "got an invalid buffer for the location's file");
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Basic/SourceLocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ FullSourceLoc FullSourceLoc::getExpansionLoc() const {
return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
}

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

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

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