[clang] 7731156 - [clang] OpenMP: fix variant template mismatch crash (#164511)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 23 09:38:03 PDT 2025


Author: Matheus Izvekov
Date: 2025-10-23T13:37:59-03:00
New Revision: 7731156f30c6e7731e08b4ff52d262be10f93b16

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

LOG: [clang] OpenMP: fix variant template mismatch crash (#164511)

This ammends the fix commited in https://reviews.llvm.org/D109770 /
6cf6fa6ef1c28

Comparing the number of template parameter lists with the number of
template parameters is obviously wrong.

Even then, the number of parameters being the same doesn't mean the
templates are compatible.

This change compares if the template parameters are actually equivalent.

This fixes the crash, but I am not sure what is the design and intention
here, this openmp template support looks too fragile.

The added test case still doesn't work, but at least we don't crash now.

Added: 
    clang/test/SemaOpenMP/openmp-begin-declare-variant_template.cpp

Modified: 
    clang/lib/Sema/SemaOpenMP.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 5b5b1b685e153..6d5cb0fcaea24 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -7246,7 +7246,9 @@ void SemaOpenMP::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope(
     FunctionDecl *UDecl = nullptr;
     if (IsTemplated && isa<FunctionTemplateDecl>(CandidateDecl)) {
       auto *FTD = cast<FunctionTemplateDecl>(CandidateDecl);
-      if (FTD->getTemplateParameters()->size() == TemplateParamLists.size())
+      // FIXME: Should this compare the template parameter lists on all levels?
+      if (SemaRef.Context.isSameTemplateParameterList(
+              FTD->getTemplateParameters(), TemplateParamLists.back()))
         UDecl = FTD->getTemplatedDecl();
     } else if (!IsTemplated)
       UDecl = dyn_cast<FunctionDecl>(CandidateDecl);

diff  --git a/clang/test/SemaOpenMP/openmp-begin-declare-variant_template.cpp b/clang/test/SemaOpenMP/openmp-begin-declare-variant_template.cpp
new file mode 100644
index 0000000000000..ded8f58253540
--- /dev/null
+++ b/clang/test/SemaOpenMP/openmp-begin-declare-variant_template.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -triple x86_64 -fopenmp -verify %s
+
+// FIXME: Is this supposed to work?
+
+#pragma omp begin declare variant match(implementation={extension(allow_templates)})
+template <class T> void f(T) {}
+// expected-note at -1 {{explicit instantiation refers here}}
+#pragma end
+template <int> struct A {};
+template <bool B> A<B> f() = delete;
+template void f<float>(float);
+// expected-error at -1 {{explicit instantiation of undefined function template 'f'}}


        


More information about the cfe-commits mailing list