-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][ASTImporter] Fix crash when template class static member imported to other translation unit. #68774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang][ASTImporter] Fix crash when template class static member imported to other translation unit. #68774
Conversation
@llvm/pr-subscribers-clang Author: Exile (mzyKi) ChangesFixes: #68769 Full diff: https://github.com/llvm/llvm-project/pull/68774.diff 6 Files Affected:
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..5c9a3eda58d2252 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -4358,7 +4358,8 @@ ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
// Try to find a variable in our own ("to") context with the same name and
// in the same context as the variable we're importing.
VarDecl *FoundByLookup = nullptr;
- if (D->isFileVarDecl()) {
+ MemberSpecializationInfo *MSI = D->getMemberSpecializationInfo();
+ if (D->isFileVarDecl() && !MSI) {
SmallVector<NamedDecl *, 4> ConflictingDecls;
unsigned IDNS = Decl::IDNS_Ordinary;
auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
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@Test>#I@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..ef4216ae4e28517
--- /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 -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 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;
+}
|
1c3b9f6
to
ad651d6
Compare
Caused by missing EDIT: test that the imported ( |
67f878a
to
99c3060
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
e45c5e2
to
a0e6d30
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix looks good but the formatting of the test code could be better, like in the other tests.
a0e6d30
to
fa32c05
Compare
4b09501
to
6e9fab5
Compare
…rted to other translation unit. Fixes: llvm#68769
6e9fab5
to
9ccc03c
Compare
Fixes: #68769