1 //===-- SBDeclaration.cpp ----------------------------------------*- C++-*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/API/SBDeclaration.h" 11 #include "lldb/API/SBStream.h" 12 #include "lldb/Host/PosixApi.h" 13 #include "lldb/Symbol/Declaration.h" 14 #include "lldb/Utility/Log.h" 15 #include "lldb/Utility/Stream.h" 16 17 #include <limits.h> 18 19 using namespace lldb; 20 using namespace lldb_private; 21 22 SBDeclaration::SBDeclaration() : m_opaque_ap() {} 23 24 SBDeclaration::SBDeclaration(const SBDeclaration &rhs) : m_opaque_ap() { 25 if (rhs.IsValid()) 26 ref() = rhs.ref(); 27 } 28 29 SBDeclaration::SBDeclaration(const lldb_private::Declaration *lldb_object_ptr) 30 : m_opaque_ap() { 31 if (lldb_object_ptr) 32 ref() = *lldb_object_ptr; 33 } 34 35 const SBDeclaration &SBDeclaration::operator=(const SBDeclaration &rhs) { 36 if (this != &rhs) { 37 if (rhs.IsValid()) 38 ref() = rhs.ref(); 39 else 40 m_opaque_ap.reset(); 41 } 42 return *this; 43 } 44 45 void SBDeclaration::SetDeclaration( 46 const lldb_private::Declaration &lldb_object_ref) { 47 ref() = lldb_object_ref; 48 } 49 50 SBDeclaration::~SBDeclaration() {} 51 52 bool SBDeclaration::IsValid() const { 53 return m_opaque_ap.get() && m_opaque_ap->IsValid(); 54 } 55 56 SBFileSpec SBDeclaration::GetFileSpec() const { 57 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 58 59 SBFileSpec sb_file_spec; 60 if (m_opaque_ap.get() && m_opaque_ap->GetFile()) 61 sb_file_spec.SetFileSpec(m_opaque_ap->GetFile()); 62 63 if (log) { 64 SBStream sstr; 65 sb_file_spec.GetDescription(sstr); 66 log->Printf("SBLineEntry(%p)::GetFileSpec () => SBFileSpec(%p): %s", 67 static_cast<void *>(m_opaque_ap.get()), 68 static_cast<const void *>(sb_file_spec.get()), sstr.GetData()); 69 } 70 71 return sb_file_spec; 72 } 73 74 uint32_t SBDeclaration::GetLine() const { 75 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 76 77 uint32_t line = 0; 78 if (m_opaque_ap.get()) 79 line = m_opaque_ap->GetLine(); 80 81 if (log) 82 log->Printf("SBLineEntry(%p)::GetLine () => %u", 83 static_cast<void *>(m_opaque_ap.get()), line); 84 85 return line; 86 } 87 88 uint32_t SBDeclaration::GetColumn() const { 89 if (m_opaque_ap.get()) 90 return m_opaque_ap->GetColumn(); 91 return 0; 92 } 93 94 void SBDeclaration::SetFileSpec(lldb::SBFileSpec filespec) { 95 if (filespec.IsValid()) 96 ref().SetFile(filespec.ref()); 97 else 98 ref().SetFile(FileSpec()); 99 } 100 void SBDeclaration::SetLine(uint32_t line) { ref().SetLine(line); } 101 102 void SBDeclaration::SetColumn(uint32_t column) { ref().SetColumn(column); } 103 104 bool SBDeclaration::operator==(const SBDeclaration &rhs) const { 105 lldb_private::Declaration *lhs_ptr = m_opaque_ap.get(); 106 lldb_private::Declaration *rhs_ptr = rhs.m_opaque_ap.get(); 107 108 if (lhs_ptr && rhs_ptr) 109 return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) == 0; 110 111 return lhs_ptr == rhs_ptr; 112 } 113 114 bool SBDeclaration::operator!=(const SBDeclaration &rhs) const { 115 lldb_private::Declaration *lhs_ptr = m_opaque_ap.get(); 116 lldb_private::Declaration *rhs_ptr = rhs.m_opaque_ap.get(); 117 118 if (lhs_ptr && rhs_ptr) 119 return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) != 0; 120 121 return lhs_ptr != rhs_ptr; 122 } 123 124 const lldb_private::Declaration *SBDeclaration::operator->() const { 125 return m_opaque_ap.get(); 126 } 127 128 lldb_private::Declaration &SBDeclaration::ref() { 129 if (m_opaque_ap.get() == NULL) 130 m_opaque_ap.reset(new lldb_private::Declaration()); 131 return *m_opaque_ap; 132 } 133 134 const lldb_private::Declaration &SBDeclaration::ref() const { 135 return *m_opaque_ap; 136 } 137 138 bool SBDeclaration::GetDescription(SBStream &description) { 139 Stream &strm = description.ref(); 140 141 if (m_opaque_ap.get()) { 142 char file_path[PATH_MAX * 2]; 143 m_opaque_ap->GetFile().GetPath(file_path, sizeof(file_path)); 144 strm.Printf("%s:%u", file_path, GetLine()); 145 if (GetColumn() > 0) 146 strm.Printf(":%u", GetColumn()); 147 } else 148 strm.PutCString("No value"); 149 150 return true; 151 } 152 153 lldb_private::Declaration *SBDeclaration::get() { return m_opaque_ap.get(); } 154