Skip to content

[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

Merged

Conversation

mzyKi
Copy link
Contributor

@mzyKi mzyKi commented Oct 11, 2023

Fixes: #68769

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html labels Oct 11, 2023
@mzyKi mzyKi changed the title [clang][ASTImporter] Fix crash when template class static member impo… [clang][ASTImporter] Fix crash when template class static member imported to other translation unit. Oct 11, 2023
@llvmbot
Copy link
Member

llvmbot commented Oct 11, 2023

@llvm/pr-subscribers-clang

Author: Exile (mzyKi)

Changes

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 --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;
+}

@mzyKi mzyKi changed the title [clang][ASTImporter] Fix crash when template class static member imported to other translation unit. Draft:[clang][ASTImporter] Fix crash when template class static member imported to other translation unit. Oct 11, 2023
@mzyKi mzyKi force-pushed the bugfix/clang-astimporter-static-template-class-member branch from 1c3b9f6 to ad651d6 Compare October 12, 2023 09:22
@mzyKi mzyKi changed the title Draft:[clang][ASTImporter] Fix crash when template class static member imported to other translation unit. [clang][ASTImporter] Fix crash when template class static member imported to other translation unit. Oct 12, 2023
@danix800
Copy link
Member

danix800 commented Oct 13, 2023

Caused by missing MemberSpecializationInfo when importing so moving testcase into ASTImporterTest as unittest would be better.

EDIT: test that the imported (DeclarationOnly) VarDecl is really a decaration rather than a definition.

@mzyKi mzyKi force-pushed the bugfix/clang-astimporter-static-template-class-member branch 2 times, most recently from 67f878a to 99c3060 Compare October 13, 2023 08:24
@github-actions
Copy link

github-actions bot commented Oct 13, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

@mzyKi mzyKi force-pushed the bugfix/clang-astimporter-static-template-class-member branch 2 times, most recently from e45c5e2 to a0e6d30 Compare October 13, 2023 08:59
@shafik shafik requested a review from balazske October 24, 2023 20:42
Copy link
Collaborator

@balazske balazske left a 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.

@mzyKi mzyKi force-pushed the bugfix/clang-astimporter-static-template-class-member branch from a0e6d30 to fa32c05 Compare November 1, 2023 02:53
@mzyKi mzyKi requested a review from balazske November 1, 2023 02:54
@mzyKi mzyKi force-pushed the bugfix/clang-astimporter-static-template-class-member branch 3 times, most recently from 4b09501 to 6e9fab5 Compare November 1, 2023 02:59
@mzyKi mzyKi force-pushed the bugfix/clang-astimporter-static-template-class-member branch from 6e9fab5 to 9ccc03c Compare November 1, 2023 03:13
@mzyKi mzyKi merged commit 39dfaf0 into llvm:main Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang][ASTImporter]:Assertion `hasBodyOrInit(ToDecl) && "Imported Decl should have body or init."
4 participants