Skip to content

Commit d3153ad

Browse files
authored
[clang] Unify SourceLocation and IdentifierInfo* pair-like data structures to IdentifierLoc (llvm#135808)
I found this issue when I working on llvm#107168. Currently we have many similiar data structures like: - `std::pair<IdentifierInfo *, SourceLocation>`. - Element type of `ModuleIdPath`. - `IdentifierLocPair`. - `IdentifierLoc`. This PR unify these data structures to `IdentifierLoc`, moved `IdentifierLoc` definition to SourceLocation.h, and deleted other similer data structures. --------- Signed-off-by: yronglin <[email protected]>
1 parent fe4a31d commit d3153ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+399
-384
lines changed

clang-tools-extra/pp-trace/PPCallbacksTracker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,8 @@ void PPCallbacksTracker::appendArgument(const char *Name, ModuleIdPath Value) {
547547
if (I)
548548
SS << ", ";
549549
SS << "{"
550-
<< "Name: " << Value[I].first->getName() << ", "
551-
<< "Loc: " << getSourceLocationString(PP, Value[I].second) << "}";
550+
<< "Name: " << Value[I].getIdentifierInfo()->getName() << ", "
551+
<< "Loc: " << getSourceLocationString(PP, Value[I].getLoc()) << "}";
552552
}
553553
SS << "]";
554554
appendArgument(Name, SS.str());

clang/include/clang/AST/OpenACCClause.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ inline bool operator!=(const OpenACCBindClause &LHS,
258258
return !(LHS == RHS);
259259
}
260260

261-
using DeviceTypeArgument = std::pair<IdentifierInfo *, SourceLocation>;
261+
using DeviceTypeArgument = IdentifierLoc;
262262
/// A 'device_type' or 'dtype' clause, takes a list of either an 'asterisk' or
263263
/// an identifier. The 'asterisk' means 'the rest'.
264264
class OpenACCDeviceTypeClause final
@@ -280,16 +280,16 @@ class OpenACCDeviceTypeClause final
280280
"Invalid clause kind for device-type");
281281

282282
assert(!llvm::any_of(Archs, [](const DeviceTypeArgument &Arg) {
283-
return Arg.second.isInvalid();
283+
return Arg.getLoc().isInvalid();
284284
}) && "Invalid SourceLocation for an argument");
285285

286-
assert(
287-
(Archs.size() == 1 || !llvm::any_of(Archs,
288-
[](const DeviceTypeArgument &Arg) {
289-
return Arg.first == nullptr;
290-
})) &&
291-
"Only a single asterisk version is permitted, and must be the "
292-
"only one");
286+
assert((Archs.size() == 1 ||
287+
!llvm::any_of(Archs,
288+
[](const DeviceTypeArgument &Arg) {
289+
return Arg.getIdentifierInfo() == nullptr;
290+
})) &&
291+
"Only a single asterisk version is permitted, and must be the "
292+
"only one");
293293

294294
std::uninitialized_copy(Archs.begin(), Archs.end(),
295295
getTrailingObjects<DeviceTypeArgument>());
@@ -302,7 +302,7 @@ class OpenACCDeviceTypeClause final
302302
}
303303
bool hasAsterisk() const {
304304
return getArchitectures().size() > 0 &&
305-
getArchitectures()[0].first == nullptr;
305+
getArchitectures()[0].getIdentifierInfo() == nullptr;
306306
}
307307

308308
ArrayRef<DeviceTypeArgument> getArchitectures() const {

clang/include/clang/Basic/IdentifierTable.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "clang/Basic/Builtins.h"
1919
#include "clang/Basic/DiagnosticIDs.h"
2020
#include "clang/Basic/LLVM.h"
21+
#include "clang/Basic/SourceLocation.h"
2122
#include "clang/Basic/TokenKinds.h"
2223
#include "llvm/ADT/DenseMapInfo.h"
2324
#include "llvm/ADT/FoldingSet.h"
@@ -76,9 +77,6 @@ inline bool isReservedInAllContexts(ReservedIdentifierStatus Status) {
7677
Status != ReservedIdentifierStatus::StartsWithUnderscoreAndIsExternC;
7778
}
7879

79-
/// A simple pair of identifier info and location.
80-
using IdentifierLocPair = std::pair<IdentifierInfo *, SourceLocation>;
81-
8280
/// IdentifierInfo and other related classes are aligned to
8381
/// 8 bytes so that DeclarationName can use the lower 3 bits
8482
/// of a pointer to one of these classes.
@@ -1165,6 +1163,28 @@ class SelectorTable {
11651163
static std::string getPropertyNameFromSetterSelector(Selector Sel);
11661164
};
11671165

1166+
/// A simple pair of identifier info and location.
1167+
class IdentifierLoc {
1168+
SourceLocation Loc;
1169+
IdentifierInfo *II = nullptr;
1170+
1171+
public:
1172+
IdentifierLoc() = default;
1173+
IdentifierLoc(SourceLocation L, IdentifierInfo *Ident) : Loc(L), II(Ident) {}
1174+
1175+
void setLoc(SourceLocation L) { Loc = L; }
1176+
void setIdentifierInfo(IdentifierInfo *Ident) { II = Ident; }
1177+
SourceLocation getLoc() const { return Loc; }
1178+
IdentifierInfo *getIdentifierInfo() const { return II; }
1179+
1180+
bool operator==(const IdentifierLoc &X) const {
1181+
return Loc == X.Loc && II == X.II;
1182+
}
1183+
1184+
bool operator!=(const IdentifierLoc &X) const {
1185+
return Loc != X.Loc || II != X.II;
1186+
}
1187+
};
11681188
} // namespace clang
11691189

