[cfe-commits] r68845 - in /cfe/trunk: include/clang/Analysis/ include/clang/Analysis/PathSensitive/ lib/Analysis/ test/Analysis/

Ted Kremenek kremenek at apple.com
Fri Apr 10 17:11:10 PDT 2009


Author: kremenek
Date: Fri Apr 10 19:11:10 2009
New Revision: 68845

URL: http://llvm.org/viewvc/llvm-project?rev=68845&view=rev
Log:
Implement analyzer support for OSCompareAndSwap. This required pushing "tagged"
ProgramPoints all the way through to GRCoreEngine.

NSString.m now fails with RegionStoreManager because of the void** cast.
Disabling use of region store for that test for now.

Modified:
    cfe/trunk/include/clang/Analysis/PathDiagnostic.h
    cfe/trunk/include/clang/Analysis/PathSensitive/BasicValueFactory.h
    cfe/trunk/include/clang/Analysis/PathSensitive/GRCoreEngine.h
    cfe/trunk/include/clang/Analysis/PathSensitive/GRExprEngine.h
    cfe/trunk/include/clang/Analysis/PathSensitive/GRExprEngineBuilders.h
    cfe/trunk/include/clang/Analysis/PathSensitive/MemRegion.h
    cfe/trunk/include/clang/Analysis/PathSensitive/ValueManager.h
    cfe/trunk/include/clang/Analysis/ProgramPoint.h
    cfe/trunk/lib/Analysis/BasicStore.cpp
    cfe/trunk/lib/Analysis/GRCoreEngine.cpp
    cfe/trunk/lib/Analysis/GRExprEngine.cpp
    cfe/trunk/lib/Analysis/MemRegion.cpp
    cfe/trunk/lib/Analysis/SVals.cpp
    cfe/trunk/test/Analysis/NSString.m

Modified: cfe/trunk/include/clang/Analysis/PathDiagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/PathDiagnostic.h?rev=68845&r1=68844&r2=68845&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathDiagnostic.h (original)
+++ cfe/trunk/include/clang/Analysis/PathDiagnostic.h Fri Apr 10 19:11:10 2009
@@ -96,6 +96,8 @@
   bool isValid() const {
     return SM != 0;
   }
+  
+  const SourceManager& getSourceManager() const { assert(isValid());return *SM;}
     
   FullSourceLoc asLocation() const;
   SourceRange asRange() const;

Modified: cfe/trunk/include/clang/Analysis/PathSensitive/BasicValueFactory.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/PathSensitive/BasicValueFactory.h?rev=68845&r1=68844&r2=68845&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathSensitive/BasicValueFactory.h (original)
+++ cfe/trunk/include/clang/Analysis/PathSensitive/BasicValueFactory.h Fri Apr 10 19:11:10 2009
@@ -126,8 +126,12 @@
     return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
   }
 
+  inline const llvm::APSInt& getTruthValue(bool b, QualType T) {
+    return getValue(b ? 1 : 0, Ctx.getTypeSize(T), false);
+  }
+  
   inline const llvm::APSInt& getTruthValue(bool b) {
-    return getValue(b ? 1 : 0, Ctx.getTypeSize(Ctx.IntTy), false);
+    return getTruthValue(b, Ctx.IntTy);
   }
   
   const CompoundValData* getCompoundValData(QualType T, 

Modified: cfe/trunk/include/clang/Analysis/PathSensitive/GRCoreEngine.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/PathSensitive/GRCoreEngine.h?rev=68845&r1=68844&r2=68845&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathSensitive/GRCoreEngine.h (original)
+++ cfe/trunk/include/clang/Analysis/PathSensitive/GRCoreEngine.h Fri Apr 10 19:11:10 2009
@@ -146,14 +146,23 @@
   
   ExplodedNodeImpl*
   generateNodeImpl(Stmt* S, const void* State, ExplodedNodeImpl* Pred,
-                   ProgramPoint::Kind K = ProgramPoint::PostStmtKind);
+                   ProgramPoint::Kind K = ProgramPoint::PostStmtKind,
+                   const void *tag = 0);
 
   ExplodedNodeImpl*
   generateNodeImpl(Stmt* S, const void* State,
-                   ProgramPoint::Kind K = ProgramPoint::PostStmtKind) {    
+                   ProgramPoint::Kind K = ProgramPoint::PostStmtKind,
+                   const void *tag = 0) {
     ExplodedNodeImpl* N = getLastNode();
     assert (N && "Predecessor of new node is infeasible.");
-    return generateNodeImpl(S, State, N, K);
+    return generateNodeImpl(S, State, N, K, tag);
+  }
+  
+  ExplodedNodeImpl*
+  generateNodeImpl(Stmt* S, const void* State, const void *tag = 0) {
+    ExplodedNodeImpl* N = getLastNode();
+    assert (N && "Predecessor of new node is infeasible.");
+    return generateNodeImpl(S, State, N, ProgramPoint::PostStmtKind, tag);
   }
   
   /// getStmt - Return the current block-level expression associated with
