26#define DEBUG_TYPE "clang-repl"
47 bool isFromThisPTU(
const void *Ptr)
const {
48 return Tracker.isFromThisPTU(Ptr, ID);
51 bool isDiscarded(
const void *Ptr)
const {
52 return Tracker.isFromThisPTU(Ptr, ID);
55 bool hasRecord()
const {
return Rec !=
nullptr; }
59 bool hasNoPreviousDecl(
const Decl *D) {
63 template <
typename DeclT> DeclT *findSurvivor(DeclT *D)
const {
65 if (!isFromThisPTU(It))
66 return dyn_cast<DeclT>(It);
70 template <
typename DeclT>
72 auto *Latest =
static_cast<DeclT *
>(DBase);
76 Latest->getFirstDecl()->RedeclLink.setLatest(Survivor);
81 for (DeclT *Dead = Latest; Dead != Survivor;) {
82 DeclT *
Next = Dead->getPreviousDecl();
90 template <
typename DeclT>
void unlinkRedeclChain(DeclT *D) {
91 DeclT *Latest = D->getMostRecentDecl();
92 if (DeclT *Survivor = findSurvivor(D))
93 resetRedeclLink(Latest, Survivor);
111 auto Pos = Map->find(Name);
112 if (Pos != Map->end()) {
134 SemaRef.IdResolver.RemoveDecl(D);
139 if (It != Map->end())
140 It->second.remove(D);
150 Decl *Prev =
nullptr;
152 if (isFromThisPTU(Cur)) {
165 class ScopedMutationRecord {
171 : Rec(Rec), OldRec(Rec) {
175 ~ScopedMutationRecord() { Rec = OldRec; }
177 ScopedMutationRecord(
const ScopedMutationRecord &) =
delete;
178 ScopedMutationRecord &operator=(
const ScopedMutationRecord &) =
delete;
182 ScopedMutationRecord Guard(Rec,
nullptr);
185 repairLexicalChain(DC);
188 template <
typename T>
189 void removeSpec(llvm::FoldingSetVector<T> &FSV,
190 llvm::function_ref<
bool(
const void *)> ShouldStop) {
193 for (T &Node : FSV) {
194 if (ShouldStop(&Node))
196 Remaining.push_back(&Node);
201 for (T *Node : Remaining)
211 : ID(ID), Tracker(Tracker), Actions(Action), SemaRef(SemaRef),
212 SemaCache(SemaCache) {}
217 const auto &Live = RD.data();
218#define FIELD(Name, Width, Merge) FP.Name = Live.Name;
219#include "clang/AST/CXXRecordDeclDefinitionBits.def"
225 auto &Live = RD.data();
226#define FIELD(Name, Width, Merge) Live.Name = FP.Name;
227#include "clang/AST/CXXRecordDeclDefinitionBits.def"
233 template <
typename Footpr
intT,
typename OwnerT>
239 template <
typename Footpr
intT,
typename OwnerT>
245 ScopedMutationRecord Guard(Rec, &R);
253 if (
auto *DC = dyn_cast<DeclContext>(D))
254 VisitDeclContext(DC);
261 if (!isDiscarded(ND))
268 removeFromDCLookup(ND);
269 removeFromLookups(ND);
275 auto *D =
static_cast<DeclT *
>(DBase);
279 unlinkRedeclChain(D);
300 return DeclShape::Template;
302 return DeclShape::Enum;
304 return DeclShape::Function;
306 return DeclShape::Var;
308 return DeclShape::Class;
310 return DeclShape::Typedef;
312 return DeclShape::None;
331 case DeclShape::Class: {
333 if (RD->hasDefinition() && RD == RD->getDefinition()) {
339 if (RD->needsImplicitDefaultConstructor() ||
340 RD->needsImplicitCopyConstructor() ||
341 RD->needsImplicitCopyAssignment() ||
342 RD->needsImplicitMoveConstructor() ||
343 RD->needsImplicitMoveAssignment() || RD->needsImplicitDestructor())
344 Kinds |=
uint32_t(MutationKind::DefinitionData);
346 Kinds |=
uint32_t(MutationKind::SpecInfo) |
347 uint32_t(MutationKind::MemberSpecInfo);
350 case DeclShape::Function:
351 Kinds |=
uint32_t(MutationKind::SpecInfo) |
352 uint32_t(MutationKind::MemberSpecInfo);
355 Kinds |=
uint32_t(MutationKind::SpecInfo) |
356 uint32_t(MutationKind::MemberSpecInfo);
358 case DeclShape::Enum:
359 Kinds |=
uint32_t(MutationKind::MemberSpecInfo);
361 case DeclShape::Template:
362 case DeclShape::Typedef:
363 case DeclShape::None:
373 assert(
Last &&
"baseline missing at verify time");
377template <
typename Footpr
intT,
typename OwnerT>
405 case DeclShape::Class: {
407 if (FlaggedKinds &
uint32_t(MutationKind::SpecInfo)) {
408 if (
const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(RD))
411 Verified |=
uint32_t(MutationKind::SpecInfo);
412 }
else if (FlaggedKinds &
uint32_t(MutationKind::MemberSpecInfo)) {
415 Verified |=
uint32_t(MutationKind::MemberSpecInfo);
419 case DeclShape::Function: {
421 if (FlaggedKinds &
uint32_t(MutationKind::SpecInfo)) {
424 Verified |=
uint32_t(MutationKind::SpecInfo);
425 }
else if (FlaggedKinds &
uint32_t(MutationKind::MemberSpecInfo)) {
428 Verified |=
uint32_t(MutationKind::MemberSpecInfo);
432 case DeclShape::Var: {
434 if (FlaggedKinds &
uint32_t(MutationKind::SpecInfo)) {
435 if (
const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(VD))
438 Verified |=
uint32_t(MutationKind::SpecInfo);
439 }
else if (FlaggedKinds &
uint32_t(MutationKind::MemberSpecInfo)) {
442 Verified |=
uint32_t(MutationKind::MemberSpecInfo);
446 case DeclShape::Enum: {
448 if (FlaggedKinds &
uint32_t(MutationKind::MemberSpecInfo)) {
451 Verified |=
uint32_t(MutationKind::MemberSpecInfo);
455 case DeclShape::Template:
456 case DeclShape::Typedef:
457 case DeclShape::None:
469template <
typename OnConfirmedFn>
471 for (
auto &Entry : Active) {
472 const Decl *D = Entry.getFirst();
473 uint32_t StillOpen = Entry.getSecond();
476 OnConfirmed(D, S, Newly);
502 HiddenMutationTracker.track(D, Flag);
510 if (!Flag || !HiddenMutationTracker.isTrackedFor(D, Flag))
513 uint32_t Closed = Flag & ~StillOpen;
515 HiddenMutationTracker.untrack(D, Closed);
532 Mutations.remove_if([&](
auto &Entry) ->
bool {
533 const Decl *D = Entry.first;
557 return Parent == ThisTU;
588 if (
const auto *RD = dyn_cast<CXXRecordDecl>(D))
589 return RD->isCompleteDefinition();
608 if (S == DeclShape::None)
631 assert(!Cur.
Commited &&
"Current PTUState is already commited");
634 LLVM_DEBUG({ llvm::dbgs() <<
"Commit ThisTU\n"; });
642 Tracker.getHiddenMutationTracker().sweep(
710 if (isDiscarded(RD)) {
713 bool DetachDefData = (Def && isFromThisPTU(Def) && !isFromThisPTU(Canon));
715 unlinkRedeclChain<TagDecl>(RD);
719 detachDefData(Canon);
731 if (isDiscarded(FD)) {
744 if (isDiscarded(VD)) {
757 if (isDiscarded(ED)) {
770 if (isDiscarded(CT)) {
772 if (
auto *RD = dyn_cast<CXXRecordDecl>(Templated)) {
784 if (
Record().
has(MutationKind::SpecializationAdded)) {
786 removeSpec(Specs, [&](
const void *Ptr) ->
bool {
787 return Tracker.isFromThisPTU(Ptr, ID);
794 if (isDiscarded(FTD)) {
797 unlinkRedeclChain(FD);
810 if (isDiscarded(VTD)) {
813 unlinkRedeclChain(VD);
851 for (
auto &Entry : SemaRef.SpecialMemberCache) {
853 if (MD && (Tracker.isFromThisPTU(MD, ID) ||
855 ToRemove.push_back(Entry.first);
857 for (
const auto &Key : ToRemove)
858 SemaRef.SpecialMemberCache.erase(Key);
868 "MostRecentTU doesn't match the TU recorded at commit time");
885 Tracker.getHiddenMutationTracker().untrackIf(
886 [&](
const Decl *D) {
return Tracker.isFromThisPTU(D, ID); });
887 Tracker.undoLastEntries();
893 Tracker.getHiddenMutationTracker().sweep(
925 Umerger.
Visit(ThisTU);
936 Tracker.popCurrentPTU();
970template <
typename TemplateT,
typename SpecT>
971void PTUMutationRecorder::noteTemplateDeclMutation(
const TemplateT *TD,
977 Cur.
noteMutated(TD, DeclShape::Template, MutationKind::SpecializationAdded);
982 noteTemplateDeclMutation(TD, D);
985void PTUMutationRecorder::noteDefinitionInstantiated(
const Decl *D) {
992 const auto *VD = dyn_cast<VarDecl>(D);
993 if (VD && VD->getPreviousDecl() &&
1008 Footprints.removeFrom(Owner, K, ID);
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
llvm::MachO::Record Record
ExternCContextDecl * getExternCContextDecl() const
ASTDeclUnmerger handles two cases based on whether a declaration should be discarded or belongs to a ...
static void restoreDefinitionDataFootprint(const DefinitionDataFootprint &FP, CXXRecordDecl &RD)
void VisitNamedDecl(NamedDecl *ND)
Remove ND from its context and the lookup tables, and make the declaration it redeclares visible agai...
friend class PTUMutationActions
static void restoreFootprint(const FootprintT &FP, OwnerT &Owner)
void VisitClassTemplateDecl(ClassTemplateDecl *CTD)
void VisitVarTemplateDecl(VarTemplateDecl *VTD)
void VisitRedeclarable(Redeclarable< DeclT > *DBase)
Remove D like VisitNamedDecl() and restore its redeclaration chain to the previous state.
void RestoreDecl(Decl *D, MutationRecord &R)
void VisitFunctionDecl(FunctionDecl *FD)
void VisitTypedefNameDecl(TypedefNameDecl *D)
void VisitTranslationUnitDecl(TranslationUnitDecl *D)
static DefinitionDataFootprint createDefinitionDataFootprint(const CXXRecordDecl &RD)
void VisitCXXRecordDecl(CXXRecordDecl *RD)
void VisitTagDecl(TagDecl *TD)
void VisitFunctionTemplateDecl(FunctionTemplateDecl *FTD)
static FootprintT createFootprint(const OwnerT &Owner)
void VisitNamespaceDecl(NamespaceDecl *D)
These are the places where we restore or undo/unlink the incremental state of a decl based on its Dec...
void VisitVarDecl(VarDecl *VD)
void VisitUsingShadowDecl(UsingShadowDecl *D)
ASTDeclUnmerger(PTUID ID, IncrementalStateTracker &Tracker, PTUMutationActions &Action, Sema &SemaRef, SemaCacheReverter &SemaCache)
void VisitEnumDecl(EnumDecl *ED)
void VisitNamespaceAliasDecl(NamespaceAliasDecl *D)
Represents a static or instance method of a struct/union/class.
Represents a C++ struct/union/class.
CXXRecordDecl * getDefinition() const
ClassTemplateDecl * getDescribedClassTemplate() const
Retrieves the class template that is described by this class declaration.
CXXRecordDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Declaration of a class template.
llvm::FoldingSetVector< ClassTemplateSpecializationDecl > & getSpecializations() const
Retrieve the set of specializations of this class template.
CXXRecordDecl * getTemplatedDecl() const
Get the underlying class declarations of the template.
Represents a class template specialization, which refers to a class template with a given set of temp...
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
DeclContext * getParent()
getParent - Returns the containing DeclContext.
bool isTransparentContext() const
isTransparentContext - Determines whether this context is a "transparent" context,...
DeclContext * getLexicalParent()
getLexicalParent - Returns the containing lexical DeclContext.
bool isTranslationUnit() const
Decl * FirstDecl
FirstDecl - The first declaration stored within this declaration context.
bool containsDecl(Decl *D) const
Checks whether a declaration is in this context.
Decl * LastDecl
LastDecl - The last declaration stored within this declaration context.
DeclContext * getPrimaryContext()
getPrimaryContext - There may be many different declarations of the same entity (including forward de...
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
bool isInlineNamespace() const
StoredDeclsMap * getLookupPtr() const
Retrieve the internal representation of the lookup structure.
A simple visitor class that helps create declaration visitors.
Decl - This represents one declaration (or definition), e.g.
Decl * getPreviousDecl()
Retrieve the previous declaration that declares the same entity as this declaration,...
Decl * getMostRecentDecl()
Retrieve the most recent declaration that declares the same entity as this declaration (which may be ...
ASTContext & getASTContext() const LLVM_READONLY
Decl * getNextDeclInContext()
llvm::PointerIntPair< Decl *, 3, ModuleOwnershipKind > NextInContextAndBits
The next declaration within the same lexical DeclContext.
bool isDefinedOutsideFunctionOrMethod() const
isDefinedOutsideFunctionOrMethod - This predicate returns true if this scoped decl is defined outside...
DeclContext * getDeclContext()
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC).
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
const LangOptions & getLangOpts() const LLVM_READONLY
Helper to get the language options from the ASTContext.
The name of a declaration.
void * getFETokenInfo() const
Get and set FETokenInfo.
bool isEmpty() const
Evaluates true when this declaration name is empty.
bool isIdentifier() const
Predicate functions for querying what type of name this is.
Declaration context for names declared as extern "C" in C++.
Represents a function declaration or definition.
Declaration of a template function.
FunctionTemplateDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
FunctionDecl * getTemplatedDecl() const
Get the underlying function declaration of the template.
FootprintStore & getFootprints()
PTUStateInfo & current() const
bool isFromThisPTU(const void *Ptr, PTUID ID) const
This represents a decl that may have a name.
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Represents a C++ namespace alias.
Represent a C++ namespace.
NamespaceDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this namespace.
uint32_t confirmMutation(const Decl *D, DeclShape S, uint32_t FlaggedKinds)
The single place to check whether a kind has actually changed.
bool shouldRecurseInto(const Decl *D)
These are the places where the incremental state of a declaration is registered based on its DeclKind...
void CommitDecl(const Decl *D, MutationRecord *Rec)
void CommitPTU(TranslationUnitDecl *MostRecentTU)
uint32_t DeclNeedingTracking(DeclShape S, const Decl *D)
This is called when a decl is created or seen for the first time.
void VisitDeclContext(const DeclContext *DC)
void untrackIfClosed(const Decl *D, DeclShape S, uint32_t Confirmed)
Stop tracking Decl D when it no longer needs verification.
void syncSweepTracking(const Decl *D, DeclShape S, uint32_t Kinds, bool IsNew)
void RestorePTU(TranslationUnitDecl *MostRecentTU)
void trackForSweep(const Decl *D, uint32_t Kinds)
Start tracking D for mutation kinds that need verification.
void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) override
A template specialization (or partial one) was added to the template declaration.
Provides common interface for the Decls that can be redeclared.
static DeclLink LatestDeclLink(const ASTContext &Ctx)
void restoreSpecialMemberCache(PTUID ID)
void restoreVTableUsed(CXXRecordDecl *RD)
Sema - This implements semantic analysis and AST building for C.
An array of decls optimized for the common case of only containing one entry.
void addOrReplaceDecl(NamedDecl *D)
If this is a redeclaration of an existing decl, replace the old one with D.
void remove(NamedDecl *D)
void sweep(PTUMutationActions &Act, OnConfirmedFn OnConfirmed)
A single function called during commit and restore (when the PTU is uncommitted).
Represents the declaration of a struct/union/class/enum.
redecl_range redecls() const
Returns an iterator range for all the redeclarations of the same decl.
bool isThisDeclarationADefinition() const
Return true if this declaration is a completion definition of the type.
The top declaration context.
Base class for declarations which introduce a typedef-name.
Represents a shadow declaration implicitly introduced into a scope by a (resolved) using-declaration ...
Represents a variable declaration or definition.
Declaration of a variable template.
VarDecl * getTemplatedDecl() const
Get the underlying variable declarations of the template.
VarTemplateDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this template.
const internal::ArgumentAdaptingMatcherFunc< internal::HasMatcher > has
Matches AST nodes that have child AST nodes that match the provided matcher.
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
unsigned PTUID
Index of a per PTU State.
static FootprintT Footprint(const OwnerT &Owner)
MutationRecord::MutationKind MutationKind
static DeclShape classifyShape(const Decl *D)
static constexpr uint32_t KindsNeedingVerification
These kinds have listeners, but listeners alone cannot reliably detect real changes.
static bool isReachableFromThisTU(const Decl *D, const TranslationUnitDecl *ThisTU)
static bool differsFromLast(IncrementalStateTracker &Tracker, const Decl *Owner, MutationKind K, const T &LiveValue)
MutationRecord::DeclShape DeclShape
U cast(CodeGen::Address addr)
const TranslationUnitDecl * ThisPTU
llvm::MapVector< const Decl *, MutationRecord > Mutations
void verifyMutations(PTUMutationActions &Actions)
Verify recorded mutations and remove any false mutation ones.
bool HadImplicitCXXMember
Set exactly when AddedCXXImplicitMember fires this PTU.
void noteMutated(const Decl *D, DeclShape S, KindT K)
llvm::SmallVector< std::pair< const Decl *, MutationKind >, 8 > TouchedFootprints
Every (Owner, Kind) key this PTU's own commit() wrote a FootprintStore entry for.
llvm::SmallPtrSet< const Decl *, 4 > ImplicitlyGeneratedDecls