1 //===- unittests/Basic/FileEntryTest.cpp - Test FileEntry/FileEntryRef ----===//
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 "clang/Basic/FileEntry.h"
10 #include "llvm/ADT/DenseSet.h"
11 #include "llvm/ADT/StringMap.h"
12 #include "llvm/Support/Path.h"
13 #include "gtest/gtest.h"
14
15 using namespace llvm;
16
17 namespace clang {
18
19 class FileEntryTestHelper {
20 StringMap<llvm::ErrorOr<FileEntryRef::MapValue>> Files;
21 StringMap<llvm::ErrorOr<DirectoryEntry &>> Dirs;
22
23 SmallVector<std::unique_ptr<FileEntry>, 5> FEs;
24 SmallVector<std::unique_ptr<DirectoryEntry>, 5> DEs;
25 DirectoryEntryRef DR;
26
27 public:
FileEntryTestHelper()28 FileEntryTestHelper() : DR(addDirectory("dir")) {}
29
addDirectory(StringRef Name)30 DirectoryEntryRef addDirectory(StringRef Name) {
31 DEs.emplace_back(new DirectoryEntry());
32 return DirectoryEntryRef(*Dirs.insert({Name, *DEs.back()}).first);
33 }
addDirectoryAlias(StringRef Name,DirectoryEntryRef Base)34 DirectoryEntryRef addDirectoryAlias(StringRef Name, DirectoryEntryRef Base) {
35 return DirectoryEntryRef(
36 *Dirs.insert({Name, const_cast<DirectoryEntry &>(Base.getDirEntry())})
37 .first);
38 }
39
addFile(StringRef Name)40 FileEntryRef addFile(StringRef Name) {
41 FEs.emplace_back(new FileEntry());
42 return FileEntryRef(
43 *Files.insert({Name, FileEntryRef::MapValue(*FEs.back().get(), DR)})
44 .first);
45 }
addFileAlias(StringRef Name,FileEntryRef Base)46 FileEntryRef addFileAlias(StringRef Name, FileEntryRef Base) {
47 return FileEntryRef(
48 *Files
49 .insert(
50 {Name, FileEntryRef::MapValue(
51 const_cast<FileEntry &>(Base.getFileEntry()), DR)})
52 .first);
53 }
addFileRedirect(StringRef Name,FileEntryRef Base)54 FileEntryRef addFileRedirect(StringRef Name, FileEntryRef Base) {
55 auto Dir = addDirectory(llvm::sys::path::parent_path(Name));
56
57 return FileEntryRef(
58 *Files
59 .insert({Name, FileEntryRef::MapValue(
60 const_cast<FileEntryRef::MapEntry &>(
61 Base.getMapEntry()),
62 Dir)})
63 .first);
64 }
65 };
66
67 namespace {
TEST(FileEntryTest,FileEntryRef)68 TEST(FileEntryTest, FileEntryRef) {
69 FileEntryTestHelper Refs;
70 FileEntryRef R1 = Refs.addFile("1");
71 FileEntryRef R2 = Refs.addFile("2");
72 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
73 FileEntryRef R1Redirect = Refs.addFileRedirect("1-redirect", R1);
74 FileEntryRef R1Redirect2 = Refs.addFileRedirect("1-redirect2", R1Redirect);
75
76 EXPECT_EQ("1", R1.getName());
77 EXPECT_EQ("2", R2.getName());
78 EXPECT_EQ("1-also", R1Also.getName());
79 EXPECT_EQ("1", R1Redirect.getName());
80 EXPECT_EQ("1", R1Redirect2.getName());
81
82 EXPECT_EQ("1", R1.getNameAsRequested());
83 EXPECT_EQ("1-redirect", R1Redirect.getNameAsRequested());
84 EXPECT_EQ("1-redirect2", R1Redirect2.getNameAsRequested());
85
86 EXPECT_NE(&R1.getFileEntry(), &R2.getFileEntry());
87 EXPECT_EQ(&R1.getFileEntry(), &R1Also.getFileEntry());
88 EXPECT_EQ(&R1.getFileEntry(), &R1Redirect.getFileEntry());
89 EXPECT_EQ(&R1Redirect.getFileEntry(), &R1Redirect2.getFileEntry());
90
91 const FileEntry *CE1 = R1;
92 EXPECT_EQ(CE1, &R1.getFileEntry());
93 }
94
TEST(FileEntryTest,equals)95 TEST(FileEntryTest, equals) {
96 FileEntryTestHelper Refs;
97 FileEntryRef R1 = Refs.addFile("1");
98 FileEntryRef R2 = Refs.addFile("2");
99 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
100 FileEntryRef R1Redirect = Refs.addFileRedirect("1-redirect", R1);
101 FileEntryRef R1Redirect2 = Refs.addFileRedirect("1-redirect2", R1Redirect);
102
103 EXPECT_EQ(R1, &R1.getFileEntry());
104 EXPECT_EQ(&R1.getFileEntry(), R1);
105 EXPECT_EQ(R1, R1Also);
106 EXPECT_NE(R1, &R2.getFileEntry());
107 EXPECT_NE(&R2.getFileEntry(), R1);
108 EXPECT_NE(R1, R2);
109 EXPECT_EQ(R1, R1Redirect);
110 EXPECT_EQ(R1, R1Redirect2);
111 }
112
TEST(FileEntryTest,isSameRef)113 TEST(FileEntryTest, isSameRef) {
114 FileEntryTestHelper Refs;
115 FileEntryRef R1 = Refs.addFile("1");
116 FileEntryRef R2 = Refs.addFile("2");
117 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
118 FileEntryRef R1Redirect = Refs.addFileRedirect("1-redirect", R1);
119 FileEntryRef R1Redirect2 = Refs.addFileRedirect("1-redirect2", R1Redirect);
120
121 EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1)));
122 EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1.getMapEntry())));
123 EXPECT_FALSE(R1.isSameRef(R2));
124 EXPECT_FALSE(R1.isSameRef(R1Also));
125 EXPECT_FALSE(R1.isSameRef(R1Redirect));
126 EXPECT_FALSE(R1.isSameRef(R1Redirect2));
127 EXPECT_FALSE(R1Redirect.isSameRef(R1Redirect2));
128 }
129
TEST(FileEntryTest,DenseMapInfo)130 TEST(FileEntryTest, DenseMapInfo) {
131 FileEntryTestHelper Refs;
132 FileEntryRef R1 = Refs.addFile("1");
133 FileEntryRef R2 = Refs.addFile("2");
134 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
135
136 // Insert R1Also first and confirm it "wins".
137 {
138 SmallDenseSet<FileEntryRef, 8> Set;
139 Set.insert(R1Also);
140 Set.insert(R1);
141 Set.insert(R2);
142 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1Also));
143 EXPECT_TRUE(Set.find(R1)->isSameRef(R1Also));
144 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
145 }
146
147 // Insert R1Also second and confirm R1 "wins".
148 {
149 SmallDenseSet<FileEntryRef, 8> Set;
150 Set.insert(R1);
151 Set.insert(R1Also);
152 Set.insert(R2);
153 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1));
154 EXPECT_TRUE(Set.find(R1)->isSameRef(R1));
155 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
156 }
157
158 // Insert R1Also first and confirm it "wins" when looked up as FileEntry.
159 {
160 SmallDenseSet<FileEntryRef, 8> Set;
161 Set.insert(R1Also);
162 Set.insert(R1);
163 Set.insert(R2);
164
165 auto R1AlsoIt = Set.find_as(&R1Also.getFileEntry());
166 ASSERT_TRUE(R1AlsoIt != Set.end());
167 EXPECT_TRUE(R1AlsoIt->isSameRef(R1Also));
168
169 auto R1It = Set.find_as(&R1.getFileEntry());
170 ASSERT_TRUE(R1It != Set.end());
171 EXPECT_TRUE(R1It->isSameRef(R1Also));
172
173 auto R2It = Set.find_as(&R2.getFileEntry());
174 ASSERT_TRUE(R2It != Set.end());
175 EXPECT_TRUE(R2It->isSameRef(R2));
176 }
177
178 // Insert R1Also second and confirm R1 "wins" when looked up as FileEntry.
179 {
180 SmallDenseSet<FileEntryRef, 8> Set;
181 Set.insert(R1);
182 Set.insert(R1Also);
183 Set.insert(R2);
184
185 auto R1AlsoIt = Set.find_as(&R1Also.getFileEntry());
186 ASSERT_TRUE(R1AlsoIt != Set.end());
187 EXPECT_TRUE(R1AlsoIt->isSameRef(R1));
188
189 auto R1It = Set.find_as(&R1.getFileEntry());
190 ASSERT_TRUE(R1It != Set.end());
191 EXPECT_TRUE(R1It->isSameRef(R1));
192
193 auto R2It = Set.find_as(&R2.getFileEntry());
194 ASSERT_TRUE(R2It != Set.end());
195 EXPECT_TRUE(R2It->isSameRef(R2));
196 }
197 }
198
TEST(DirectoryEntryTest,isSameRef)199 TEST(DirectoryEntryTest, isSameRef) {
200 FileEntryTestHelper Refs;
201 DirectoryEntryRef R1 = Refs.addDirectory("1");
202 DirectoryEntryRef R2 = Refs.addDirectory("2");
203 DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", R1);
204
205 EXPECT_TRUE(R1.isSameRef(DirectoryEntryRef(R1)));
206 EXPECT_TRUE(R1.isSameRef(DirectoryEntryRef(R1.getMapEntry())));
207 EXPECT_FALSE(R1.isSameRef(R2));
208 EXPECT_FALSE(R1.isSameRef(R1Also));
209 }
210
TEST(DirectoryEntryTest,DenseMapInfo)211 TEST(DirectoryEntryTest, DenseMapInfo) {
212 FileEntryTestHelper Refs;
213 DirectoryEntryRef R1 = Refs.addDirectory("1");
214 DirectoryEntryRef R2 = Refs.addDirectory("2");
215 DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", R1);
216
217 // Insert R1Also first and confirm it "wins".
218 {
219 SmallDenseSet<DirectoryEntryRef, 8> Set;
220 Set.insert(R1Also);
221 Set.insert(R1);
222 Set.insert(R2);
223 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1Also));
224 EXPECT_TRUE(Set.find(R1)->isSameRef(R1Also));
225 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
226 }
227
228 // Insert R1Also second and confirm R1 "wins".
229 {
230 SmallDenseSet<DirectoryEntryRef, 8> Set;
231 Set.insert(R1);
232 Set.insert(R1Also);
233 Set.insert(R2);
234 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1));
235 EXPECT_TRUE(Set.find(R1)->isSameRef(R1));
236 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
237 }
238 }
239
240 } // end namespace
241 } // namespace clang
242