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