1 //===-- SourceLocationSpec.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/Core/SourceLocationSpec.h"
10 #include "lldb/Utility/StreamString.h"
11 #include <optional>
12 
13 using namespace lldb;
14 using namespace lldb_private;
15 
SourceLocationSpec(FileSpec file_spec,uint32_t line,std::optional<uint16_t> column,bool check_inlines,bool exact_match)16 SourceLocationSpec::SourceLocationSpec(FileSpec file_spec, uint32_t line,
17                                        std::optional<uint16_t> column,
18                                        bool check_inlines, bool exact_match)
19     : m_declaration(file_spec, line,
20                     column.value_or(LLDB_INVALID_COLUMN_NUMBER)),
21       m_check_inlines(check_inlines), m_exact_match(exact_match) {}
22 
operator bool() const23 SourceLocationSpec::operator bool() const { return m_declaration.IsValid(); }
24 
operator !() const25 bool SourceLocationSpec::operator!() const { return !operator bool(); }
26 
operator ==(const SourceLocationSpec & rhs) const27 bool SourceLocationSpec::operator==(const SourceLocationSpec &rhs) const {
28   return m_declaration == rhs.m_declaration &&
29          m_check_inlines == rhs.GetCheckInlines() &&
30          m_exact_match == rhs.GetExactMatch();
31 }
32 
operator !=(const SourceLocationSpec & rhs) const33 bool SourceLocationSpec::operator!=(const SourceLocationSpec &rhs) const {
34   return !(*this == rhs);
35 }
36 
operator <(const SourceLocationSpec & rhs) const37 bool SourceLocationSpec::operator<(const SourceLocationSpec &rhs) const {
38   return SourceLocationSpec::Compare(*this, rhs) < 0;
39 }
40 
operator <<(Stream & s,const SourceLocationSpec & loc)41 Stream &lldb_private::operator<<(Stream &s, const SourceLocationSpec &loc) {
42   loc.Dump(s);
43   return s;
44 }
45 
Compare(const SourceLocationSpec & lhs,const SourceLocationSpec & rhs)46 int SourceLocationSpec::Compare(const SourceLocationSpec &lhs,
47                                 const SourceLocationSpec &rhs) {
48   return Declaration::Compare(lhs.m_declaration, rhs.m_declaration);
49 }
50 
Equal(const SourceLocationSpec & lhs,const SourceLocationSpec & rhs,bool full)51 bool SourceLocationSpec::Equal(const SourceLocationSpec &lhs,
52                                const SourceLocationSpec &rhs, bool full) {
53   return full ? lhs == rhs
54               : (lhs.GetFileSpec() == rhs.GetFileSpec() &&
55                  lhs.GetLine() == rhs.GetLine());
56 }
57 
Dump(Stream & s) const58 void SourceLocationSpec::Dump(Stream &s) const {
59   s << "check inlines = " << llvm::toStringRef(m_check_inlines);
60   s << ", exact match = " << llvm::toStringRef(m_exact_match);
61   m_declaration.Dump(&s, true);
62 }
63 
GetString() const64 std::string SourceLocationSpec::GetString() const {
65   StreamString ss;
66   Dump(ss);
67   return ss.GetString().str();
68 }
69 
GetLine() const70 std::optional<uint32_t> SourceLocationSpec::GetLine() const {
71   uint32_t line = m_declaration.GetLine();
72   if (line == 0 || line == LLDB_INVALID_LINE_NUMBER)
73     return std::nullopt;
74   return line;
75 }
76 
GetColumn() const77 std::optional<uint16_t> SourceLocationSpec::GetColumn() const {
78   uint16_t column = m_declaration.GetColumn();
79   if (column == LLDB_INVALID_COLUMN_NUMBER)
80     return std::nullopt;
81   return column;
82 }
83