[cfe-commits] r84695 - /cfe/trunk/lib/Analysis/CFG.cpp

Ted Kremenek kremenek at apple.com
Tue Oct 20 16:46:27 PDT 2009


Author: kremenek
Date: Tue Oct 20 18:46:25 2009
New Revision: 84695

URL: http://llvm.org/viewvc/llvm-project?rev=84695&view=rev
Log:
Use llvm::OwningPtr in CFGBuilder, fixing a leak on an error path.

Modified:
    cfe/trunk/lib/Analysis/CFG.cpp

Modified: cfe/trunk/lib/Analysis/CFG.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CFG.cpp?rev=84695&r1=84694&r2=84695&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/CFG.cpp (original)
+++ cfe/trunk/lib/Analysis/CFG.cpp Tue Oct 20 18:46:25 2009
@@ -22,6 +22,7 @@
 #include "llvm/Support/Format.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/ADT/OwningPtr.h"
 
 using namespace clang;
 
@@ -51,7 +52,7 @@
 ///
 class VISIBILITY_HIDDEN CFGBuilder {
   ASTContext *Context;
-  CFG* cfg;
+  llvm::OwningPtr<CFG> cfg;
 
   CFGBlock* Block;
   CFGBlock* Succ;
@@ -79,8 +80,6 @@
                           ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
                           SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {}
 
-  ~CFGBuilder() { delete cfg; }
-
   // buildCFG - Used by external clients to construct the CFG.
   CFG* buildCFG(Stmt *Statement, ASTContext *C);
 
@@ -195,7 +194,7 @@
 ///  NULL.
 CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
   Context = C;
-  assert(cfg);
+  assert(cfg.get());
   if (!Statement)
     return NULL;
 
@@ -210,7 +209,8 @@
 
   // Visit the statements and create the CFG.
   CFGBlock* B = addStmt(Statement);
-  if (!B) B = Succ;
+  if (!B)
+    B = Succ;
 
   if (B) {
     // Finalize the last constructed block.  This usually involves reversing the
@@ -254,17 +254,12 @@
   // Create an empty entry block that has no predecessors.
   cfg->setEntry(createBlock());
 
-  if (badCFG) {
-    delete cfg;
-    cfg = NULL;
+  if (badCFG)
     return NULL;
-  }
 
   // NULL out cfg so that repeated calls to the builder will fail and that the
   // ownership of the constructed CFG is passed to the caller.
-  CFG* t = cfg;
-  cfg = NULL;
-  return t;
+  return cfg.take();
 }
 
 /// createBlock - Used to lazily create blocks that are connected





More information about the cfe-commits mailing list