From d1d0722e833a9b66206c008a4cd3f6c5b4548b5c Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Wed, 27 Nov 2024 23:08:55 +0800 Subject: [PATCH 1/6] [ast-matcher] add `exportDecl` matcher --- clang/docs/ReleaseNotes.rst | 2 ++ clang/include/clang/ASTMatchers/ASTMatchers.h | 11 +++++++++++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp | 1 + 3 files changed, 14 insertions(+) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 356b091c13af4..4dfbd03d7756f 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -926,6 +926,8 @@ AST Matchers - Ensure ``hasName`` matches template specializations across inline namespaces, making `matchesNodeFullSlow` and `matchesNodeFullFast` consistent. +- Add ``exportDecl`` matches export declaration. + clang-format ------------ diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h index 4bcaa953a61af..efad600a3c58c 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchers.h +++ b/clang/include/clang/ASTMatchers/ASTMatchers.h @@ -1236,6 +1236,17 @@ AST_MATCHER_P(TemplateArgument, equalsIntegralValue, extern const internal::VariadicDynCastAllOfMatcher autoreleasePoolStmt; +/// Matches any export declaration. +/// +/// Example matches following declarations. +/// \code +/// export void foo(); +/// export { void foo(); } +/// export namespace { void foo(); } +/// export int v; +/// \endcode +extern const internal::VariadicDynCastAllOfMatcher exportDecl; + /// Matches any value declaration. /// /// Example matches A, B, C and F diff --git a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp index 46dd44e6f2b24..cdbdb65195409 100644 --- a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp +++ b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp @@ -799,6 +799,7 @@ const internal::VariadicDynCastAllOfMatcher const internal::VariadicDynCastAllOfMatcher unaryExprOrTypeTraitExpr; +const internal::VariadicDynCastAllOfMatcher exportDecl; const internal::VariadicDynCastAllOfMatcher valueDecl; const internal::VariadicDynCastAllOfMatcher cxxConstructorDecl; From 3e521e700690cf3bbadf1ca800957c1fed6e6eb9 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Wed, 27 Nov 2024 23:41:57 +0800 Subject: [PATCH 2/6] [clang-tidy][use-internal-linkage]fix false positives for `ExportDecl` C++20 modules supports to declare external linkage by `ExportDecl`. --- .../misc/UseInternalLinkageCheck.cpp | 2 +- clang-tools-extra/docs/ReleaseNotes.rst | 3 ++- .../checks/misc/use-internal-linkage.rst | 5 +++++ .../misc/use-internal-linkage-module.cpp | 20 +++++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-module.cpp diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp index 71eb2d94cd4f2..0656238a0e676 100644 --- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp @@ -101,7 +101,7 @@ void UseInternalLinkageCheck::registerMatchers(MatchFinder *Finder) { // 3. template isExplicitTemplateSpecialization(), // 4. friend - hasAncestor(friendDecl())))); + hasAncestor(decl(anyOf(friendDecl(), exportDecl())))))); Finder->addMatcher( functionDecl(Common, hasBody(), unless(cxxMethodDecl()), unless(isMain())) .bind("fn"), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index f050391110385..42b1e4e9fd1fe 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -237,7 +237,8 @@ Changes in existing checks - Improved :doc:`misc-use-internal-linkage ` check to insert ``static`` keyword before type qualifiers such as ``const`` and ``volatile`` and fix - false positives for function declaration without body. + false positives for function declaration without body and fix false positives + for C++20 export declarations. - Improved :doc:`modernize-avoid-c-arrays ` check to suggest using diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc/use-internal-linkage.rst b/clang-tools-extra/docs/clang-tidy/checks/misc/use-internal-linkage.rst index b8bbcc6270610..508b0cac09a91 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/misc/use-internal-linkage.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/misc/use-internal-linkage.rst @@ -29,6 +29,11 @@ Example: void fn3(); // without function body in all declaration, maybe external linkage void fn3(); + // export declarations + export void fn4() {} + export namespace t { void fn5() {} } + export int v2; + Options ------- diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-module.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-module.cpp new file mode 100644 index 0000000000000..9b0d8cde429b6 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-module.cpp @@ -0,0 +1,20 @@ +// RUN: %check_clang_tidy -std=c++20 %s misc-use-internal-linkage %t -- -- -I%S/Inputs/use-internal-linkage + +module; + +export module test; + +export void single_export_fn() {} +export int single_export_var; + +export { + void group_export_fn1() {} + void group_export_fn2() {} + int group_export_var1; + int group_export_var2; +} + +export namespace aa { +void namespace_export_fn() {} +int namespace_export_var; +} // namespace aa From 04acf88916647fee078a3b0d66ac6c7379be3613 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 28 Nov 2024 06:46:58 +0800 Subject: [PATCH 3/6] add doc and test --- clang/docs/LibASTMatchersReference.html | 11 +++++++++++ .../unittests/ASTMatchers/ASTMatchersNodeTest.cpp | 15 +++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html index c6307954d7f1b..f18e9cf134169 100644 --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -804,6 +804,17 @@

Node Matchers

+Matcher<Decl>exportDeclMatcher<ExportDecl>... +
Matches any export declaration.
+
+Example matches following declarations.
+  export void foo();
+  export { void foo(); }
+  export namespace { void foo(); }
+  export int v;
+
+ + Matcher<Decl>fieldDeclMatcher<FieldDecl>...
Matches field declarations.
 
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
index ebf548eb25431..9bc287e07224a 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -189,6 +189,21 @@ TEST(ASTMatchersTestCUDA, HasAttrCUDA) {
                                   hasAttr(clang::attr::CUDAGlobal)));
 }
 
