[PATCH] D41409: [analyzer] Fix intermediate diagnostics on paths that go through operator new().

Artem Dergachev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 19 11:51:12 PST 2017

NoQ created this revision.
NoQ added reviewers: dcoughlin, xazax.hun, a.sidorin, george.karpenkov, szepet.
Herald added subscribers: cfe-commits, rnkovacs.

When operator new() is inlined, diagnostic pieces may appear within it. They'd be surrounded by `Calling 'operator new'` and `Returning from 'operator new'` pieces. These pieces were already in place, but the analyzer didn't know where to put them when the operator was called through operator syntax. This patch fixes it.

  rC Clang



Index: test/Analysis/new-ctor-malloc.cpp
--- test/Analysis/new-ctor-malloc.cpp
+++ test/Analysis/new-ctor-malloc.cpp
@@ -1,18 +1,21 @@
-// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection,unix.Malloc -analyzer-config c++-allocator-inlining=true -std=c++11 -verify %s
+// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection,unix.Malloc -analyzer-config c++-allocator-inlining=true -analyzer-output=text -std=c++11 -verify %s
 void clang_analyzer_eval(bool);
 typedef __typeof__(sizeof(int)) size_t;
 void *malloc(size_t size);
 void *operator new(size_t size) throw() {
-  void *x = malloc(size);
-  if (!x)
+  void *x = malloc(size); // expected-note {{Memory is allocated}}
+  if (!x) // expected-note    {{Assuming 'x' is non-null}}
+          // expected-note at -1 {{Taking false branch}}
     return nullptr;
   return x;
 void checkNewAndConstructorInlining() {
-  int *s = new int;
+  int *s = new int; // expected-note   {{Calling 'operator new'}}
+                    // expected-note at -1{{Returning from 'operator new'}}
 } // expected-warning {{Potential leak of memory pointed to by 's'}}
+  // expected-note at -1 {{Potential leak of memory pointed to by 's'}}
Index: lib/StaticAnalyzer/Core/PathDiagnostic.cpp
--- lib/StaticAnalyzer/Core/PathDiagnostic.cpp
+++ lib/StaticAnalyzer/Core/PathDiagnostic.cpp
@@ -576,8 +576,11 @@
       return PathDiagnosticLocation::createEnd(CallerBody, SM, CallerCtx);
     return PathDiagnosticLocation::create(CallerInfo->getDecl(), SM);
+  case CFGElement::NewAllocator: {
+    const CFGNewAllocator &Alloc = Source.castAs<CFGNewAllocator>();
+    return PathDiagnosticLocation(Alloc.getAllocatorExpr(), SM, CallerCtx);
+  }
   case CFGElement::TemporaryDtor:
-  case CFGElement::NewAllocator:
     llvm_unreachable("not yet implemented!");
   case CFGElement::LifetimeEnds:
   case CFGElement::LoopExit:

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41409.127581.patch
Type: text/x-patch
Size: 2036 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171219/de1b72f7/attachment.bin>

More information about the cfe-commits mailing list