@@ -183,7 +192,7 @@
   GRStmtNodeBuilder(GRStmtNodeBuilderImpl& nb, StateManagerTy& mgr) :
     NB(nb), Mgr(mgr), Auditor(0), PurgingDeadSymbols(false),
     BuildSinks(false), HasGeneratedNode(false),
-    PointKind(ProgramPoint::PostStmtKind) {
+    PointKind(ProgramPoint::PostStmtKind), Tag(0) {
       
     CleanedState = getLastNode()->getState();
   }
@@ -204,7 +213,7 @@
                        ProgramPoint::Kind K) {
     HasGeneratedNode = true;
     if (PurgingDeadSymbols) K = ProgramPoint::PostPurgeDeadSymbolsKind;      
-    return static_cast<NodeTy*>(NB.generateNodeImpl(S, St, Pred, K));
+    return static_cast<NodeTy*>(NB.generateNodeImpl(S, St, Pred, K, Tag));
   }
   
   NodeTy* generateNode(Stmt* S, const StateTy* St, NodeTy* Pred) {
@@ -214,7 +223,7 @@
   NodeTy* generateNode(Stmt* S, const StateTy* St, ProgramPoint::Kind K) {
     HasGeneratedNode = true;
     if (PurgingDeadSymbols) K = ProgramPoint::PostPurgeDeadSymbolsKind;      
-    return static_cast<NodeTy*>(NB.generateNodeImpl(S, St, K));
+    return static_cast<NodeTy*>(NB.generateNodeImpl(S, St, K, Tag));
   }
   
   NodeTy* generateNode(Stmt* S, const StateTy* St) {
@@ -286,6 +295,7 @@
   bool BuildSinks;
   bool HasGeneratedNode;
   ProgramPoint::Kind PointKind;
+  const void *Tag;
 };
   
 class GRBranchNodeBuilderImpl {

Modified: cfe/trunk/include/clang/Analysis/PathSensitive/GRExprEngine.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/PathSensitive/GRExprEngine.h?rev=68845&r1=68844&r2=68845&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathSensitive/GRExprEngine.h (original)
+++ cfe/trunk/include/clang/Analysis/PathSensitive/GRExprEngine.h Fri Apr 10 19:11:10 2009
@@ -538,11 +538,11 @@
     return StateMgr.AssumeInBound(St, Idx, UpperBound, Assumption, isFeasible);
   }
 
+public:
   NodeTy* MakeNode(NodeSet& Dst, Stmt* S, NodeTy* Pred, const GRState* St,
-                   ProgramPoint::Kind K = ProgramPoint::PostStmtKind) {
-    assert (Builder && "GRStmtNodeBuilder not present.");
-    return Builder->MakeNode(Dst, S, Pred, St, K);
-  }
+                   ProgramPoint::Kind K = ProgramPoint::PostStmtKind,
+                   const void *tag = 0);
+protected:
     
   /// Visit - Transfer function logic for all statements.  Dispatches to
   ///  other functions that handle specific kinds of statements.
@@ -673,6 +673,8 @@
     return X.isValid() ? getTF().EvalComplement(*this, cast<NonLoc>(X)) : X;
   }
   
