]> granicus.if.org Git - clang/commitdiff
Generate unique identifiers for Decl objects
authorGeorge Karpenkov <ekarpenkov@apple.com>
Sat, 15 Sep 2018 02:03:58 +0000 (02:03 +0000)
committerGeorge Karpenkov <ekarpenkov@apple.com>
Sat, 15 Sep 2018 02:03:58 +0000 (02:03 +0000)
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

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

index 37ce07b6db6232ef7d87d6989fe0db7a2f9ecbed..0d521200170e416fef16e7637fcb1bfeab043e0a 100644 (file)
@@ -1141,6 +1141,9 @@ public:
 
   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
   /// when possible. Will return null if the type underlying the Decl does not
   /// have a FunctionType.
index 796b5325ee8576e350c413008604dda7f468c968..6bb2034a9ea95965666cc97cc209f2d46f6ac7f4 100644 (file)
@@ -930,6 +930,13 @@ bool Decl::AccessDeclContextSanity() const {
 static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
 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 {
   QualType Ty;
   if (const auto *D = dyn_cast<ValueDecl>(this))