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