[cfe-commits] r52553 - in /cfe/trunk: include/clang/AST/ParentMap.h lib/AST/ParentMap.cpp

Ted Kremenek kremenek at apple.com
Fri Jun 20 14:40:36 PDT 2008


Author: kremenek
Date: Fri Jun 20 16:40:36 2008
New Revision: 52553

URL: http://llvm.org/viewvc/llvm-project?rev=52553&view=rev
Log:
Added ParentMap, a class to represent a lazily constructed mapping from child to parents.

Added:
    cfe/trunk/include/clang/AST/ParentMap.h
    cfe/trunk/lib/AST/ParentMap.cpp

Added: cfe/trunk/include/clang/AST/ParentMap.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ParentMap.h?rev=52553&view=auto

==============================================================================
--- cfe/trunk/include/clang/AST/ParentMap.h (added)
+++ cfe/trunk/include/clang/AST/ParentMap.h Fri Jun 20 16:40:36 2008
@@ -0,0 +1,36 @@
+//===--- ParentMap.h - Mappings from Stmts to their Parents -----*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines the ParentMap class.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_PARENTMAP_H
+#define LLVM_CLANG_PARENTMAP_H
+
+namespace clang {
+class Stmt;
+  
+class ParentMap {
+  void* Impl;
+public:
+  ParentMap(Stmt* ASTRoot);
+  ~ParentMap();
+
+  Stmt* getParent(Stmt*) const;  
+
+  bool hasParent(Stmt* S) const {
+    return !getParent(S);
+  }
+  
+  bool isSubExpr(Stmt *S) const;
+};
+  
+} // end clang namespace
+#endif

Added: cfe/trunk/lib/AST/ParentMap.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ParentMap.cpp?rev=52553&view=auto

==============================================================================
--- cfe/trunk/lib/AST/ParentMap.cpp (added)
+++ cfe/trunk/lib/AST/ParentMap.cpp Fri Jun 20 16:40:36 2008
@@ -0,0 +1,54 @@
+//===--- ParentMap.cpp - Mappings from Stmts to their Parents ---*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines the ParentMap class.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/ParentMap.h"
+#include "clang/AST/Expr.h"
+#include "llvm/ADT/DenseMap.h"
+
+using namespace clang;
+
+typedef llvm::DenseMap<Stmt*, Stmt*> MapTy;
+
+static void BuildParentMap(MapTy& M, Stmt* S) {
+  for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
+    if (*I) {
+      M[*I] = S;
+      BuildParentMap(M, *I);
+    }
+}
+
+ParentMap::ParentMap(Stmt* S) : Impl(0) {
+  if (S) {
+    MapTy *M = new MapTy();
+    BuildParentMap(*M, S);
+    Impl = M;    
+  }
+}
+
+ParentMap::~ParentMap() {
+  delete (MapTy*) Impl;
+}
+
+Stmt* ParentMap::getParent(Stmt* S) const {
+  MapTy* M = (MapTy*) Impl;
+  MapTy::iterator I = M->find(S);
+  return I == M->end() ? 0 : I->second;
+}
+
+bool ParentMap::isSubExpr(Stmt* S) const {
+  if (!isa<Expr>(S))
+    return false;
+  
+  Stmt* P = getParent(S);
+  return P ? !isa<CompoundStmt>(P) : false;
+}





More information about the cfe-commits mailing list