r342309 - Support generating unique identifiers for Stmt objects

George Karpenkov via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 14 19:01:47 PDT 2018


Author: george.karpenkov
Date: Fri Sep 14 19:01:47 2018
New Revision: 342309

URL: http://llvm.org/viewvc/llvm-project?rev=342309&view=rev
Log:
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

Modified:
    cfe/trunk/include/clang/AST/Stmt.h
    cfe/trunk/lib/AST/Stmt.cpp

Modified: cfe/trunk/include/clang/AST/Stmt.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Stmt.h?rev=342309&r1=342308&r2=342309&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Stmt.h (original)
+++ cfe/trunk/include/clang/AST/Stmt.h Fri Sep 14 19:01:47 2018
@@ -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;
 

Modified: cfe/trunk/lib/AST/Stmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Stmt.cpp?rev=342309&r1=342308&r2=342309&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Stmt.cpp (original)
+++ cfe/trunk/lib/AST/Stmt.cpp Fri Sep 14 19:01:47 2018
@@ -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) {




More information about the cfe-commits mailing list