+TEST_P(ASTMatchersTest, ExportDecl) {
+  if (!GetParam().isCXX20OrLater()) {
+    return;
+  }
+  const std::string moduleHeader = "module;export module ast_matcher_test;";
+  EXPECT_TRUE(matches(moduleHeader + "export void foo();",
+                      exportDecl(has(functionDecl()))));
+  EXPECT_TRUE(matches(moduleHeader + "export { void foo(); int v; }",
+                      exportDecl(has(functionDecl()))));
+  EXPECT_TRUE(matches(moduleHeader + "export { void foo(); int v; }",
+                      exportDecl(has(varDecl()))));
+  EXPECT_TRUE(matches(moduleHeader + "export namespace aa { void foo(); }",
+                      exportDecl(has(namespaceDecl()))));
+}
+
 TEST_P(ASTMatchersTest, ValueDecl) {
   if (!GetParam().isCXX()) {
     // FIXME: Fix this test in non-C++ language modes.

From 27f092f9fdc673eccac73eb12804469231f16aa8 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Thu, 28 Nov 2024 21:35:46 +0800
Subject: [PATCH 4/6] fix

---
 .../clang-tidy/misc/UseInternalLinkageCheck.cpp            | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
index 0656238a0e676..81f30e2fa3647 100644
--- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
@@ -100,8 +100,11 @@ void UseInternalLinkageCheck::registerMatchers(MatchFinder *Finder) {
                 isExternStorageClass(), isExternC(),
                 // 3. template
                 isExplicitTemplateSpecialization(),
-                // 4. friend
-                hasAncestor(decl(anyOf(friendDecl(), exportDecl()))))));
+                hasAncestor(decl(anyOf(
+                    // 4. friend
+                    friendDecl(),
+                    // 5. module export decl
+                    exportDecl()))))));
   Finder->addMatcher(
       functionDecl(Common, hasBody(), unless(cxxMethodDecl()), unless(isMain()))
           .bind("fn"),

From f2d5fb08154489232f15429d05f63132dc7ea329 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Sun, 1 Dec 2024 21:30:03 +0800
Subject: [PATCH 5/6] reformat

---
 clang-tools-extra/docs/ReleaseNotes.rst | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index cf567247f6f34..453a91e3b504c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -248,8 +248,8 @@ Changes in existing checks
   ` check to insert ``static``
   keyword before type qualifiers such as ``const`` and ``volatile`` and fix
   false positives for function declaration without body and fix false positives
-  for C++20 export declarations and fix false positives
-  for global scoped overloaded ``operator new`` and ``operator delete``.
+  for C++20 export declarations and fix false positives for global scoped
+  overloaded ``operator new`` and ``operator delete``.
 
 - Improved :doc:`modernize-avoid-c-arrays
   ` check to suggest using 

From 5d86e308c59367b5660185b9bc0578607580e37c Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Sun, 1 Dec 2024 21:50:08 +0800
Subject: [PATCH 6/6] update release note

---
 clang/docs/ReleaseNotes.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b1a4138610169..57be1222734ee 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -965,7 +965,7 @@ AST Matchers
 - Ensure ``hasName`` matches template specializations across inline namespaces,
   making `matchesNodeFullSlow` and `matchesNodeFullFast` consistent.
 
-- Add ``exportDecl`` matches export declaration.
+- Add ``exportDecl`` matcher to match export declaration.
 
 clang-format
 ------------