Skip to content

Commit 11b47c1

Browse files
committed
Reland "[clang-repl] Implement partial translation units and error recovery."
Original commit message: [clang-repl] Implement partial translation units and error recovery. https://reviews.llvm.org/D96033 contained a discussion regarding efficient modeling of error recovery. @rjmccall has outlined the key ideas: Conceptually, we can split the translation unit into a sequence of partial translation units (PTUs). Every declaration will be associated with a unique PTU that owns it. The first key insight here is that the owning PTU isn't always the "active" (most recent) PTU, and it isn't always the PTU that the declaration "comes from". A new declaration (that isn't a redeclaration or specialization of anything) does belong to the active PTU. A template specialization, however, belongs to the most recent PTU of all the declarations in its signature - mostly that means that it can be pulled into a more recent PTU by its template arguments. The second key insight is that processing a PTU might extend an earlier PTU. Rolling back the later PTU shouldn't throw that extension away. For example, if the second PTU defines a template, and the third PTU requires that template to be instantiated at float, that template specialization is still part of the second PTU. Similarly, if the fifth PTU uses an inline function belonging to the fourth, that definition still belongs to the fourth. When we go to emit code in a new PTU, we map each declaration we have to emit back to its owning PTU and emit it in a new module for just the extensions to that PTU. We keep track of all the modules we've emitted for a PTU so that we can unload them all if we decide to roll it back. Most declarations/definitions will only refer to entities from the same or earlier PTUs. However, it is possible (primarily by defining a previously-declared entity, but also through templates or ADL) for an entity that belongs to one PTU to refer to something from a later PTU. We will have to keep track of this and prevent unwinding to later PTU when we recognize it. Fortunately, this should be very rare; and crucially, we don't have to do the bookkeeping for this if we've only got one PTU, e.g. in normal compilation. Otherwise, PTUs after the first just need to record enough metadata to be able to revert any changes they've made to declarations belonging to earlier PTUs, e.g. to redeclaration chains or template specialization lists. It should even eventually be possible for PTUs to provide their own slab allocators which can be thrown away as part of rolling back the PTU. We can maintain a notion of the active allocator and allocate things like Stmt/Expr nodes in it, temporarily changing it to the appropriate PTU whenever we go to do something like instantiate a function template. More care will be required when allocating declarations and types, though. We would want the PTU to be efficiently recoverable from a Decl; I'm not sure how best to do that. An easy option that would cover most declarations would be to make multiple TranslationUnitDecls and parent the declarations appropriately, but I don't think that's good enough for things like member function templates, since an instantiation of that would still be parented by its original class. Maybe we can work this into the DC chain somehow, like how lexical DCs are. We add a different kind of translation unit `TU_Incremental` which is a complete translation unit that we might nonetheless incrementally extend later. Because it is complete (and we might want to generate code for it), we do perform template instantiation, but because it might be extended later, we don't warn if it declares or uses undefined internal-linkage symbols. This patch teaches clang-repl how to recover from errors by disconnecting the most recent PTU and update the primary PTU lookup tables. For instance: ```./clang-repl clang-repl> int i = 12; error; In file included from <<< inputs >>>:1: input_line_0:1:13: error: C++ requires a type specifier for all declarations int i = 12; error; ^ error: Parsing failed. clang-repl> int i = 13; extern "C" int printf(const char*,...); clang-repl> auto r1 = printf("i=%d\n", i); i=13 clang-repl> quit ``` Differential revision: https://reviews.llvm.org/D104918
1 parent 112c090 commit 11b47c1

24 files changed

+226
-126
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
459459
friend class ASTWriter;
460460
template <class> friend class serialization::AbstractTypeReader;
461461
friend class CXXRecordDecl;
462+
friend class IncrementalParser;
462463

463464
/// A mapping to contain the template or declaration that
464465
/// a variable declaration describes or was instantiated from,
@@ -567,7 +568,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
567568
ImportDecl *FirstLocalImport = nullptr;
568569
ImportDecl *LastLocalImport = nullptr;
569570

