Skip to content
Merged
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
18 changes: 15 additions & 3 deletions clang/lib/Sema/AnalysisBasedWarnings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2502,14 +2502,25 @@ static void flushDiagnostics(Sema &S, const sema::FunctionScopeInfo *fscope) {
class CallableVisitor : public DynamicRecursiveASTVisitor {
private:
llvm::function_ref<void(const Decl *)> Callback;
const Module *const TUModule;

public:
CallableVisitor(llvm::function_ref<void(const Decl *)> Callback)
: Callback(Callback) {
CallableVisitor(llvm::function_ref<void(const Decl *)> Callback,
const Module *const TUModule)
: Callback(Callback), TUModule(TUModule) {
ShouldVisitTemplateInstantiations = true;
ShouldVisitImplicitCode = false;
}

bool TraverseDecl(Decl *Node) override {
// For performance reasons, only validate the current translation unit's
// module, and not modules it depends on.
// See https://issues.chromium.org/issues/351909443 for details.
if (Node && Node->getOwningModule() == TUModule)
return DynamicRecursiveASTVisitor::TraverseDecl(Node);
return true;
}

bool VisitFunctionDecl(FunctionDecl *Node) override {
if (cast<DeclContext>(Node)->isDependentContext())
return true; // Not to analyze dependent decl
Expand Down Expand Up @@ -2589,7 +2600,8 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings(
SourceLocation()) ||
(!Diags.isIgnored(diag::warn_unsafe_buffer_libc_call, SourceLocation()) &&
S.getLangOpts().CPlusPlus /* only warn about libc calls in C++ */)) {
CallableVisitor(CallAnalyzers).TraverseTranslationUnitDecl(TU);
CallableVisitor(CallAnalyzers, TU->getOwningModule())
.TraverseTranslationUnitDecl(TU);
}
}

Expand Down
28 changes: 8 additions & 20 deletions clang/test/Modules/safe_buffers_optout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,10 @@ int textual(int *p) {
// `safe_buffers_test_optout`, which uses another top-level module
// `safe_buffers_test_base`. (So the module dependencies form a DAG.)

// No expected warnings from base.h because base.h is a separate
// module and in a separate TU that is not textually included. The
// explicit command that builds base.h has no `-Wunsafe-buffer-usage`.

// [email protected]:3{{unsafe buffer access}}
// [email protected]:3{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}
// expected-warning@test_sub1.h:5{{unsafe buffer access}}
// expected-note@test_sub1.h:5{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}
// expected-warning@test_sub1.h:14{{unsafe buffer access}}
// expected-note@test_sub1.h:14{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}
// expected-warning@test_sub2.h:5{{unsafe buffer access}}
// expected-note@test_sub2.h:5{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}
// No expected warnings from base.h, test_sub1, or test_sub2 because they are
// in seperate modules, and the explicit commands that builds them have no
// `-Wunsafe-buffer-usage`.

int foo(int * p) {
int x = p[5]; // expected-warning{{unsafe buffer access}} expected-note{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}
#pragma clang unsafe_buffer_usage begin
Expand All @@ -129,14 +121,10 @@ int foo(int * p) {
// `safe_buffers_test_optout`, which uses another top-level module
// `safe_buffers_test_base`. (So the module dependencies form a DAG.)

// [email protected]:3{{unsafe buffer access}}
// [email protected]:3{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}
// expected-warning@test_sub1.h:5{{unsafe buffer access}}
// expected-note@test_sub1.h:5{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}
// expected-warning@test_sub1.h:14{{unsafe buffer access}}
// expected-note@test_sub1.h:14{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}
// expected-warning@test_sub2.h:5{{unsafe buffer access}}
// expected-note@test_sub2.h:5{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}
// No expected warnings from base.h, test_sub1, or test_sub2 because they are
// in seperate modules, and the explicit commands that builds them have no
// `-Wunsafe-buffer-usage`.

int foo(int * p) {
int x = p[5]; // expected-warning{{unsafe buffer access}} expected-note{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}
#pragma clang unsafe_buffer_usage begin
Expand Down