Browse Source

Generate unique identifiers for Decl objects

The generated identifier is stable across multiple runs,
and can be a great visualization or debugging aide.

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@342315 91177308-0d34-0410-b5e6-96231b3b80d8
George Karpenkov 7 years ago
parent
commit
a495c43386
2 changed files with 10 additions and 0 deletions
  1. 3 0
      include/clang/AST/DeclBase.h
  2. 7 0
      lib/AST/DeclBase.cpp

+ 3 - 0
include/clang/AST/DeclBase.h

@@ -1141,6 +1141,9 @@ public:
 
 
   void dump(raw_ostream &Out, bool Deserialize = false) const;
   void dump(raw_ostream &Out, bool Deserialize = false) const;
 
 
+  /// \return Unique reproducible object identifier
+  int64_t getID() const;
+
   /// Looks through the Decl's underlying type to extract a FunctionType
   /// Looks through the Decl's underlying type to extract a FunctionType
   /// when possible. Will return null if the type underlying the Decl does not
   /// when possible. Will return null if the type underlying the Decl does not
   /// have a FunctionType.
   /// have a FunctionType.

+ 7 - 0
lib/AST/DeclBase.cpp

@@ -930,6 +930,13 @@ bool Decl::AccessDeclContextSanity() const {
 static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
 static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
 static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
 static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
 
 
+int64_t Decl::getID() const {
+  Optional<int64_t> Out = getASTContext().getAllocator().identifyObject(this);
+  assert(Out && "Wrong allocator used");
+  assert(*Out % alignof(Decl) == 0 && "Wrong alignment information");
+  return *Out / alignof(Decl);
+}
+
 const FunctionType *Decl::getFunctionType(bool BlocksToo) const {
 const FunctionType *Decl::getFunctionType(bool BlocksToo) const {
   QualType Ty;
   QualType Ty;
   if (const auto *D = dyn_cast<ValueDecl>(this))
   if (const auto *D = dyn_cast<ValueDecl>(this))