[PATCH] D76937: Fix infinite recursion in deferred diagnostic emitter

Yaxun Liu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 27 09:50:36 PDT 2020


yaxunl created this revision.
yaxunl added reviewers: rjmccall, ABataev.
Herald added a reviewer: jdoerfert.

Currently deferred diagnostic emitter checks variable decl in DeclRefExpr, which
causes infinite recursion for cases like `long a = (long)&a;`.

Deferred diagnostic emitter does not need check variable decls in DeclRefExpr
since reference of a variable does not cause emission of functions directly or
indirectly. Therefore there is no need to check variable decls in DeclRefExpr.


https://reviews.llvm.org/D76937

Files:
  clang/lib/Sema/Sema.cpp
  clang/test/OpenMP/nvptx_target_exceptions_messages.cpp


Index: clang/test/OpenMP/nvptx_target_exceptions_messages.cpp
===================================================================
--- clang/test/OpenMP/nvptx_target_exceptions_messages.cpp
+++ clang/test/OpenMP/nvptx_target_exceptions_messages.cpp
@@ -1,5 +1,10 @@
-// RUN: %clang_cc1 -fopenmp -x c++ -triple powerpc64le-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm-bc %s -o %t-ppc-host.bc -fexceptions -fcxx-exceptions
-// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple nvptx64-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm %s -fopenmp-is-device -fopenmp-host-ir-file-path %t-ppc-host.bc -o - -fexceptions -fcxx-exceptions -ferror-limit 100
+// RUN: %clang_cc1 -fopenmp -x c++ -triple powerpc64le-unknown-unknown \
+// RUN:   -verify=host -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm-bc \
+// RUN:   %s -o %t-ppc-host.bc -fexceptions -fcxx-exceptions
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple nvptx64-unknown-unknown \
+// RUN:   -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm %s \
+// RUN:   -fopenmp-is-device -fopenmp-host-ir-file-path %t-ppc-host.bc -o - \
+// RUN:   -fexceptions -fcxx-exceptions -ferror-limit 100
 
 #ifndef HEADER
 #define HEADER
@@ -81,4 +86,17 @@
 int foobar1() { throw 1; }
 int foobar2() { throw 1; } // expected-error {{cannot use 'throw' with exceptions disabled}}
 
+
+int foobar3();
+int (*C)() = &foobar3; // expected-warning {{declaration is not declared in any declare target region}}
+                       // host-warning at -1 {{declaration is not declared in any declare target region}}
+#pragma omp declare target
+int (*D)() = C; // expected-note {{used here}}
+                // host-note at -1 {{used here}}
+#pragma omp end declare target
+int foobar3() { throw 1; }
+
+// Check no infinite recursion in deferred diagnostic emitter.
+long E = (long)&E;
+
 #endif // HEADER
Index: clang/lib/Sema/Sema.cpp
===================================================================
--- clang/lib/Sema/Sema.cpp
+++ clang/lib/Sema/Sema.cpp
@@ -1494,6 +1494,13 @@
   DeferredDiagnosticsEmitter(Sema &S)
       : Inherited(S), ShouldEmit(false), InOMPDeviceContext(0) {}
 
+  void VisitDeclRefExpr(DeclRefExpr *E) {
+    auto *D = E->getDecl();
+    if (isa<FunctionDecl>(D)) {
+      visitUsedDecl(E->getLocation(), D);
+    }
+  }
+
   void VisitOMPTargetDirective(OMPTargetDirective *Node) {
     ++InOMPDeviceContext;
     Inherited::VisitOMPTargetDirective(Node);
@@ -1525,6 +1532,11 @@
       UseStack.pop_back();
       Visited.erase(D);
     } else if (auto *VD = dyn_cast<VarDecl>(D)) {
+      assert(Visited.count(D) == 0 &&
+             "Variables should not recursively visited");
+      Visited.insert(D);
+      assert(VD->hasGlobalStorage() &&
+             "Should only check variables with global storage");
       if (auto *Init = VD->getInit()) {
         auto DevTy = OMPDeclareTargetDeclAttr::getDeviceType(VD);
         bool IsDev = DevTy && (*DevTy == OMPDeclareTargetDeclAttr::DT_NoHost ||
@@ -1535,6 +1547,7 @@
         if (IsDev)
           --InOMPDeviceContext;
       }
+      Visited.erase(D);
     } else
       Inherited::visitUsedDecl(Loc, D);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76937.253149.patch
Type: text/x-patch
Size: 3161 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200327/95c11a73/attachment.bin>


More information about the cfe-commits mailing list