Skip to content

Commit

Permalink
[clang-repl] Refactor locking of runtime PTU stack (NFC) (#84176)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
weliveindetail committed Mar 11, 2024
1 parent 702e2da commit aec9283
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions clang/include/clang/Interpreter/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class Interpreter {

private:
size_t getEffectivePTUSize() const;
void markUserCodeStart();

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

Expand Down
12 changes: 8 additions & 4 deletions clang/lib/Interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,14 @@ Interpreter::create(std::unique_ptr<CompilerInstance> CI) {
if (Err)
return std::move(Err);

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

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

Expand Down Expand Up @@ -345,6 +344,11 @@ const ASTContext &Interpreter::getASTContext() const {
return getCompilerInstance()->getASTContext();
}

void Interpreter::markUserCodeStart() {
assert(!InitPTUSize && "We only do this once");
InitPTUSize = IncrParser->getPTUs().size();
}

size_t Interpreter::getEffectivePTUSize() const {
std::list<PartialTranslationUnit> &PTUs = IncrParser->getPTUs();
assert(PTUs.size() >= InitPTUSize && "empty PTU list?");
Expand Down

0 comments on commit aec9283

Please sign in to comment.