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 "gtest/gtest.h"
13
14 using namespace llvm;
15 using namespace clang;
16
17 namespace {
18
19 using FileMap = StringMap<llvm::ErrorOr<FileEntryRef::MapValue>>;
20 using DirMap = StringMap<llvm::ErrorOr<DirectoryEntry &>>;
21
22 struct RefMaps {
23 FileMap Files;
24 DirMap Dirs;
25
26 SmallVector<std::unique_ptr<FileEntry>, 5> FEs;
27 SmallVector<std::unique_ptr<DirectoryEntry>, 5> DEs;
28 DirectoryEntryRef DR;
29
RefMaps__anon7bcd8f300111::RefMaps30 RefMaps() : DR(addDirectory("dir")) {}
31
addDirectory__anon7bcd8f300111::RefMaps32 DirectoryEntryRef addDirectory(StringRef Name) {
33 DEs.push_back(std::make_unique<DirectoryEntry>());
34 return DirectoryEntryRef(*Dirs.insert({Name, *DEs.back()}).first);
35 }
addDirectoryAlias__anon7bcd8f300111::RefMaps36 DirectoryEntryRef addDirectoryAlias(StringRef Name, DirectoryEntryRef Base) {
37 return DirectoryEntryRef(
38 *Dirs.insert({Name, const_cast<DirectoryEntry &>(Base.getDirEntry())})
39 .first);
40 }
41
addFile__anon7bcd8f300111::RefMaps42 FileEntryRef addFile(StringRef Name) {
43 FEs.push_back(std::make_unique<FileEntry>());
44 return FileEntryRef(
45 *Files.insert({Name, FileEntryRef::MapValue(*FEs.back().get(), DR)})
46 .first);
47 }
addFileAlias__anon7bcd8f300111::RefMaps48 FileEntryRef addFileAlias(StringRef Name, FileEntryRef Base) {
49 return FileEntryRef(
50 *Files
51 .insert(
52 {Name, FileEntryRef::MapValue(
53 const_cast<FileEntry &>(Base.getFileEntry()), DR)})
54 .first);
55 }
56 };
57
TEST(FileEntryTest,FileEntryRef)58 TEST(FileEntryTest, FileEntryRef) {
59 RefMaps Refs;
60 FileEntryRef R1 = Refs.addFile("1");
61 FileEntryRef R2 = Refs.addFile("2");
62 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
63
64 EXPECT_EQ("1", R1.getName());
65 EXPECT_EQ("2", R2.getName());
66 EXPECT_EQ("1-also", R1Also.getName());
67
68 EXPECT_NE(&R1.getFileEntry(), &R2.getFileEntry());
69 EXPECT_EQ(&R1.getFileEntry(), &R1Also.getFileEntry());
70
71 const FileEntry *CE1 = R1;
72 EXPECT_EQ(CE1, &R1.getFileEntry());
73 }
74
TEST(FileEntryTest,OptionalFileEntryRefDegradesToFileEntryPtr)75 TEST(FileEntryTest, OptionalFileEntryRefDegradesToFileEntryPtr) {
76 RefMaps Refs;
77 OptionalFileEntryRefDegradesToFileEntryPtr M0;
78 OptionalFileEntryRefDegradesToFileEntryPtr M1 = Refs.addFile("1");
79 OptionalFileEntryRefDegradesToFileEntryPtr M2 = Refs.addFile("2");
80 OptionalFileEntryRefDegradesToFileEntryPtr M0Also = None;
81 OptionalFileEntryRefDegradesToFileEntryPtr M1Also =
82 Refs.addFileAlias("1-also", *M1);
83
84 EXPECT_EQ(M0, M0Also);
85 EXPECT_EQ(StringRef("1"), M1->getName());
86 EXPECT_EQ(StringRef("2"), M2->getName());
87 EXPECT_EQ(StringRef("1-also"), M1Also->getName());
88
89 const FileEntry *CE1 = M1;
90 EXPECT_EQ(CE1, &M1->getFileEntry());
91 }
92
TEST(FileEntryTest,equals)93 TEST(FileEntryTest, equals) {
94 RefMaps Refs;
95 FileEntryRef R1 = Refs.addFile("1");
96 FileEntryRef R2 = Refs.addFile("2");
97 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
98
99 EXPECT_EQ(R1, &R1.getFileEntry());
100 EXPECT_EQ(&R1.getFileEntry(), R1);
101 EXPECT_EQ(R1, R1Also);
102 EXPECT_NE(R1, &R2.getFileEntry());
103 EXPECT_NE(&R2.getFileEntry(), R1);
104 EXPECT_NE(R1, R2);
105
106 OptionalFileEntryRefDegradesToFileEntryPtr M1 = R1;
107
108 EXPECT_EQ(M1, &R1.getFileEntry());
109 EXPECT_EQ(&R1.getFileEntry(), M1);
110 EXPECT_NE(M1, &R2.getFileEntry());
111 EXPECT_NE(&R2.getFileEntry(), M1);
112 }
113
TEST(FileEntryTest,isSameRef)114 TEST(FileEntryTest, isSameRef) {
115 RefMaps Refs;
116 FileEntryRef R1 = Refs.addFile("1");
117 FileEntryRef R2 = Refs.addFile("2");
118 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
119
120 EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1)));
121 EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1.getMapEntry())));
122 EXPECT_FALSE(R1.isSameRef(R2));
123 EXPECT_FALSE(R1.isSameRef(R1Also));
124 }
125
TEST(FileEntryTest,DenseMapInfo)126 TEST(FileEntryTest, DenseMapInfo) {
127 RefMaps Refs;
128 FileEntryRef R1 = Refs.addFile("1");
129 FileEntryRef R2 = Refs.addFile("2");
130 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
131
132 // Insert R1Also first and confirm it "wins".
133 {
134 SmallDenseSet<FileEntryRef, 8> Set;
135 Set.insert(R1Also);
136 Set.insert(R1);
137 Set.insert(R2);
138 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1Also));
139 EXPECT_TRUE(Set.find(R1)->isSameRef(R1Also));
140 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
141 }
142
143 // Insert R1Also second and confirm R1 "wins".
144 {
145 SmallDenseSet<FileEntryRef, 8> Set;
146 Set.insert(R1);
147 Set.insert(R1Also);
148 Set.insert(R2);
149 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1));
150 EXPECT_TRUE(Set.find(R1)->isSameRef(R1));
151 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
152 }
153 }
154
TEST(DirectoryEntryTest,isSameRef)155 TEST(DirectoryEntryTest, isSameRef) {
156 RefMaps Refs;
157 DirectoryEntryRef R1 = Refs.addDirectory("1");
158 DirectoryEntryRef R2 = Refs.addDirectory("2");
159 DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", R1);
160
161 EXPECT_TRUE(R1.isSameRef(DirectoryEntryRef(R1)));
162 EXPECT_TRUE(R1.isSameRef(DirectoryEntryRef(R1.getMapEntry())));
163 EXPECT_FALSE(R1.isSameRef(R2));
164 EXPECT_FALSE(R1.isSameRef(R1Also));
165 }
166
TEST(DirectoryEntryTest,DenseMapInfo)167 TEST(DirectoryEntryTest, DenseMapInfo) {
168 RefMaps Refs;
169 DirectoryEntryRef R1 = Refs.addDirectory("1");
170 DirectoryEntryRef R2 = Refs.addDirectory("2");
171 DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", R1);
172
173 // Insert R1Also first and confirm it "wins".
174 {
175 SmallDenseSet<DirectoryEntryRef, 8> Set;
176 Set.insert(R1Also);
177 Set.insert(R1);
178 Set.insert(R2);
179 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1Also));
180 EXPECT_TRUE(Set.find(R1)->isSameRef(R1Also));
181 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
182 }
183
184 // Insert R1Also second and confirm R1 "wins".
185 {
186 SmallDenseSet<DirectoryEntryRef, 8> Set;
187 Set.insert(R1);
188 Set.insert(R1Also);
189 Set.insert(R2);
190 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1));
191 EXPECT_TRUE(Set.find(R1)->isSameRef(R1));
192 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
193 }
194 }
195
196 } // end namespace
197