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 "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/Host/PosixApi.h"
14 #include "lldb/Symbol/Declaration.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   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBDeclaration);
24 }
25 
26 SBDeclaration::SBDeclaration(const SBDeclaration &rhs) : m_opaque_up() {
27   LLDB_RECORD_CONSTRUCTOR(SBDeclaration, (const lldb::SBDeclaration &), rhs);
28 
29   m_opaque_up = clone(rhs.m_opaque_up);
30 }
31 
32 SBDeclaration::SBDeclaration(const lldb_private::Declaration *lldb_object_ptr)
33     : m_opaque_up() {
34   if (lldb_object_ptr)
35     m_opaque_up = llvm::make_unique<Declaration>(*lldb_object_ptr);
36 }
37 
38 const SBDeclaration &SBDeclaration::operator=(const SBDeclaration &rhs) {
39   LLDB_RECORD_METHOD(const lldb::SBDeclaration &,
40                      SBDeclaration, operator=,(const lldb::SBDeclaration &),
41                      rhs);
42 
43   if (this != &rhs)
44     m_opaque_up = clone(rhs.m_opaque_up);
45   return *this;
46 }
47 
48 void SBDeclaration::SetDeclaration(
49     const lldb_private::Declaration &lldb_object_ref) {
50   ref() = lldb_object_ref;
51 }
52 
53 SBDeclaration::~SBDeclaration() {}
54 
55 bool SBDeclaration::IsValid() const {
56   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDeclaration, IsValid);
57 
58   return m_opaque_up.get() && m_opaque_up->IsValid();
59 }
60 
61 SBFileSpec SBDeclaration::GetFileSpec() const {
62   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBDeclaration,
63                                    GetFileSpec);
64 
65 
66   SBFileSpec sb_file_spec;
67   if (m_opaque_up.get() && m_opaque_up->GetFile())
68     sb_file_spec.SetFileSpec(m_opaque_up->GetFile());
69 
70 
71   return LLDB_RECORD_RESULT(sb_file_spec);
72 }
73 
74 uint32_t SBDeclaration::GetLine() const {
75   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBDeclaration, GetLine);
76 
77 
78   uint32_t line = 0;
79   if (m_opaque_up)
80     line = m_opaque_up->GetLine();
81 
82 
83   return line;
84 }
85 
86 uint32_t SBDeclaration::GetColumn() const {
87   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBDeclaration, GetColumn);
88 
89   if (m_opaque_up)
90     return m_opaque_up->GetColumn();
91   return 0;
92 }
93 
94 void SBDeclaration::SetFileSpec(lldb::SBFileSpec filespec) {
95   LLDB_RECORD_METHOD(void, SBDeclaration, SetFileSpec, (lldb::SBFileSpec),
96                      filespec);
97 
98   if (filespec.IsValid())
99     ref().SetFile(filespec.ref());
100   else
101     ref().SetFile(FileSpec());
102 }
103 void SBDeclaration::SetLine(uint32_t line) {
104   LLDB_RECORD_METHOD(void, SBDeclaration, SetLine, (uint32_t), line);
105 
106   ref().SetLine(line);
107 }
108 
109 void SBDeclaration::SetColumn(uint32_t column) {
110   LLDB_RECORD_METHOD(void, SBDeclaration, SetColumn, (uint32_t), column);
111 
112   ref().SetColumn(column);
113 }
114 
115 bool SBDeclaration::operator==(const SBDeclaration &rhs) const {
116   LLDB_RECORD_METHOD_CONST(
117       bool, SBDeclaration, operator==,(const lldb::SBDeclaration &), rhs);
118 
119   lldb_private::Declaration *lhs_ptr = m_opaque_up.get();
120   lldb_private::Declaration *rhs_ptr = rhs.m_opaque_up.get();
121 
122   if (lhs_ptr && rhs_ptr)
123     return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) == 0;
124 
125   return lhs_ptr == rhs_ptr;
126 }
127 
128 bool SBDeclaration::operator!=(const SBDeclaration &rhs) const {
129   LLDB_RECORD_METHOD_CONST(
130       bool, SBDeclaration, operator!=,(const lldb::SBDeclaration &), rhs);
131 
132   lldb_private::Declaration *lhs_ptr = m_opaque_up.get();
133   lldb_private::Declaration *rhs_ptr = rhs.m_opaque_up.get();
134 
135   if (lhs_ptr && rhs_ptr)
136     return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) != 0;
137 
138   return lhs_ptr != rhs_ptr;
139 }
140 
141 const lldb_private::Declaration *SBDeclaration::operator->() const {
142   return m_opaque_up.get();
143 }
144 
145 lldb_private::Declaration &SBDeclaration::ref() {
146   if (m_opaque_up == NULL)
147     m_opaque_up.reset(new lldb_private::Declaration());
148   return *m_opaque_up;
149 }
150 
151 const lldb_private::Declaration &SBDeclaration::ref() const {
152   return *m_opaque_up;
153 }
154 
155 bool SBDeclaration::GetDescription(SBStream &description) {
156   LLDB_RECORD_METHOD(bool, SBDeclaration, GetDescription, (lldb::SBStream &),
157                      description);
158 
159   Stream &strm = description.ref();
160 
161   if (m_opaque_up) {
162     char file_path[PATH_MAX * 2];
163     m_opaque_up->GetFile().GetPath(file_path, sizeof(file_path));
164     strm.Printf("%s:%u", file_path, GetLine());
165     if (GetColumn() > 0)
166       strm.Printf(":%u", GetColumn());
167   } else
168     strm.PutCString("No value");
169 
170   return true;
171 }
172 
173 lldb_private::Declaration *SBDeclaration::get() { return m_opaque_up.get(); }
174