]> granicus.if.org Git - clang/commitdiff
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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@342309 91177308-0d34-0410-b5e6-96231b3b80d8

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

index b9cfda7c6b8f059ef029b348dec4b337bf52d073..33d4e70e02ac5b3f53b856468c6f450cbcd8998f 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 6ed71fd6733a32ed88d550e6e2f0d488f70f6ab9..687b9c24b0939f3c6939e65511894364ade48551 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) {