1//===-- SourceLocationSpecTest.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 "gtest/gtest.h"
10
11#include "lldb/Core/SourceLocationSpec.h"
12#include "lldb/Utility/LLDBAssert.h"
13
14#include "llvm/Testing/Support/Error.h"
15
16using namespace lldb_private;
17
18TEST(SourceLocationSpecTest, OperatorBool) {
19 SourceLocationSpec invalid(FileSpec(), 0);
20 EXPECT_FALSE(invalid);
21
22 SourceLocationSpec invalid_filespec(FileSpec(), 4);
23 EXPECT_FALSE(invalid_filespec);
24
25 SourceLocationSpec invalid_line(FileSpec("/foo/bar"), 0);
26 EXPECT_FALSE(invalid_line);
27
28 SourceLocationSpec valid_fs_line_no_column(FileSpec("/foo/bar"), 4);
29 EXPECT_TRUE(valid_fs_line_no_column);
30
31 SourceLocationSpec invalid_fs_column(FileSpec(), 4, 0);
32 EXPECT_FALSE(invalid_fs_column);
33
34 SourceLocationSpec invalid_line_column(FileSpec("/foo/bar"), 0, 19);
35 EXPECT_FALSE(invalid_line_column);
36
37 SourceLocationSpec valid_fs_line_zero_column(FileSpec("/foo/bar"), 4, 0);
38 EXPECT_TRUE(valid_fs_line_zero_column);
39
40 SourceLocationSpec valid_fs_line_column(FileSpec("/foo/bar"), 4, 19);
41 EXPECT_TRUE(valid_fs_line_column);
42}
43
44TEST(SourceLocationSpecTest, FileLineColumnComponents) {
45 FileSpec fs("/foo/bar", FileSpec::Style::posix);
46 const uint32_t line = 19;
47 const uint16_t column = 4;
48
49 SourceLocationSpec without_column(fs, line, LLDB_INVALID_COLUMN_NUMBER, false,
50 true);
51 EXPECT_TRUE(without_column);
52 EXPECT_EQ(fs, without_column.GetFileSpec());
53 EXPECT_EQ(line, without_column.GetLine().value_or(0));
54 EXPECT_EQ(std::nullopt, without_column.GetColumn());
55 EXPECT_FALSE(without_column.GetCheckInlines());
56 EXPECT_TRUE(without_column.GetExactMatch());
57 EXPECT_STREQ("check inlines = false, exact match = true, decl = /foo/bar:19",
58 without_column.GetString().c_str());
59
60 SourceLocationSpec with_column(fs, line, column, true, false);
61 EXPECT_TRUE(with_column);
62 EXPECT_EQ(column, *with_column.GetColumn());
63 EXPECT_TRUE(with_column.GetCheckInlines());
64 EXPECT_FALSE(with_column.GetExactMatch());
65 EXPECT_STREQ(
66 "check inlines = true, exact match = false, decl = /foo/bar:19:4",
67 with_column.GetString().c_str());
68}
69
70static SourceLocationSpec Create(bool check_inlines, bool exact_match,
71 FileSpec fs, uint32_t line,
72 uint16_t column = LLDB_INVALID_COLUMN_NUMBER) {
73 return SourceLocationSpec(fs, line, column, check_inlines, exact_match);
74}
75
76TEST(SourceLocationSpecTest, Equal) {
77 auto Equal = [](SourceLocationSpec lhs, SourceLocationSpec rhs, bool full) {
78 return SourceLocationSpec::Equal(lhs, rhs, full);
79 };
80
81 const FileSpec fs("/foo/bar", FileSpec::Style::posix);
82 const FileSpec other_fs("/foo/baz", FileSpec::Style::posix);
83
84 // mutating FileSpec + const Inlined, ExactMatch, Line
85 EXPECT_TRUE(
86 Equal(Create(false, false, fs, 4), Create(false, false, fs, 4), true));
87 EXPECT_TRUE(
88 Equal(Create(true, true, fs, 4), Create(true, true, fs, 4), false));
89 EXPECT_FALSE(Equal(Create(false, false, fs, 4),
90 Create(false, false, other_fs, 4), true));
91 EXPECT_FALSE(
92 Equal(Create(true, true, fs, 4), Create(true, true, other_fs, 4), false));
93
94 // Mutating FileSpec + const Inlined, ExactMatch, Line, Column
95 EXPECT_TRUE(Equal(Create(false, false, fs, 4, 19),
96 Create(false, false, fs, 4, 19), true));
97 EXPECT_TRUE(Equal(Create(true, true, fs, 4, 19),
98 Create(true, true, fs, 4, 19), false));
99 EXPECT_FALSE(Equal(Create(false, false, fs, 4, 19),
100 Create(false, false, other_fs, 4, 19), true));
101 EXPECT_FALSE(Equal(Create(true, true, fs, 4, 19),
102 Create(true, true, other_fs, 4, 19), false));
103
104 // Asymetric match
105 EXPECT_FALSE(
106 Equal(Create(true, true, fs, 4), Create(true, true, fs, 4, 19), true));
107 EXPECT_TRUE(Equal(Create(false, false, fs, 4),
108 Create(false, false, fs, 4, 19), false));
109
110 // Mutating Inlined, ExactMatch
111 EXPECT_FALSE(
112 Equal(Create(true, false, fs, 4), Create(false, true, fs, 4), true));
113 EXPECT_TRUE(
114 Equal(Create(false, true, fs, 4), Create(true, false, fs, 4), false));
115
116 // Mutating Column
117 EXPECT_FALSE(Equal(Create(true, true, fs, 4, 96),
118 Create(true, true, fs, 4, 19), true));
119 EXPECT_TRUE(Equal(Create(false, false, fs, 4, 96),
120 Create(false, false, fs, 4, 19), false));
121}
122
123TEST(SourceLocationSpecTest, Compare) {
124 auto Cmp = [](SourceLocationSpec a, SourceLocationSpec b) {
125 return std::clamp(val: SourceLocationSpec::Compare(lhs: a, rhs: b), lo: -1, hi: 1);
126 };
127
128 FileSpec fs("/foo/bar", FileSpec::Style::posix);
129 FileSpec other_fs("/foo/baz", FileSpec::Style::posix);
130
131 // Asymetric comparaison
132 EXPECT_EQ(-1, Cmp(Create(true, true, fs, 4), Create(true, true, fs, 4, 19)));
133 EXPECT_EQ(-1,
134 Cmp(Create(false, false, fs, 4), Create(false, false, fs, 4, 19)));
135 EXPECT_EQ(1, Cmp(Create(true, true, fs, 4, 19), Create(true, true, fs, 4)));
136
137 // Mutating FS, const Line
138 EXPECT_EQ(
139 -1, Cmp(Create(false, false, fs, 4), Create(false, false, other_fs, 4)));
140 EXPECT_EQ(-1,
141 Cmp(Create(true, true, fs, 4), Create(true, true, other_fs, 4)));
142 EXPECT_EQ(1,
143 Cmp(Create(false, true, other_fs, 4), Create(false, true, fs, 4)));
144 EXPECT_EQ(1,
145 Cmp(Create(true, false, other_fs, 4), Create(true, false, fs, 4)));
146
147 // Const FS, mutating Line
148 EXPECT_EQ(-1, Cmp(Create(false, false, fs, 1), Create(false, false, fs, 4)));
149 EXPECT_EQ(-1, Cmp(Create(true, true, fs, 1), Create(true, true, fs, 4)));
150 EXPECT_EQ(0, Cmp(Create(false, true, fs, 4), Create(false, true, fs, 4)));
151 EXPECT_EQ(0, Cmp(Create(true, false, fs, 4), Create(true, false, fs, 4)));
152 EXPECT_EQ(1, Cmp(Create(false, false, fs, 4), Create(false, false, fs, 1)));
153 EXPECT_EQ(1, Cmp(Create(true, true, fs, 4), Create(true, true, fs, 1)));
154
155 // Const FS, mutating Line, const Column
156 EXPECT_EQ(-1,
157 Cmp(Create(false, true, fs, 1), Create(false, true, fs, 4, 19)));
158 EXPECT_EQ(-1, Cmp(Create(true, true, fs, 1), Create(true, true, fs, 4, 19)));
159 EXPECT_EQ(1, Cmp(Create(true, false, fs, 4, 19), Create(true, false, fs, 1)));
160 EXPECT_EQ(1, Cmp(Create(true, false, fs, 4, 19), Create(true, false, fs, 1)));
161
162 // Mutating FS, const Line, const Column
163 EXPECT_EQ(-1, Cmp(Create(false, false, fs, 4, 19),
164 Create(false, false, other_fs, 4, 19)));
165 EXPECT_EQ(-1, Cmp(Create(true, true, fs, 4, 19),
166 Create(true, true, other_fs, 4, 19)));
167 EXPECT_EQ(
168 0, Cmp(Create(false, false, fs, 4, 19), Create(false, false, fs, 4, 19)));
169 EXPECT_EQ(0,
170 Cmp(Create(true, true, fs, 4, 19), Create(true, true, fs, 4, 19)));
171 EXPECT_EQ(1, Cmp(Create(false, true, other_fs, 4, 19),
172 Create(false, true, fs, 4, 19)));
173 EXPECT_EQ(1, Cmp(Create(true, false, other_fs, 4, 19),
174 Create(true, false, fs, 4, 19)));
175
176 // Const FS, const Line, mutating Column
177 EXPECT_EQ(-1, Cmp(Create(false, false, fs, 4, 19),
178 Create(false, false, fs, 4, 96)));
179 EXPECT_EQ(1,
180 Cmp(Create(true, true, fs, 4, 96), Create(true, true, fs, 4, 19)));
181 EXPECT_EQ(
182 1, Cmp(Create(false, true, fs, 4, 96), Create(false, true, fs, 4, 19)));
183}
184

source code of lldb/unittests/Core/SourceLocationSpecTest.cpp