+public:
+  
   SVal EvalBinOp(BinaryOperator::Opcode Op, NonLoc L, NonLoc R, QualType T) {
     return R.isValid() ? getTF().DetermEvalBinOpNN(*this, Op, L, R, T)
                        : R;
@@ -692,40 +694,41 @@
   
   SVal EvalBinOp(BinaryOperator::Opcode Op, SVal L, SVal R, QualType T);
   
-  void EvalCall(NodeSet& Dst, CallExpr* CE, SVal L, NodeTy* Pred) {
-    assert (Builder && "GRStmtNodeBuilder must be defined.");
-    getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
-  }
+protected:
+  
+  void EvalCall(NodeSet& Dst, CallExpr* CE, SVal L, NodeTy* Pred);
   
   void EvalObjCMessageExpr(NodeSet& Dst, ObjCMessageExpr* ME, NodeTy* Pred) {
     assert (Builder && "GRStmtNodeBuilder must be defined.");
     getTF().EvalObjCMessageExpr(Dst, *this, *Builder, ME, Pred);
   }
+
+  void EvalReturn(NodeSet& Dst, ReturnStmt* s, NodeTy* Pred);
+  
+  const GRState* MarkBranch(const GRState* St, Stmt* Terminator, 
+                            bool branchTaken);
   
   /// EvalBind - Handle the semantics of binding a value to a specific location.
   ///  This method is used by EvalStore, VisitDeclStmt, and others.
   void EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
                 const GRState* St, SVal location, SVal Val);
   
-  void EvalStore(NodeSet& Dst, Expr* E, NodeTy* Pred, const GRState* St,
-                 SVal TargetLV, SVal Val);
-  
-  void EvalStore(NodeSet& Dst, Expr* E, Expr* StoreE, NodeTy* Pred,
-                 const GRState* St, SVal TargetLV, SVal Val);
-  
-  // FIXME: The "CheckOnly" option exists only because Array and Field
-  //  loads aren't fully implemented.  Eventually this option will go away.
-  
+public:
   void EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
-                const GRState* St, SVal location);
+                const GRState* St, SVal location, const void *tag = 0);
   
   NodeTy* EvalLocation(Stmt* Ex, NodeTy* Pred,
-                       const GRState* St, SVal location);
+                       const GRState* St, SVal location,
+                       const void *tag = 0);
+
   
-  void EvalReturn(NodeSet& Dst, ReturnStmt* s, NodeTy* Pred);
+  void EvalStore(NodeSet& Dst, Expr* E, NodeTy* Pred, const GRState* St,
+                 SVal TargetLV, SVal Val, const void *tag = 0);
+  
+  void EvalStore(NodeSet& Dst, Expr* E, Expr* StoreE, NodeTy* Pred,
+                 const GRState* St, SVal TargetLV, SVal Val,
+                 const void *tag = 0);
   
-  const GRState* MarkBranch(const GRState* St, Stmt* Terminator, 
-                            bool branchTaken);
 };
   
 } // end clang namespace

Modified: cfe/trunk/include/clang/Analysis/PathSensitive/GRExprEngineBuilders.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/PathSensitive/GRExprEngineBuilders.h?rev=68845&r1=68844&r2=68845&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathSensitive/GRExprEngineBuilders.h (original)
+++ cfe/trunk/include/clang/Analysis/PathSensitive/GRExprEngineBuilders.h Fri Apr 10 19:11:10 2009
@@ -52,6 +52,7 @@
   const unsigned OldSize;
   const bool AutoCreateNode;
   SaveAndRestore<bool> OldSink;
+  SaveAndRestore<const void*> OldTag;
   SaveOr OldHasGen;
 
 private:
@@ -68,7 +69,7 @@
                        const Stmt* s, bool auto_create_node)
   : Dst(dst), B(builder), Eng(eng), Pred(pred),
     state(st), stmt(s), OldSize(Dst.size()), AutoCreateNode(auto_create_node),
-    OldSink(B.BuildSinks), OldHasGen(B.HasGeneratedNode) {}
+    OldSink(B.BuildSinks), OldTag(B.Tag), OldHasGen(B.HasGeneratedNode) {}
   
 public:
 

Modified: cfe/trunk/include/clang/Analysis/PathSensitive/MemRegion.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/PathSensitive/MemRegion.h?rev=68845&r1=68844&r2=68845&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathSensitive/MemRegion.h (original)
+++ cfe/trunk/include/clang/Analysis/PathSensitive/MemRegion.h Fri Apr 10 19:11:10 2009
@@ -320,6 +320,8 @@
   static bool classof(const MemRegion* R) {
     return R->getKind() == TypedViewRegionKind;
   }
