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 }; 54 55 namespace { 56 TEST(FileEntryTest, FileEntryRef) { 57 FileEntryTestHelper Refs; 58 FileEntryRef R1 = Refs.addFile("1"); 59 FileEntryRef R2 = Refs.addFile("2"); 60 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1); 61 62 EXPECT_EQ("1", R1.getName()); 63 EXPECT_EQ("2", R2.getName()); 64 EXPECT_EQ("1-also", R1Also.getName()); 65 66 EXPECT_NE(&R1.getFileEntry(), &R2.getFileEntry()); 67 EXPECT_EQ(&R1.getFileEntry(), &R1Also.getFileEntry()); 68 69 const FileEntry *CE1 = R1; 70 EXPECT_EQ(CE1, &R1.getFileEntry()); 71 } 72 73 TEST(FileEntryTest, OptionalFileEntryRefDegradesToFileEntryPtr) { 74 FileEntryTestHelper Refs; 75 OptionalFileEntryRefDegradesToFileEntryPtr M0; 76 OptionalFileEntryRefDegradesToFileEntryPtr M1 = Refs.addFile("1"); 77 OptionalFileEntryRefDegradesToFileEntryPtr M2 = Refs.addFile("2"); 78 OptionalFileEntryRefDegradesToFileEntryPtr M0Also = None; 79 OptionalFileEntryRefDegradesToFileEntryPtr M1Also = 80 Refs.addFileAlias("1-also", *M1); 81 82 EXPECT_EQ(M0, M0Also); 83 EXPECT_EQ(StringRef("1"), M1->getName()); 84 EXPECT_EQ(StringRef("2"), M2->getName()); 85 EXPECT_EQ(StringRef("1-also"), M1Also->getName()); 86 87 const FileEntry *CE1 = M1; 88 EXPECT_EQ(CE1, &M1->getFileEntry()); 89 } 90 91 TEST(FileEntryTest, equals) { 92 FileEntryTestHelper Refs; 93 FileEntryRef R1 = Refs.addFile("1"); 94 FileEntryRef R2 = Refs.addFile("2"); 95 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1); 96 97 EXPECT_EQ(R1, &R1.getFileEntry()); 98 EXPECT_EQ(&R1.getFileEntry(), R1); 99 EXPECT_EQ(R1, R1Also); 100 EXPECT_NE(R1, &R2.getFileEntry()); 101 EXPECT_NE(&R2.getFileEntry(), R1); 102 EXPECT_NE(R1, R2); 103 104 OptionalFileEntryRefDegradesToFileEntryPtr M1 = R1; 105 106 EXPECT_EQ(M1, &R1.getFileEntry()); 107 EXPECT_EQ(&R1.getFileEntry(), M1); 108 EXPECT_NE(M1, &R2.getFileEntry()); 109 EXPECT_NE(&R2.getFileEntry(), M1); 110 } 111 112 TEST(FileEntryTest, isSameRef) { 113 FileEntryTestHelper Refs; 114 FileEntryRef R1 = Refs.addFile("1"); 115 FileEntryRef R2 = Refs.addFile("2"); 116 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1); 117 118 EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1))); 119 EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1.getMapEntry()))); 120 EXPECT_FALSE(R1.isSameRef(R2)); 121 EXPECT_FALSE(R1.isSameRef(R1Also)); 122 } 123 124 TEST(FileEntryTest, DenseMapInfo) { 125 FileEntryTestHelper Refs; 126 FileEntryRef R1 = Refs.addFile("1"); 127 FileEntryRef R2 = Refs.addFile("2"); 128 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1); 129 130 // Insert R1Also first and confirm it "wins". 131 { 132 SmallDenseSet<FileEntryRef, 8> Set; 133 Set.insert(R1Also); 134 Set.insert(R1); 135 Set.insert(R2); 136 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1Also)); 137 EXPECT_TRUE(Set.find(R1)->isSameRef(R1Also)); 138 EXPECT_TRUE(Set.find(R2)->isSameRef(R2)); 139 } 140 141 // Insert R1Also second and confirm R1 "wins". 142 { 143 SmallDenseSet<FileEntryRef, 8> Set; 144 Set.insert(R1); 145 Set.insert(R1Also); 146 Set.insert(R2); 147 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1)); 148 EXPECT_TRUE(Set.find(R1)->isSameRef(R1)); 149 EXPECT_TRUE(Set.find(R2)->isSameRef(R2)); 150 } 151 } 152 153 TEST(DirectoryEntryTest, isSameRef) { 154 FileEntryTestHelper Refs; 155 DirectoryEntryRef R1 = Refs.addDirectory("1"); 156 DirectoryEntryRef R2 = Refs.addDirectory("2"); 157 DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", R1); 158 159 EXPECT_TRUE(R1.isSameRef(DirectoryEntryRef(R1))); 160 EXPECT_TRUE(R1.isSameRef(DirectoryEntryRef(R1.getMapEntry()))); 161 EXPECT_FALSE(R1.isSameRef(R2)); 162 EXPECT_FALSE(R1.isSameRef(R1Also)); 163 } 164 165 TEST(DirectoryEntryTest, DenseMapInfo) { 166 FileEntryTestHelper Refs; 167 DirectoryEntryRef R1 = Refs.addDirectory("1"); 168 DirectoryEntryRef R2 = Refs.addDirectory("2"); 169 DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", R1); 170 171 // Insert R1Also first and confirm it "wins". 172 { 173 SmallDenseSet<DirectoryEntryRef, 8> Set; 174 Set.insert(R1Also); 175 Set.insert(R1); 176 Set.insert(R2); 177 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1Also)); 178 EXPECT_TRUE(Set.find(R1)->isSameRef(R1Also)); 179 EXPECT_TRUE(Set.find(R2)->isSameRef(R2)); 180 } 181 182 // Insert R1Also second and confirm R1 "wins". 183 { 184 SmallDenseSet<DirectoryEntryRef, 8> Set; 185 Set.insert(R1); 186 Set.insert(R1Also); 187 Set.insert(R2); 188 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1)); 189 EXPECT_TRUE(Set.find(R1)->isSameRef(R1)); 190 EXPECT_TRUE(Set.find(R2)->isSameRef(R2)); 191 } 192 } 193 194 } // end namespace 195 } // namespace clang 196