Skip to content

Commit aec9283

Browse files
[clang-repl] Refactor locking of runtime PTU stack (NFC) (#84176)
The Interpreter locks PTUs that originate from implicit runtime code and initialization to prevent users from undoing them accidentally. The previous implementation seemed hacky, because it required the reader to be familiar with the internal workings of the PTU stack. The concept itself is a pragmatic solution and not very surprising. This patch introduces a function for it and adds a comment.
1 parent 702e2da commit aec9283

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

clang/include/clang/Interpreter/Interpreter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ class Interpreter {
169169

170170
private:
171171
size_t getEffectivePTUSize() const;
172+
void markUserCodeStart();
172173

173174
llvm::DenseMap<CXXRecordDecl *, llvm::orc::ExecutorAddr> Dtors;
174175

clang/lib/Interpreter/Interpreter.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,14 @@ Interpreter::create(std::unique_ptr<CompilerInstance> CI) {
280280
if (Err)
281281
return std::move(Err);
282282

283+
// Add runtime code and set a marker to hide it from user code. Undo will not
284+
// go through that.
283285
auto PTU = Interp->Parse(Runtimes);
284286
if (!PTU)
285287
return PTU.takeError();
288+
Interp->markUserCodeStart();
286289

287290
Interp->ValuePrintingInfo.resize(4);
288-
// FIXME: This is a ugly hack. Undo command checks its availability by looking
289-
// at the size of the PTU list. However we have parsed something in the
290-
// beginning of the REPL so we have to mark them as 'Irrevocable'.
291-
Interp->InitPTUSize = Interp->IncrParser->getPTUs().size();
292291
return std::move(Interp);
293292
}
294293

@@ -345,6 +344,11 @@ const ASTContext &Interpreter::getASTContext() const {
345344
return getCompilerInstance()->getASTContext();
346345
}
347346

347+
void Interpreter::markUserCodeStart() {
348+
assert(!InitPTUSize && "We only do this once");
349+
InitPTUSize = IncrParser->getPTUs().size();
350+
}
351+
348352
size_t Interpreter::getEffectivePTUSize() const {
349353
std::list<PartialTranslationUnit> &PTUs = IncrParser->getPTUs();
350354
assert(PTUs.size() >= InitPTUSize && "empty PTU list?");

0 commit comments

Comments
 (0)