xref: /llvm-project/clang/unittests/Basic/FileEntryTest.cpp (revision 0cb0a48cdea730e885e8c955ba1687a8191f824c)
1ac49500cSDuncan P. N. Exon Smith //===- unittests/Basic/FileEntryTest.cpp - Test FileEntry/FileEntryRef ----===//
2ac49500cSDuncan P. N. Exon Smith //
3ac49500cSDuncan P. N. Exon Smith // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ac49500cSDuncan P. N. Exon Smith // See https://llvm.org/LICENSE.txt for license information.
5ac49500cSDuncan P. N. Exon Smith // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ac49500cSDuncan P. N. Exon Smith //
7ac49500cSDuncan P. N. Exon Smith //===----------------------------------------------------------------------===//
8ac49500cSDuncan P. N. Exon Smith 
9ac49500cSDuncan P. N. Exon Smith #include "clang/Basic/FileEntry.h"
102878e965SDuncan P. N. Exon Smith #include "llvm/ADT/DenseSet.h"
11ac49500cSDuncan P. N. Exon Smith #include "llvm/ADT/StringMap.h"
12bdc3ce9eSJan Svoboda #include "llvm/Support/Path.h"
13ac49500cSDuncan P. N. Exon Smith #include "gtest/gtest.h"
14ac49500cSDuncan P. N. Exon Smith 
15ac49500cSDuncan P. N. Exon Smith using namespace llvm;
16ac49500cSDuncan P. N. Exon Smith 
17b2a7f1c3SSam McCall namespace clang {
18ac49500cSDuncan P. N. Exon Smith 
19b2a7f1c3SSam McCall class FileEntryTestHelper {
20b2a7f1c3SSam McCall   StringMap<llvm::ErrorOr<FileEntryRef::MapValue>> Files;
21b2a7f1c3SSam McCall   StringMap<llvm::ErrorOr<DirectoryEntry &>> Dirs;
221b042de5SDuncan P. N. Exon Smith 
231b042de5SDuncan P. N. Exon Smith   SmallVector<std::unique_ptr<FileEntry>, 5> FEs;
242878e965SDuncan P. N. Exon Smith   SmallVector<std::unique_ptr<DirectoryEntry>, 5> DEs;
252878e965SDuncan P. N. Exon Smith   DirectoryEntryRef DR;
261b042de5SDuncan P. N. Exon Smith 
27b2a7f1c3SSam McCall public:
FileEntryTestHelper()28b2a7f1c3SSam McCall   FileEntryTestHelper() : DR(addDirectory("dir")) {}
292878e965SDuncan P. N. Exon Smith 
addDirectory(StringRef Name)302878e965SDuncan P. N. Exon Smith   DirectoryEntryRef addDirectory(StringRef Name) {
31b2a7f1c3SSam McCall     DEs.emplace_back(new DirectoryEntry());
322878e965SDuncan P. N. Exon Smith     return DirectoryEntryRef(*Dirs.insert({Name, *DEs.back()}).first);
332878e965SDuncan P. N. Exon Smith   }
addDirectoryAlias(StringRef Name,DirectoryEntryRef Base)342878e965SDuncan P. N. Exon Smith   DirectoryEntryRef addDirectoryAlias(StringRef Name, DirectoryEntryRef Base) {
352878e965SDuncan P. N. Exon Smith     return DirectoryEntryRef(
362878e965SDuncan P. N. Exon Smith         *Dirs.insert({Name, const_cast<DirectoryEntry &>(Base.getDirEntry())})
372878e965SDuncan P. N. Exon Smith              .first);
382878e965SDuncan P. N. Exon Smith   }
391b042de5SDuncan P. N. Exon Smith 
addFile(StringRef Name)401b042de5SDuncan P. N. Exon Smith   FileEntryRef addFile(StringRef Name) {
41b2a7f1c3SSam McCall     FEs.emplace_back(new FileEntry());
421b042de5SDuncan P. N. Exon Smith     return FileEntryRef(
431b042de5SDuncan P. N. Exon Smith         *Files.insert({Name, FileEntryRef::MapValue(*FEs.back().get(), DR)})
441b042de5SDuncan P. N. Exon Smith              .first);
45ac49500cSDuncan P. N. Exon Smith   }
addFileAlias(StringRef Name,FileEntryRef Base)461b042de5SDuncan P. N. Exon Smith   FileEntryRef addFileAlias(StringRef Name, FileEntryRef Base) {
471b042de5SDuncan P. N. Exon Smith     return FileEntryRef(
481b042de5SDuncan P. N. Exon Smith         *Files
491b042de5SDuncan P. N. Exon Smith              .insert(
501b042de5SDuncan P. N. Exon Smith                  {Name, FileEntryRef::MapValue(
511b042de5SDuncan P. N. Exon Smith                             const_cast<FileEntry &>(Base.getFileEntry()), DR)})
521b042de5SDuncan P. N. Exon Smith              .first);
531b042de5SDuncan P. N. Exon Smith   }
addFileRedirect(StringRef Name,FileEntryRef Base)546a79e2ffSBen Langmuir   FileEntryRef addFileRedirect(StringRef Name, FileEntryRef Base) {
55bdc3ce9eSJan Svoboda     auto Dir = addDirectory(llvm::sys::path::parent_path(Name));
56bdc3ce9eSJan Svoboda 
576a79e2ffSBen Langmuir     return FileEntryRef(
586a79e2ffSBen Langmuir         *Files
596a79e2ffSBen Langmuir              .insert({Name, FileEntryRef::MapValue(
606a79e2ffSBen Langmuir                                 const_cast<FileEntryRef::MapEntry &>(
61bdc3ce9eSJan Svoboda                                     Base.getMapEntry()),
62bdc3ce9eSJan Svoboda                                 Dir)})
636a79e2ffSBen Langmuir              .first);
646a79e2ffSBen Langmuir   }
651b042de5SDuncan P. N. Exon Smith };
66ac49500cSDuncan P. N. Exon Smith 
67b2a7f1c3SSam McCall namespace {
TEST(FileEntryTest,FileEntryRef)68ac49500cSDuncan P. N. Exon Smith TEST(FileEntryTest, FileEntryRef) {
69b2a7f1c3SSam McCall   FileEntryTestHelper Refs;
701b042de5SDuncan P. N. Exon Smith   FileEntryRef R1 = Refs.addFile("1");
711b042de5SDuncan P. N. Exon Smith   FileEntryRef R2 = Refs.addFile("2");
721b042de5SDuncan P. N. Exon Smith   FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
736a79e2ffSBen Langmuir   FileEntryRef R1Redirect = Refs.addFileRedirect("1-redirect", R1);
746a79e2ffSBen Langmuir   FileEntryRef R1Redirect2 = Refs.addFileRedirect("1-redirect2", R1Redirect);
75ac49500cSDuncan P. N. Exon Smith 
76ac49500cSDuncan P. N. Exon Smith   EXPECT_EQ("1", R1.getName());
77ac49500cSDuncan P. N. Exon Smith   EXPECT_EQ("2", R2.getName());
78ac49500cSDuncan P. N. Exon Smith   EXPECT_EQ("1-also", R1Also.getName());
796a79e2ffSBen Langmuir   EXPECT_EQ("1", R1Redirect.getName());
806a79e2ffSBen Langmuir   EXPECT_EQ("1", R1Redirect2.getName());
816a79e2ffSBen Langmuir 
826a79e2ffSBen Langmuir   EXPECT_EQ("1", R1.getNameAsRequested());
836a79e2ffSBen Langmuir   EXPECT_EQ("1-redirect", R1Redirect.getNameAsRequested());
846a79e2ffSBen Langmuir   EXPECT_EQ("1-redirect2", R1Redirect2.getNameAsRequested());
85ac49500cSDuncan P. N. Exon Smith 
861b042de5SDuncan P. N. Exon Smith   EXPECT_NE(&R1.getFileEntry(), &R2.getFileEntry());
871b042de5SDuncan P. N. Exon Smith   EXPECT_EQ(&R1.getFileEntry(), &R1Also.getFileEntry());
886a79e2ffSBen Langmuir   EXPECT_EQ(&R1.getFileEntry(), &R1Redirect.getFileEntry());
896a79e2ffSBen Langmuir   EXPECT_EQ(&R1Redirect.getFileEntry(), &R1Redirect2.getFileEntry());
90ac49500cSDuncan P. N. Exon Smith 
91ac49500cSDuncan P. N. Exon Smith   const FileEntry *CE1 = R1;
921b042de5SDuncan P. N. Exon Smith   EXPECT_EQ(CE1, &R1.getFileEntry());
93ac49500cSDuncan P. N. Exon Smith }
94ac49500cSDuncan P. N. Exon Smith 
TEST(FileEntryTest,equals)95ac49500cSDuncan P. N. Exon Smith TEST(FileEntryTest, equals) {
96b2a7f1c3SSam McCall   FileEntryTestHelper Refs;
971b042de5SDuncan P. N. Exon Smith   FileEntryRef R1 = Refs.addFile("1");
981b042de5SDuncan P. N. Exon Smith   FileEntryRef R2 = Refs.addFile("2");
991b042de5SDuncan P. N. Exon Smith   FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
1006a79e2ffSBen Langmuir   FileEntryRef R1Redirect = Refs.addFileRedirect("1-redirect", R1);
1016a79e2ffSBen Langmuir   FileEntryRef R1Redirect2 = Refs.addFileRedirect("1-redirect2", R1Redirect);
102ac49500cSDuncan P. N. Exon Smith 
1031b042de5SDuncan P. N. Exon Smith   EXPECT_EQ(R1, &R1.getFileEntry());
1041b042de5SDuncan P. N. Exon Smith   EXPECT_EQ(&R1.getFileEntry(), R1);
105ac49500cSDuncan P. N. Exon Smith   EXPECT_EQ(R1, R1Also);
1061b042de5SDuncan P. N. Exon Smith   EXPECT_NE(R1, &R2.getFileEntry());
1071b042de5SDuncan P. N. Exon Smith   EXPECT_NE(&R2.getFileEntry(), R1);
108ac49500cSDuncan P. N. Exon Smith   EXPECT_NE(R1, R2);
1096a79e2ffSBen Langmuir   EXPECT_EQ(R1, R1Redirect);
1106a79e2ffSBen Langmuir   EXPECT_EQ(R1, R1Redirect2);
111ac49500cSDuncan P. N. Exon Smith }
112ac49500cSDuncan P. N. Exon Smith 
TEST(FileEntryTest,isSameRef)113ac49500cSDuncan P. N. Exon Smith TEST(FileEntryTest, isSameRef) {
114b2a7f1c3SSam McCall   FileEntryTestHelper Refs;
1151b042de5SDuncan P. N. Exon Smith   FileEntryRef R1 = Refs.addFile("1");
1161b042de5SDuncan P. N. Exon Smith   FileEntryRef R2 = Refs.addFile("2");
1171b042de5SDuncan P. N. Exon Smith   FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
1186a79e2ffSBen Langmuir   FileEntryRef R1Redirect = Refs.addFileRedirect("1-redirect", R1);
1196a79e2ffSBen Langmuir   FileEntryRef R1Redirect2 = Refs.addFileRedirect("1-redirect2", R1Redirect);
120ac49500cSDuncan P. N. Exon Smith 
121ac49500cSDuncan P. N. Exon Smith   EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1)));
122ac49500cSDuncan P. N. Exon Smith   EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1.getMapEntry())));
123ac49500cSDuncan P. N. Exon Smith   EXPECT_FALSE(R1.isSameRef(R2));
124ac49500cSDuncan P. N. Exon Smith   EXPECT_FALSE(R1.isSameRef(R1Also));
1256a79e2ffSBen Langmuir   EXPECT_FALSE(R1.isSameRef(R1Redirect));
1266a79e2ffSBen Langmuir   EXPECT_FALSE(R1.isSameRef(R1Redirect2));
1276a79e2ffSBen Langmuir   EXPECT_FALSE(R1Redirect.isSameRef(R1Redirect2));
128ac49500cSDuncan P. N. Exon Smith }
129ac49500cSDuncan P. N. Exon Smith 
TEST(FileEntryTest,DenseMapInfo)1302878e965SDuncan P. N. Exon Smith TEST(FileEntryTest, DenseMapInfo) {
131b2a7f1c3SSam McCall   FileEntryTestHelper Refs;
1322878e965SDuncan P. N. Exon Smith   FileEntryRef R1 = Refs.addFile("1");
1332878e965SDuncan P. N. Exon Smith   FileEntryRef R2 = Refs.addFile("2");
1342878e965SDuncan P. N. Exon Smith   FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
1352878e965SDuncan P. N. Exon Smith 
1362878e965SDuncan P. N. Exon Smith   // Insert R1Also first and confirm it "wins".
1372878e965SDuncan P. N. Exon Smith   {
1382878e965SDuncan P. N. Exon Smith     SmallDenseSet<FileEntryRef, 8> Set;
1392878e965SDuncan P. N. Exon Smith     Set.insert(R1Also);
1402878e965SDuncan P. N. Exon Smith     Set.insert(R1);
1412878e965SDuncan P. N. Exon Smith     Set.insert(R2);
1422878e965SDuncan P. N. Exon Smith     EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1Also));
1432878e965SDuncan P. N. Exon Smith     EXPECT_TRUE(Set.find(R1)->isSameRef(R1Also));
1442878e965SDuncan P. N. Exon Smith     EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
1452878e965SDuncan P. N. Exon Smith   }
1462878e965SDuncan P. N. Exon Smith 
1472878e965SDuncan P. N. Exon Smith   // Insert R1Also second and confirm R1 "wins".
1482878e965SDuncan P. N. Exon Smith   {
1492878e965SDuncan P. N. Exon Smith     SmallDenseSet<FileEntryRef, 8> Set;
1502878e965SDuncan P. N. Exon Smith     Set.insert(R1);
1512878e965SDuncan P. N. Exon Smith     Set.insert(R1Also);
1522878e965SDuncan P. N. Exon Smith     Set.insert(R2);
1532878e965SDuncan P. N. Exon Smith     EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1));
1542878e965SDuncan P. N. Exon Smith     EXPECT_TRUE(Set.find(R1)->isSameRef(R1));
1552878e965SDuncan P. N. Exon Smith     EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
1562878e965SDuncan P. N. Exon Smith   }
157*8a2fb139SJan Svoboda 
158*8a2fb139SJan Svoboda   // Insert R1Also first and confirm it "wins" when looked up as FileEntry.
159*8a2fb139SJan Svoboda   {
160*8a2fb139SJan Svoboda     SmallDenseSet<FileEntryRef, 8> Set;
161*8a2fb139SJan Svoboda     Set.insert(R1Also);
162*8a2fb139SJan Svoboda     Set.insert(R1);
163*8a2fb139SJan Svoboda     Set.insert(R2);
164*8a2fb139SJan Svoboda 
165*8a2fb139SJan Svoboda     auto R1AlsoIt = Set.find_as(&R1Also.getFileEntry());
166*8a2fb139SJan Svoboda     ASSERT_TRUE(R1AlsoIt != Set.end());
167*8a2fb139SJan Svoboda     EXPECT_TRUE(R1AlsoIt->isSameRef(R1Also));
168*8a2fb139SJan Svoboda 
169*8a2fb139SJan Svoboda     auto R1It = Set.find_as(&R1.getFileEntry());
170*8a2fb139SJan Svoboda     ASSERT_TRUE(R1It != Set.end());
171*8a2fb139SJan Svoboda     EXPECT_TRUE(R1It->isSameRef(R1Also));
172*8a2fb139SJan Svoboda 
173*8a2fb139SJan Svoboda     auto R2It = Set.find_as(&R2.getFileEntry());
174*8a2fb139SJan Svoboda     ASSERT_TRUE(R2It != Set.end());
175*8a2fb139SJan Svoboda     EXPECT_TRUE(R2It->isSameRef(R2));
176*8a2fb139SJan Svoboda   }
177*8a2fb139SJan Svoboda 
178*8a2fb139SJan Svoboda   // Insert R1Also second and confirm R1 "wins" when looked up as FileEntry.
179*8a2fb139SJan Svoboda   {
180*8a2fb139SJan Svoboda     SmallDenseSet<FileEntryRef, 8> Set;
181*8a2fb139SJan Svoboda     Set.insert(R1);
182*8a2fb139SJan Svoboda     Set.insert(R1Also);
183*8a2fb139SJan Svoboda     Set.insert(R2);
184*8a2fb139SJan Svoboda 
185*8a2fb139SJan Svoboda     auto R1AlsoIt = Set.find_as(&R1Also.getFileEntry());
186*8a2fb139SJan Svoboda     ASSERT_TRUE(R1AlsoIt != Set.end());
187*8a2fb139SJan Svoboda     EXPECT_TRUE(R1AlsoIt->isSameRef(R1));
188*8a2fb139SJan Svoboda 
189*8a2fb139SJan Svoboda     auto R1It = Set.find_as(&R1.getFileEntry());
190*8a2fb139SJan Svoboda     ASSERT_TRUE(R1It != Set.end());
191*8a2fb139SJan Svoboda     EXPECT_TRUE(R1It->isSameRef(R1));
192*8a2fb139SJan Svoboda 
193*8a2fb139SJan Svoboda     auto R2It = Set.find_as(&R2.getFileEntry());
194*8a2fb139SJan Svoboda     ASSERT_TRUE(R2It != Set.end());
195*8a2fb139SJan Svoboda     EXPECT_TRUE(R2It->isSameRef(R2));
196*8a2fb139SJan Svoboda   }
1972878e965SDuncan P. N. Exon Smith }
1982878e965SDuncan P. N. Exon Smith 
TEST(DirectoryEntryTest,isSameRef)1992878e965SDuncan P. N. Exon Smith TEST(DirectoryEntryTest, isSameRef) {
200b2a7f1c3SSam McCall   FileEntryTestHelper Refs;
2012878e965SDuncan P. N. Exon Smith   DirectoryEntryRef R1 = Refs.addDirectory("1");
2022878e965SDuncan P. N. Exon Smith   DirectoryEntryRef R2 = Refs.addDirectory("2");
2032878e965SDuncan P. N. Exon Smith   DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", R1);
2042878e965SDuncan P. N. Exon Smith 
2052878e965SDuncan P. N. Exon Smith   EXPECT_TRUE(R1.isSameRef(DirectoryEntryRef(R1)));
2062878e965SDuncan P. N. Exon Smith   EXPECT_TRUE(R1.isSameRef(DirectoryEntryRef(R1.getMapEntry())));
2072878e965SDuncan P. N. Exon Smith   EXPECT_FALSE(R1.isSameRef(R2));
2082878e965SDuncan P. N. Exon Smith   EXPECT_FALSE(R1.isSameRef(R1Also));
2092878e965SDuncan P. N. Exon Smith }
2102878e965SDuncan P. N. Exon Smith 
TEST(DirectoryEntryTest,DenseMapInfo)2112878e965SDuncan P. N. Exon Smith TEST(DirectoryEntryTest, DenseMapInfo) {
212b2a7f1c3SSam McCall   FileEntryTestHelper Refs;
2132878e965SDuncan P. N. Exon Smith   DirectoryEntryRef R1 = Refs.addDirectory("1");
2142878e965SDuncan P. N. Exon Smith   DirectoryEntryRef R2 = Refs.addDirectory("2");
2152878e965SDuncan P. N. Exon Smith   DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", R1);
2162878e965SDuncan P. N. Exon Smith 
2172878e965SDuncan P. N. Exon Smith   // Insert R1Also first and confirm it "wins".
2182878e965SDuncan P. N. Exon Smith   {
2192878e965SDuncan P. N. Exon Smith     SmallDenseSet<DirectoryEntryRef, 8> Set;
2202878e965SDuncan P. N. Exon Smith     Set.insert(R1Also);
2212878e965SDuncan P. N. Exon Smith     Set.insert(R1);
2222878e965SDuncan P. N. Exon Smith     Set.insert(R2);
2232878e965SDuncan P. N. Exon Smith     EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1Also));
2242878e965SDuncan P. N. Exon Smith     EXPECT_TRUE(Set.find(R1)->isSameRef(R1Also));
2252878e965SDuncan P. N. Exon Smith     EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
2262878e965SDuncan P. N. Exon Smith   }
2272878e965SDuncan P. N. Exon Smith 
2282878e965SDuncan P. N. Exon Smith   // Insert R1Also second and confirm R1 "wins".
2292878e965SDuncan P. N. Exon Smith   {
2302878e965SDuncan P. N. Exon Smith     SmallDenseSet<DirectoryEntryRef, 8> Set;
2312878e965SDuncan P. N. Exon Smith     Set.insert(R1);
2322878e965SDuncan P. N. Exon Smith     Set.insert(R1Also);
2332878e965SDuncan P. N. Exon Smith     Set.insert(R2);
2342878e965SDuncan P. N. Exon Smith     EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1));
2352878e965SDuncan P. N. Exon Smith     EXPECT_TRUE(Set.find(R1)->isSameRef(R1));
2362878e965SDuncan P. N. Exon Smith     EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
2372878e965SDuncan P. N. Exon Smith   }
2382878e965SDuncan P. N. Exon Smith }
2392878e965SDuncan P. N. Exon Smith 
240ac49500cSDuncan P. N. Exon Smith } // end namespace
241b2a7f1c3SSam McCall } // namespace clang
242