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. Repository: rC Clang https://reviews.llvm.org/D41409 Files: lib/StaticAnalyzer/Core/PathDiagnostic.cpp test/Analysis/new-ctor-malloc.cpp 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@-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@-1{{Returning from 'operator new'}} } // expected-warning {{Potential leak of memory pointed to by 's'}} + // expected-note@-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:
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@-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@-1{{Returning from 'operator new'}} } // expected-warning {{Potential leak of memory pointed to by 's'}} + // expected-note@-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:
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits