Skip to content

[clang] Unify SourceLocation and IdentifierInfo* pair-like data structures to IdentifierLoc #135808

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 4 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions clang/include/clang/AST/OpenACCClause.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ inline bool operator!=(const OpenACCBindClause &LHS,
return !(LHS == RHS);
}

using DeviceTypeArgument = std::pair<IdentifierInfo *, SourceLocation>;
using DeviceTypeArgument = IdentifierLoc;
/// A 'device_type' or 'dtype' clause, takes a list of either an 'asterisk' or
/// an identifier. The 'asterisk' means 'the rest'.
class OpenACCDeviceTypeClause final
Expand Down Expand Up @@ -302,7 +302,7 @@ class OpenACCDeviceTypeClause final
}
bool hasAsterisk() const {
return getArchitectures().size() > 0 &&
getArchitectures()[0].first == nullptr;
getArchitectures()[0].getIdentifierInfo() == nullptr;
}

ArrayRef<DeviceTypeArgument> getArchitectures() const {
Expand Down
3 changes: 0 additions & 3 deletions clang/include/clang/Basic/IdentifierTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ inline bool isReservedInAllContexts(ReservedIdentifierStatus Status) {
Status != ReservedIdentifierStatus::StartsWithUnderscoreAndIsExternC;
}

/// A simple pair of identifier info and location.
using IdentifierLocPair = std::pair<IdentifierInfo *, SourceLocation>;

/// IdentifierInfo and other related classes are aligned to
/// 8 bytes so that DeclarationName can use the lower 3 bits
/// of a pointer to one of these classes.
Expand Down
24 changes: 24 additions & 0 deletions clang/include/clang/Basic/SourceLocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ template <typename T, typename Enable> struct FoldingSetTrait;

namespace clang {

class IdentifierInfo;
class SourceManager;

/// An opaque identifier used by SourceManager which refers to a
Expand Down Expand Up @@ -466,6 +467,29 @@ class FullSourceLoc : public SourceLocation {
}
};

/// A simple pair of identifier info and location.
class IdentifierLoc {
SourceLocation Loc;
IdentifierInfo *II = nullptr;

public:
IdentifierLoc() = default;
IdentifierLoc(SourceLocation L, IdentifierInfo *Ident) : Loc(L), II(Ident) {}

void setLoc(SourceLocation L) { Loc = L; }
void setIdentifierInfo(IdentifierInfo *Ident) { II = Ident; }
SourceLocation getLoc() const { return Loc; }
IdentifierInfo *getIdentifierInfo() const { return II; }

bool operator==(const IdentifierLoc &X) const {
return Loc == X.Loc && II == X.II;
}

bool operator!=(const IdentifierLoc &X) const {
return Loc != X.Loc || II != X.II;
}
};

} // namespace clang