+  
+  const MemRegion *removeViews() const;
 };
   
 

Modified: cfe/trunk/include/clang/Analysis/PathSensitive/ValueManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/PathSensitive/ValueManager.h?rev=68845&r1=68844&r2=68845&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathSensitive/ValueManager.h (original)
+++ cfe/trunk/include/clang/Analysis/PathSensitive/ValueManager.h Fri Apr 10 19:11:10 2009
@@ -91,7 +91,9 @@
                     const llvm::APSInt& rhs, QualType T);
   
   NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
-                    const SymExpr *rhs, QualType T);  
+                    const SymExpr *rhs, QualType T);
+  
+  NonLoc makeTruthVal(bool b, QualType T);
 };
 } // end clang namespace
 #endif

Modified: cfe/trunk/include/clang/Analysis/ProgramPoint.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/ProgramPoint.h?rev=68845&r1=68844&r2=68845&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/ProgramPoint.h (original)
+++ cfe/trunk/include/clang/Analysis/ProgramPoint.h Fri Apr 10 19:11:10 2009
@@ -57,9 +57,9 @@
     : Data(reinterpret_cast<uintptr_t>(P1) | TwoPointers,
            reinterpret_cast<uintptr_t>(P2)), Tag(tag) {}
 
-  ProgramPoint(const void* P1, const void* P2, bool)
+  ProgramPoint(const void* P1, const void* P2, bool, const void *tag = 0)
     : Data(reinterpret_cast<uintptr_t>(P1) | Custom,
-           reinterpret_cast<uintptr_t>(P2)), Tag(0) {}
+           reinterpret_cast<uintptr_t>(P2)), Tag(tag) {}
 
 protected:
   void* getData1NoMask() const {
@@ -173,10 +173,13 @@
   PostStmt(const Stmt* S, Kind k,const void *tag = 0)
     : ProgramPoint(S, k, tag) {}
 
-  PostStmt(const Stmt* S, const void* data) : ProgramPoint(S, data, true) {}
+  PostStmt(const Stmt* S, const void* data, bool, const void *tag =0)
+    : ProgramPoint(S, data, true, tag) {}
   
 public:
-  PostStmt(const Stmt* S) : ProgramPoint(S, PostStmtKind) {}
+  PostStmt(const Stmt* S, const void *tag = 0)
+    : ProgramPoint(S, PostStmtKind, tag) {}
+
       
   Stmt* getStmt() const { return (Stmt*) getData1(); }
 
@@ -200,7 +203,7 @@
 public:
   PostStmtCustom(const Stmt* S,
                  const std::pair<const void*, const void*>* TaggedData)
-    : PostStmt(S, TaggedData) {
+    : PostStmt(S, TaggedData, true) {
     assert(getKind() == PostStmtCustomKind);
   }
 
@@ -219,8 +222,8 @@
   
 class PostOutOfBoundsCheckFailed : public PostStmt {
 public:
-  PostOutOfBoundsCheckFailed(const Stmt* S)
-  : PostStmt(S, PostOutOfBoundsCheckFailedKind) {}
+  PostOutOfBoundsCheckFailed(const Stmt* S, const void *tag = 0)
+  : PostStmt(S, PostOutOfBoundsCheckFailedKind, tag) {}
   
   static bool classof(const ProgramPoint* Location) {
     return Location->getKind() == PostOutOfBoundsCheckFailedKind;
@@ -229,8 +232,8 @@
 
 class PostUndefLocationCheckFailed : public PostStmt {
 public:
-  PostUndefLocationCheckFailed(const Stmt* S)
-  : PostStmt(S, PostUndefLocationCheckFailedKind) {}
+  PostUndefLocationCheckFailed(const Stmt* S, const void *tag = 0)
+  : PostStmt(S, PostUndefLocationCheckFailedKind, tag) {}
   
   static bool classof(const ProgramPoint* Location) {
     return Location->getKind() == PostUndefLocationCheckFailedKind;
@@ -239,8 +242,8 @@
   
 class PostNullCheckFailed : public PostStmt {
 public:
-  PostNullCheckFailed(const Stmt* S)
-  : PostStmt(S, PostNullCheckFailedKind) {}
+  PostNullCheckFailed(const Stmt* S, const void *tag = 0)
+  : PostStmt(S, PostNullCheckFailedKind, tag) {}
   
   static bool classof(const ProgramPoint* Location) {
     return Location->getKind() == PostNullCheckFailedKind;
@@ -269,7 +272,8 @@
   
 class PostPurgeDeadSymbols : public PostStmt {
 public:
-  PostPurgeDeadSymbols(const Stmt* S) : PostStmt(S, PostPurgeDeadSymbolsKind) {}
+  PostPurgeDeadSymbols(const Stmt* S, const void *tag = 0)
+    : PostStmt(S, PostPurgeDeadSymbolsKind, tag) {}
   
   static bool classof(const ProgramPoint* Location) {
     return Location->getKind() == PostPurgeDeadSymbolsKind;

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

==============================================================================
--- cfe/trunk/lib/Analysis/BasicStore.cpp (original)
+++ cfe/trunk/lib/Analysis/BasicStore.cpp Fri Apr 10 19:11:10 2009
@@ -282,6 +282,23 @@
     return UnknownVal();
 }
 
+static bool isHigherOrderVoidPtr(QualType T, ASTContext &C) {
+  bool foundPointer = false;
+  while (1) {  
+    const PointerType *PT = T->getAsPointerType();
+    if (!PT) {
+      if (!foundPointer)
+        return false;
+      
+      QualType X = C.getCanonicalType(T).getUnqualifiedType();
+      return X == C.VoidTy;
+    }
+    
+    foundPointer = true;
+    T = PT->getPointeeType();
+  }  
+}
+
 SVal BasicStoreManager::Retrieve(const GRState* state, Loc loc, QualType T) {
   
   if (isa<UnknownVal>(loc))
@@ -294,6 +311,20 @@
     case loc::MemRegionKind: {
       const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
       
+      if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) {
+        // Just support void**, void***, etc., for now.  This is needed
+        // to handle OSCompareAndSwapPtr().
+        ASTContext &Ctx = StateMgr.getContext();
+        QualType T = TR->getLValueType(Ctx);
+
+        if (!isHigherOrderVoidPtr(T, Ctx))
+          return UnknownVal();
+        
+        // Otherwise, strip the views.
+        // FIXME: Should we layer a TypedView on the result?
+        R = TR->removeViews();
+      }
+      
       if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
         return UnknownVal();
       

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

==============================================================================
--- cfe/trunk/lib/Analysis/GRCoreEngine.cpp (original)
+++ cfe/trunk/lib/Analysis/GRCoreEngine.cpp Fri Apr 10 19:11:10 2009
@@ -368,47 +368,49 @@
     Eng.WList->Enqueue(Succ, B, Idx+1);
 }
 
-static inline PostStmt GetPostLoc(Stmt* S, ProgramPoint::Kind K) {
+static inline PostStmt GetPostLoc(Stmt* S, ProgramPoint::Kind K,
+                                  const void *tag) {
   switch (K) {
     default:
       assert(false && "Invalid PostXXXKind.");
       
     case ProgramPoint::PostStmtKind:
-      return PostStmt(S);
+      return PostStmt(S, tag);
       
     case ProgramPoint::PostLoadKind:
-      return PostLoad(S);
+      return PostLoad(S, tag);
 
     case ProgramPoint::PostUndefLocationCheckFailedKind:
-      return PostUndefLocationCheckFailed(S);
+      return PostUndefLocationCheckFailed(S, tag);
 
     case ProgramPoint::PostLocationChecksSucceedKind:
-      return PostLocationChecksSucceed(S);
+      return PostLocationChecksSucceed(S, tag);
       
     case ProgramPoint::PostOutOfBoundsCheckFailedKind:
-      return PostOutOfBoundsCheckFailed(S);
+      return PostOutOfBoundsCheckFailed(S, tag);
       
     case ProgramPoint::PostNullCheckFailedKind:
-      return PostNullCheckFailed(S);
+      return PostNullCheckFailed(S, tag);
       
     case ProgramPoint::PostStoreKind:
-      return PostStore(S);
+      return PostStore(S, tag);
       
     case ProgramPoint::PostPurgeDeadSymbolsKind:
-      return PostPurgeDeadSymbols(S);
+      return PostPurgeDeadSymbols(S, tag);
   }
 }
 
 ExplodedNodeImpl*
 GRStmtNodeBuilderImpl::generateNodeImpl(Stmt* S, const void* State,
                                         ExplodedNodeImpl* Pred,
-                                        ProgramPoint::Kind K) {
-  return generateNodeImpl(GetPostLoc(S, K), State, Pred); 
+                                        ProgramPoint::Kind K,
+                                        const void *tag) {
+  return generateNodeImpl(GetPostLoc(S, K, tag), State, Pred); 
 }
 
 ExplodedNodeImpl*
 GRStmtNodeBuilderImpl::generateNodeImpl(PostStmt Loc, const void* State,
-                                        ExplodedNodeImpl* Pred) {  
+                                        ExplodedNodeImpl* Pred) {
   bool IsNew;
   ExplodedNodeImpl* N = Eng.G->getNodeImpl(Loc, State, &IsNew);
   N->addPredecessor(Pred);

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

==============================================================================
--- cfe/trunk/lib/Analysis/GRExprEngine.cpp (original)
+++ cfe/trunk/lib/Analysis/GRExprEngine.cpp Fri Apr 10 19:11:10 2009
@@ -531,6 +531,22 @@
 }
 
 //===----------------------------------------------------------------------===//
+// Generic node creation.
+//===----------------------------------------------------------------------===//
+
+GRExprEngine::NodeTy* GRExprEngine::MakeNode(NodeSet& Dst, Stmt* S,
+                                             NodeTy* Pred,
+                                             const GRState* St,
+                                             ProgramPoint::Kind K,
+                                             const void *tag) {
+  
+  assert (Builder && "GRStmtNodeBuilder not present.");
+  SaveAndRestore<const void*> OldTag(Builder->Tag);
+  Builder->Tag = tag;
+  return Builder->MakeNode(Dst, S, Pred, St, K);
+}
+
+//===----------------------------------------------------------------------===//
 // Branch processing.
 //===----------------------------------------------------------------------===//
 
@@ -1069,12 +1085,13 @@
 ///  @param location The location to store the value
 ///  @param Val The value to be stored
 void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
-                             const GRState* state, SVal location, SVal Val) {
+                             const GRState* state, SVal location, SVal Val,
+                             const void *tag) {
   
   assert (Builder && "GRStmtNodeBuilder must be defined.");
   
   // Evaluate the location (checks for bad dereferences).
-  Pred = EvalLocation(Ex, Pred, state, location);
+  Pred = EvalLocation(Ex, Pred, state, location, tag);
   
   if (!Pred)
     return;
@@ -1084,15 +1101,18 @@
 
   // Proceed with the store.  
   SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
-  Builder->PointKind = ProgramPoint::PostStoreKind;  
+  SaveAndRestore<const void*> OldTag(Builder->Tag);
+  Builder->PointKind = ProgramPoint::PostStoreKind;
+  Builder->Tag = tag;
   EvalBind(Dst, Ex, Pred, state, location, Val);
 }
 
 void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
-                            const GRState* state, SVal location) {
+                            const GRState* state, SVal location,
+                            const void *tag) {
 
   // Evaluate the location (checks for bad dereferences).  
-  Pred = EvalLocation(Ex, Pred, state, location);
+  Pred = EvalLocation(Ex, Pred, state, location, tag);
   
   if (!Pred)
     return;
@@ -1107,27 +1127,32 @@
 
   if (location.isUnknown()) {
     // This is important.  We must nuke the old binding.
-    MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K);
+    MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K, tag);
   }
   else {
     SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
-    MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K);  
+    MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K, tag);
   }
 }
 
 void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
-                             const GRState* state, SVal location, SVal Val) {
+                             const GRState* state, SVal location, SVal Val,
+                             const void *tag) {
  
   NodeSet TmpDst;
-  EvalStore(TmpDst, StoreE, Pred, state, location, Val);
+  EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag);
 
   for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
-    MakeNode(Dst, Ex, *I, (*I)->getState());
+    MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag);
 }
 
 GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
                                                  const GRState* state,
-                                                 SVal location) {
+                                                 SVal location,
+                                                 const void *tag) {
+  
+  SaveAndRestore<const void*> OldTag(Builder->Tag);
+  Builder->Tag = tag;
   
   // Check for loads/stores from/to undefined values.  
   if (location.isUndef()) {
@@ -1235,8 +1260,144 @@
 }
 
 //===----------------------------------------------------------------------===//
+// Transfer function: OSAtomics.
+//
+// FIXME: Eventually refactor into a more "plugin" infrastructure.
+//===----------------------------------------------------------------------===//
+
+// Mac OS X:
+// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
+// atomic.3.html
+//
+static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet<GRState>& Dst,
+                                       GRExprEngine& Engine,
+                                       GRStmtNodeBuilder<GRState>& Builder,
+                                       CallExpr* CE, SVal L,                 
+                                       ExplodedNode<GRState>* Pred) {
+
+  // Not enough arguments to match OSAtomicCompareAndSwap?
+  if (CE->getNumArgs() != 3)
+    return false;
+  
+  ASTContext &C = Engine.getContext();
+  Expr *oldValueExpr = CE->getArg(0);
+  QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
+
+  Expr *newValueExpr = CE->getArg(1);
+  QualType newValueType = C.getCanonicalType(newValueExpr->getType());
+  
+  // Do the types of 'oldValue' and 'newValue' match?
+  if (oldValueType != newValueType)
+    return false;
+  
+  Expr *theValueExpr = CE->getArg(2);
+  const PointerType *theValueType = theValueExpr->getType()->getAsPointerType();
+  
+  // theValueType not a pointer?
+  if (!theValueType)
+    return false;
+  
+  QualType theValueTypePointee =
+    C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
+  
+  // The pointee must match newValueType and oldValueType.
+  if (theValueTypePointee != newValueType)
+    return false;
+  
+  static unsigned magic_load = 0;
+  static unsigned magic_store = 0;
+
+  const void *OSAtomicLoadTag = &magic_load;
+  const void *OSAtomicStoreTag = &magic_store;
+  
+  // Load 'theValue'.
+  GRStateManager &StateMgr = Engine.getStateManager();
+  const GRState *state = Pred->getState();
+  ExplodedNodeSet<GRState> Tmp;
+  SVal location = StateMgr.GetSVal(state, theValueExpr);
+  Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
+
+  for (ExplodedNodeSet<GRState>::iterator I = Tmp.begin(), E = Tmp.end();
+       I != E; ++I) {
+  
+    ExplodedNode<GRState> *N = *I;
+    const GRState *stateLoad = N->getState();
+    SVal theValueVal = StateMgr.GetSVal(stateLoad, theValueExpr);
+    SVal oldValueVal = StateMgr.GetSVal(stateLoad, oldValueExpr);
+        
+    // Perform the comparison.
+    SVal Cmp = Engine.EvalBinOp(BinaryOperator::EQ, theValueVal, oldValueVal,
+                                Engine.getContext().IntTy);
+    bool isFeasible = false;
+    const GRState *stateEqual = StateMgr.Assume(stateLoad, Cmp, true,
+                                                isFeasible);
+    
+    // Were they equal?
+    if (isFeasible) {
+      // Perform the store.
+      ExplodedNodeSet<GRState> TmpStore;
+      Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location, 
+                       StateMgr.GetSVal(stateEqual, newValueExpr),
+                       OSAtomicStoreTag);
+      
+      // Now bind the result of the comparison.
+      for (ExplodedNodeSet<GRState>::iterator I2 = TmpStore.begin(),
+           E2 = TmpStore.end(); I2 != E2; ++I2) {
+        ExplodedNode<GRState> *predNew = *I2;
+        const GRState *stateNew = predNew->getState();
+        SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
+        Engine.MakeNode(Dst, CE, predNew, Engine.BindExpr(stateNew, CE, Res));
+      }
+    }
+    
+    // Were they not equal?
+    isFeasible = false;
+    const GRState *stateNotEqual = StateMgr.Assume(stateLoad, Cmp, false,
+                                                   isFeasible);
+    
+    if (isFeasible) {
+      SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
+      Engine.MakeNode(Dst, CE, N, Engine.BindExpr(stateNotEqual, CE, Res));
+    }
+  }
+      
+  return true;
+}
+
+static bool EvalOSAtomic(ExplodedNodeSet<GRState>& Dst,
+                         GRExprEngine& Engine,
+                         GRStmtNodeBuilder<GRState>& Builder,
+                         CallExpr* CE, SVal L,
+                         ExplodedNode<GRState>* Pred) {
+  
+  if (!isa<loc::FuncVal>(L))
+    return false;
+  
+  const FunctionDecl *FD = cast<loc::FuncVal>(L).getDecl();
+  const char *FName = FD->getNameAsCString();
+  
+  // Check for compare and swap.
+  if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0)
+    return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
+  
+  // FIXME: Other atomics.
+  return false;
+}
+
+//===----------------------------------------------------------------------===//
 // Transfer function: Function calls.
 //===----------------------------------------------------------------------===//