11701190
namespace llvm {

clang/include/clang/Lex/ModuleLoader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_CLANG_LEX_MODULELOADER_H
1515
#define LLVM_CLANG_LEX_MODULELOADER_H
1616

17+
#include "clang/Basic/IdentifierTable.h"
1718
#include "clang/Basic/LLVM.h"
1819
#include "clang/Basic/Module.h"
1920
#include "clang/Basic/SourceLocation.h"
@@ -29,7 +30,7 @@ class IdentifierInfo;
2930

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

3435
/// Describes the result of attempting to load a module.
3536
class ModuleLoadResult {

clang/include/clang/Lex/PPCallbacks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define LLVM_CLANG_LEX_PPCALLBACKS_H
1616

1717
#include "clang/Basic/DiagnosticIDs.h"
18+
#include "clang/Basic/IdentifierTable.h"
1819
#include "clang/Basic/SourceLocation.h"
1920
#include "clang/Basic/SourceManager.h"
2021
#include "clang/Lex/ModuleLoader.h"

clang/include/clang/Lex/Preprocessor.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ class Preprocessor {
327327
SourceLocation ModuleImportLoc;
328328

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

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

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

627627
/// The source location of the currently-active
628628
/// \#pragma clang assume_nonnull begin.
@@ -1998,16 +1998,15 @@ class Preprocessor {
19981998
/// arc_cf_code_audited begin.
19991999
///
20002000
/// Returns an invalid location if there is no such pragma active.
2001-
std::pair<IdentifierInfo *, SourceLocation>
2002-
getPragmaARCCFCodeAuditedInfo() const {
2001+
IdentifierLoc getPragmaARCCFCodeAuditedInfo() const {
20032002
return PragmaARCCFCodeAuditedInfo;
20042003
}
20052004

20062005
/// Set the location of the currently-active \#pragma clang
20072006
/// arc_cf_code_audited begin. An invalid location ends the pragma.
20082007
void setPragmaARCCFCodeAuditedInfo(IdentifierInfo *Ident,
20092008
SourceLocation Loc) {
2010-
PragmaARCCFCodeAuditedInfo = {Ident, Loc};
2009+
PragmaARCCFCodeAuditedInfo = IdentifierLoc(Loc, Ident);
20112010
}
20122011

20132012
/// The location of the currently-active \#pragma clang

clang/include/clang/Parse/LoopHint.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
#ifndef LLVM_CLANG_PARSE_LOOPHINT_H
1010
#define LLVM_CLANG_PARSE_LOOPHINT_H
1111

12+
#include "clang/Basic/IdentifierTable.h"
1213
#include "clang/Basic/SourceLocation.h"
1314

1415
namespace clang {
1516

1617
class Expr;
17-
struct IdentifierLoc;
1818

1919
/// Loop optimization hint for loop and unroll pragmas.
2020
struct LoopHint {

clang/include/clang/Parse/Parser.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,8 +1725,8 @@ class Parser : public CodeCompletionHandler {
17251725
ObjCTypeParamList *parseObjCTypeParamList();
17261726
ObjCTypeParamList *parseObjCTypeParamListOrProtocolRefs(
17271727
ObjCTypeParamListScope &Scope, SourceLocation &lAngleLoc,
1728-
SmallVectorImpl<IdentifierLocPair> &protocolIdents,
1729-
SourceLocation &rAngleLoc, bool mayBeProtocolList = true);
1728+
SmallVectorImpl<IdentifierLoc> &protocolIdents, SourceLocation &rAngleLoc,
1729+
bool mayBeProtocolList = true);
17301730

17311731
void HelperActionsForIvarDeclarations(ObjCContainerDecl *interfaceDecl,
17321732
SourceLocation atLoc,
@@ -3818,8 +3818,7 @@ class Parser : public CodeCompletionHandler {
38183818
SourceLocation Loc,
38193819
llvm::SmallVectorImpl<Expr *> &IntExprs);
38203820
/// Parses the 'device-type-list', which is a list of identifiers.
3821-
bool ParseOpenACCDeviceTypeList(
3822-
llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>> &Archs);
3821+
bool ParseOpenACCDeviceTypeList(llvm::SmallVector<IdentifierLoc> &Archs);
38233822
/// Parses the 'async-argument', which is an integral value with two
38243823
/// 'special' values that are likely negative (but come from Macros).
38253824
OpenACCIntExprParseResult ParseOpenACCAsyncArgument(OpenACCDirectiveKind DK,
@@ -3951,10 +3950,8 @@ class Parser : public CodeCompletionHandler {
39513950
return false;
39523951
}
39533952

3954-
bool ParseModuleName(
3955-
SourceLocation UseLoc,
3956-
SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>> &Path,
3957-
bool IsImport);
3953+
bool ParseModuleName(SourceLocation UseLoc,
3954+
SmallVectorImpl<IdentifierLoc> &Path, bool IsImport);
39583955

39593956
//===--------------------------------------------------------------------===//
39603957
// C++11/G++: Type Traits [Type-Traits.html in the GCC manual]

clang/include/clang/Sema/ParsedAttr.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class LangOptions;
4040
class Sema;
4141
class Stmt;
4242
class TargetInfo;
43-
struct IdentifierLoc;
4443

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

10099
} // namespace detail
101100

102-
/// Wraps an identifier and optional source location for the identifier.
103-
struct IdentifierLoc {
104-
SourceLocation Loc;
105-
IdentifierInfo *Ident;
106-
107-
static IdentifierLoc *create(ASTContext &Ctx, SourceLocation Loc,
108-
IdentifierInfo *Ident);
109-
};
110-
111101
/// A union of the various pointer types that can be passed to an
112102
/// ParsedAttr as an argument.
113103
using ArgsUnion = llvm::PointerUnion<Expr *, IdentifierLoc *>;

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ enum class LangAS : unsigned int;
143143
class LocalInstantiationScope;
144144
class LookupResult;
145145
class MangleNumberingContext;
146-
typedef ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> ModuleIdPath;
146+
typedef ArrayRef<IdentifierLoc> ModuleIdPath;
147147
class ModuleLoader;
148148
class MultiLevelTemplateArgumentList;
149149
struct NormalizedConstraint;

clang/include/clang/Sema/SemaCodeCompletion.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ class SemaCodeCompletion : public SemaBase {
193193
void CodeCompleteObjCForCollection(Scope *S, DeclGroupPtrTy IterationVar);
194194
void CodeCompleteObjCSelector(Scope *S,
195195
ArrayRef<const IdentifierInfo *> SelIdents);
196-
void
197-
CodeCompleteObjCProtocolReferences(ArrayRef<IdentifierLocPair> Protocols);
196+
void CodeCompleteObjCProtocolReferences(ArrayRef<IdentifierLoc> Protocols);
198197
void CodeCompleteObjCProtocolDecl(Scope *S);
199198
void CodeCompleteObjCInterfaceDecl(Scope *S);
200199
void CodeCompleteObjCClassForwardDecl(Scope *S);

clang/include/clang/Sema/SemaObjC.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,11 @@ class SemaObjC : public SemaBase {
307307

308308
DeclGroupPtrTy
309309
ActOnForwardProtocolDeclaration(SourceLocation AtProtoclLoc,
310-
ArrayRef<IdentifierLocPair> IdentList,
310+
ArrayRef<IdentifierLoc> IdentList,
311311
const ParsedAttributesView &attrList);
312312

313313
void FindProtocolDeclaration(bool WarnOnDeclarations, bool ForObjCContainer,
314-
ArrayRef<IdentifierLocPair> ProtocolId,
314+
ArrayRef<IdentifierLoc> ProtocolId,
315315
SmallVectorImpl<Decl *> &Protocols);
316316

317317
void DiagnoseTypeArgsAndProtocols(IdentifierInfo *ProtocolId,

clang/include/clang/Sema/SemaOpenACC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class SemaOpenACC : public SemaBase {
212212
} LoopWithoutSeqInfo;
213213

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

217217
/// A type to represent all the data for an OpenACC Clause that has been
218218
/// parsed, but not yet created/semantically analyzed. This is effectively a

clang/lib/AST/OpenACCClause.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,10 +891,10 @@ void OpenACCClausePrinter::VisitDeviceTypeClause(
891891
OS << "(";
892892
llvm::interleaveComma(C.getArchitectures(), OS,
893893
[&](const DeviceTypeArgument &Arch) {
894-
if (Arch.first == nullptr)
894+
if (Arch.getIdentifierInfo() == nullptr)
895895
OS << "*";
896896
else
897-
OS << Arch.first->getName();
897+
OS << Arch.getIdentifierInfo()->getName();
898898
});
899899
OS << ")";
900900
}

clang/lib/AST/TextNodeDumper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,10 +500,10 @@ void TextNodeDumper::Visit(const OpenACCClause *C) {
500500
llvm::interleaveComma(
501501
cast<OpenACCDeviceTypeClause>(C)->getArchitectures(), OS,
502502
[&](const DeviceTypeArgument &Arch) {
503-
if (Arch.first == nullptr)
503+
if (Arch.getIdentifierInfo() == nullptr)
504504
OS << "*";
505505
else
506-
OS << Arch.first->getName();
506+
OS << Arch.getIdentifierInfo()->getName();
507507
});
508508
OS << ")";
509509
break;

0 commit comments

Comments
 (0)