[clang] [clang-tools-extra] [clang-tidy] [Modules] Skip checking decls in clang-tidy (PR #145630)
Chuanqi Xu via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 25 23:01:51 PDT 2025
https://github.com/ChuanqiXu9 updated https://github.com/llvm/llvm-project/pull/145630
>From 3e526e9a666dcc2ea0e6bdcb666dca43ab6ba740 Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <chuanqi.xcq at alibaba-inc.com>
Date: Wed, 17 Jan 2024 13:22:10 +0800
Subject: [PATCH 1/2] [clang-tidy] [Modules] Skip checking decls in clang-tidy
---
clang-tools-extra/clang-tidy/ClangTidy.cpp | 3 ++
.../clang-tidy/checkers/cxx20-modules.cppm | 29 +++++++++++++++++++
clang-tools-extra/test/lit.cfg.py | 1 +
.../clang/ASTMatchers/ASTMatchFinder.h | 5 ++++
clang/lib/ASTMatchers/ASTMatchFinder.cpp | 7 +++++
5 files changed, 45 insertions(+)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/cxx20-modules.cppm
diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp
index f4ab93b51f4a7..68192f7ad6240 100644
--- a/clang-tools-extra/clang-tidy/ClangTidy.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp
@@ -417,6 +417,9 @@ ClangTidyASTConsumerFactory::createASTConsumer(
ast_matchers::MatchFinder::MatchFinderOptions FinderOptions;
+ // We should always skip the declarations in modules.
+ FinderOptions.SkipDeclsInModules = true;
+
std::unique_ptr<ClangTidyProfiling> Profiling;
if (Context.getEnableProfiling()) {
Profiling =
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cxx20-modules.cppm b/clang-tools-extra/test/clang-tidy/checkers/cxx20-modules.cppm
new file mode 100644
index 0000000000000..b7e39e2295a1f
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/cxx20-modules.cppm
@@ -0,0 +1,29 @@
+// RUN: rm -fr %t
+// RUN: mkdir %t
+// RUN: split-file %s %t
+// RUN: mkdir %t/tmp
+//
+// RUN: %check_clang_tidy -std=c++20 -check-suffix=DEFAULT %t/a.cpp \
+// RUN: cppcoreguidelines-narrowing-conversions %t/a.cpp -- \
+// RUN: -config='{}'
+
+// RUN: %clang -std=c++20 -x c++-module %t/a.cpp --precompile -o %t/a.pcm
+
+// RUN: %check_clang_tidy -std=c++20 -check-suffix=DEFAULT %t/use.cpp \
+// RUN: cppcoreguidelines-narrowing-conversions %t/a.cpp -- \
+// RUN: -config='{}' -- -fmodule-file=a=%t/a.pcm
+
+//--- a.cpp
+export module a;
+export void most_narrowing_is_not_ok() {
+ int i;
+ long long ui;
+ i = ui;
+ // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
+}
+
+//--- use.cpp
+import a;
+void use() {
+ most_narrowing_is_not_ok();
+}
diff --git a/clang-tools-extra/test/lit.cfg.py b/clang-tools-extra/test/lit.cfg.py
index 9f64fd3d2ffa2..73882851345bf 100644
--- a/clang-tools-extra/test/lit.cfg.py
+++ b/clang-tools-extra/test/lit.cfg.py
@@ -19,6 +19,7 @@
config.suffixes = [
".c",
".cpp",
+ ".cppm",
".hpp",
".m",
".mm",
diff --git a/clang/include/clang/ASTMatchers/ASTMatchFinder.h b/clang/include/clang/ASTMatchers/ASTMatchFinder.h
index 73cbcf1f25025..69d569a7b09cc 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchFinder.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchFinder.h
@@ -139,6 +139,11 @@ class MatchFinder {
///
/// It prints a report after match.
std::optional<Profiling> CheckProfiling;
+
+ bool SkipDeclsInModules = false;
+
+ MatchFinderOptions()
+ : CheckProfiling(std::nullopt), SkipDeclsInModules(false) {}
};
MatchFinder(MatchFinderOptions Options = MatchFinderOptions());
diff --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
index 6d0ba0b7907a1..224bc261fa9bd 100644
--- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -20,6 +20,7 @@
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Basic/Module.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringMap.h"
@@ -1469,6 +1470,12 @@ bool MatchASTVisitor::TraverseDecl(Decl *DeclNode) {
return true;
}
+ if (Options.SkipDeclsInModules && DeclNode->isFromASTFile()) {
+ auto *M = DeclNode->getOwningModule();
+ if (M && (M->isInterfaceOrPartition() || M->isGlobalModule()))
+ return true;
+ }
+
bool ScopedTraversal =
TraversingASTNodeNotSpelledInSource || DeclNode->isImplicit();
bool ScopedChildren = TraversingASTChildrenNotSpelledInSource;
>From 7d21ebc8d1cf763e4699feb919e79219afb2d6c0 Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <yedeng.yd at linux.alibaba.com>
Date: Thu, 26 Jun 2025 14:01:24 +0800
Subject: [PATCH 2/2] Use a new API
---
clang/lib/ASTMatchers/ASTMatchFinder.cpp | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
index 224bc261fa9bd..d35565bdb257f 100644
--- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -1470,11 +1470,8 @@ bool MatchASTVisitor::TraverseDecl(Decl *DeclNode) {
return true;
}
- if (Options.SkipDeclsInModules && DeclNode->isFromASTFile()) {
- auto *M = DeclNode->getOwningModule();
- if (M && (M->isInterfaceOrPartition() || M->isGlobalModule()))
- return true;
- }
+ if (Options.SkipDeclsInModules && DeclNode->isInAnotherModuleUnit())
+ return true;
bool ScopedTraversal =
TraversingASTNodeNotSpelledInSource || DeclNode->isImplicit();
More information about the cfe-commits
mailing list