+
+void GRExprEngine::EvalCall(NodeSet& Dst, CallExpr* CE, SVal L, NodeTy* Pred) {
+  assert (Builder && "GRStmtNodeBuilder must be defined.");
+  
+  // FIXME: Allow us to chain together transfer functions.
+  if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
+      return;
+      
+  getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
+}
+
 void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
                              CallExpr::arg_iterator AI,
                              CallExpr::arg_iterator AE,

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

==============================================================================
--- cfe/trunk/lib/Analysis/MemRegion.cpp (original)
+++ cfe/trunk/lib/Analysis/MemRegion.cpp Fri Apr 10 19:11:10 2009
@@ -481,6 +481,21 @@
     
     SR = dyn_cast<SubRegion>(R);    
   }
-  
+
   return false;
 }
+
+
+//===----------------------------------------------------------------------===//
+// View handling.
+//===----------------------------------------------------------------------===//
+
+const MemRegion *TypedViewRegion::removeViews() const {
+  const SubRegion *SR = this;
+  const MemRegion *R = SR;
+  while (SR && isa<TypedViewRegion>(SR)) {
+    R = SR->getSuperRegion();
+    SR = dyn_cast<SubRegion>(R);
+  }
+  return R;
+}

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

==============================================================================
--- cfe/trunk/lib/Analysis/SVals.cpp (original)
+++ cfe/trunk/lib/Analysis/SVals.cpp Fri Apr 10 19:11:10 2009
@@ -271,6 +271,10 @@
   return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
 }
 
