[clang] aec9283 - [clang-repl] Refactor locking of runtime PTU stack (NFC) (#84176)

via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 11 05:39:27 PDT 2024


Author: Stefan Gränitz
Date: 2024-03-11T13:39:23+01:00
New Revision: aec92830b79a8c49cdce0d592627d5f18bb6370b

URL: https://github.com/llvm/llvm-project/commit/aec92830b79a8c49cdce0d592627d5f18bb6370b
DIFF: https://github.com/llvm/llvm-project/commit/aec92830b79a8c49cdce0d592627d5f18bb6370b.diff

LOG: [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.

Added: 
    

Modified: 
    clang/include/clang/Interpreter/Interpreter.h
    clang/lib/Interpreter/Interpreter.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h
index d972d960dcb7cd..469ce1fd75bf84 100644
--- a/clang/include/clang/Interpreter/Interpreter.h
+++ b/clang/include/clang/Interpreter/Interpreter.h
@@ -169,6 +169,7 @@ class Interpreter {
 
 private:
   size_t getEffectivePTUSize() const;
+  void markUserCodeStart();
 
   llvm::DenseMap<CXXRecordDecl *, llvm::orc::ExecutorAddr> Dtors;
 

diff  --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index 3485da8196683a..e293fefb524963 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -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);
 }
 
@@ -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?");


        


More information about the cfe-commits mailing list