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