namespace llvm {
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Lex/ModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class IdentifierInfo;

/// A sequence of identifier/location pairs used to describe a particular
/// module or submodule, e.g., std.vector.
using ModuleIdPath = ArrayRef<std::pair<IdentifierInfo *, SourceLocation>>;
using ModuleIdPath = ArrayRef<IdentifierLoc>;

/// Describes the result of attempting to load a module.
class ModuleLoadResult {
Expand Down
9 changes: 4 additions & 5 deletions clang/include/clang/Lex/Preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ class Preprocessor {
SourceLocation ModuleImportLoc;

/// The import path for named module that we're currently processing.
SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> NamedModuleImportPath;
SmallVector<IdentifierLoc, 2> NamedModuleImportPath;

llvm::DenseMap<FileID, SmallVector<const char *>> CheckPoints;
unsigned CheckPointCounter = 0;
Expand Down Expand Up @@ -622,7 +622,7 @@ class Preprocessor {

/// The identifier and source location of the currently-active
/// \#pragma clang arc_cf_code_audited begin.
std::pair<IdentifierInfo *, SourceLocation> PragmaARCCFCodeAuditedInfo;
IdentifierLoc PragmaARCCFCodeAuditedInfo;

/// The source location of the currently-active
/// \#pragma clang assume_nonnull begin.
Expand Down Expand Up @@ -1998,16 +1998,15 @@ class Preprocessor {
/// arc_cf_code_audited begin.
///
/// Returns an invalid location if there is no such pragma active.
std::pair<IdentifierInfo *, SourceLocation>
getPragmaARCCFCodeAuditedInfo() const {
IdentifierLoc getPragmaARCCFCodeAuditedInfo() const {
return PragmaARCCFCodeAuditedInfo;
}

/// Set the location of the currently-active \#pragma clang
/// arc_cf_code_audited begin. An invalid location ends the pragma.
void setPragmaARCCFCodeAuditedInfo(IdentifierInfo *Ident,
SourceLocation Loc) {
PragmaARCCFCodeAuditedInfo = {Ident, Loc};
PragmaARCCFCodeAuditedInfo = IdentifierLoc(Loc, Ident);
}

/// The location of the currently-active \#pragma clang
Expand Down
1 change: 0 additions & 1 deletion clang/include/clang/Parse/LoopHint.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace clang {

class Expr;
struct IdentifierLoc;

/// Loop optimization hint for loop and unroll pragmas.
struct LoopHint {
Expand Down
13 changes: 5 additions & 8 deletions clang/include/clang/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1725,8 +1725,8 @@ class Parser : public CodeCompletionHandler {
ObjCTypeParamList *parseObjCTypeParamList();
ObjCTypeParamList *parseObjCTypeParamListOrProtocolRefs(
ObjCTypeParamListScope &Scope, SourceLocation &lAngleLoc,
SmallVectorImpl<IdentifierLocPair> &protocolIdents,
SourceLocation &rAngleLoc, bool mayBeProtocolList = true);
SmallVectorImpl<IdentifierLoc> &protocolIdents, SourceLocation &rAngleLoc,
bool mayBeProtocolList = true);

void HelperActionsForIvarDeclarations(ObjCContainerDecl *interfaceDecl,
SourceLocation atLoc,
Expand Down Expand Up @@ -3816,8 +3816,7 @@ class Parser : public CodeCompletionHandler {
SourceLocation Loc,
llvm::SmallVectorImpl<Expr *> &IntExprs);
/// Parses the 'device-type-list', which is a list of identifiers.
bool ParseOpenACCDeviceTypeList(
llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>> &Archs);
bool ParseOpenACCDeviceTypeList(llvm::SmallVector<IdentifierLoc> &Archs);
/// Parses the 'async-argument', which is an integral value with two
/// 'special' values that are likely negative (but come from Macros).
OpenACCIntExprParseResult ParseOpenACCAsyncArgument(OpenACCDirectiveKind DK,
Expand Down Expand Up @@ -3949,10 +3948,8 @@ class Parser : public CodeCompletionHandler {
return false;
}

bool ParseModuleName(
SourceLocation UseLoc,
SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>> &Path,
bool IsImport);
bool ParseModuleName(SourceLocation UseLoc,
SmallVectorImpl<IdentifierLoc> &Path, bool IsImport);

//===--------------------------------------------------------------------===//
// C++11/G++: Type Traits [Type-Traits.html in the GCC manual]
Expand Down
10 changes: 0 additions & 10 deletions clang/include/clang/Sema/ParsedAttr.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class LangOptions;
class Sema;
class Stmt;
class TargetInfo;
struct IdentifierLoc;

/// Represents information about a change in availability for
/// an entity, which is part of the encoding of the 'availability'
Expand Down Expand Up @@ -99,15 +98,6 @@ struct PropertyData {

} // namespace detail

/// Wraps an identifier and optional source location for the identifier.
struct IdentifierLoc {
SourceLocation Loc;
IdentifierInfo *Ident;

static IdentifierLoc *create(ASTContext &Ctx, SourceLocation Loc,
IdentifierInfo *Ident);
};

/// A union of the various pointer types that can be passed to an
/// ParsedAttr as an argument.
using ArgsUnion = llvm::PointerUnion<Expr *, IdentifierLoc *>;
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ enum class LangAS : unsigned int;
class LocalInstantiationScope;
class LookupResult;
class MangleNumberingContext;
typedef ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> ModuleIdPath;
typedef ArrayRef<IdentifierLoc> ModuleIdPath;
class ModuleLoader;
class MultiLevelTemplateArgumentList;
struct NormalizedConstraint;
Expand Down
3 changes: 1 addition & 2 deletions clang/include/clang/Sema/SemaCodeCompletion.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ class SemaCodeCompletion : public SemaBase {
void CodeCompleteObjCForCollection(Scope *S, DeclGroupPtrTy IterationVar);
void CodeCompleteObjCSelector(Scope *S,
ArrayRef<const IdentifierInfo *> SelIdents);
void
CodeCompleteObjCProtocolReferences(ArrayRef<IdentifierLocPair> Protocols);
void CodeCompleteObjCProtocolReferences(ArrayRef<IdentifierLoc> Protocols);
void CodeCompleteObjCProtocolDecl(Scope *S);
void CodeCompleteObjCInterfaceDecl(Scope *S);
void CodeCompleteObjCClassForwardDecl(Scope *S);
Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/Sema/SemaObjC.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,11 @@ class SemaObjC : public SemaBase {

DeclGroupPtrTy
ActOnForwardProtocolDeclaration(SourceLocation AtProtoclLoc,
ArrayRef<IdentifierLocPair> IdentList,
ArrayRef<IdentifierLoc> IdentList,
const ParsedAttributesView &attrList);

void FindProtocolDeclaration(bool WarnOnDeclarations, bool ForObjCContainer,
ArrayRef<IdentifierLocPair> ProtocolId,
ArrayRef<IdentifierLoc> ProtocolId,
SmallVectorImpl<Decl *> &Protocols);

void DiagnoseTypeArgsAndProtocols(IdentifierInfo *ProtocolId,
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Sema/SemaOpenACC.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class SemaOpenACC : public SemaBase {
} LoopWithoutSeqInfo;

// Redeclaration of the version in OpenACCClause.h.
using DeviceTypeArgument = std::pair<IdentifierInfo *, SourceLocation>;
using DeviceTypeArgument = IdentifierLoc;

/// A type to represent all the data for an OpenACC Clause that has been
/// parsed, but not yet created/semantically analyzed. This is effectively a
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/AST/OpenACCClause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,10 +891,10 @@ void OpenACCClausePrinter::VisitDeviceTypeClause(
OS << "(";
llvm::interleaveComma(C.getArchitectures(), OS,
[&](const DeviceTypeArgument &Arch) {
if (Arch.first == nullptr)
if (Arch.getIdentifierInfo() == nullptr)
OS << "*";
else
OS << Arch.first->getName();
OS << Arch.getIdentifierInfo()->getName();
});
OS << ")";
}
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/AST/TextNodeDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,10 @@ void TextNodeDumper::Visit(const OpenACCClause *C) {
llvm::interleaveComma(
cast<OpenACCDeviceTypeClause>(C)->getArchitectures(), OS,
[&](const DeviceTypeArgument &Arch) {
if (Arch.first == nullptr)
if (Arch.getIdentifierInfo() == nullptr)
OS << "*";
else
OS << Arch.first->getName();
OS << Arch.getIdentifierInfo()->getName();
});
OS << ")";
break;
Expand Down
53 changes: 28 additions & 25 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Sema/CodeCompleteConsumer.h"
#include "clang/Sema/ParsedAttr.h"
#include "clang/Sema/Sema.h"
#include "clang/Serialization/ASTReader.h"
#include "clang/Serialization/GlobalModuleIndex.h"
Expand Down Expand Up @@ -2028,8 +2029,8 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
Module::NameVisibilityKind Visibility,
bool IsInclusionDirective) {
// Determine what file we're searching from.
StringRef ModuleName = Path[0].first->getName();
SourceLocation ModuleNameLoc = Path[0].second;
StringRef ModuleName = Path[0].getIdentifierInfo()->getName();
SourceLocation ModuleNameLoc = Path[0].getLoc();

// If we've already handled this import, just return the cached result.
// This one-element cache is important to eliminate redundant diagnostics
Expand All @@ -2045,7 +2046,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
// If we don't already have information on this module, load the module now.
Module *Module = nullptr;
ModuleMap &MM = getPreprocessor().getHeaderSearchInfo().getModuleMap();
if (auto MaybeModule = MM.getCachedModuleLoad(*Path[0].first)) {
if (auto MaybeModule = MM.getCachedModuleLoad(*Path[0].getIdentifierInfo())) {
// Use the cached result, which may be nullptr.
Module = *MaybeModule;
// Config macros are already checked before building a module, but they need
Expand All @@ -2065,7 +2066,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
// * `Preprocessor::HandleHeaderIncludeOrImport` will never call this
// function as the `#include` or `#import` is textual.

MM.cacheModuleLoad(*Path[0].first, Module);
MM.cacheModuleLoad(*Path[0].getIdentifierInfo(), Module);
} else {
ModuleLoadResult Result = findOrCompileModuleAndReadAST(
ModuleName, ImportLoc, ModuleNameLoc, IsInclusionDirective);
Expand All @@ -2074,7 +2075,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
if (!Result)
DisableGeneratingGlobalModuleIndex = true;
Module = Result;
MM.cacheModuleLoad(*Path[0].first, Module);
MM.cacheModuleLoad(*Path[0].getIdentifierInfo(), Module);
}

// If we never found the module, fail. Otherwise, verify the module and link
Expand All @@ -2086,7 +2087,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
// a submodule.
bool MapPrivateSubModToTopLevel = false;
for (unsigned I = 1, N = Path.size(); I != N; ++I) {
StringRef Name = Path[I].first->getName();
StringRef Name = Path[I].getIdentifierInfo()->getName();
clang::Module *Sub = Module->findSubmodule(Name);

// If the user is requesting Foo.Private and it doesn't exist, try to
Expand All @@ -2097,10 +2098,10 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
SmallString<128> PrivateModule(Module->Name);
PrivateModule.append("_Private");

SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> PrivPath;
SmallVector<IdentifierLoc, 2> PrivPath;
auto &II = PP->getIdentifierTable().get(
PrivateModule, PP->getIdentifierInfo(Module->Name)->getTokenID());
PrivPath.push_back(std::make_pair(&II, Path[0].second));
PrivPath.emplace_back(Path[0].getLoc(), &II);

std::string FileName;
// If there is a modulemap module or prebuilt module, load it.
Expand All @@ -2114,11 +2115,12 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
PP->markClangModuleAsAffecting(Module);
if (!getDiagnostics().isIgnored(
diag::warn_no_priv_submodule_use_toplevel, ImportLoc)) {
getDiagnostics().Report(Path[I].second,
getDiagnostics().Report(Path[I].getLoc(),
diag::warn_no_priv_submodule_use_toplevel)
<< Path[I].first << Module->getFullModuleName() << PrivateModule
<< SourceRange(Path[0].second, Path[I].second)
<< FixItHint::CreateReplacement(SourceRange(Path[0].second),
<< Path[I].getIdentifierInfo() << Module->getFullModuleName()
<< PrivateModule
<< SourceRange(Path[0].getLoc(), Path[I].getLoc())
<< FixItHint::CreateReplacement(SourceRange(Path[0].getLoc()),
PrivateModule);
getDiagnostics().Report(Sub->DefinitionLoc,
diag::note_private_top_level_defined);
Expand Down Expand Up @@ -2147,10 +2149,11 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,

// If there was a clear winner, user it.
if (Best.size() == 1) {
getDiagnostics().Report(Path[I].second, diag::err_no_submodule_suggest)
<< Path[I].first << Module->getFullModuleName() << Best[0]
<< SourceRange(Path[0].second, Path[I - 1].second)
<< FixItHint::CreateReplacement(SourceRange(Path[I].second),
getDiagnostics().Report(Path[I].getLoc(),
diag::err_no_submodule_suggest)
<< Path[I].getIdentifierInfo() << Module->getFullModuleName()
<< Best[0] << SourceRange(Path[0].getLoc(), Path[I - 1].getLoc())
<< FixItHint::CreateReplacement(SourceRange(Path[I].getLoc()),
Best[0]);

Sub = Module->findSubmodule(Best[0]);
Expand All @@ -2160,9 +2163,9 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
if (!Sub) {
// No submodule by this name. Complain, and don't look for further
// submodules.
getDiagnostics().Report(Path[I].second, diag::err_no_submodule)
<< Path[I].first << Module->getFullModuleName()
<< SourceRange(Path[0].second, Path[I - 1].second);
getDiagnostics().Report(Path[I].getLoc(), diag::err_no_submodule)
<< Path[I].getIdentifierInfo() << Module->getFullModuleName()
<< SourceRange(Path[0].getLoc(), Path[I - 1].getLoc());
break;
}

Expand All @@ -2180,8 +2183,8 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
// FIXME: Should we detect this at module load time? It seems fairly
// expensive (and rare).
getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule)
<< Module->getFullModuleName()
<< SourceRange(Path.front().second, Path.back().second);
<< Module->getFullModuleName()
<< SourceRange(Path.front().getLoc(), Path.back().getLoc());

return ModuleLoadResult(Module, ModuleLoadResult::MissingExpected);
}
Expand All @@ -2190,7 +2193,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
if (Preprocessor::checkModuleIsAvailable(getLangOpts(), getTarget(),
*Module, getDiagnostics())) {
getDiagnostics().Report(ImportLoc, diag::note_module_import_here)
<< SourceRange(Path.front().second, Path.back().second);
<< SourceRange(Path.front().getLoc(), Path.back().getLoc());
LastModuleImportLoc = ImportLoc;
LastModuleImportResult = ModuleLoadResult();
return ModuleLoadResult();
Expand Down Expand Up @@ -2316,9 +2319,9 @@ GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(
Module *TheModule = I->second;
OptionalFileEntryRef Entry = TheModule->getASTFile();
if (!Entry) {
SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
Path.push_back(std::make_pair(
getPreprocessor().getIdentifierInfo(TheModule->Name), TriggerLoc));
SmallVector<IdentifierLoc, 2> Path;
Path.emplace_back(TriggerLoc,
getPreprocessor().getIdentifierInfo(TheModule->Name));
std::reverse(Path.begin(), Path.end());
// Load a module as hidden. This also adds it to the global index.
loadModule(TheModule->DefinitionLoc, Path, Module::Hidden, false);
Expand Down
Loading
Loading