570-
TranslationUnitDecl *TUDecl;
571+
TranslationUnitDecl *TUDecl = nullptr;
571572
mutable ExternCContextDecl *ExternCContext = nullptr;
572573
mutable BuiltinTemplateDecl *MakeIntegerSeqDecl = nullptr;
573574
mutable BuiltinTemplateDecl *TypePackElementDecl = nullptr;
@@ -624,6 +625,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
624625
IdentifierTable &Idents;
625626
SelectorTable &Selectors;
626627
Builtin::Context &BuiltinInfo;
628+
const TranslationUnitKind TUKind;
627629
mutable DeclarationNameTable DeclarationNames;
628630
IntrusiveRefCntPtr<ExternalASTSource> ExternalSource;
629631
ASTMutationListener *Listener = nullptr;
@@ -1022,7 +1024,18 @@ class ASTContext : public RefCountedBase<ASTContext> {
10221024
/// Get the initializations to perform when importing a module, if any.
10231025
ArrayRef<Decl*> getModuleInitializers(Module *M);
10241026

1025-
TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; }
1027+
TranslationUnitDecl *getTranslationUnitDecl() const {
1028+
return TUDecl->getMostRecentDecl();
1029+
}
1030+
void addTranslationUnitDecl() {
1031+
assert(!TUDecl || TUKind == TU_Incremental);
1032+
TranslationUnitDecl *NewTUDecl = TranslationUnitDecl::Create(*this);
1033+
if (TraversalScope.empty() || TraversalScope.back() == TUDecl)
1034+
TraversalScope = {NewTUDecl};
1035+
if (TUDecl)
1036+
NewTUDecl->setPreviousDecl(TUDecl);
1037+
TUDecl = NewTUDecl;
1038+
}
10261039

10271040
ExternCContextDecl *getExternCContextDecl() const;
10281041
BuiltinTemplateDecl *getMakeIntegerSeqDecl() const;
@@ -1099,7 +1112,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
10991112
llvm::DenseSet<const VarDecl *> CUDADeviceVarODRUsedByHost;
11001113

11011114
ASTContext(LangOptions &LOpts, SourceManager &SM, IdentifierTable &idents,
1102-
SelectorTable &sels, Builtin::Context &builtins);
1115+
SelectorTable &sels, Builtin::Context &builtins,
1116+
TranslationUnitKind TUKind);
11031117
ASTContext(const ASTContext &) = delete;
11041118
ASTContext &operator=(const ASTContext &) = delete;
11051119
~ASTContext();

clang/include/clang/AST/Decl.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,23 @@ class UnresolvedSetImpl;
7979
class VarTemplateDecl;
8080

