[llvm-dev] [RFC PATCH 1/2] [clang]: Add AuxAttr support

Wang Nan via llvm-dev llvm-dev at lists.llvm.org
Tue Aug 18 07:53:32 PDT 2015


This patch adds EmitTypeAuxAttribute() function to CGDebugInfo, which
allows other parts of clang issue auxiliary information through an
enumeration type in Dwarf information. For example, by calling

  DI->EmitTypeAuxAttribute(type, "ID", 1234);

We can get following information in dwarf:

 <1><3f>: Abbrev Number: 3 (DW_TAG_structure_type)
    <40>   DW_AT_name        : (indirect string, offset: 0xeb): my_str
    ...
 <2><47>: Abbrev Number: 4 (DW_TAG_member)
    <48>   DW_AT_name        : (indirect string, offset: 0xe3): x
    <4c>   DW_AT_type        : <0x6d>
    ...
    ...
 <2><5f>: Abbrev Number: 5 (DW_TAG_enumeration_type)
    <60>   DW_AT_name        : (indirect string, offset: 0x140): .llvm.aux.attr
    <64>   DW_AT_byte_size   : 0
 <3><65>: Abbrev Number: 6 (DW_TAG_enumerator)
    <66>   DW_AT_name        : (indirect string, offset: 0x13c): .ID
    <6a>   DW_AT_const_value : 1234

By DW_TAG_enumeration_type '.llvm.aux.attr', we can connect an
attribute named ".ID" to this structure type, whose value should be
1234.

Note that enumeration types and enumerators generated by
EmitTypeAuxAttribute() are all leaded with a '.', which would be never
issued by normal C programs.

Signed-off-by: Wang Nan <wangnan0 at huawei.com>
---
 lib/CodeGen/CGDebugInfo.cpp | 31 +++++++++++++++++++++++++++++++
 lib/CodeGen/CGDebugInfo.h   |  5 +++++
 2 files changed, 36 insertions(+)

diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp
index eaa1e25..99d4230 100644
--- a/lib/CodeGen/CGDebugInfo.cpp
+++ b/lib/CodeGen/CGDebugInfo.cpp
@@ -3363,6 +3363,31 @@ void CGDebugInfo::finalize() {
          RE = RetainedTypes.end(); RI != RE; ++RI)
     DBuilder.retainType(cast<llvm::DIType>(TypeCache[*RI]));
 
+
+  if (AuxAttrs.size() > 0) {
+    for (const auto &p_type : AuxAttrs) {
+      SmallVector<llvm::Metadata *, 16> Enumerators;
+      const Type *type = p_type.first;
+
+      for (const auto &p_attr : p_type.second) {
+        std::string key("." + p_attr.first);
+        std::replace(key.begin(), key.end(), ' ', '.');
+
+        Enumerators.push_back(DBuilder.createEnumerator(StringRef(key),
+			      p_attr.second));
+      }
+
+      llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
+
+      llvm::DIFile *file = getOrCreateFile(SourceLocation());
+
+      DBuilder.createEnumerationType(
+                    type == nullptr ? file : getContextDescriptor(type->getAsTagDecl()),
+		    ".llvm.aux.attr", file, 0, 0, 0,
+		    EltArray, nullptr, SmallString<256>());
+    }
+  }
+
   DBuilder.finalize();
 }
 
@@ -3374,3 +3399,9 @@ void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
     // Don't ignore in case of explicit cast where it is referenced indirectly.
     DBuilder.retainType(DieTy);
 }
+
+void CGDebugInfo::EmitTypeAuxAttribute(const Type *Ty, std::string key,
+				       uint64_t val)
+{
+  AuxAttrs[Ty].push_back(std::make_pair(key, val));
+}
diff --git a/lib/CodeGen/CGDebugInfo.h b/lib/CodeGen/CGDebugInfo.h
index 9c62f6c..94abd3b 100644
--- a/lib/CodeGen/CGDebugInfo.h
+++ b/lib/CodeGen/CGDebugInfo.h
@@ -121,6 +121,9 @@ class CGDebugInfo {
   llvm::DenseMap<const Decl *, llvm::TypedTrackingMDRef<llvm::DIDerivedType>>
       StaticDataMemberCache;
 
+  llvm::DenseMap<const Type *, std::vector<std::pair<std::string, uint64_t> > >
+      AuxAttrs;
+
   /// Helper functions for getOrCreateType.
   /// @{
   /// Currently the checksum of an interface includes the number of
@@ -337,6 +340,8 @@ public:
   /// Emit C++ namespace alias.
   llvm::DIImportedEntity *EmitNamespaceAlias(const NamespaceAliasDecl &NA);
 
+  void EmitTypeAuxAttribute(const Type *Ty, std::string key, uint64_t val);
+
   /// Emit record type's standalone debug info.
   llvm::DIType *getOrCreateRecordType(QualType Ty, SourceLocation L);
 
-- 
1.8.3.4



More information about the llvm-dev mailing list