Skip to content

[clang] AST: remove source locations from [Variable/Dependent]SizedArrayType #135511

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
Apr 14, 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
7 changes: 3 additions & 4 deletions clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1564,8 +1564,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// Return a non-unique reference to the type for a variable array of
/// the specified element type.
QualType getVariableArrayType(QualType EltTy, Expr *NumElts,
ArraySizeModifier ASM, unsigned IndexTypeQuals,
SourceRange Brackets) const;
ArraySizeModifier ASM,
unsigned IndexTypeQuals) const;

/// Return a non-unique reference to the type for a dependently-sized
/// array of the specified element type.
Expand All @@ -1574,8 +1574,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// point.
QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
ArraySizeModifier ASM,
unsigned IndexTypeQuals,
SourceRange Brackets) const;
unsigned IndexTypeQuals) const;

/// Return a unique reference to the type for an incomplete array of
/// the specified element type.
Expand Down
25 changes: 4 additions & 21 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -3827,14 +3827,9 @@ class VariableArrayType : public ArrayType {
/// a function block.
Stmt *SizeExpr;

/// The range spanned by the left and right array brackets.
SourceRange Brackets;

VariableArrayType(QualType et, QualType can, Expr *e,
ArraySizeModifier sm, unsigned tq,
SourceRange brackets)
: ArrayType(VariableArray, et, can, sm, tq, e),
SizeExpr((Stmt*) e), Brackets(brackets) {}
VariableArrayType(QualType et, QualType can, Expr *e, ArraySizeModifier sm,
unsigned tq)
: ArrayType(VariableArray, et, can, sm, tq, e), SizeExpr((Stmt *)e) {}

public:
friend class StmtIteratorBase;
Expand All @@ -3845,10 +3840,6 @@ class VariableArrayType : public ArrayType {
return (Expr*) SizeExpr;
}

SourceRange getBracketsRange() const { return Brackets; }
SourceLocation getLBracketLoc() const { return Brackets.getBegin(); }
SourceLocation getRBracketLoc() const { return Brackets.getEnd(); }

bool isSugared() const { return false; }
QualType desugar() const { return QualType(this, 0); }

Expand Down Expand Up @@ -3884,12 +3875,8 @@ class DependentSizedArrayType : public ArrayType {
/// type will have its size deduced from an initializer.
Stmt *SizeExpr;

/// The range spanned by the left and right array brackets.
SourceRange Brackets;

DependentSizedArrayType(QualType et, QualType can, Expr *e,
ArraySizeModifier sm, unsigned tq,
SourceRange brackets);
ArraySizeModifier sm, unsigned tq);

public:
friend class StmtIteratorBase;
Expand All @@ -3900,10 +3887,6 @@ class DependentSizedArrayType : public ArrayType {
return (Expr*) SizeExpr;
}

SourceRange getBracketsRange() const { return Brackets; }
SourceLocation getLBracketLoc() const { return Brackets.getBegin(); }
SourceLocation getRBracketLoc() const { return Brackets.getEnd(); }

bool isSugared() const { return false; }
QualType desugar() const { return QualType(this, 0); }

Expand Down
24 changes: 3 additions & 21 deletions clang/include/clang/AST/TypeProperties.td
Original file line number Diff line number Diff line change
Expand Up @@ -154,40 +154,22 @@ let Class = IncompleteArrayType in {
}

let Class = VariableArrayType in {
def : Property<"leftBracketLoc", SourceLocation> {
let Read = [{ node->getLBracketLoc() }];
}
def : Property<"rightBracketLoc", SourceLocation> {
let Read = [{ node->getRBracketLoc() }];
}
def : Property<"size", ExprRef> {
let Read = [{ node->getSizeExpr() }];
}

def : Creator<[{
return ctx.getVariableArrayType(elementType, size, sizeModifier,
indexQualifiers.getCVRQualifiers(),
SourceRange(leftBracketLoc,
rightBracketLoc));
indexQualifiers.getCVRQualifiers());
}]>;
}

let Class = DependentSizedArrayType in {
def : Property<"size", ExprRef> {
let Read = [{ node->getSizeExpr() }];
}
def : Property<"leftBracketLoc", SourceLocation> {
let Read = [{ node->getLBracketLoc() }];
}
def : Property<"rightBracketLoc", SourceLocation> {
let Read = [{ node->getRBracketLoc() }];
}
def : Property<"size", ExprRef> { let Read = [{ node->getSizeExpr() }]; }

def : Creator<[{
return ctx.getDependentSizedArrayType(elementType, size, sizeModifier,
indexQualifiers.getCVRQualifiers(),
SourceRange(leftBracketLoc,
rightBracketLoc));
indexQualifiers.getCVRQualifiers());
}]>;
}

