Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
1 change: 1 addition & 0 deletions clang-tools-extra/test/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
config.suffixes = [
".c",
".cpp",
".cppm",
".hpp",
".m",
".mm",
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/ASTMatchers/ASTMatchFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/ASTMatchers/ASTMatchFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -1469,6 +1470,9 @@ bool MatchASTVisitor::TraverseDecl(Decl *DeclNode) {
return true;
}

if (Options.SkipDeclsInModules && DeclNode->isInAnotherModuleUnit())
return true;

bool ScopedTraversal =
TraversingASTNodeNotSpelledInSource || DeclNode->isImplicit();
bool ScopedChildren = TraversingASTChildrenNotSpelledInSource;
Expand Down