[clang] Draft:[clang][ASTImporter] Fix crash when template class static member imported to other translation unit. (PR #68774)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 12 02:22:39 PDT 2023
https://github.com/mzyKi updated https://github.com/llvm/llvm-project/pull/68774
>From ad651d68e6ac161da4e521cc0bebd6d59a5f38cb Mon Sep 17 00:00:00 2001
From: miaozhiyuan <miaozhiyuan at feysh.com>
Date: Wed, 11 Oct 2023 15:45:36 +0800
Subject: [PATCH] [clang][ASTImporter] Fix crash when template class static
member imported to other translation unit. Fixes: #68769
---
clang/docs/ReleaseNotes.rst | 4 ++++
clang/lib/AST/ASTImporter.cpp | 11 +++++++++++
.../Inputs/externalDefMap.txt | 1 +
.../Inputs/template-class-static-member.cpp | 3 +++
.../Inputs/template-class-static-member.h | 7 +++++++
.../ctu/template-class-static-member/main.cpp | 19 +++++++++++++++++++
6 files changed, 45 insertions(+)
create mode 100644 clang/test/Analysis/ctu/template-class-static-member/Inputs/externalDefMap.txt
create mode 100644 clang/test/Analysis/ctu/template-class-static-member/Inputs/template-class-static-member.cpp
create mode 100644 clang/test/Analysis/ctu/template-class-static-member/Inputs/template-class-static-member.h
create mode 100644 clang/test/Analysis/ctu/template-class-static-member/main.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2d918967e7f0b02..3be9e60c82a18d0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -497,6 +497,10 @@ Bug Fixes to C++ Support
rather than prefer the non-templated constructor as specified in
[standard.group]p3.
+- Fix crash when template class static member imported to other translation unit.
+ Fixes:
+ (`#68769 <https://github.com/llvm/llvm-project/issues/68769>`_)
+
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
- Fixed an import failure of recursive friend class template.
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index 72e70427161bb0e..2a8add84f55dc3b 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -4475,6 +4475,17 @@ ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
auto ToVTOrErr = import(D->getDescribedVarTemplate());
if (!ToVTOrErr)
return ToVTOrErr.takeError();
+ } else if (MemberSpecializationInfo *MSI = D->getMemberSpecializationInfo()) {
+ TemplateSpecializationKind SK = MSI->getTemplateSpecializationKind();
+ VarDecl *FromInst = D->getInstantiatedFromStaticDataMember();
+ if (Expected<VarDecl *> ToInstOrErr = import(FromInst))
+ ToVar->setInstantiationOfStaticDataMember(*ToInstOrErr, SK);
+ else
+ return ToInstOrErr.takeError();
+ if (ExpectedSLoc POIOrErr = import(MSI->getPointOfInstantiation()))
+ ToVar->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
+ else
+ return POIOrErr.takeError();
}
if (Error Err = ImportInitializer(D, ToVar))
diff --git a/clang/test/Analysis/ctu/template-class-static-member/Inputs/externalDefMap.txt b/clang/test/Analysis/ctu/template-class-static-member/Inputs/externalDefMap.txt
new file mode 100644
index 000000000000000..2cc394701d12e1d
--- /dev/null
+++ b/clang/test/Analysis/ctu/template-class-static-member/Inputs/externalDefMap.txt
@@ -0,0 +1 @@
+19:c:@S at Test>#I at length template-class-static-member.cpp.ast
diff --git a/clang/test/Analysis/ctu/template-class-static-member/Inputs/template-class-static-member.cpp b/clang/test/Analysis/ctu/template-class-static-member/Inputs/template-class-static-member.cpp
new file mode 100644
index 000000000000000..489aa41aec70452
--- /dev/null
+++ b/clang/test/Analysis/ctu/template-class-static-member/Inputs/template-class-static-member.cpp
@@ -0,0 +1,3 @@
+#include "template-class-static-member.h"
+
+template<> const unsigned int Test<int>::length = 0;
diff --git a/clang/test/Analysis/ctu/template-class-static-member/Inputs/template-class-static-member.h b/clang/test/Analysis/ctu/template-class-static-member/Inputs/template-class-static-member.h
new file mode 100644
index 000000000000000..f31d05728594a9a
--- /dev/null
+++ b/clang/test/Analysis/ctu/template-class-static-member/Inputs/template-class-static-member.h
@@ -0,0 +1,7 @@
+template <class H> class Test
+{
+public:
+ static const unsigned int length;
+};
+
+template<> const unsigned int Test<int>::length;
diff --git a/clang/test/Analysis/ctu/template-class-static-member/main.cpp b/clang/test/Analysis/ctu/template-class-static-member/main.cpp
new file mode 100644
index 000000000000000..6138c43ffcd4e07
--- /dev/null
+++ b/clang/test/Analysis/ctu/template-class-static-member/main.cpp
@@ -0,0 +1,19 @@
+// RUN: rm -rf %t && mkdir %t
+// RUN: mkdir -p %t/ctudir
+// RUN: %clang_cc1 -std=c++14 -triple x86_64-pc-linux-gnu \
+// RUN: -emit-pch -o %t/ctudir/template-class-static-member.cpp.ast %p/Inputs/template-class-static-member.cpp
+// RUN: cp %p/Inputs/externalDefMap.txt %t/ctudir/externalDefMap.txt
+// RUN: %clang_analyze_cc1 -triple x86_64-pc-linux-gnu \
+// RUN: -analyzer-checker=core.DivideZero \
+// RUN: -analyzer-config experimental-enable-naive-ctu-analysis=true \
+// RUN: -analyzer-config ctu-dir=%t/ctudir \
+// RUN: -analyzer-config display-ctu-progress=true -std=c++14 2>&1 %s | FileCheck %s
+
+
+// CHECK: CTU loaded AST file: {{.*}}template-class-static-member.cpp.ast
+
+#include "Inputs/template-class-static-member.h"
+
+void foo(){
+ int i = 1 / Test<int>::length; // expected-warning{{Division by zero [core.DivideZero]}}
+}
More information about the cfe-commits
mailing list