Expand Down
70 changes: 27 additions & 43 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4261,11 +4261,8 @@ QualType ASTContext::getVariableArrayDecayedType(QualType type) const {
case Type::DependentSizedArray: {
const auto *dat = cast<DependentSizedArrayType>(ty);
result = getDependentSizedArrayType(
getVariableArrayDecayedType(dat->getElementType()),
dat->getSizeExpr(),
dat->getSizeModifier(),
dat->getIndexTypeCVRQualifiers(),
dat->getBracketsRange());
getVariableArrayDecayedType(dat->getElementType()), dat->getSizeExpr(),
dat->getSizeModifier(), dat->getIndexTypeCVRQualifiers());
break;
}

Expand All @@ -4275,17 +4272,17 @@ QualType ASTContext::getVariableArrayDecayedType(QualType type) const {
result =
getVariableArrayType(getVariableArrayDecayedType(iat->getElementType()),
/*size*/ nullptr, ArraySizeModifier::Normal,
iat->getIndexTypeCVRQualifiers(), SourceRange());
iat->getIndexTypeCVRQualifiers());
break;
}

// Turn VLA types into [*] types.
case Type::VariableArray: {
const auto *vat = cast<VariableArrayType>(ty);
result = getVariableArrayType(
getVariableArrayDecayedType(vat->getElementType()),
/*size*/ nullptr, ArraySizeModifier::Star,
vat->getIndexTypeCVRQualifiers(), vat->getBracketsRange());
result =
getVariableArrayType(getVariableArrayDecayedType(vat->getElementType()),
/*size*/ nullptr, ArraySizeModifier::Star,
vat->getIndexTypeCVRQualifiers());
break;
}
}
Expand All @@ -4298,8 +4295,7 @@ QualType ASTContext::getVariableArrayDecayedType(QualType type) const {
/// variable array of the specified element type.
QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
ArraySizeModifier ASM,
unsigned IndexTypeQuals,
SourceRange Brackets) const {
unsigned IndexTypeQuals) const {
// Since we don't unique expressions, it isn't possible to unique VLA's
// that have an expression provided for their size.
QualType Canon;
Expand All @@ -4309,12 +4305,12 @@ QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
SplitQualType canonSplit = getCanonicalType(EltTy).split();
Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM,
IndexTypeQuals, Brackets);
IndexTypeQuals);
Canon = getQualifiedType(Canon, canonSplit.Quals);
}

auto *New = new (*this, alignof(VariableArrayType))
VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets);
VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals);

VariableArrayTypes.push_back(New);
Types.push_back(New);
Expand All @@ -4324,11 +4320,10 @@ QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
/// getDependentSizedArrayType - Returns a non-unique reference to
/// the type for a dependently-sized array of the specified element
/// type.
QualType ASTContext::getDependentSizedArrayType(QualType elementType,
Expr *numElements,
ArraySizeModifier ASM,
unsigned elementTypeQuals,
SourceRange brackets) const {
QualType
ASTContext::getDependentSizedArrayType(QualType elementType, Expr *numElements,
ArraySizeModifier ASM,
unsigned elementTypeQuals) const {
assert((!numElements || numElements->isTypeDependent() ||
numElements->isValueDependent()) &&
"Size must be type- or value-dependent!");
Expand All @@ -4354,7 +4349,7 @@ QualType ASTContext::getDependentSizedArrayType(QualType elementType,

auto *newType = new (*this, alignof(DependentSizedArrayType))
DependentSizedArrayType(elementType, QualType(), numElements, ASM,
elementTypeQuals, brackets);
elementTypeQuals);
DependentSizedArrayTypes.InsertNode(newType, insertPos);
Types.push_back(newType);
return QualType(newType, 0);
Expand All @@ -4364,7 +4359,7 @@ QualType ASTContext::getDependentSizedArrayType(QualType elementType,
if (!canonTy) {
canonTy = new (*this, alignof(DependentSizedArrayType))
DependentSizedArrayType(QualType(canonElementType.Ty, 0), QualType(),
numElements, ASM, elementTypeQuals, brackets);
numElements, ASM, elementTypeQuals);
DependentSizedArrayTypes.InsertNode(canonTy, insertPos);
Types.push_back(canonTy);
}
Expand All @@ -4383,7 +4378,7 @@ QualType ASTContext::getDependentSizedArrayType(QualType elementType,
// of the element type.
auto *sugaredType = new (*this, alignof(DependentSizedArrayType))
DependentSizedArrayType(elementType, canon, numElements, ASM,
elementTypeQuals, brackets);
elementTypeQuals);
Types.push_back(sugaredType);
return QualType(sugaredType, 0);
}
Expand Down Expand Up @@ -6775,17 +6770,14 @@ QualType ASTContext::getUnqualifiedArrayType(QualType type,
}

if (const auto *VAT = dyn_cast<VariableArrayType>(AT)) {
return getVariableArrayType(unqualElementType,
VAT->getSizeExpr(),
return getVariableArrayType(unqualElementType, VAT->getSizeExpr(),
VAT->getSizeModifier(),
VAT->getIndexTypeCVRQualifiers(),
VAT->getBracketsRange());
VAT->getIndexTypeCVRQualifiers());
}

const auto *DSAT = cast<DependentSizedArrayType>(AT);
return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(),
DSAT->getSizeModifier(), 0,
SourceRange());
DSAT->getSizeModifier(), 0);
}

