1 //===-- SBFileSpecList.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/SBFileSpecList.h"
10 #include "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBFileSpec.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/Core/FileSpecList.h"
15 #include "lldb/Host/PosixApi.h"
16 #include "lldb/Utility/FileSpec.h"
17 #include "lldb/Utility/Log.h"
18 #include "lldb/Utility/Stream.h"
19 
20 #include <limits.h>
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 
25 SBFileSpecList::SBFileSpecList() : m_opaque_up(new FileSpecList()) {
26   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBFileSpecList);
27 }
28 
29 SBFileSpecList::SBFileSpecList(const SBFileSpecList &rhs) : m_opaque_up() {
30   LLDB_RECORD_CONSTRUCTOR(SBFileSpecList, (const lldb::SBFileSpecList &), rhs);
31 
32   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
33 
34   m_opaque_up = clone(rhs.m_opaque_up);
35 
36   if (log) {
37     log->Printf("SBFileSpecList::SBFileSpecList (const SBFileSpecList "
38                 "rhs.ap=%p) => SBFileSpecList(%p)",
39                 static_cast<void *>(rhs.m_opaque_up.get()),
40                 static_cast<void *>(m_opaque_up.get()));
41   }
42 }
43 
44 SBFileSpecList::~SBFileSpecList() {}
45 
46 const SBFileSpecList &SBFileSpecList::operator=(const SBFileSpecList &rhs) {
47   LLDB_RECORD_METHOD(const lldb::SBFileSpecList &,
48                      SBFileSpecList, operator=,(const lldb::SBFileSpecList &),
49                      rhs);
50 
51   if (this != &rhs)
52     m_opaque_up = clone(rhs.m_opaque_up);
53   return *this;
54 }
55 
56 uint32_t SBFileSpecList::GetSize() const {
57   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBFileSpecList, GetSize);
58 
59   return m_opaque_up->GetSize();
60 }
61 
62 void SBFileSpecList::Append(const SBFileSpec &sb_file) {
63   LLDB_RECORD_METHOD(void, SBFileSpecList, Append, (const lldb::SBFileSpec &),
64                      sb_file);
65 
66   m_opaque_up->Append(sb_file.ref());
67 }
68 
69 bool SBFileSpecList::AppendIfUnique(const SBFileSpec &sb_file) {
70   LLDB_RECORD_METHOD(bool, SBFileSpecList, AppendIfUnique,
71                      (const lldb::SBFileSpec &), sb_file);
72 
73   return m_opaque_up->AppendIfUnique(sb_file.ref());
74 }
75 
76 void SBFileSpecList::Clear() {
77   LLDB_RECORD_METHOD_NO_ARGS(void, SBFileSpecList, Clear);
78 
79   m_opaque_up->Clear();
80 }
81 
82 uint32_t SBFileSpecList::FindFileIndex(uint32_t idx, const SBFileSpec &sb_file,
83                                        bool full) {
84   LLDB_RECORD_METHOD(uint32_t, SBFileSpecList, FindFileIndex,
85                      (uint32_t, const lldb::SBFileSpec &, bool), idx, sb_file,
86                      full);
87 
88   return m_opaque_up->FindFileIndex(idx, sb_file.ref(), full);
89 }
90 
91 const SBFileSpec SBFileSpecList::GetFileSpecAtIndex(uint32_t idx) const {
92   LLDB_RECORD_METHOD_CONST(const lldb::SBFileSpec, SBFileSpecList,
93                            GetFileSpecAtIndex, (uint32_t), idx);
94 
95   SBFileSpec new_spec;
96   new_spec.SetFileSpec(m_opaque_up->GetFileSpecAtIndex(idx));
97   return LLDB_RECORD_RESULT(new_spec);
98 }
99 
100 const lldb_private::FileSpecList *SBFileSpecList::operator->() const {
101   return m_opaque_up.get();
102 }
103 
104 const lldb_private::FileSpecList *SBFileSpecList::get() const {
105   return m_opaque_up.get();
106 }
107 
108 const lldb_private::FileSpecList &SBFileSpecList::operator*() const {
109   return *m_opaque_up;
110 }
111 
112 const lldb_private::FileSpecList &SBFileSpecList::ref() const {
113   return *m_opaque_up;
114 }
115 
116 bool SBFileSpecList::GetDescription(SBStream &description) const {
117   LLDB_RECORD_METHOD_CONST(bool, SBFileSpecList, GetDescription,
118                            (lldb::SBStream &), description);
119 
120   Stream &strm = description.ref();
121 
122   if (m_opaque_up) {
123     uint32_t num_files = m_opaque_up->GetSize();
124     strm.Printf("%d files: ", num_files);
125     for (uint32_t i = 0; i < num_files; i++) {
126       char path[PATH_MAX];
127       if (m_opaque_up->GetFileSpecAtIndex(i).GetPath(path, sizeof(path)))
128         strm.Printf("\n    %s", path);
129     }
130   } else
131     strm.PutCString("No value");
132 
133   return true;
134 }
135