From 95dd80c2e83bbc18d29056081b3fb233a1664359 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Tue, 7 Aug 2018 23:13:28 +0000 Subject: [PATCH] [analyzer] Avoid querying this-pointers for static-methods. Summary: The loop-widening code processes c++ methods looking for `this` pointers. In the case of static methods (which do not have `this` pointers), an assertion was triggering. This patch avoids trying to process `this` pointers for static methods, and thus avoids triggering the assertion . Reviewers: dcoughlin, george.karpenkov, NoQ Reviewed By: NoQ Subscribers: NoQ, xazax.hun, szepet, a.sidorin, mikhail.ramalho, cfe-commits Differential Revision: https://reviews.llvm.org/D50408 llvm-svn: 339201 --- clang/lib/StaticAnalyzer/Core/LoopWidening.cpp | 6 ++++-- .../Analysis/loop-widening-ignore-static-methods.cpp | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 clang/test/Analysis/loop-widening-ignore-static-methods.cpp diff --git a/clang/lib/StaticAnalyzer/Core/LoopWidening.cpp b/clang/lib/StaticAnalyzer/Core/LoopWidening.cpp index 9192f49eac6d2..d7fed2558cdf2 100644 --- a/clang/lib/StaticAnalyzer/Core/LoopWidening.cpp +++ b/clang/lib/StaticAnalyzer/Core/LoopWidening.cpp @@ -81,8 +81,10 @@ ProgramStateRef getWidenedLoopState(ProgramStateRef PrevState, // 'this' pointer is not an lvalue, we should not invalidate it. If the loop // is located in a method, constructor or destructor, the value of 'this' - // pointer shoule remain unchanged. - if (const CXXMethodDecl *CXXMD = dyn_cast(STC->getDecl())) { + // pointer should remain unchanged. Ignore static methods, since they do not + // have 'this' pointers. + const CXXMethodDecl *CXXMD = dyn_cast(STC->getDecl()); + if (CXXMD && !CXXMD->isStatic()) { const CXXThisRegion *ThisR = MRMgr.getCXXThisRegion( CXXMD->getThisType(STC->getAnalysisDeclContext()->getASTContext()), STC); diff --git a/clang/test/Analysis/loop-widening-ignore-static-methods.cpp b/clang/test/Analysis/loop-widening-ignore-static-methods.cpp new file mode 100644 index 0000000000000..bcf4f8b23a606 --- /dev/null +++ b/clang/test/Analysis/loop-widening-ignore-static-methods.cpp @@ -0,0 +1,12 @@ +// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-config widen-loops=true -analyzer-max-loop 2 %s +// REQUIRES: asserts +// expected-no-diagnostics +// +// This test checks that the loop-widening code ignores static methods. If that is not the +// case, then an assertion will trigger. + +class Test { + static void foo() { + for (;;) {} + } +};