/// Attempt to unwrap two types that may both be array types with the same bound
Expand Down Expand Up @@ -7729,19 +7721,14 @@ const ArrayType *ASTContext::getAsArrayType(QualType T) const {
IAT->getIndexTypeCVRQualifiers()));

if (const auto *DSAT = dyn_cast<DependentSizedArrayType>(ATy))
return cast<ArrayType>(
getDependentSizedArrayType(NewEltTy,
DSAT->getSizeExpr(),
DSAT->getSizeModifier(),
DSAT->getIndexTypeCVRQualifiers(),
DSAT->getBracketsRange()));
return cast<ArrayType>(getDependentSizedArrayType(
NewEltTy, DSAT->getSizeExpr(), DSAT->getSizeModifier(),
DSAT->getIndexTypeCVRQualifiers()));

const auto *VAT = cast<VariableArrayType>(ATy);
return cast<ArrayType>(getVariableArrayType(NewEltTy,
VAT->getSizeExpr(),
VAT->getSizeModifier(),
VAT->getIndexTypeCVRQualifiers(),
VAT->getBracketsRange()));
return cast<ArrayType>(
getVariableArrayType(NewEltTy, VAT->getSizeExpr(), VAT->getSizeModifier(),
VAT->getIndexTypeCVRQualifiers()));
}

QualType ASTContext::getAdjustedParameterType(QualType T) const {
Expand Down Expand Up @@ -13846,10 +13833,7 @@ static QualType getCommonNonSugarTypeNode(ASTContext &Ctx, const Type *X,
return Ctx.getDependentSizedArrayType(
getCommonArrayElementType(Ctx, AX, QX, AY, QY),
getCommonSizeExpr(Ctx, AX, AY), getCommonSizeModifier(AX, AY),
getCommonIndexTypeCVRQualifiers(AX, AY),
AX->getBracketsRange() == AY->getBracketsRange()
? AX->getBracketsRange()
: SourceRange());
getCommonIndexTypeCVRQualifiers(AX, AY));
}
case Type::ConstantArray: {
const auto *AX = cast<ConstantArrayType>(X),
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/AST/ASTDiagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ QualType clang::desugarForDiagnostic(ASTContext &Context, QualType QT,
ElementTy, CAT->getSize(), CAT->getSizeExpr(),
CAT->getSizeModifier(), CAT->getIndexTypeCVRQualifiers());
else if (const auto *VAT = dyn_cast<VariableArrayType>(AT))
QT = Context.getVariableArrayType(
ElementTy, VAT->getSizeExpr(), VAT->getSizeModifier(),
VAT->getIndexTypeCVRQualifiers(), VAT->getBracketsRange());
QT = Context.getVariableArrayType(ElementTy, VAT->getSizeExpr(),
VAT->getSizeModifier(),
VAT->getIndexTypeCVRQualifiers());
else if (const auto *DSAT = dyn_cast<DependentSizedArrayType>(AT))
QT = Context.getDependentSizedArrayType(
ElementTy, DSAT->getSizeExpr(), DSAT->getSizeModifier(),
DSAT->getIndexTypeCVRQualifiers(), DSAT->getBracketsRange());
DSAT->getIndexTypeCVRQualifiers());
else if (const auto *IAT = dyn_cast<IncompleteArrayType>(AT))
QT = Context.getIncompleteArrayType(ElementTy, IAT->getSizeModifier(),
IAT->getIndexTypeCVRQualifiers());
Expand Down
6 changes: 2 additions & 4 deletions clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1295,28 +1295,26 @@ ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Error Err = Error::success();
QualType ToElementType = importChecked(Err, T->getElementType());
Expr *ToSizeExpr = importChecked(Err, T->getSizeExpr());
SourceRange ToBracketsRange = importChecked(Err, T->getBracketsRange());
if (Err)
return std::move(Err);
return Importer.getToContext().getVariableArrayType(
ToElementType, ToSizeExpr, T->getSizeModifier(),
T->getIndexTypeCVRQualifiers(), ToBracketsRange);
T->getIndexTypeCVRQualifiers());
}

ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
const DependentSizedArrayType *T) {
Error Err = Error::success();
QualType ToElementType = importChecked(Err, T->getElementType());
Expr *ToSizeExpr = importChecked(Err, T->getSizeExpr());
SourceRange ToBracketsRange = importChecked(Err, T->getBracketsRange());
if (Err)
return std::move(Err);
// SizeExpr may be null if size is not specified directly.
// For example, 'int a[]'.

return Importer.getToContext().getDependentSizedArrayType(
ToElementType, ToSizeExpr, T->getSizeModifier(),
T->getIndexTypeCVRQualifiers(), ToBracketsRange);
T->getIndexTypeCVRQualifiers());
}

