Skip to content

[Clang] Fix pack indexing profiling #139276

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
May 9, 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
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ Bug Fixes to C++ Support
whose type depends on itself. (#GH51347), (#GH55872)
- Improved parser recovery of invalid requirement expressions. In turn, this
fixes crashes from follow-on processing of the invalid requirement. (#GH138820)
- Fixed the handling of pack indexing types in the constraints of a member function redeclaration. (#GH138255)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
mutable llvm::ContextualFoldingSet<DependentDecltypeType, ASTContext &>
DependentDecltypeTypes;

mutable llvm::FoldingSet<PackIndexingType> DependentPackIndexingTypes;
mutable llvm::ContextualFoldingSet<PackIndexingType, ASTContext &>
DependentPackIndexingTypes;

mutable llvm::FoldingSet<TemplateTypeParmType> TemplateTypeParmTypes;
mutable llvm::FoldingSet<ObjCTypeParamType> ObjCTypeParamTypes;
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/AST/ExprCXX.h
Original file line number Diff line number Diff line change
Expand Up @@ -4549,6 +4549,10 @@ class PackIndexingExpr final

bool isFullySubstituted() const { return FullySubstituted; }

bool isPartiallySubstituted() const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document the difference with FullySubstituted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got rid of it entierly, it's cleaner

return isValueDependent() && TransformedExpressions;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing semicolon


/// Determine if the expression was expanded to empty.
bool expandsToEmptyPack() const {
return isFullySubstituted() && TransformedExpressions == 0;
Expand Down
16 changes: 5 additions & 11 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -5976,7 +5976,6 @@ class PackIndexingType final
private llvm::TrailingObjects<PackIndexingType, QualType> {
friend TrailingObjects;

const ASTContext &Context;
QualType Pattern;
Expr *IndexExpr;

Expand All @@ -5987,9 +5986,8 @@ class PackIndexingType final

protected:
friend class ASTContext; // ASTContext creates these.
PackIndexingType(const ASTContext &Context, QualType Canonical,
QualType Pattern, Expr *IndexExpr, bool FullySubstituted,
ArrayRef<QualType> Expansions = {});
PackIndexingType(QualType Canonical, QualType Pattern, Expr *IndexExpr,
bool FullySubstituted, ArrayRef<QualType> Expansions = {});

public:
Expr *getIndexExpr() const { return IndexExpr; }
Expand Down Expand Up @@ -6024,14 +6022,10 @@ class PackIndexingType final
return T->getTypeClass() == PackIndexing;
}

void Profile(llvm::FoldingSetNodeID &ID) {
if (hasSelectedType())
getSelectedType().Profile(ID);
else
Profile(ID, Context, getPattern(), getIndexExpr(), isFullySubstituted());
}
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context);
static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
QualType Pattern, Expr *E, bool FullySubstituted);
QualType Pattern, Expr *E, bool FullySubstituted,
ArrayRef<QualType> Expansions);

private:
const QualType *getExpansionsPtr() const {
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM,
DependentSizedMatrixTypes(this_()),
FunctionProtoTypes(this_(), FunctionProtoTypesLog2InitSize),
DependentTypeOfExprTypes(this_()), DependentDecltypeTypes(this_()),
TemplateSpecializationTypes(this_()),
DependentPackIndexingTypes(this_()), TemplateSpecializationTypes(this_()),
DependentTemplateSpecializationTypes(this_()),
DependentBitIntTypes(this_()), SubstTemplateTemplateParmPacks(this_()),
DeducedTemplates(this_()), ArrayParameterTypes(this_()),
Expand Down Expand Up @@ -6438,7 +6438,7 @@ QualType ASTContext::getPackIndexingType(QualType Pattern, Expr *IndexExpr,
} else {
llvm::FoldingSetNodeID ID;
PackIndexingType::Profile(ID, *this, Pattern.getCanonicalType(), IndexExpr,
FullySubstituted);
FullySubstituted, Expansions);
void *InsertPos = nullptr;
PackIndexingType *Canon =
DependentPackIndexingTypes.FindNodeOrInsertPos(ID, InsertPos);
Expand All @@ -6447,7 +6447,7 @@ QualType ASTContext::getPackIndexingType(QualType Pattern, Expr *IndexExpr,
PackIndexingType::totalSizeToAlloc<QualType>(Expansions.size()),
TypeAlignment);
Canon = new (Mem)
PackIndexingType(*this, QualType(), Pattern.getCanonicalType(),
PackIndexingType(QualType(), Pattern.getCanonicalType(),
IndexExpr, FullySubstituted, Expansions);
DependentPackIndexingTypes.InsertNode(Canon, InsertPos);
}
Expand All @@ -6457,7 +6457,7 @@ QualType ASTContext::getPackIndexingType(QualType Pattern, Expr *IndexExpr,
void *Mem =
Allocate(PackIndexingType::totalSizeToAlloc<QualType>(Expansions.size()),
TypeAlignment);
auto *T = new (Mem) PackIndexingType(*this, Canonical, Pattern, IndexExpr,
auto *T = new (Mem) PackIndexingType(Canonical, Pattern, IndexExpr,
FullySubstituted, Expansions);
Types.push_back(T);
return QualType(T, 0);
Expand Down
10 changes: 8 additions & 2 deletions clang/lib/AST/StmtProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2291,9 +2291,15 @@ void StmtProfiler::VisitSizeOfPackExpr(const SizeOfPackExpr *S) {
}

void StmtProfiler::VisitPackIndexingExpr(const PackIndexingExpr *E) {
VisitExpr(E);
VisitExpr(E->getPackIdExpression());
VisitExpr(E->getIndexExpr());

if (E->isPartiallySubstituted()) {
ID.AddInteger(E->getExpressions().size());
for (const Expr *Sub : E->getExpressions())
Visit(Sub);
} else {
VisitExpr(E->getPackIdExpression());
}
}

void StmtProfiler::VisitSubstNonTypeTemplateParmPackExpr(
Expand Down
26 changes: 20 additions & 6 deletions clang/lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4126,14 +4126,14 @@ void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
E->Profile(ID, Context, true);
}

PackIndexingType::PackIndexingType(const ASTContext &Context,
QualType Canonical, QualType Pattern,
PackIndexingType::PackIndexingType(QualType Canonical, QualType Pattern,
Expr *IndexExpr, bool FullySubstituted,
ArrayRef<QualType> Expansions)
: Type(PackIndexing, Canonical,
computeDependence(Pattern, IndexExpr, Expansions)),
Context(Context), Pattern(Pattern), IndexExpr(IndexExpr),
Size(Expansions.size()), FullySubstituted(FullySubstituted) {
Pattern(Pattern), IndexExpr(IndexExpr), Size(Expansions.size()),
FullySubstituted(FullySubstituted) {

llvm::uninitialized_copy(Expansions, getTrailingObjects<QualType>());
}

Expand Down Expand Up @@ -4174,12 +4174,26 @@ PackIndexingType::computeDependence(QualType Pattern, Expr *IndexExpr,
return TD;
}

void PackIndexingType::Profile(llvm::FoldingSetNodeID &ID,
const ASTContext &Context) {
Profile(ID, Context, getPattern(), getIndexExpr(), isFullySubstituted(), getExpansions());
}

void PackIndexingType::Profile(llvm::FoldingSetNodeID &ID,
const ASTContext &Context, QualType Pattern,
Expr *E, bool FullySubstituted) {
Pattern.Profile(ID);
Expr *E, bool FullySubstituted,
ArrayRef<QualType> Expansions) {

E->Profile(ID, Context, true);
ID.AddBoolean(FullySubstituted);
if(!Expansions.empty()) {
ID.AddInteger(Expansions.size());
for(QualType T : Expansions)
T.getCanonicalType().Profile(ID);
}
else {
Pattern.Profile(ID);
}
}

UnaryTransformType::UnaryTransformType(QualType BaseType,
Expand Down
76 changes: 75 additions & 1 deletion clang/test/SemaTemplate/concepts-out-of-line-def.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// RUN: %clang_cc1 -std=c++20 -verify %s
// RUN: %clang_cc1 -std=c++20 -Wno-c++26-extensions -verify %s
// RUN: %clang_cc1 -std=c++2c -Wno-c++26-extensions -verify %s


static constexpr int PRIMARY = 0;
static constexpr int SPECIALIZATION_CONCEPT = 1;
Expand Down Expand Up @@ -779,3 +781,75 @@ template <typename T>
consteval void S::mfn() requires (bool(&fn)) {}

}


namespace GH138255 {

template <typename... T>
concept C = true;

struct Func {
template<typename... Ts>
requires C<Ts...[0]>
static auto buggy() -> void;

template<typename... Ts>
requires C<Ts...[0]>
friend auto fr() -> void;

template<typename... Ts>
requires C<Ts...[0]>
friend auto fr2() -> void{}; // expected-note{{previous definition is here}}
};

template<typename... Ts>
requires C<Ts...[0]>
auto Func::buggy() -> void {}

template<typename... Ts>
requires C<Ts...[0]>
auto fr() -> void {}

template<typename... Ts>
requires C<Ts...[0]>
auto fr2() -> void {} // expected-error{{redefinition of 'fr2'}}


template <typename... Ts>
requires C<Ts...[0]>
struct Class;

template <typename... Ts>
requires C<Ts...[0]>
struct Class;


template <typename...>
struct TplClass {
template<typename... Ts>
requires C<Ts...[0]>
static auto buggy() -> void;
};

template<>
template<typename... Ts>
requires C<Ts...[0]>
auto TplClass<int>::buggy() -> void {}

}

namespace PackIndexExpr {
template <int... T>
concept C = true;

template <typename...> struct TplClass {
template <int... Ts>
requires C<Ts...[0]>
static auto buggy() -> void;
};

template <>
template <int... Ts>
requires C<Ts...[0]>
auto TplClass<int>::buggy() -> void {}
}
Loading