8181
/// The top declaration context.
82-
class TranslationUnitDecl : public Decl, public DeclContext {
82+
class TranslationUnitDecl : public Decl,
83+
public DeclContext,
84+
public Redeclarable<TranslationUnitDecl> {
85+
using redeclarable_base = Redeclarable<TranslationUnitDecl>;
86+
87+
TranslationUnitDecl *getNextRedeclarationImpl() override {
88+
return getNextRedeclaration();
89+
}
90+
91+
TranslationUnitDecl *getPreviousDeclImpl() override {
92+
return getPreviousDecl();
93+
}
94+
95+
TranslationUnitDecl *getMostRecentDeclImpl() override {
96+
return getMostRecentDecl();
97+
}
98+
8399
ASTContext &Ctx;
84100

85101
/// The (most recently entered) anonymous namespace for this
@@ -91,6 +107,16 @@ class TranslationUnitDecl : public Decl, public DeclContext {
91107
virtual void anchor();
92108

93109
public:
110+
using redecl_range = redeclarable_base::redecl_range;
111+
using redecl_iterator = redeclarable_base::redecl_iterator;
112+
113+
using redeclarable_base::getMostRecentDecl;
114+
using redeclarable_base::getPreviousDecl;
115+
using redeclarable_base::isFirstDecl;
116+
using redeclarable_base::redecls;
117+
using redeclarable_base::redecls_begin;
118+
using redeclarable_base::redecls_end;
119+
94120
ASTContext &getASTContext() const { return Ctx; }
95121

96122
NamespaceDecl *getAnonymousNamespace() const { return AnonymousNamespace; }

clang/include/clang/AST/Redeclarable.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ class Redeclarable {
193193
public:
194194
friend class ASTDeclReader;
195195
friend class ASTDeclWriter;
196+
friend class IncrementalParser;
196197

197198
Redeclarable(const ASTContext &Ctx)
198199
: RedeclLink(LatestDeclLink(Ctx)),

clang/include/clang/Basic/LangOptions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,11 @@ enum TranslationUnitKind {
697697
TU_Prefix,
698698

699699
/// The translation unit is a module.
700-
TU_Module
700+
TU_Module,
701+
702+
/// The translation unit is a is a complete translation unit that we might
703+
/// incrementally extend later.
704+
TU_Incremental
701705
};
702706

703707
} // namespace clang

clang/include/clang/Interpreter/Interpreter.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#ifndef LLVM_CLANG_INTERPRETER_INTERPRETER_H
1515
#define LLVM_CLANG_INTERPRETER_INTERPRETER_H
1616

17-
#include "clang/Interpreter/Transaction.h"
17+
#include "clang/Interpreter/PartialTranslationUnit.h"
1818

1919
#include "llvm/Support/Error.h"
2020

@@ -55,14 +55,14 @@ class Interpreter {
5555
static llvm::Expected<std::unique_ptr<Interpreter>>
5656
create(std::unique_ptr<CompilerInstance> CI);
5757
const CompilerInstance *getCompilerInstance() const;
58-
llvm::Expected<Transaction &> Parse(llvm::StringRef Code);
59-
llvm::Error Execute(Transaction &T);
58+
llvm::Expected<PartialTranslationUnit &> Parse(llvm::StringRef Code);
59+
llvm::Error Execute(PartialTranslationUnit &T);
6060
llvm::Error ParseAndExecute(llvm::StringRef Code) {
61-
auto ErrOrTransaction = Parse(Code);
62-
if (auto Err = ErrOrTransaction.takeError())
63-
return Err;
64-
if (ErrOrTransaction->TheModule)
65-
return Execute(*ErrOrTransaction);
61+
auto PTU = Parse(Code);
62+
if (!PTU)
63+
return PTU.takeError();
64+
if (PTU->TheModule)
65+
return Execute(*PTU);
6666
return llvm::Error::success();
6767
}
6868
};

clang/include/clang/Interpreter/Transaction.h renamed to clang/include/clang/Interpreter/PartialTranslationUnit.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,27 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#ifndef LLVM_CLANG_INTERPRETER_TRANSACTION_H
15-
#define LLVM_CLANG_INTERPRETER_TRANSACTION_H
14+
#ifndef LLVM_CLANG_INTERPRETER_PARTIALTRANSLATIONUNIT_H
15+
#define LLVM_CLANG_INTERPRETER_PARTIALTRANSLATIONUNIT_H
1616

1717
#include <memory>
18-
#include <vector>
1918

2019
namespace llvm {
2120
class Module;
2221
}
2322

2423
namespace clang {
2524

26-
class DeclGroupRef;
25+
class TranslationUnitDecl;
2726

2827
/// The class keeps track of various objects created as part of processing
2928
/// incremental inputs.
30-
struct Transaction {
31-
/// The decls created for the input.
32-
std::vector<clang::DeclGroupRef> Decls;
29+
struct PartialTranslationUnit {
30+
TranslationUnitDecl *TUPart = nullptr;
3331

3432
/// The llvm IR produced for the input.
3533
std::unique_ptr<llvm::Module> TheModule;
3634
};
3735
} // namespace clang
3836

39-
#endif // LLVM_CLANG_INTERPRETER_TRANSACTION_H
37+
#endif // LLVM_CLANG_INTERPRETER_PARTIALTRANSLATIONUNIT_H

clang/include/clang/Lex/Preprocessor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,11 @@ class Preprocessor {
264264
/// avoid tearing the Lexer and etc. down).
265265
bool IncrementalProcessing = false;
266266

267+
public:
267268
/// The kind of translation unit we are processing.
268-
TranslationUnitKind TUKind;
269+
const TranslationUnitKind TUKind;
269270

271+
private:
270272
/// The code-completion handler.
271273
CodeCompletionHandler *CodeComplete = nullptr;
272274

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,7 @@ class Sema final {
13771377
/// initializers for tentative definitions in C) once parsing has
13781378
/// completed. Modules and precompiled headers perform different kinds of
13791379
/// checks.
1380-
TranslationUnitKind TUKind;
1380+
const TranslationUnitKind TUKind;
13811381

13821382
llvm::BumpPtrAllocator BumpAlloc;
13831383

clang/lib/AST/ASTContext.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ static bool isAddrSpaceMapManglingEnabled(const TargetInfo &TI,
966966

967967
ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM,
968968
IdentifierTable &idents, SelectorTable &sels,
969-
Builtin::Context &builtins)
969+
Builtin::Context &builtins, TranslationUnitKind TUKind)
970970
: ConstantArrayTypes(this_()), FunctionProtoTypes(this_()),
971971
TemplateSpecializationTypes(this_()),
972972
DependentTemplateSpecializationTypes(this_()), AutoTypes(this_()),
@@ -978,11 +978,10 @@ ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM,
978978
LangOpts.XRayAttrListFiles, SM)),
979979
ProfList(new ProfileList(LangOpts.ProfileListFiles, SM)),
980980
PrintingPolicy(LOpts), Idents(idents), Selectors(sels),
981-
BuiltinInfo(builtins), DeclarationNames(*this), Comments(SM),
982-
CommentCommandTraits(BumpAlloc, LOpts.CommentOpts),
981+
BuiltinInfo(builtins), TUKind(TUKind), DeclarationNames(*this),
982+
Comments(SM), CommentCommandTraits(BumpAlloc, LOpts.CommentOpts),
983983
CompCategories(this_()), LastSDM(nullptr, 0) {
984-
TUDecl = TranslationUnitDecl::Create(*this);
985-
TraversalScope = {TUDecl};
984+
addTranslationUnitDecl();
986985
}
987986

988987
ASTContext::~ASTContext() {
@@ -1196,9 +1195,10 @@ ExternCContextDecl *ASTContext::getExternCContextDecl() const {
11961195
BuiltinTemplateDecl *
11971196
ASTContext::buildBuiltinTemplateDecl(BuiltinTemplateKind BTK,
11981197
const IdentifierInfo *II) const {
1199-
auto *BuiltinTemplate = BuiltinTemplateDecl::Create(*this, TUDecl, II, BTK);
1198+
auto *BuiltinTemplate =
1199+
BuiltinTemplateDecl::Create(*this, getTranslationUnitDecl(), II, BTK);
12001200
BuiltinTemplate->setImplicit();
1201-
TUDecl->addDecl(BuiltinTemplate);
1201+
getTranslationUnitDecl()->addDecl(BuiltinTemplate);
12021202

12031203
return BuiltinTemplate;
12041204
}
@@ -1485,7 +1485,7 @@ void ASTContext::InitBuiltinTypes(const TargetInfo &Target,
14851485
// MSVC predeclares struct _GUID, and we need it to create MSGuidDecls.
14861486
if (LangOpts.MicrosoftExt || LangOpts.Borland) {
14871487
MSGuidTagDecl = buildImplicitRecord("_GUID");
1488-
TUDecl->addDecl(MSGuidTagDecl);
1488+
getTranslationUnitDecl()->addDecl(MSGuidTagDecl);
14891489
}
14901490
}
14911491

@@ -6622,7 +6622,7 @@ QualType ASTContext::getCFConstantStringType() const {
66226622
QualType ASTContext::getObjCSuperType() const {
66236623
if (ObjCSuperType.isNull()) {
66246624
RecordDecl *ObjCSuperTypeDecl = buildImplicitRecord("objc_super");
6625-
TUDecl->addDecl(ObjCSuperTypeDecl);
6625+
getTranslationUnitDecl()->addDecl(ObjCSuperTypeDecl);
66266626
ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl);
66276627
}
66286628
return ObjCSuperType;

clang/lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ bool Decl::isOutOfLine() const {
102102

103103
TranslationUnitDecl::TranslationUnitDecl(ASTContext &ctx)
104104
: Decl(TranslationUnit, nullptr, SourceLocation()),
105-
DeclContext(TranslationUnit), Ctx(ctx) {}
105+
DeclContext(TranslationUnit), redeclarable_base(ctx), Ctx(ctx) {}
106106

107107
//===----------------------------------------------------------------------===//
108108
// NamedDecl Implementation

clang/lib/AST/DeclBase.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,6 @@ bool DeclContext::Encloses(const DeclContext *DC) const {
12191219

12201220
DeclContext *DeclContext::getPrimaryContext() {
12211221
switch (getDeclKind()) {
1222-
case Decl::TranslationUnit:
12231222
case Decl::ExternCContext:
12241223
case Decl::LinkageSpec:
12251224
case Decl::Export:
@@ -1231,6 +1230,8 @@ DeclContext *DeclContext::getPrimaryContext() {
12311230
// There is only one DeclContext for these entities.
12321231
return this;
12331232

1233+
case Decl::TranslationUnit:
1234+
return static_cast<TranslationUnitDecl *>(this)->getFirstDecl();
12341235
case Decl::Namespace:
12351236
// The original namespace is our primary context.
12361237
return static_cast<NamespaceDecl *>(this)->getOriginalNamespace();
@@ -1285,21 +1286,25 @@ DeclContext *DeclContext::getPrimaryContext() {
12851286
}
12861287
}
12871288

1288-
void
1289-
DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
1290-
Contexts.clear();
1289+
template <typename T>
1290+
void collectAllContextsImpl(T *Self, SmallVectorImpl<DeclContext *> &Contexts) {
1291+
for (T *D = Self->getMostRecentDecl(); D; D = D->getPreviousDecl())
1292+
Contexts.push_back(D);
12911293

1292-
if (getDeclKind() != Decl::Namespace) {
1293-
Contexts.push_back(this);
1294-
return;
1295-
}
1294+
std::reverse(Contexts.begin(), Contexts.end());
1295+
}
12961296

1297-
auto *Self = static_cast<NamespaceDecl *>(this);
1298-
for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
1299-
N = N->getPreviousDecl())
1300-
Contexts.push_back(N);
1297+
void DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts) {
1298+
Contexts.clear();
13011299

1302-
std::reverse(Contexts.begin(), Contexts.end());
1300+
Decl::Kind Kind = getDeclKind();
1301+
1302+
if (Kind == Decl::TranslationUnit)
1303+
collectAllContextsImpl(static_cast<TranslationUnitDecl *>(this), Contexts);
1304+
else if (Kind == Decl::Namespace)
1305+
collectAllContextsImpl(static_cast<NamespaceDecl *>(this), Contexts);
1306+
else
1307+
Contexts.push_back(this);
13031308
}
13041309

13051310
std::pair<Decl *, Decl *>

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,8 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
807807
if (ToLoad >= LoadASTOnly)
808808
AST->Ctx = new ASTContext(*AST->LangOpts, AST->getSourceManager(),
809809
PP.getIdentifierTable(), PP.getSelectorTable(),
810-
PP.getBuiltinInfo());
810+
PP.getBuiltinInfo(),
811+
AST->getTranslationUnitKind());
811812

812813
DisableValidationForModuleKind disableValid =
813814
DisableValidationForModuleKind::None;

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ void CompilerInstance::createASTContext() {
551551
Preprocessor &PP = getPreprocessor();
552552
auto *Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
553553
PP.getIdentifierTable(), PP.getSelectorTable(),
554-
PP.getBuiltinInfo());
554+
PP.getBuiltinInfo(), PP.TUKind);
555555
Context->InitBuiltinTypes(getTarget(), getAuxTarget());
556556
setASTContext(Context);
557557
}

0 commit comments

Comments
 (0)