1 //===- unittests/Basic/FileEntryTest.cpp - Test FileEntry/FileEntryRef ----===//
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
7 //===----------------------------------------------------------------------===//
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"
19 class FileEntryTestHelper
{
20 StringMap
<llvm::ErrorOr
<FileEntryRef::MapValue
>> Files
;
21 StringMap
<llvm::ErrorOr
<DirectoryEntry
&>> Dirs
;
23 SmallVector
<std::unique_ptr
<FileEntry
>, 5> FEs
;
24 SmallVector
<std::unique_ptr
<DirectoryEntry
>, 5> DEs
;
28 FileEntryTestHelper() : DR(addDirectory("dir")) {}
30 DirectoryEntryRef
addDirectory(StringRef Name
) {
31 DEs
.emplace_back(new DirectoryEntry());
32 return DirectoryEntryRef(*Dirs
.insert({Name
, *DEs
.back()}).first
);
34 DirectoryEntryRef
addDirectoryAlias(StringRef Name
, DirectoryEntryRef Base
) {
35 return DirectoryEntryRef(
36 *Dirs
.insert({Name
, const_cast<DirectoryEntry
&>(Base
.getDirEntry())})
40 FileEntryRef
addFile(StringRef Name
) {
41 FEs
.emplace_back(new FileEntry());
43 *Files
.insert({Name
, FileEntryRef::MapValue(*FEs
.back().get(), DR
)})
46 FileEntryRef
addFileAlias(StringRef Name
, FileEntryRef Base
) {
50 {Name
, FileEntryRef::MapValue(
51 const_cast<FileEntry
&>(Base
.getFileEntry()), DR
)})
54 FileEntryRef
addFileRedirect(StringRef Name
, FileEntryRef Base
) {
55 auto Dir
= addDirectory(llvm::sys::path::parent_path(Name
));
59 .insert({Name
, FileEntryRef::MapValue(
60 const_cast<FileEntryRef::MapEntry
&>(
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
);
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());
82 EXPECT_EQ("1", R1
.getNameAsRequested());
83 EXPECT_EQ("1-redirect", R1Redirect
.getNameAsRequested());
84 EXPECT_EQ("1-redirect2", R1Redirect2
.getNameAsRequested());
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());
91 const FileEntry
*CE1
= R1
;
92 EXPECT_EQ(CE1
, &R1
.getFileEntry());
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
);
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
);
109 EXPECT_EQ(R1
, R1Redirect
);
110 EXPECT_EQ(R1
, R1Redirect2
);
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
);
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
));
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
);
136 // Insert R1Also first and confirm it "wins".
138 SmallDenseSet
<FileEntryRef
, 8> Set
;
142 EXPECT_TRUE(Set
.find(R1Also
)->isSameRef(R1Also
));
143 EXPECT_TRUE(Set
.find(R1
)->isSameRef(R1Also
));
144 EXPECT_TRUE(Set
.find(R2
)->isSameRef(R2
));
147 // Insert R1Also second and confirm R1 "wins".
149 SmallDenseSet
<FileEntryRef
, 8> Set
;
153 EXPECT_TRUE(Set
.find(R1Also
)->isSameRef(R1
));
154 EXPECT_TRUE(Set
.find(R1
)->isSameRef(R1
));
155 EXPECT_TRUE(Set
.find(R2
)->isSameRef(R2
));
158 // Insert R1Also first and confirm it "wins" when looked up as FileEntry.
160 SmallDenseSet
<FileEntryRef
, 8> Set
;
165 auto R1AlsoIt
= Set
.find_as(&R1Also
.getFileEntry());
166 ASSERT_TRUE(R1AlsoIt
!= Set
.end());
167 EXPECT_TRUE(R1AlsoIt
->isSameRef(R1Also
));
169 auto R1It
= Set
.find_as(&R1
.getFileEntry());
170 ASSERT_TRUE(R1It
!= Set
.end());
171 EXPECT_TRUE(R1It
->isSameRef(R1Also
));
173 auto R2It
= Set
.find_as(&R2
.getFileEntry());
174 ASSERT_TRUE(R2It
!= Set
.end());
175 EXPECT_TRUE(R2It
->isSameRef(R2
));
178 // Insert R1Also second and confirm R1 "wins" when looked up as FileEntry.
180 SmallDenseSet
<FileEntryRef
, 8> Set
;
185 auto R1AlsoIt
= Set
.find_as(&R1Also
.getFileEntry());
186 ASSERT_TRUE(R1AlsoIt
!= Set
.end());
187 EXPECT_TRUE(R1AlsoIt
->isSameRef(R1
));
189 auto R1It
= Set
.find_as(&R1
.getFileEntry());
190 ASSERT_TRUE(R1It
!= Set
.end());
191 EXPECT_TRUE(R1It
->isSameRef(R1
));
193 auto R2It
= Set
.find_as(&R2
.getFileEntry());
194 ASSERT_TRUE(R2It
!= Set
.end());
195 EXPECT_TRUE(R2It
->isSameRef(R2
));
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
);
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
));
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
);
217 // Insert R1Also first and confirm it "wins".
219 SmallDenseSet
<DirectoryEntryRef
, 8> Set
;
223 EXPECT_TRUE(Set
.find(R1Also
)->isSameRef(R1Also
));
224 EXPECT_TRUE(Set
.find(R1
)->isSameRef(R1Also
));
225 EXPECT_TRUE(Set
.find(R2
)->isSameRef(R2
));
228 // Insert R1Also second and confirm R1 "wins".
230 SmallDenseSet
<DirectoryEntryRef
, 8> Set
;
234 EXPECT_TRUE(Set
.find(R1Also
)->isSameRef(R1
));
235 EXPECT_TRUE(Set
.find(R1
)->isSameRef(R1
));
236 EXPECT_TRUE(Set
.find(R2
)->isSameRef(R2
));