Skip to content

Commit 14560a1

Browse files
committed
[Fix] Speedup -Wunsafe-buffer-usage when using clang modules.
See https://issues.chromium.org/issues/351909443 for details and benchmarks. This improves the performance of a file containing a single line, `#include <iostream>`, from ~1 second to ~100ms on my machine.
1 parent dc79c66 commit 14560a1

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

clang/lib/Sema/AnalysisBasedWarnings.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,14 +2546,27 @@ static void flushDiagnostics(Sema &S, const sema::FunctionScopeInfo *fscope) {
25462546
class CallableVisitor : public DynamicRecursiveASTVisitor {
25472547
private:
25482548
llvm::function_ref<void(const Decl *)> Callback;
2549+
const unsigned int TUModuleID;
25492550

25502551
public:
2551-
CallableVisitor(llvm::function_ref<void(const Decl *)> Callback)
2552-
: Callback(Callback) {
2552+
CallableVisitor(llvm::function_ref<void(const Decl *)> Callback,
2553+
unsigned int TUModuleID)
2554+
: Callback(Callback), TUModuleID(TUModuleID) {
25532555
ShouldVisitTemplateInstantiations = true;
25542556
ShouldVisitImplicitCode = false;
25552557
}
25562558

2559+
bool TraverseDecl(Decl *Node) override {
2560+
// For performance reasons, only validate the current translation unit's
2561+
// module, and not modules it depends on.
2562+
// See https://issues.chromium.org/issues/351909443 for details.
2563+
if (Node->getOwningModuleID() == TUModuleID) {
2564+
return DynamicRecursiveASTVisitor::TraverseDecl(Node);
2565+
} else {
2566+
return true;
2567+
}
2568+
}
2569+
25572570
bool VisitFunctionDecl(FunctionDecl *Node) override {
25582571
if (cast<DeclContext>(Node)->isDependentContext())
25592572
return true; // Not to analyze dependent decl
@@ -2633,7 +2646,7 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings(
26332646
SourceLocation()) ||
26342647
(!Diags.isIgnored(diag::warn_unsafe_buffer_libc_call, SourceLocation()) &&
26352648
S.getLangOpts().CPlusPlus /* only warn about libc calls in C++ */)) {
2636-
CallableVisitor(CallAnalyzers).TraverseTranslationUnitDecl(TU);
2649+
CallableVisitor(CallAnalyzers, TU->getOwningModuleID()).TraverseTranslationUnitDecl(TU);
26372650
}
26382651
}
26392652

0 commit comments

Comments
 (0)