[clang] [Clang][NFC] Use const ASTContext reference in Decl Create methods (PR #185401)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 9 05:31:02 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Sameer Sahasrabuddhe (ssahasra)
<details>
<summary>Changes</summary>
Update Create() static factory methods, CreateDeserialized() methods, and constructors in Decl.h to accept const ASTContext& instead of ASTContext&. This change makes ASTContext parameters const-correct for declaration creation and deserialization, affecting all Decl subclasses declared in Decl.h.
Exceptions kept as non-const (only 3 methods):
- TranslationUnitDecl::Create() and constructor: stores non-const ASTContext& member that is returned by getASTContext()
- DefaultedOrDeletedFunctionInfo::Create(): calls Context.Allocate() which requires non-const access
Assisted-By: Claude Sonnect 4.5
---
Patch is 49.07 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/185401.diff
3 Files Affected:
- (modified) clang/include/clang/AST/Decl.h (+92-80)
- (modified) clang/lib/AST/Decl.cpp (+82-71)
- (modified) clang/lib/AST/DeclCXX.cpp (+3-3)
``````````diff
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index c3cd74a5b34db..a5b83aa55c1b0 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -182,8 +182,8 @@ class PragmaCommentDecl final
SourceLocation CommentLoc,
PragmaMSCommentKind CommentKind,
StringRef Arg);
- static PragmaCommentDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID,
- unsigned ArgSize);
+ static PragmaCommentDecl *
+ CreateDeserialized(const ASTContext &C, GlobalDeclID ID, unsigned ArgSize);
PragmaMSCommentKind getCommentKind() const { return CommentKind; }
@@ -216,8 +216,9 @@ class PragmaDetectMismatchDecl final
TranslationUnitDecl *DC,
SourceLocation Loc, StringRef Name,
StringRef Value);
- static PragmaDetectMismatchDecl *
- CreateDeserialized(ASTContext &C, GlobalDeclID ID, unsigned NameValueSize);
+ static PragmaDetectMismatchDecl *CreateDeserialized(const ASTContext &C,
+ GlobalDeclID ID,
+ unsigned NameValueSize);
StringRef getName() const { return getTrailingObjects(); }
StringRef getValue() const { return getTrailingObjects() + ValueStart; }
@@ -538,12 +539,12 @@ class LabelDecl : public NamedDecl {
void anchor() override;
public:
- static LabelDecl *Create(ASTContext &C, DeclContext *DC,
+ static LabelDecl *Create(const ASTContext &C, DeclContext *DC,
SourceLocation IdentL, IdentifierInfo *II);
- static LabelDecl *Create(ASTContext &C, DeclContext *DC,
+ static LabelDecl *Create(const ASTContext &C, DeclContext *DC,
SourceLocation IdentL, IdentifierInfo *II,
SourceLocation GnuLabelL);
- static LabelDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
+ static LabelDecl *CreateDeserialized(const ASTContext &C, GlobalDeclID ID);
LabelStmt *getStmt() const { return TheStmt; }
void setStmt(LabelStmt *T) { TheStmt = T; }
@@ -600,7 +601,7 @@ class NamespaceDecl : public NamespaceBaseDecl,
/// The unnamed namespace that inhabits this namespace, if any.
NamespaceDecl *AnonymousNamespace = nullptr;
- NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
+ NamespaceDecl(const ASTContext &C, DeclContext *DC, bool Inline,
SourceLocation StartLoc, SourceLocation IdLoc,
IdentifierInfo *Id, NamespaceDecl *PrevDecl, bool Nested);
@@ -614,12 +615,13 @@ class NamespaceDecl : public NamespaceBaseDecl,
friend class ASTDeclReader;
friend class ASTDeclWriter;
- static NamespaceDecl *Create(ASTContext &C, DeclContext *DC, bool Inline,
- SourceLocation StartLoc, SourceLocation IdLoc,
- IdentifierInfo *Id, NamespaceDecl *PrevDecl,
- bool Nested);
+ static NamespaceDecl *Create(const ASTContext &C, DeclContext *DC,
+ bool Inline, SourceLocation StartLoc,
+ SourceLocation IdLoc, IdentifierInfo *Id,
+ NamespaceDecl *PrevDecl, bool Nested);
- static NamespaceDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
+ static NamespaceDecl *CreateDeserialized(const ASTContext &C,
+ GlobalDeclID ID);
using redecl_range = redeclarable_base::redecl_range;
using redecl_iterator = redeclarable_base::redecl_iterator;
@@ -1125,9 +1127,10 @@ class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> {
NonParmVarDeclBitfields NonParmVarDeclBits;
};
- VarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
- SourceLocation IdLoc, const IdentifierInfo *Id, QualType T,
- TypeSourceInfo *TInfo, StorageClass SC);
+ VarDecl(Kind DK, const ASTContext &C, DeclContext *DC,
+ SourceLocation StartLoc, SourceLocation IdLoc,
+ const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
+ StorageClass SC);
using redeclarable_base = Redeclarable<VarDecl>;
@@ -1154,12 +1157,12 @@ class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> {
using redeclarable_base::getMostRecentDecl;
using redeclarable_base::isFirstDecl;
- static VarDecl *Create(ASTContext &C, DeclContext *DC,
+ static VarDecl *Create(const ASTContext &C, DeclContext *DC,
SourceLocation StartLoc, SourceLocation IdLoc,
const IdentifierInfo *Id, QualType T,
TypeSourceInfo *TInfo, StorageClass S);
- static VarDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
+ static VarDecl *CreateDeserialized(const ASTContext &C, GlobalDeclID ID);
SourceRange getSourceRange() const override LLVM_READONLY;
@@ -1751,15 +1754,16 @@ class ImplicitParamDecl : public VarDecl {
public:
/// Create implicit parameter.
- static ImplicitParamDecl *Create(ASTContext &C, DeclContext *DC,
+ static ImplicitParamDecl *Create(const ASTContext &C, DeclContext *DC,
SourceLocation IdLoc, IdentifierInfo *Id,
QualType T, ImplicitParamKind ParamKind);
- static ImplicitParamDecl *Create(ASTContext &C, QualType T,
+ static ImplicitParamDecl *Create(const ASTContext &C, QualType T,
ImplicitParamKind ParamKind);
- static ImplicitParamDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
+ static ImplicitParamDecl *CreateDeserialized(const ASTContext &C,
+ GlobalDeclID ID);
- ImplicitParamDecl(ASTContext &C, DeclContext *DC, SourceLocation IdLoc,
+ ImplicitParamDecl(const ASTContext &C, DeclContext *DC, SourceLocation IdLoc,
const IdentifierInfo *Id, QualType Type,
ImplicitParamKind ParamKind)
: VarDecl(ImplicitParam, C, DC, IdLoc, IdLoc, Id, Type,
@@ -1768,7 +1772,8 @@ class ImplicitParamDecl : public VarDecl {
setImplicit();
}
- ImplicitParamDecl(ASTContext &C, QualType Type, ImplicitParamKind ParamKind)
+ ImplicitParamDecl(const ASTContext &C, QualType Type,
+ ImplicitParamKind ParamKind)
: VarDecl(ImplicitParam, C, /*DC=*/nullptr, SourceLocation(),
SourceLocation(), /*Id=*/nullptr, Type,
/*TInfo=*/nullptr, SC_None) {
@@ -1793,9 +1798,10 @@ class ParmVarDecl : public VarDecl {
enum { MaxFunctionScopeIndex = 255 };
protected:
- ParmVarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
- SourceLocation IdLoc, const IdentifierInfo *Id, QualType T,
- TypeSourceInfo *TInfo, StorageClass S, Expr *DefArg)
+ ParmVarDecl(Kind DK, const ASTContext &C, DeclContext *DC,
+ SourceLocation StartLoc, SourceLocation IdLoc,
+ const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
+ StorageClass S, Expr *DefArg)
: VarDecl(DK, C, DC, StartLoc, IdLoc, Id, T, TInfo, S) {
assert(ParmVarDeclBits.HasInheritedDefaultArg == false);
assert(ParmVarDeclBits.DefaultArgKind == DAK_None);
@@ -1805,13 +1811,13 @@ class ParmVarDecl : public VarDecl {
}
public:
- static ParmVarDecl *Create(ASTContext &C, DeclContext *DC,
+ static ParmVarDecl *Create(const ASTContext &C, DeclContext *DC,
SourceLocation StartLoc, SourceLocation IdLoc,
const IdentifierInfo *Id, QualType T,
TypeSourceInfo *TInfo, StorageClass S,
Expr *DefArg);
- static ParmVarDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
+ static ParmVarDecl *CreateDeserialized(const ASTContext &C, GlobalDeclID ID);
SourceRange getSourceRange() const override LLVM_READONLY;
@@ -2151,10 +2157,11 @@ class FunctionDecl : public DeclaratorDecl,
void setHasODRHash(bool B = true) { FunctionDeclBits.HasODRHash = B; }
protected:
- FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
- const DeclarationNameInfo &NameInfo, QualType T,
- TypeSourceInfo *TInfo, StorageClass S, bool UsesFPIntrin,
- bool isInlineSpecified, ConstexprSpecKind ConstexprKind,
+ FunctionDecl(Kind DK, const ASTContext &C, DeclContext *DC,
+ SourceLocation StartLoc, const DeclarationNameInfo &NameInfo,
+ QualType T, TypeSourceInfo *TInfo, StorageClass S,
+ bool UsesFPIntrin, bool isInlineSpecified,
+ ConstexprSpecKind ConstexprKind,
const AssociatedConstraint &TrailingRequiresClause);
using redeclarable_base = Redeclarable<FunctionDecl>;
@@ -2186,7 +2193,7 @@ class FunctionDecl : public DeclaratorDecl,
using redeclarable_base::isFirstDecl;
static FunctionDecl *
- Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
+ Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
SourceLocation NLoc, DeclarationName N, QualType T,
TypeSourceInfo *TInfo, StorageClass SC, bool UsesFPIntrin = false,
bool isInlineSpecified = false, bool hasWrittenPrototype = true,
@@ -2200,13 +2207,13 @@ class FunctionDecl : public DeclaratorDecl,
}
static FunctionDecl *
- Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
+ Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
StorageClass SC, bool UsesFPIntrin, bool isInlineSpecified,
bool hasWrittenPrototype, ConstexprSpecKind ConstexprKind,
const AssociatedConstraint &TrailingRequiresClause);
- static FunctionDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
+ static FunctionDecl *CreateDeserialized(const ASTContext &C, GlobalDeclID ID);
DeclarationNameInfo getNameInfo() const {
return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc);
@@ -3238,7 +3245,7 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
InClassInitStyle InitStyle);
- static FieldDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
+ static FieldDecl *CreateDeserialized(const ASTContext &C, GlobalDeclID ID);
/// Returns the index of this field within its record,
/// as appropriate for passing to ASTRecordLayout::getFieldOffset.
@@ -3432,11 +3439,11 @@ class EnumConstantDecl : public ValueDecl,
public:
friend class StmtIteratorBase;
- static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC,
+ static EnumConstantDecl *Create(const ASTContext &C, EnumDecl *DC,
SourceLocation L, IdentifierInfo *Id,
- QualType T, Expr *E,
- const llvm::APSInt &V);
- static EnumConstantDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
+ QualType T, Expr *E, const llvm::APSInt &V);
+ static EnumConstantDecl *CreateDeserialized(const ASTContext &C,
+ GlobalDeclID ID);
const Expr *getInitExpr() const { return (const Expr*) Init; }
Expr *getInitExpr() { return (Expr*) Init; }
@@ -3468,7 +3475,7 @@ class IndirectFieldDecl : public ValueDecl,
NamedDecl **Chaining;
unsigned ChainingSize;
- IndirectFieldDecl(ASTContext &C, DeclContext *DC, SourceLocation L,
+ IndirectFieldDecl(const ASTContext &C, DeclContext *DC, SourceLocation L,
DeclarationName N, QualType T,
MutableArrayRef<NamedDecl *> CH);
@@ -3477,11 +3484,12 @@ class IndirectFieldDecl : public ValueDecl,
public:
friend class ASTDeclReader;
- static IndirectFieldDecl *Create(ASTContext &C, DeclContext *DC,
+ static IndirectFieldDecl *Create(const ASTContext &C, DeclContext *DC,
SourceLocation L, const IdentifierInfo *Id,
QualType T, MutableArrayRef<NamedDecl *> CH);
- static IndirectFieldDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
+ static IndirectFieldDecl *CreateDeserialized(const ASTContext &C,
+ GlobalDeclID ID);
using chain_iterator = ArrayRef<NamedDecl *>::const_iterator;
@@ -3574,7 +3582,7 @@ class TypedefNameDecl : public TypeDecl, public Redeclarable<TypedefNameDecl> {
void anchor() override;
protected:
- TypedefNameDecl(Kind DK, ASTContext &C, DeclContext *DC,
+ TypedefNameDecl(Kind DK, const ASTContext &C, DeclContext *DC,
SourceLocation StartLoc, SourceLocation IdLoc,
const IdentifierInfo *Id, TypeSourceInfo *TInfo)
: TypeDecl(DK, DC, IdLoc, Id, StartLoc), redeclarable_base(C),
@@ -3665,16 +3673,16 @@ class TypedefNameDecl : public TypeDecl, public Redeclarable<TypedefNameDecl> {
/// Represents the declaration of a typedef-name via the 'typedef'
/// type specifier.
class TypedefDecl : public TypedefNameDecl {
- TypedefDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
+ TypedefDecl(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
SourceLocation IdLoc, const IdentifierInfo *Id,
TypeSourceInfo *TInfo)
: TypedefNameDecl(Typedef, C, DC, StartLoc, IdLoc, Id, TInfo) {}
public:
- static TypedefDecl *Create(ASTContext &C, DeclContext *DC,
+ static TypedefDecl *Create(const ASTContext &C, DeclContext *DC,
SourceLocation StartLoc, SourceLocation IdLoc,
const IdentifierInfo *Id, TypeSourceInfo *TInfo);
- static TypedefDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
+ static TypedefDecl *CreateDeserialized(const ASTContext &C, GlobalDeclID ID);
SourceRange getSourceRange() const override LLVM_READONLY;
@@ -3689,17 +3697,18 @@ class TypeAliasDecl : public TypedefNameDecl {
/// The template for which this is the pattern, if any.
TypeAliasTemplateDecl *Template;
- TypeAliasDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
+ TypeAliasDecl(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
SourceLocation IdLoc, const IdentifierInfo *Id,
TypeSourceInfo *TInfo)
: TypedefNameDecl(TypeAlias, C, DC, StartLoc, IdLoc, Id, TInfo),
Template(nullptr) {}
public:
- static TypeAliasDecl *Create(ASTContext &C, DeclContext *DC,
+ static TypeAliasDecl *Create(const ASTContext &C, DeclContext *DC,
SourceLocation StartLoc, SourceLocation IdLoc,
const IdentifierInfo *Id, TypeSourceInfo *TInfo);
- static TypeAliasDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
+ static TypeAliasDecl *CreateDeserialized(const ASTContext &C,
+ GlobalDeclID ID);
SourceRange getSourceRange() const override LLVM_READONLY;
@@ -4051,7 +4060,7 @@ class EnumDecl : public TagDecl {
/// - 'enum class|struct' (scoped)
SourceRange EnumKeyRange;
- EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
+ EnumDecl(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl,
bool Scoped, bool ScopedUsingClassTag, bool Fixed);
@@ -4130,12 +4139,11 @@ class EnumDecl : public TagDecl {
return cast_or_null<EnumDecl>(TagDecl::getDefinitionOrSelf());
}
- static EnumDecl *Create(ASTContext &C, DeclContext *DC,
+ static EnumDecl *Create(const ASTContext &C, DeclContext *DC,
SourceLocation StartLoc, SourceLocation IdLoc,
- IdentifierInfo *Id, EnumDecl *PrevDecl,
- bool IsScoped, bool IsScopedUsingClassTag,
- bool IsFixed);
- static EnumDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
+ IdentifierInfo *Id, EnumDecl *PrevDecl, bool IsScoped,
+ bool IsScopedUsingClassTag, bool IsFixed);
+ static EnumDecl *CreateDeserialized(const ASTContext &C, GlobalDeclID ID);
/// Overrides to provide correct range when there's an enum-base specifier
/// with forward declarations.
@@ -4606,11 +4614,12 @@ class FileScopeAsmDecl : public Decl {
virtual void anchor();
public:
- static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC, Expr *Str,
- SourceLocation AsmLoc,
+ static FileScopeAsmDecl *Create(const ASTContext &C, DeclContext *DC,
+ Expr *Str, SourceLocation AsmLoc,
SourceLocation RParenLoc);
- static FileScopeAsmDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
+ static FileScopeAsmDecl *CreateDeserialized(const ASTContext &C,
+ GlobalDeclID ID);
SourceLocation getAsmLoc() const { return getLocation(); }
SourceLocation getRParenLoc() const { return RParenLoc; }
@@ -4647,8 +4656,9 @@ class TopLevelStmtDecl : public Decl, public DeclContext {
virtual void anchor();
public:
- static TopLevelStmtDecl *Create(ASTContext &C, Stmt *Statement);
- static TopLevelStmtDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
+ static TopLevelStmtDecl *Create(const ASTContext &C, Stmt *Statement);
+ static TopLevelStmtDecl *CreateDeserialized(const ASTContext &C,
+ GlobalDeclID ID);
SourceRange getSourceRange() const override LLVM_READONLY;
Stmt *getStmt() { return Statement; }
@@ -4741,8 +4751,9 @@ class BlockDecl : public Decl, public DeclContext {
BlockDecl(DeclContext *DC, SourceLocation CaretLoc);
public:
- static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L);
- static BlockDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
+ static BlockDecl *Create(const ASTContext &C, DeclContext *DC,
+ SourceLocation L);
+ static BlockDecl *CreateDeserialized(const ASTContext &C, GlobalDeclID ID);
SourceLocation getCaretLocation() const { return getLocation(); }
@@ -4897,10 +4908,10 @@ class OutlinedFunctionDecl final
friend class ASTDeclWriter;
friend TrailingObjects;
- static OutlinedFunctionDecl *Create(ASTContext &C, DeclContext *DC,
+ static OutlinedFunctionDecl *Create(const ASTContext &C, DeclContext *DC,
unsigned NumParams);
static OutlinedFunctionDecl *
- CreateDeserialized(ASTContext &C, GlobalDeclID ID, unsigned NumParams);
+ CreateDeserialized(const ASTContext &C, GlobalDeclID ID, unsigned NumParams);
Stmt *getBody() const override;
void setBody(Stmt *B);
@@ -4970,9 +4981,9 @@ class CapturedDecl final
friend class ASTDeclWriter;
friend TrailingObjects;
- static CapturedDecl *Create(ASTContext &C, DeclContext *DC,
+ static CapturedDecl *Create(const ASTContext &C, DeclContext *DC,
unsigned NumParams);
- static CapturedDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID,
+ static CapturedDecl *CreateDeserialized(const ASTContext &C, GlobalDeclID ID,
unsigned NumParams);
Stmt *getBody() const override;
@@ -5095,18 +5106,18 @@ class ImportDecl final : public Decl,
public:
/// Create a new module import declaration.
- static ImportDecl *Create(ASTContext &C, DeclContext *DC,
+ static ImportDecl *Create(const ASTContext &C, DeclContext *DC,
SourceLocation StartLoc, Module *Imported,
ArrayRef<SourceLocation> IdentifierLocs);
/// Create a new module import declaration for an implicitly-generated
/// import.
- static ImportDecl *CreateImplicit(ASTContext &C, DeclContext *DC,
+ static ImportDecl *CreateImplicit(const ASTContext &C, DeclContext *DC,
...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/185401
More information about the cfe-commits
mailing list