[clang-tidy] [Modules] Skip checking decls in clang-tidy (#145630) (#190733)

Close https://github.com/llvm/llvm-project/issues/145628

Note that I am not sure if this is the proper fix. On the one hand, the
fix lives in ASTMachers instead of clang-tidy. On the other hand, I feel
this may be a more general fix.
This commit is contained in:
Chuanqi Xu
2026-04-07 14:55:22 +08:00
committed by GitHub
parent a89f174e50
commit 8460cb9bbb
5 changed files with 39 additions and 0 deletions

View File

@@ -434,6 +434,9 @@ ClangTidyASTConsumerFactory::createASTConsumer(CompilerInstance &Compiler,
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 =

View File

@@ -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();
}

View File

@@ -27,6 +27,7 @@ config.test_format = lit.formats.ShTest(not use_lit_shell)
config.suffixes = [
".c",
".cpp",
".cppm",
".hpp",
".m",
".mm",

View File

@@ -144,6 +144,8 @@ public:
/// Avoids matching declarations in system headers.
bool IgnoreSystemHeaders{false};
bool SkipDeclsInModules{false};
};
MatchFinder(MatchFinderOptions Options = MatchFinderOptions());

View File

@@ -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"
@@ -1511,6 +1512,9 @@ bool MatchASTVisitor::TraverseDecl(Decl *DeclNode) {
if (shouldSkipNode(DeclNode))
return true;
if (Options.SkipDeclsInModules && DeclNode->isInAnotherModuleUnit())
return true;
bool ScopedTraversal =
TraversingASTNodeNotSpelledInSource || DeclNode->isImplicit();
bool ScopedChildren = TraversingASTChildrenNotSpelledInSource;