From 5eb4cc66eb716f9cc47b842ac1a30610462e5a45 Mon Sep 17 00:00:00 2001 From: George Karpenkov Date: Sat, 15 Sep 2018 02:01:47 +0000 Subject: [PATCH] Support generating unique identifiers for Stmt objects 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 | 3 +++ clang/lib/AST/Stmt.cpp | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/clang/include/clang/AST/Stmt.h b/clang/include/clang/AST/Stmt.h index b9cfda7..33d4e70 100644 --- a/clang/include/clang/AST/Stmt.h +++ b/clang/include/clang/AST/Stmt.h @@ -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; diff --git a/clang/lib/AST/Stmt.cpp b/clang/lib/AST/Stmt.cpp index 6ed71fd..687b9c2 100644 --- a/clang/lib/AST/Stmt.cpp +++ b/clang/lib/AST/Stmt.cpp @@ -302,6 +302,14 @@ SourceLocation Stmt::getEndLoc() const { llvm_unreachable("unknown statement kind"); } +int64_t Stmt::getID(const ASTContext &Context) const { + Optional 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 Stmts, SourceLocation LB, SourceLocation RB) : Stmt(CompoundStmtClass), LBraceLoc(LB), RBraceLoc(RB) { -- 2.7.4