Author: Tarun Thammisetty Date: 2026-03-04T13:56:47Z New Revision: 5c274078422435b1c666d99375e958d30c4b77a6
URL: https://github.com/llvm/llvm-project/commit/5c274078422435b1c666d99375e958d30c4b77a6 DIFF: https://github.com/llvm/llvm-project/commit/5c274078422435b1c666d99375e958d30c4b77a6.diff LOG: [analyzer] Suppress optin.cplusplus.VirtualCall warnings in system headers (#184183) Fixes #184178 The optin.cplusplus.VirtualCall checker reports warnings for virtual method calls during construction/destruction even when the call site is in a system header (included via -isystem). Users cannot fix such code and must resort to NOLINT suppressions. Add a system header check in checkPreCall before emitting the report, consistent with how other checkers (e.g. MallocChecker) handle this. Added: clang/test/Analysis/Inputs/virtualcall-system-header.h Modified: clang/lib/StaticAnalyzer/Checkers/VirtualCallChecker.cpp clang/test/Analysis/virtualcall.cpp Removed: ################################################################################ diff --git a/clang/lib/StaticAnalyzer/Checkers/VirtualCallChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/VirtualCallChecker.cpp index 6c27f58d308aa..160fc2596e485 100644 --- a/clang/lib/StaticAnalyzer/Checkers/VirtualCallChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/VirtualCallChecker.cpp @@ -118,6 +118,11 @@ void VirtualCallChecker::checkPreCall(const CallEvent &Call, if (!isVirtualCall(CE)) return; + // Don't warn about virtual calls in system headers (e.g. libraries included + // via -isystem), as the user has no control over such code. + if (C.getSourceManager().isInSystemHeader(CE->getBeginLoc())) + return; + const MemRegion *Reg = MC->getCXXThisVal().getAsRegion(); const ObjectState *ObState = State->get<CtorDtorMap>(Reg); if (!ObState) diff --git a/clang/test/Analysis/Inputs/virtualcall-system-header.h b/clang/test/Analysis/Inputs/virtualcall-system-header.h new file mode 100644 index 0000000000000..2cdde63677364 --- /dev/null +++ b/clang/test/Analysis/Inputs/virtualcall-system-header.h @@ -0,0 +1,11 @@ +#pragma clang system_header + +struct SysBase { + virtual void shutdown() = 0; + virtual ~SysBase() = default; +}; + +struct SysService : SysBase { + void shutdown() override {} + ~SysService() override { shutdown(); } // no-warning +}; diff --git a/clang/test/Analysis/virtualcall.cpp b/clang/test/Analysis/virtualcall.cpp index 82285b6d12844..ebbfe3c1f9d90 100644 --- a/clang/test/Analysis/virtualcall.cpp +++ b/clang/test/Analysis/virtualcall.cpp @@ -11,7 +11,12 @@ // RUN: -analyzer-checker=debug.ExprInspection \ // RUN: -std=c++11 -verify=pure,impure -std=c++11 %s +// Verify no warnings for virtual calls in system headers. +// RUN: %clang_analyze_cc1 -analyzer-checker=core,optin.cplusplus.VirtualCall \ +// RUN: -std=c++11 -verify=system,impure %s + #include "virtualcall.h" +#include "Inputs/virtualcall-system-header.h" void clang_analyzer_warnIfReached(); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
