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 1 commit
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
29 changes: 29 additions & 0 deletions clang-tools-extra/test/clang-tidy/checkers/cxx20-modules.cppm
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
7 changes: 7 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,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;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with modules at all, I just want to make sure we check this the right way as there are about 8 member functions matching the is*Module() pattern.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think this is fine. isFromASTFile means this is deserialized. (M->isInterfaceOrPartition() || M->isGlobalModule()) means it is from named modules. There are other kinds of modules but they are header modules, which have headers semantics. While the C++20 named modules is another TU, not a header. So I think it is proper here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I picked a new API isInAnotherModuleUnit, this looks much more descriptive.

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