ExpectedType ASTNodeImporter::VisitDependentSizedExtVectorType(
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/MicrosoftMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3312,7 +3312,7 @@ void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
const DependentSizedArrayType *DSAT =
getASTContext().getAsDependentSizedArrayType(ElementTy);
Error(DSAT->getSizeExpr()->getExprLoc(), "dependent-length")
<< DSAT->getBracketsRange();
<< DSAT->getSizeExpr()->getSourceRange();
return;
} else {
break;
Expand Down
4 changes: 0 additions & 4 deletions clang/lib/AST/TextNodeDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1941,16 +1941,12 @@ void TextNodeDumper::VisitConstantArrayType(const ConstantArrayType *T) {
}

void TextNodeDumper::VisitVariableArrayType(const VariableArrayType *T) {
OS << " ";
dumpSourceRange(T->getBracketsRange());
VisitArrayType(T);
}

void TextNodeDumper::VisitDependentSizedArrayType(
const DependentSizedArrayType *T) {
VisitArrayType(T);
OS << " ";
dumpSourceRange(T->getBracketsRange());
}

void TextNodeDumper::VisitDependentSizedExtVectorType(
Expand Down
9 changes: 3 additions & 6 deletions clang/lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,8 @@ QualType ArrayParameterType::getConstantArrayType(const ASTContext &Ctx) const {

DependentSizedArrayType::DependentSizedArrayType(QualType et, QualType can,
Expr *e, ArraySizeModifier sm,
unsigned tq,
SourceRange brackets)
: ArrayType(DependentSizedArray, et, can, sm, tq, e), SizeExpr((Stmt *)e),
Brackets(brackets) {}
unsigned tq)
: ArrayType(DependentSizedArray, et, can, sm, tq, e), SizeExpr((Stmt *)e) {}

void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
const ASTContext &Context,
Expand Down Expand Up @@ -1099,8 +1097,7 @@ struct SimpleTransformVisitor : public TypeVisitor<Derived, QualType> {

return Ctx.getVariableArrayType(elementType, T->getSizeExpr(),
T->getSizeModifier(),
T->getIndexTypeCVRQualifiers(),
T->getBracketsRange());
T->getIndexTypeCVRQualifiers());
}

QualType VisitIncompleteArrayType(const IncompleteArrayType *T) {
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGOpenMPRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3797,7 +3797,7 @@ CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
RValue::get(NumOfElements));
KmpTaskAffinityInfoArrayTy = C.getVariableArrayType(
KmpTaskAffinityInfoTy, OVE, ArraySizeModifier::Normal,
/*IndexTypeQuals=*/0, SourceRange(Loc, Loc));
/*IndexTypeQuals=*/0);
// Properly emit variable-sized array.
auto *PD = ImplicitParamDecl::Create(C, KmpTaskAffinityInfoArrayTy,
ImplicitParamKind::Other);
Expand Down Expand Up @@ -4260,7 +4260,7 @@ std::pair<llvm::Value *, Address> CGOpenMPRuntime::emitDependClause(
RValue::get(NumOfElements));
KmpDependInfoArrayTy =
C.getVariableArrayType(KmpDependInfoTy, OVE, ArraySizeModifier::Normal,
/*IndexTypeQuals=*/0, SourceRange(Loc, Loc));
/*IndexTypeQuals=*/0);
// CGF.EmitVariablyModifiedType(KmpDependInfoArrayTy);
// Properly emit variable-sized array.
auto *PD = ImplicitParamDecl::Create(C, KmpDependInfoArrayTy,
Expand Down
Loading