[PATCH] D125944: Template instantiation error recovery

Purva Chaudhari via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 18 22:46:03 PDT 2022


Purva-Chaudhari created this revision.
Purva-Chaudhari added a reviewer: v.g.vassilev.
Herald added a project: All.
Purva-Chaudhari requested review of this revision.

If error was encountered after template instantiation, the clang-repl interactive mode was aborted. The patch adds recovery support for template instantiation

Eg:

  purva at purva-HP-Laptop-15-bs0xx:~/llvm-project/build$ bin/clang-repl
  clang-repl> template<class T> T f() { return T(); }
  clang-repl> auto ptu2 = f<float>(); err;
  In file included from <<< inputs >>>:1:
  input_line_1:1:25: error: C++ requires a type specifier for all declarations
  auto ptu2 = f<float>(); err;
                          ^
  clang-repl: /home/purva/llvm-project/clang/include/clang/Sema/Sema.h:9406: clang::Sema::GlobalEagerInstantiationScope::~GlobalEagerInstantiationScope(): Assertion `S.PendingInstantiations.empty() && "PendingInstantiations should be empty before it is discarded."' failed.
  Aborted
   (core dumped)


https://reviews.llvm.org/D125944

Files:
  clang/include/clang/Sema/Sema.h
  clang/lib/Interpreter/IncrementalParser.cpp
  clang/test/Interpreter/execute.cpp


Index: clang/include/clang/Sema/Sema.h
===================================================================
--- clang/include/clang/Sema/Sema.h
+++ clang/include/clang/Sema/Sema.h
@@ -9456,6 +9456,19 @@
         SavedPendingLocalImplicitInstantiations;
   };
 
+  class PerformPendingInstantiationsRAII {
+  public:
+    PerformPendingInstantiationsRAII(Sema &S): S(S) {} ;
+
+    ~PerformPendingInstantiationsRAII() {
+      S.PerformPendingInstantiations();
+      assert(S.PendingInstantiations.empty() &&
+             "there shouldn't be any pending instantiations");
+    }
+  private:
+    Sema &S;
+  };
+
   /// A helper class for building up ExtParameterInfos.
   class ExtParameterInfoBuilder {
     SmallVector<FunctionProtoType::ExtParameterInfo, 16> Infos;
Index: clang/lib/Interpreter/IncrementalParser.cpp
===================================================================
--- clang/lib/Interpreter/IncrementalParser.cpp
+++ clang/lib/Interpreter/IncrementalParser.cpp
@@ -177,6 +177,7 @@
   }
 
   DiagnosticsEngine &Diags = getCI()->getDiagnostics();
+  Sema::PerformPendingInstantiationsRAII PerformPendingInstantiations(S);
   if (Diags.hasErrorOccurred()) {
     TranslationUnitDecl *MostRecentTU = C.getTranslationUnitDecl();

Index: clang/test/Interpreter/execute.cpp
===================================================================
--- clang/test/Interpreter/execute.cpp
+++ clang/test/Interpreter/execute.cpp
@@ -1,3 +1,5 @@
+// RUN: clang-repl "template<class T> T f() { return T(); }" "auto ptu2 = f<float>(); err;" \
+// RUN: "auto ptu2 = f<float>();" "int i = 0;"
 // RUN: clang-repl "int i = 10;" 'extern "C" int printf(const char*,...);' \
 // RUN:            'auto r1 = printf("i = %d\n", i);' | FileCheck --check-prefix=CHECK-DRIVER %s
 // REQUIRES: host-supports-jit


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125944.430569.patch
Type: text/x-patch
Size: 1804 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220519/2a5d7955/attachment-0001.bin>


More information about the cfe-commits mailing list