+NonLoc ValueManager::makeTruthVal(bool b, QualType T) {
+  return nonloc::ConcreteInt(BasicVals.getTruthValue(b, T));
+}
+
 NonLoc NonLoc::MakeCompoundVal(QualType T, llvm::ImmutableList<SVal> Vals,
                                BasicValueFactory& BasicVals) {
   return nonloc::CompoundVal(BasicVals.getCompoundValData(T, Vals));

Modified: cfe/trunk/test/Analysis/NSString.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/NSString.m?rev=68845&r1=68844&r2=68845&view=diff

==============================================================================
--- cfe/trunk/test/Analysis/NSString.m (original)
+++ cfe/trunk/test/Analysis/NSString.m Fri Apr 10 19:11:10 2009
@@ -1,7 +1,9 @@
 // RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -verify %s &&
-// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -verify %s &&
-// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify %s &&
-// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=range -verify %s
+// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -verify %s
+
+
+// NOTWORK: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify %s &&
+// NOTWORK: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=range -verify %s
 
 //===----------------------------------------------------------------------===//
 // The following code is reduced using delta-debugging from
@@ -212,3 +214,15 @@
   return [[SharedClass alloc] _init]; // no-warning
 }
 
+// Test OSCompareAndSwap
+_Bool OSAtomicCompareAndSwapPtr( void *__oldValue, void *__newValue, void * volatile *__theValue );
+
+void testOSCompareAndSwap() {
+  NSString *old = 0;
+  NSString *s = [[NSString alloc] init];
+  if (!OSAtomicCompareAndSwapPtr(0, s, (void**) &old))
+    [s release];
+  else    
+    [old release];
+}
+





More information about the cfe-commits mailing list