Skip to content

Commit c7981ba

Browse files
habermanmemfrob
authored andcommitted
[clang] Replaced some manual pointer tagging with llvm::PointerIntPair.
There is no functional change here (hence no new tests). The only change is to replace a couple uintptr_t members with llvm::PointerIntPair<> to clean up the code, making it more readable and less error prone. This cleanup highlighted that the old code was effectively casting away const. This is fixed by changing some function signatures. Reviewed By: rsmith Differential Revision: https://reviews.llvm.org/D98889
1 parent fd6bd7a commit c7981ba

File tree

4 files changed

+18
-24
lines changed

4 files changed

+18
-24
lines changed

clang/include/clang/Sema/Initialization.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ class alignas(8) InitializedEntity {
187187
ObjCMethodDecl *MethodDecl;
188188

189189
/// When Kind == EK_Parameter, the ParmVarDecl, with the
190-
/// low bit indicating whether the parameter is "consumed".
191-
uintptr_t Parameter;
190+
/// integer indicating whether the parameter is "consumed".
191+
llvm::PointerIntPair<ParmVarDecl *, 1> Parameter;
192192

193193
/// When Kind == EK_Temporary or EK_CompoundLiteralInit, the type
194194
/// source information for the temporary.
@@ -197,9 +197,9 @@ class alignas(8) InitializedEntity {
197197
struct LN LocAndNRVO;
198198

199199
/// When Kind == EK_Base, the base specifier that provides the
200-
/// base class. The lower bit specifies whether the base is an inherited
200+
/// base class. The integer specifies whether the base is an inherited
201201
/// virtual base.
202-
uintptr_t Base;
202+
llvm::PointerIntPair<const CXXBaseSpecifier *, 1> Base;
203203

204204
/// When Kind == EK_ArrayElement, EK_VectorElement, or
205205
/// EK_ComplexElement, the index of the array or vector element being
@@ -252,15 +252,14 @@ class alignas(8) InitializedEntity {
252252

253253
/// Create the initialization entity for a parameter.
254254
static InitializedEntity InitializeParameter(ASTContext &Context,
255-
const ParmVarDecl *Parm) {
255+
ParmVarDecl *Parm) {
256256
return InitializeParameter(Context, Parm, Parm->getType());
257257
}
258258

259259
/// Create the initialization entity for a parameter, but use
260260
/// another type.
261-
static InitializedEntity InitializeParameter(ASTContext &Context,
262-
const ParmVarDecl *Parm,
263-
QualType Type) {
261+
static InitializedEntity
262+
InitializeParameter(ASTContext &Context, ParmVarDecl *Parm, QualType Type) {
264263
bool Consumed = (Context.getLangOpts().ObjCAutoRefCount &&
265264
Parm->hasAttr<NSConsumedAttr>());
266265

@@ -269,8 +268,7 @@ class alignas(8) InitializedEntity {
269268
Entity.Type =
270269
Context.getVariableArrayDecayedType(Type.getUnqualifiedType());
271270
Entity.Parent = nullptr;
272-
Entity.Parameter
273-
= (static_cast<uintptr_t>(Consumed) | reinterpret_cast<uintptr_t>(Parm));
271+
Entity.Parameter = {Parm, Consumed};
274272
return Entity;
275273
}
276274

@@ -283,7 +281,7 @@ class alignas(8) InitializedEntity {
283281
Entity.Kind = EK_Parameter;
284282
Entity.Type = Context.getVariableArrayDecayedType(Type);
285283
Entity.Parent = nullptr;
286-
Entity.Parameter = (Consumed);
284+
Entity.Parameter = {nullptr, Consumed};
287285
return Entity;
288286
}
289287

@@ -466,19 +464,19 @@ class alignas(8) InitializedEntity {
466464
/// parameter.
467465
bool isParameterConsumed() const {
468466
assert(isParameterKind() && "Not a parameter");
469-
return (Parameter & 1);
467+
return Parameter.getInt();
470468
}
471469

472470
/// Retrieve the base specifier.
473471
const CXXBaseSpecifier *getBaseSpecifier() const {
474472
assert(getKind() == EK_Base && "Not a base specifier");
475-
return reinterpret_cast<const CXXBaseSpecifier *>(Base & ~0x1);
473+
return Base.getPointer();
476474
}
477475

478476
/// Return whether the base is an inherited virtual base.
479477
bool isInheritedVirtualBase() const {
480478
assert(getKind() == EK_Base && "Not a base specifier");
481-
return Base & 0x1;
479+
return Base.getInt();
482480
}
483481

484482
/// Determine whether this is an array new with an unknown bound.

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2702,8 +2702,7 @@ class Sema final {
27022702
void ActOnParamUnparsedDefaultArgument(Decl *param, SourceLocation EqualLoc,
27032703
SourceLocation ArgLoc);
27042704
void ActOnParamDefaultArgumentError(Decl *param, SourceLocation EqualLoc);
2705-
ExprResult ConvertParamDefaultArgument(const ParmVarDecl *Param,
2706-
Expr *DefaultArg,
2705+
ExprResult ConvertParamDefaultArgument(ParmVarDecl *Param, Expr *DefaultArg,
27072706
SourceLocation EqualLoc);
27082707
void SetParamDefaultArgument(ParmVarDecl *Param, Expr *DefaultArg,
27092708
SourceLocation EqualLoc);

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ void Sema::ImplicitExceptionSpecification::CalledStmt(Stmt *S) {
254254
ComputedEST = EST_None;
255255
}
256256

257-
ExprResult Sema::ConvertParamDefaultArgument(const ParmVarDecl *Param,
258-
Expr *Arg,
257+
ExprResult Sema::ConvertParamDefaultArgument(ParmVarDecl *Param, Expr *Arg,
259258
SourceLocation EqualLoc) {
260259
if (RequireCompleteType(Param->getLocation(), Param->getType(),
261260
diag::err_typecheck_decl_incomplete_type))

clang/lib/Sema/SemaInit.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "clang/Sema/Lookup.h"
2525
#include "clang/Sema/SemaInternal.h"
2626
#include "llvm/ADT/APInt.h"
27+
#include "llvm/ADT/PointerIntPair.h"
2728
#include "llvm/ADT/SmallString.h"
2829
#include "llvm/Support/ErrorHandling.h"
2930
#include "llvm/Support/raw_ostream.h"
@@ -3281,10 +3282,7 @@ InitializedEntity::InitializeBase(ASTContext &Context,
32813282
InitializedEntity Result;
32823283
Result.Kind = EK_Base;
32833284
Result.Parent = Parent;
3284-
Result.Base = reinterpret_cast<uintptr_t>(Base);
3285-
if (IsInheritedVirtualBase)
3286-
Result.Base |= 0x01;
3287-
3285+
Result.Base = {Base, IsInheritedVirtualBase};
32883286
Result.Type = Base->getType();
32893287
return Result;
32903288
}
@@ -3293,7 +3291,7 @@ DeclarationName InitializedEntity::getName() const {
32933291
switch (getKind()) {
32943292
case EK_Parameter:
32953293
case EK_Parameter_CF_Audited: {
3296-
ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1);
3294+
ParmVarDecl *D = Parameter.getPointer();
32973295
return (D ? D->getDeclName() : DeclarationName());
32983296
}
32993297

@@ -3336,7 +3334,7 @@ ValueDecl *InitializedEntity::getDecl() const {
33363334

33373335
case EK_Parameter:
33383336
case EK_Parameter_CF_Audited:
3339-
return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1);
3337+
return Parameter.getPointer();
33403338

33413339
case EK_Result:
33423340
case EK_StmtExprResult:

0 commit comments

Comments
 (0)