[clang] [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
Wed Oct 11 00:50:20 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Exile (mzyKi)

<details>
<summary>Changes</summary>

Fixes: #<!-- -->68769

---
Full diff: https://github.com/llvm/llvm-project/pull/68774.diff


6 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+4) 
- (modified) clang/lib/AST/ASTImporter.cpp (+2-1) 
- (added) clang/test/Analysis/ctu/template-class-static-member/Inputs/externalDefMap.txt (+1) 
- (added) clang/test/Analysis/ctu/template-class-static-member/Inputs/template-class-static-member.cpp (+3) 
- (added) clang/test/Analysis/ctu/template-class-static-member/Inputs/template-class-static-member.h (+7) 
- (added) clang/test/Analysis/ctu/template-class-static-member/main.cpp (+19) 


``````````diff
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 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..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;
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/68774


More information about the cfe-commits mailing list