https://github.com/mzyKi created https://github.com/llvm/llvm-project/pull/68774
…rted to other translation unit. Fixes: #68769 https://github.com/llvm/llvm-project/issues/68769 >From 1c3b9f6696842230b49962531c065956a1b3ab8c Mon Sep 17 00:00:00 2001 From: miaozhiyuan <miaozhiy...@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 https://github.com/llvm/llvm-project/issues/68769 --- clang/docs/ReleaseNotes.rst | 4 ++++ clang/lib/AST/ASTImporter.cpp | 3 ++- .../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, 36 insertions(+), 1 deletion(-) 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..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; +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits