1 //===--- DWARFVisitor.h -----------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 //===----------------------------------------------------------------------===// 10 11 #ifndef LLVM_OBJECTYAML_DWARFVISITOR_H 12 #define LLVM_OBJECTYAML_DWARFVISITOR_H 13 14 #include "llvm/ADT/StringRef.h" 15 #include "llvm/BinaryFormat/Dwarf.h" 16 #include "llvm/Support/MemoryBuffer.h" 17 18 namespace llvm { 19 20 namespace DWARFYAML { 21 22 struct Data; 23 struct Unit; 24 struct Entry; 25 struct FormValue; 26 struct AttributeAbbrev; 27 28 /// A class to visits DWARFYAML Compile Units and DIEs in preorder. 29 /// 30 /// Extensions of this class can either maintain const or non-const references 31 /// to the DWARFYAML::Data object. 32 template <typename T> class VisitorImpl { 33 protected: 34 T &DebugInfo; 35 36 /// Visitor Functions 37 /// @{ onStartCompileUnit(Unit & CU)38 virtual void onStartCompileUnit(Unit &CU) {} onEndCompileUnit(Unit & CU)39 virtual void onEndCompileUnit(Unit &CU) {} onStartDIE(Unit & CU,Entry & DIE)40 virtual void onStartDIE(Unit &CU, Entry &DIE) {} onEndDIE(Unit & CU,Entry & DIE)41 virtual void onEndDIE(Unit &CU, Entry &DIE) {} onForm(AttributeAbbrev & AttAbbrev,FormValue & Value)42 virtual void onForm(AttributeAbbrev &AttAbbrev, FormValue &Value) {} 43 /// @} 44 45 /// Const Visitor Functions 46 /// @{ onStartCompileUnit(const Unit & CU)47 virtual void onStartCompileUnit(const Unit &CU) {} onEndCompileUnit(const Unit & CU)48 virtual void onEndCompileUnit(const Unit &CU) {} onStartDIE(const Unit & CU,const Entry & DIE)49 virtual void onStartDIE(const Unit &CU, const Entry &DIE) {} onEndDIE(const Unit & CU,const Entry & DIE)50 virtual void onEndDIE(const Unit &CU, const Entry &DIE) {} onForm(const AttributeAbbrev & AttAbbrev,const FormValue & Value)51 virtual void onForm(const AttributeAbbrev &AttAbbrev, 52 const FormValue &Value) {} 53 /// @} 54 55 /// Value visitors 56 /// @{ onValue(const uint8_t U)57 virtual void onValue(const uint8_t U) {} onValue(const uint16_t U)58 virtual void onValue(const uint16_t U) {} onValue(const uint32_t U)59 virtual void onValue(const uint32_t U) {} 60 virtual void onValue(const uint64_t U, const bool LEB = false) {} 61 virtual void onValue(const int64_t S, const bool LEB = false) {} onValue(const StringRef String)62 virtual void onValue(const StringRef String) {} onValue(const MemoryBufferRef MBR)63 virtual void onValue(const MemoryBufferRef MBR) {} 64 /// @} 65 66 public: VisitorImpl(T & DI)67 VisitorImpl(T &DI) : DebugInfo(DI) {} 68 ~VisitorImpl()69 virtual ~VisitorImpl() {} 70 71 void traverseDebugInfo(); 72 73 private: 74 void onVariableSizeValue(uint64_t U, unsigned Size); 75 }; 76 77 // Making the visior instantiations extern and explicit in the cpp file. This 78 // prevents them from being instantiated in every compile unit that uses the 79 // visitors. 80 extern template class VisitorImpl<DWARFYAML::Data>; 81 extern template class VisitorImpl<const DWARFYAML::Data>; 82 83 class Visitor : public VisitorImpl<Data> { 84 public: Visitor(Data & DI)85 Visitor(Data &DI) : VisitorImpl<Data>(DI) {} 86 }; 87 88 class ConstVisitor : public VisitorImpl<const Data> { 89 public: ConstVisitor(const Data & DI)90 ConstVisitor(const Data &DI) : VisitorImpl<const Data>(DI) {} 91 }; 92 93 } // namespace DWARFYAML 94 } // namespace llvm 95 96 #endif 97