Support generating unique identifiers for Stmt objects
authorGeorge Karpenkov <ekarpenkov@apple.com>
Sat, 15 Sep 2018 02:01:47 +0000 (02:01 +0000)
committerGeorge Karpenkov <ekarpenkov@apple.com>
Sat, 15 Sep 2018 02:01:47 +0000 (02:01 +0000)
The generated identifiers are stable across multiple runs, and can be a
great debug or visualization aid.

Differential Revision: https://reviews.llvm.org/D51822

llvm-svn: 342309

clang/include/clang/AST/Stmt.h
clang/lib/AST/Stmt.cpp

index b9cfda7..33d4e70 100644 (file)
@@ -413,6 +413,9 @@ public:
   void dump(raw_ostream &OS, SourceManager &SM) const;
   void dump(raw_ostream &OS) const;
 
+  /// \return Unique reproducible object identifier
+  int64_t getID(const ASTContext &Context) const;
+
   /// dumpColor - same as dump(), but forces color highlighting.
   void dumpColor() const;
 
index 6ed71fd..687b9c2 100644 (file)
@@ -302,6 +302,14 @@ SourceLocation Stmt::getEndLoc() const {
   llvm_unreachable("unknown statement kind");
 }
 
+int64_t Stmt::getID(const ASTContext &Context) const {
+  Optional<int64_t> Out = Context.getAllocator().identifyObject(this);
+  assert(Out && "Wrong allocator used");
+  assert(*Out % alignof(Stmt) == 0 && "Wrong alignment information");
+  return *Out / alignof(Stmt);
+
+}
+
 CompoundStmt::CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB,
                            SourceLocation RB)
     : Stmt(CompoundStmtClass), LBraceLoc(LB), RBraceLoc(RB) {