brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.6 KiB · de3b9dd Raw
241 lines · cpp
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-exception6//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 15using namespace llvm;16 17namespace clang {18 19class 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 27public:28  FileEntryTestHelper() : DR(addDirectory("dir")) {}29 30  DirectoryEntryRef addDirectory(StringRef Name) {31    DEs.emplace_back(new DirectoryEntry());32    return DirectoryEntryRef(*Dirs.insert({Name, *DEs.back()}).first);33  }34  DirectoryEntryRef addDirectoryAlias(StringRef Name, DirectoryEntryRef Base) {35    return DirectoryEntryRef(36        *Dirs.insert({Name, const_cast<DirectoryEntry &>(Base.getDirEntry())})37             .first);38  }39 40  FileEntryRef addFile(StringRef Name) {41    FEs.emplace_back(new FileEntry());42    return FileEntryRef(43        *Files.insert({Name, FileEntryRef::MapValue(*FEs.back(), DR)}).first);44  }45  FileEntryRef addFileAlias(StringRef Name, FileEntryRef Base) {46    return FileEntryRef(47        *Files48             .insert(49                 {Name, FileEntryRef::MapValue(50                            const_cast<FileEntry &>(Base.getFileEntry()), DR)})51             .first);52  }53  FileEntryRef addFileRedirect(StringRef Name, FileEntryRef Base) {54    auto Dir = addDirectory(llvm::sys::path::parent_path(Name));55 56    return FileEntryRef(57        *Files58             .insert({Name, FileEntryRef::MapValue(59                                const_cast<FileEntryRef::MapEntry &>(60                                    Base.getMapEntry()),61                                Dir)})62             .first);63  }64};65 66namespace {67TEST(FileEntryTest, FileEntryRef) {68  FileEntryTestHelper Refs;69  FileEntryRef R1 = Refs.addFile("1");70  FileEntryRef R2 = Refs.addFile("2");71  FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);72  FileEntryRef R1Redirect = Refs.addFileRedirect("1-redirect", R1);73  FileEntryRef R1Redirect2 = Refs.addFileRedirect("1-redirect2", 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 94TEST(FileEntryTest, equals) {95  FileEntryTestHelper Refs;96  FileEntryRef R1 = Refs.addFile("1");97  FileEntryRef R2 = Refs.addFile("2");98  FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);99  FileEntryRef R1Redirect = Refs.addFileRedirect("1-redirect", R1);100  FileEntryRef R1Redirect2 = Refs.addFileRedirect("1-redirect2", 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 112TEST(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  FileEntryRef R1Redirect = Refs.addFileRedirect("1-redirect", R1);118  FileEntryRef R1Redirect2 = Refs.addFileRedirect("1-redirect2", 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 129TEST(FileEntryTest, DenseMapInfo) {130  FileEntryTestHelper Refs;131  FileEntryRef R1 = Refs.addFile("1");132  FileEntryRef R2 = Refs.addFile("2");133  FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);134 135  // Insert R1Also first and confirm it "wins".136  {137    SmallDenseSet<FileEntryRef, 8> Set;138    Set.insert(R1Also);139    Set.insert(R1);140    Set.insert(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(R1);150    Set.insert(R1Also);151    Set.insert(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(R1Also);161    Set.insert(R1);162    Set.insert(R2);163 164    auto R1AlsoIt = Set.find_as(&R1Also.getFileEntry());165    ASSERT_TRUE(R1AlsoIt != Set.end());166    EXPECT_TRUE(R1AlsoIt->isSameRef(R1Also));167 168    auto R1It = Set.find_as(&R1.getFileEntry());169    ASSERT_TRUE(R1It != Set.end());170    EXPECT_TRUE(R1It->isSameRef(R1Also));171 172    auto R2It = Set.find_as(&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(R1);181    Set.insert(R1Also);182    Set.insert(R2);183 184    auto R1AlsoIt = Set.find_as(&R1Also.getFileEntry());185    ASSERT_TRUE(R1AlsoIt != Set.end());186    EXPECT_TRUE(R1AlsoIt->isSameRef(R1));187 188    auto R1It = Set.find_as(&R1.getFileEntry());189    ASSERT_TRUE(R1It != Set.end());190    EXPECT_TRUE(R1It->isSameRef(R1));191 192    auto R2It = Set.find_as(&R2.getFileEntry());193    ASSERT_TRUE(R2It != Set.end());194    EXPECT_TRUE(R2It->isSameRef(R2));195  }196}197 198TEST(DirectoryEntryTest, isSameRef) {199  FileEntryTestHelper Refs;200  DirectoryEntryRef R1 = Refs.addDirectory("1");201  DirectoryEntryRef R2 = Refs.addDirectory("2");202  DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", 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 210TEST(DirectoryEntryTest, DenseMapInfo) {211  FileEntryTestHelper Refs;212  DirectoryEntryRef R1 = Refs.addDirectory("1");213  DirectoryEntryRef R2 = Refs.addDirectory("2");214  DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", R1);215 216  // Insert R1Also first and confirm it "wins".217  {218    SmallDenseSet<DirectoryEntryRef, 8> Set;219    Set.insert(R1Also);220    Set.insert(R1);221    Set.insert(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(R1);231    Set.insert(R1Also);232    Set.insert(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 namespace240} // namespace clang241