[PATCH] D156693: [clang][ASTImporter]Don't add template decl as in friend decl

Qizhi Hu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 31 07:15:52 PDT 2023


jcsxky created this revision.
jcsxky added reviewers: a.sidorin, shafik, balazske, steakhal.
jcsxky added a project: clang.
Herald added a subscriber: martong.
Herald added a project: All.
jcsxky requested review of this revision.
Herald added subscribers: cfe-commits, wangpc.

There is no need to add a ``` NameDecl ``` into ``` LookupTable ```when it is declared in friend declaration located in the template class definition. If it's ``` FriendType ```, then ``` VisitFriendDecl ``` can help us do it, or it is in declaring in current class definition, this has been processed by ``` VisitNamedDecl ``` before.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156693

Files:
  clang/lib/AST/ASTImporterLookupTable.cpp
  clang/test/Analysis/Inputs/ctu-friend-template-other.cpp
  clang/test/Analysis/Inputs/ctu-friend-template.cpp.externalDefMap-dump.txt
  clang/test/Analysis/Inputs/ctu-friend-template.h
  clang/test/Analysis/ctu-friend-template.cpp


Index: clang/test/Analysis/ctu-friend-template.cpp
===================================================================
--- /dev/null
+++ clang/test/Analysis/ctu-friend-template.cpp
@@ -0,0 +1,21 @@
+// 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/ctu-friend-template-other.cpp.ast %S/Inputs/ctu-friend-template-other.cpp
+// RUN: cp %S/Inputs/ctu-friend-template.cpp.externalDefMap-dump.txt %t/ctudir/externalDefMap.txt
+// RUN: %clang_analyze_cc1 -std=c++14 -triple x86_64-pc-linux-gnu \
+// RUN:   -analyzer-checker=core,debug.ExprInspection \
+// RUN:   -analyzer-config experimental-enable-naive-ctu-analysis=true \
+// RUN:   -analyzer-config ctu-dir=%t/ctudir \
+// RUN:   -Werror=ctu \
+// RUN:   -verify %s
+
+// CHECK: CTU loaded AST file
+
+#include "Inputs/ctu-friend-template.h"
+
+void bar();
+
+int main(){
+	bar(); // expected-no-diagnostics
+}
Index: clang/test/Analysis/Inputs/ctu-friend-template.h
===================================================================
--- /dev/null
+++ clang/test/Analysis/Inputs/ctu-friend-template.h
@@ -0,0 +1,20 @@
+namespace __1{
+
+template<class T, T U>
+class A;
+
+template<class T, T U>
+class A{
+public:
+	template<class P, P Q>
+	friend class A;
+
+	A(T x):x(x){}
+	
+	void foo(){}
+	
+private:
+	T x;
+};
+
+}
Index: clang/test/Analysis/Inputs/ctu-friend-template.cpp.externalDefMap-dump.txt
===================================================================
--- /dev/null
+++ clang/test/Analysis/Inputs/ctu-friend-template.cpp.externalDefMap-dump.txt
@@ -0,0 +1 @@
+9:c:@F at bar# ctu-friend-template-other.cpp.ast
Index: clang/test/Analysis/Inputs/ctu-friend-template-other.cpp
===================================================================
--- /dev/null
+++ clang/test/Analysis/Inputs/ctu-friend-template-other.cpp
@@ -0,0 +1,6 @@
+#include "ctu-friend-template.h"
+
+void bar(){
+	__1::A<int,3> a1(0);
+	a1.foo();
+}
Index: clang/lib/AST/ASTImporterLookupTable.cpp
===================================================================
--- clang/lib/AST/ASTImporterLookupTable.cpp
+++ clang/lib/AST/ASTImporterLookupTable.cpp
@@ -14,6 +14,7 @@
 #include "clang/AST/ASTImporterLookupTable.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/AST/ParentMapContext.h"
 #include "llvm/Support/FormatVariadic.h"
 
 namespace clang {
@@ -38,6 +39,10 @@
   }
 
   bool VisitNamedDecl(NamedDecl *D) {
+    auto Parents = D->getASTContext().getParents(*D);
+    if (!Parents.empty() && nullptr != Parents.begin()->get<FriendDecl>()) {
+      return true;
+    }
     LT.add(D);
     return true;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156693.545639.patch
Type: text/x-patch
Size: 2704 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230731/d4a109db/attachment.bin>


More information about the cfe-commits mailing list