[Clang][OpenMP] Bail out early if `Scope` is nullptr in case of any crash
authorShilei Tian <i@tianshilei.me>
Fri, 20 Jan 2023 19:40:16 +0000 (14:40 -0500)
committerShilei Tian <i@tianshilei.me>
Fri, 20 Jan 2023 19:40:24 +0000 (14:40 -0500)
When there is any compile error, clang still tries to compile as many code as
possible, therefore `Scope` can be `nullptr` here. However, we didn't check it
beforehand, causing compiler crash.

Fix #59944.

Reviewed By: ABataev

Differential Revision: https://reviews.llvm.org/D142233

clang/lib/Sema/SemaOpenMP.cpp
clang/test/OpenMP/bug59944.c [new file with mode: 0644]

index 97eba46..1b9d06b 100644 (file)
@@ -7259,7 +7259,7 @@ ExprResult Sema::ActOnOpenMPCall(ExprResult Call, Scope *Scope,
   if (LangOpts.OpenMP >= 51 && CalleeFnDecl->getIdentifier() &&
       CalleeFnDecl->getName().startswith_insensitive("omp_")) {
     // checking for any calls inside an Order region
-    if (Scope->isOpenMPOrderClauseScope())
+    if (Scope && Scope->isOpenMPOrderClauseScope())
       Diag(LParenLoc, diag::err_omp_unexpected_call_to_omp_runtime_api);
   }
 
diff --git a/clang/test/OpenMP/bug59944.c b/clang/test/OpenMP/bug59944.c
new file mode 100644 (file)
index 0000000..6d061b5
--- /dev/null
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -fopenmp -fopenmp-version=52 -x c -triple x86_64-apple-darwin10 %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK
+
+extern int omp_get_initial_device();
+extern void *omp_get_mapped_ptr(void *, int);
+
+void t() {
+  omp_get_mapped_ptr(&x, omp_get_initial_device());
+}
+
+// CHECK: error: use of undeclared identifier 'x'
+// CHECK-NOT: crash