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