Index: include/clang/Analysis/ProgramPoint.h =================================================================== --- include/clang/Analysis/ProgramPoint.h +++ include/clang/Analysis/ProgramPoint.h @@ -80,6 +80,7 @@ CallEnterKind, CallExitBeginKind, CallExitEndKind, + FunctionExitKind, PreImplicitCallKind, PostImplicitCallKind, MinImplicitCallKind = PreImplicitCallKind, @@ -329,6 +330,29 @@ } }; +class FunctionExitPoint : public ProgramPoint { +public: + explicit FunctionExitPoint(const ReturnStmt *S, + const LocationContext *LC, + const ProgramPointTag *tag = nullptr) + : ProgramPoint(S, FunctionExitKind, LC, tag) {} + + const CFGBlock *getBlock() const { + return &getLocationContext()->getCFG()->getExit(); + } + + const ReturnStmt *getStmt() const { + return reinterpret_cast(getData1()); + } + +private: + friend class ProgramPoint; + FunctionExitPoint() = default; + static bool isKind(const ProgramPoint &Location) { + return Location.getKind() == FunctionExitKind; + } +}; + // PostCondition represents the post program point of a branch condition. class PostCondition : public PostStmt { public: Index: lib/StaticAnalyzer/Core/CheckerManager.cpp =================================================================== --- lib/StaticAnalyzer/Core/CheckerManager.cpp +++ lib/StaticAnalyzer/Core/CheckerManager.cpp @@ -446,9 +446,8 @@ // autotransition for it. NodeBuilder Bldr(Pred, Dst, BC); for (const auto checkFn : EndFunctionCheckers) { - const ProgramPoint &L = BlockEntrance(BC.Block, - Pred->getLocationContext(), - checkFn.Checker); + const ProgramPoint &L = + FunctionExitPoint(RS, Pred->getLocationContext(), checkFn.Checker); CheckerContext C(Bldr, Eng, Pred, L); checkFn(RS, C); } Index: lib/StaticAnalyzer/Core/ExprEngine.cpp =================================================================== --- lib/StaticAnalyzer/Core/ExprEngine.cpp +++ lib/StaticAnalyzer/Core/ExprEngine.cpp @@ -2984,6 +2984,17 @@ << Loc.castAs().getBlock()->getBlockID(); break; + case ProgramPoint::FunctionExitKind: { + auto FEP = Loc.getAs(); + Out << "Function Exit: B" + << FEP->getBlock()->getBlockID(); + if (const ReturnStmt *RS = FEP->getStmt()) { + Out << "\\l Return: S" << RS->getID(Context) << "\\l"; + RS->printPretty(Out, /*helper=*/nullptr, Context.getPrintingPolicy(), + /*Indentation=*/2, /*NewlineSymbol=*/"\\l"); + } + break; + } case ProgramPoint::BlockExitKind: assert(false); break; Index: lib/StaticAnalyzer/Core/PathDiagnostic.cpp =================================================================== --- lib/StaticAnalyzer/Core/PathDiagnostic.cpp +++ lib/StaticAnalyzer/Core/PathDiagnostic.cpp @@ -774,18 +774,20 @@ } // Otherwise, see if the node's program point directly points to a statement. ProgramPoint P = N->getLocation(); - if (Optional SP = P.getAs()) + if (auto SP = P.getAs()) return SP->getStmt(); - if (Optional BE = P.getAs()) + if (auto BE = P.getAs()) return BE->getSrc()->getTerminator(); - if (Optional CE = P.getAs()) + if (auto CE = P.getAs()) return CE->getCallExpr(); - if (Optional CEE = P.getAs()) + if (auto CEE = P.getAs()) return CEE->getCalleeContext()->getCallSite(); - if (Optional PIPP = P.getAs()) + if (auto PIPP = P.getAs()) return PIPP->getInitializer()->getInit(); - if (Optional CEB = P.getAs()) + if (auto CEB = P.getAs()) return CEB->getReturnStmt(); + if (auto FEP = P.getAs()) + return FEP->getStmt(); return nullptr; } @@ -822,17 +824,21 @@ const SourceManager &SM) { assert(N && "Cannot create a location with a null node."); const Stmt *S = getStmt(N); + const LocationContext *LC = N->getLocationContext(); if (!S) { // If this is an implicit call, return the implicit call point location. if (Optional PIE = N->getLocationAs()) return PathDiagnosticLocation(PIE->getLocation(), SM); + if (auto FE = N->getLocationAs()) { + if (const ReturnStmt *RS = FE->getStmt()) + return PathDiagnosticLocation::createBegin(RS, SM, LC); + } S = getNextStmt(N); } if (S) { ProgramPoint P = N->getLocation(); - const LocationContext *LC = N->getLocationContext(); // For member expressions, return the location of the '.' or '->'. if (const auto *ME = dyn_cast(S)) Index: test/Analysis/inner-pointer.cpp =================================================================== --- test/Analysis/inner-pointer.cpp +++ test/Analysis/inner-pointer.cpp @@ -412,8 +412,9 @@ std::string s; return s.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}} // expected-note@-1 {{Inner buffer of 'std::string' deallocated by call to destructor}} -} // expected-warning {{Inner pointer of container used after re/deallocation}} -// expected-note@-1 {{Inner pointer of container used after re/deallocation}} + // expected-warning@-2 {{Inner pointer of container used after re/deallocation}} + // expected-note@-3 {{Inner pointer of container used after re/deallocation}} +} char *c(); Index: test/Analysis/malloc-free-after-return.cpp =================================================================== --- test/Analysis/malloc-free-after-return.cpp +++ test/Analysis/malloc-free-after-return.cpp @@ -17,5 +17,5 @@ int *freeAfterReturnLocal() { S X; - return X.getData(); -} // expected-warning {{Use of memory after it is freed}} + return X.getData(); // expected-warning {{Use of memory after it is freed}} +}