1696bd635SAlexander Shaposhnikov //===-- SBFileSpecList.cpp --------------------------------------*- C++ -*-===//
2969795f1SJim Ingham //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6969795f1SJim Ingham //
7969795f1SJim Ingham //===----------------------------------------------------------------------===//
8969795f1SJim Ingham 
9*baf5664fSJonas Devlieghere #include "lldb/API/SBFileSpecList.h"
10*baf5664fSJonas Devlieghere #include "SBReproducerPrivate.h"
11bd4bf82aSJonas Devlieghere #include "Utils.h"
12969795f1SJim Ingham #include "lldb/API/SBFileSpec.h"
13969795f1SJim Ingham #include "lldb/API/SBStream.h"
14969795f1SJim Ingham #include "lldb/Core/FileSpecList.h"
158d48cd60SZachary Turner #include "lldb/Host/PosixApi.h"
165713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
176f9e6901SZachary Turner #include "lldb/Utility/Log.h"
18bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
19969795f1SJim Ingham 
20*baf5664fSJonas Devlieghere #include <limits.h>
21*baf5664fSJonas Devlieghere 
22969795f1SJim Ingham using namespace lldb;
23969795f1SJim Ingham using namespace lldb_private;
24969795f1SJim Ingham 
25*baf5664fSJonas Devlieghere SBFileSpecList::SBFileSpecList() : m_opaque_up(new FileSpecList()) {
26*baf5664fSJonas Devlieghere   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBFileSpecList);
27*baf5664fSJonas Devlieghere }
28969795f1SJim Ingham 
29d5b44036SJonas Devlieghere SBFileSpecList::SBFileSpecList(const SBFileSpecList &rhs) : m_opaque_up() {
30*baf5664fSJonas Devlieghere   LLDB_RECORD_CONSTRUCTOR(SBFileSpecList, (const lldb::SBFileSpecList &), rhs);
31*baf5664fSJonas Devlieghere 
325160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
33969795f1SJim Ingham 
34bd4bf82aSJonas Devlieghere   m_opaque_up = clone(rhs.m_opaque_up);
35969795f1SJim Ingham 
36b9c1b51eSKate Stone   if (log) {
37b9c1b51eSKate Stone     log->Printf("SBFileSpecList::SBFileSpecList (const SBFileSpecList "
38b9c1b51eSKate Stone                 "rhs.ap=%p) => SBFileSpecList(%p)",
39d5b44036SJonas Devlieghere                 static_cast<void *>(rhs.m_opaque_up.get()),
40d5b44036SJonas Devlieghere                 static_cast<void *>(m_opaque_up.get()));
41969795f1SJim Ingham   }
42969795f1SJim Ingham }
43969795f1SJim Ingham 
44b9c1b51eSKate Stone SBFileSpecList::~SBFileSpecList() {}
45969795f1SJim Ingham 
46b9c1b51eSKate Stone const SBFileSpecList &SBFileSpecList::operator=(const SBFileSpecList &rhs) {
47*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(const lldb::SBFileSpecList &,
48*baf5664fSJonas Devlieghere                      SBFileSpecList, operator=,(const lldb::SBFileSpecList &),
49*baf5664fSJonas Devlieghere                      rhs);
50*baf5664fSJonas Devlieghere 
51bd4bf82aSJonas Devlieghere   if (this != &rhs)
52bd4bf82aSJonas Devlieghere     m_opaque_up = clone(rhs.m_opaque_up);
53969795f1SJim Ingham   return *this;
54969795f1SJim Ingham }
55969795f1SJim Ingham 
56*baf5664fSJonas Devlieghere uint32_t SBFileSpecList::GetSize() const {
57*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBFileSpecList, GetSize);
58*baf5664fSJonas Devlieghere 
59*baf5664fSJonas Devlieghere   return m_opaque_up->GetSize();
60*baf5664fSJonas Devlieghere }
61969795f1SJim Ingham 
62b9c1b51eSKate Stone void SBFileSpecList::Append(const SBFileSpec &sb_file) {
63*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(void, SBFileSpecList, Append, (const lldb::SBFileSpec &),
64*baf5664fSJonas Devlieghere                      sb_file);
65*baf5664fSJonas Devlieghere 
66d5b44036SJonas Devlieghere   m_opaque_up->Append(sb_file.ref());
67969795f1SJim Ingham }
68969795f1SJim Ingham 
69b9c1b51eSKate Stone bool SBFileSpecList::AppendIfUnique(const SBFileSpec &sb_file) {
70*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(bool, SBFileSpecList, AppendIfUnique,
71*baf5664fSJonas Devlieghere                      (const lldb::SBFileSpec &), sb_file);
72*baf5664fSJonas Devlieghere 
73d5b44036SJonas Devlieghere   return m_opaque_up->AppendIfUnique(sb_file.ref());
74969795f1SJim Ingham }
75969795f1SJim Ingham 
76*baf5664fSJonas Devlieghere void SBFileSpecList::Clear() {
77*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_NO_ARGS(void, SBFileSpecList, Clear);
78*baf5664fSJonas Devlieghere 
79*baf5664fSJonas Devlieghere   m_opaque_up->Clear();
80*baf5664fSJonas Devlieghere }
81969795f1SJim Ingham 
82b9c1b51eSKate Stone uint32_t SBFileSpecList::FindFileIndex(uint32_t idx, const SBFileSpec &sb_file,
83b9c1b51eSKate Stone                                        bool full) {
84*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(uint32_t, SBFileSpecList, FindFileIndex,
85*baf5664fSJonas Devlieghere                      (uint32_t, const lldb::SBFileSpec &, bool), idx, sb_file,
86*baf5664fSJonas Devlieghere                      full);
87*baf5664fSJonas Devlieghere 
88d5b44036SJonas Devlieghere   return m_opaque_up->FindFileIndex(idx, sb_file.ref(), full);
89969795f1SJim Ingham }
90969795f1SJim Ingham 
91b9c1b51eSKate Stone const SBFileSpec SBFileSpecList::GetFileSpecAtIndex(uint32_t idx) const {
92*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST(const lldb::SBFileSpec, SBFileSpecList,
93*baf5664fSJonas Devlieghere                            GetFileSpecAtIndex, (uint32_t), idx);
94*baf5664fSJonas Devlieghere 
95969795f1SJim Ingham   SBFileSpec new_spec;
96d5b44036SJonas Devlieghere   new_spec.SetFileSpec(m_opaque_up->GetFileSpecAtIndex(idx));
97*baf5664fSJonas Devlieghere   return LLDB_RECORD_RESULT(new_spec);
98969795f1SJim Ingham }
99969795f1SJim Ingham 
100b9c1b51eSKate Stone const lldb_private::FileSpecList *SBFileSpecList::operator->() const {
101d5b44036SJonas Devlieghere   return m_opaque_up.get();
102969795f1SJim Ingham }
103969795f1SJim Ingham 
104b9c1b51eSKate Stone const lldb_private::FileSpecList *SBFileSpecList::get() const {
105d5b44036SJonas Devlieghere   return m_opaque_up.get();
106969795f1SJim Ingham }
107969795f1SJim Ingham 
108b9c1b51eSKate Stone const lldb_private::FileSpecList &SBFileSpecList::operator*() const {
109d5b44036SJonas Devlieghere   return *m_opaque_up;
110969795f1SJim Ingham }
111969795f1SJim Ingham 
112b9c1b51eSKate Stone const lldb_private::FileSpecList &SBFileSpecList::ref() const {
113d5b44036SJonas Devlieghere   return *m_opaque_up;
114969795f1SJim Ingham }
115969795f1SJim Ingham 
116b9c1b51eSKate Stone bool SBFileSpecList::GetDescription(SBStream &description) const {
117*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST(bool, SBFileSpecList, GetDescription,
118*baf5664fSJonas Devlieghere                            (lldb::SBStream &), description);
119*baf5664fSJonas Devlieghere 
120da7bc7d0SGreg Clayton   Stream &strm = description.ref();
121da7bc7d0SGreg Clayton 
122d5b44036SJonas Devlieghere   if (m_opaque_up) {
123d5b44036SJonas Devlieghere     uint32_t num_files = m_opaque_up->GetSize();
124da7bc7d0SGreg Clayton     strm.Printf("%d files: ", num_files);
125b9c1b51eSKate Stone     for (uint32_t i = 0; i < num_files; i++) {
126969795f1SJim Ingham       char path[PATH_MAX];
127d5b44036SJonas Devlieghere       if (m_opaque_up->GetFileSpecAtIndex(i).GetPath(path, sizeof(path)))
128da7bc7d0SGreg Clayton         strm.Printf("\n    %s", path);
129969795f1SJim Ingham     }
130b9c1b51eSKate Stone   } else
131da7bc7d0SGreg Clayton     strm.PutCString("No value");
132969795f1SJim Ingham 
133969795f1SJim Ingham   return true;
134969795f1SJim Ingham }
135