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