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 "gtest/gtest.h"
15 using namespace clang
;
19 using FileMap
= StringMap
<llvm::ErrorOr
<FileEntryRef::MapValue
>>;
20 using DirMap
= StringMap
<llvm::ErrorOr
<DirectoryEntry
&>>;
26 SmallVector
<std::unique_ptr
<FileEntry
>, 5> FEs
;
27 SmallVector
<std::unique_ptr
<DirectoryEntry
>, 5> DEs
;
30 RefMaps() : DR(addDirectory("dir")) {}
32 DirectoryEntryRef
addDirectory(StringRef Name
) {
33 DEs
.push_back(std::make_unique
<DirectoryEntry
>());
34 return DirectoryEntryRef(*Dirs
.insert({Name
, *DEs
.back()}).first
);
36 DirectoryEntryRef
addDirectoryAlias(StringRef Name
, DirectoryEntryRef Base
) {
37 return DirectoryEntryRef(
38 *Dirs
.insert({Name
, const_cast<DirectoryEntry
&>(Base
.getDirEntry())})
42 FileEntryRef
addFile(StringRef Name
) {
43 FEs
.push_back(std::make_unique
<FileEntry
>());
45 *Files
.insert({Name
, FileEntryRef::MapValue(*FEs
.back().get(), DR
)})
48 FileEntryRef
addFileAlias(StringRef Name
, FileEntryRef Base
) {
52 {Name
, FileEntryRef::MapValue(
53 const_cast<FileEntry
&>(Base
.getFileEntry()), DR
)})
58 TEST(FileEntryTest
, Constructor
) {
60 EXPECT_EQ(0, FE
.getSize());
61 EXPECT_EQ(0, FE
.getModificationTime());
62 EXPECT_EQ(nullptr, FE
.getDir());
63 EXPECT_EQ(0U, FE
.getUniqueID().getDevice());
64 EXPECT_EQ(0U, FE
.getUniqueID().getFile());
65 EXPECT_EQ(false, FE
.isNamedPipe());
66 EXPECT_EQ(false, FE
.isValid());
69 TEST(FileEntryTest
, FileEntryRef
) {
71 FileEntryRef R1
= Refs
.addFile("1");
72 FileEntryRef R2
= Refs
.addFile("2");
73 FileEntryRef R1Also
= Refs
.addFileAlias("1-also", R1
);
75 EXPECT_EQ("1", R1
.getName());
76 EXPECT_EQ("2", R2
.getName());
77 EXPECT_EQ("1-also", R1Also
.getName());
79 EXPECT_NE(&R1
.getFileEntry(), &R2
.getFileEntry());
80 EXPECT_EQ(&R1
.getFileEntry(), &R1Also
.getFileEntry());
82 const FileEntry
*CE1
= R1
;
83 EXPECT_EQ(CE1
, &R1
.getFileEntry());
86 TEST(FileEntryTest
, OptionalFileEntryRefDegradesToFileEntryPtr
) {
88 OptionalFileEntryRefDegradesToFileEntryPtr M0
;
89 OptionalFileEntryRefDegradesToFileEntryPtr M1
= Refs
.addFile("1");
90 OptionalFileEntryRefDegradesToFileEntryPtr M2
= Refs
.addFile("2");
91 OptionalFileEntryRefDegradesToFileEntryPtr M0Also
= None
;
92 OptionalFileEntryRefDegradesToFileEntryPtr M1Also
=
93 Refs
.addFileAlias("1-also", *M1
);
95 EXPECT_EQ(M0
, M0Also
);
96 EXPECT_EQ(StringRef("1"), M1
->getName());
97 EXPECT_EQ(StringRef("2"), M2
->getName());
98 EXPECT_EQ(StringRef("1-also"), M1Also
->getName());
100 const FileEntry
*CE1
= M1
;
101 EXPECT_EQ(CE1
, &M1
->getFileEntry());
104 TEST(FileEntryTest
, equals
) {
106 FileEntryRef R1
= Refs
.addFile("1");
107 FileEntryRef R2
= Refs
.addFile("2");
108 FileEntryRef R1Also
= Refs
.addFileAlias("1-also", R1
);
110 EXPECT_EQ(R1
, &R1
.getFileEntry());
111 EXPECT_EQ(&R1
.getFileEntry(), R1
);
112 EXPECT_EQ(R1
, R1Also
);
113 EXPECT_NE(R1
, &R2
.getFileEntry());
114 EXPECT_NE(&R2
.getFileEntry(), R1
);
117 OptionalFileEntryRefDegradesToFileEntryPtr M1
= R1
;
119 EXPECT_EQ(M1
, &R1
.getFileEntry());
120 EXPECT_EQ(&R1
.getFileEntry(), M1
);
121 EXPECT_NE(M1
, &R2
.getFileEntry());
122 EXPECT_NE(&R2
.getFileEntry(), M1
);
125 TEST(FileEntryTest
, isSameRef
) {
127 FileEntryRef R1
= Refs
.addFile("1");
128 FileEntryRef R2
= Refs
.addFile("2");
129 FileEntryRef R1Also
= Refs
.addFileAlias("1-also", R1
);
131 EXPECT_TRUE(R1
.isSameRef(FileEntryRef(R1
)));
132 EXPECT_TRUE(R1
.isSameRef(FileEntryRef(R1
.getMapEntry())));
133 EXPECT_FALSE(R1
.isSameRef(R2
));
134 EXPECT_FALSE(R1
.isSameRef(R1Also
));
137 TEST(FileEntryTest
, DenseMapInfo
) {
139 FileEntryRef R1
= Refs
.addFile("1");
140 FileEntryRef R2
= Refs
.addFile("2");
141 FileEntryRef R1Also
= Refs
.addFileAlias("1-also", R1
);
143 // Insert R1Also first and confirm it "wins".
145 SmallDenseSet
<FileEntryRef
, 8> Set
;
149 EXPECT_TRUE(Set
.find(R1Also
)->isSameRef(R1Also
));
150 EXPECT_TRUE(Set
.find(R1
)->isSameRef(R1Also
));
151 EXPECT_TRUE(Set
.find(R2
)->isSameRef(R2
));
154 // Insert R1Also second and confirm R1 "wins".
156 SmallDenseSet
<FileEntryRef
, 8> Set
;
160 EXPECT_TRUE(Set
.find(R1Also
)->isSameRef(R1
));
161 EXPECT_TRUE(Set
.find(R1
)->isSameRef(R1
));
162 EXPECT_TRUE(Set
.find(R2
)->isSameRef(R2
));
166 TEST(DirectoryEntryTest
, isSameRef
) {
168 DirectoryEntryRef R1
= Refs
.addDirectory("1");
169 DirectoryEntryRef R2
= Refs
.addDirectory("2");
170 DirectoryEntryRef R1Also
= Refs
.addDirectoryAlias("1-also", R1
);
172 EXPECT_TRUE(R1
.isSameRef(DirectoryEntryRef(R1
)));
173 EXPECT_TRUE(R1
.isSameRef(DirectoryEntryRef(R1
.getMapEntry())));
174 EXPECT_FALSE(R1
.isSameRef(R2
));
175 EXPECT_FALSE(R1
.isSameRef(R1Also
));
178 TEST(DirectoryEntryTest
, DenseMapInfo
) {
180 DirectoryEntryRef R1
= Refs
.addDirectory("1");
181 DirectoryEntryRef R2
= Refs
.addDirectory("2");
182 DirectoryEntryRef R1Also
= Refs
.addDirectoryAlias("1-also", R1
);
184 // Insert R1Also first and confirm it "wins".
186 SmallDenseSet
<DirectoryEntryRef
, 8> Set
;
190 EXPECT_TRUE(Set
.find(R1Also
)->isSameRef(R1Also
));
191 EXPECT_TRUE(Set
.find(R1
)->isSameRef(R1Also
));
192 EXPECT_TRUE(Set
.find(R2
)->isSameRef(R2
));
195 // Insert R1Also second and confirm R1 "wins".
197 SmallDenseSet
<DirectoryEntryRef
, 8> Set
;
201 EXPECT_TRUE(Set
.find(R1Also
)->isSameRef(R1
));
202 EXPECT_TRUE(Set
.find(R1
)->isSameRef(R1
));
203 EXPECT_TRUE(Set
.find(R2
)->isSameRef(R2
));