[SCCP] Avoid modifying AdditionalUsers while iterating over it
[llvm-project.git] / clang / unittests / Basic / FileEntryTest.cpp
blob16c8e57d9a177a754e221b2752f01ea059d136b8
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 //===----------------------------------------------------------------------===//
9 #include "clang/Basic/FileEntry.h"
10 #include "llvm/ADT/DenseSet.h"
11 #include "llvm/ADT/StringMap.h"
12 #include "gtest/gtest.h"
14 using namespace llvm;
15 using namespace clang;
17 namespace {
19 using FileMap = StringMap<llvm::ErrorOr<FileEntryRef::MapValue>>;
20 using DirMap = StringMap<llvm::ErrorOr<DirectoryEntry &>>;
22 struct RefMaps {
23 FileMap Files;
24 DirMap Dirs;
26 SmallVector<std::unique_ptr<FileEntry>, 5> FEs;
27 SmallVector<std::unique_ptr<DirectoryEntry>, 5> DEs;
28 DirectoryEntryRef DR;
30 RefMaps() : DR(addDirectory("dir")) {}
32 DirectoryEntryRef addDirectory(StringRef Name) {
33 DEs.push_back(std::make_unique<DirectoryEntry>());
34 return DirectoryEntryRef(*Dirs.insert({Name, *DEs.back()}).first);
36 DirectoryEntryRef addDirectoryAlias(StringRef Name, DirectoryEntryRef Base) {
37 return DirectoryEntryRef(
38 *Dirs.insert({Name, const_cast<DirectoryEntry &>(Base.getDirEntry())})
39 .first);
42 FileEntryRef addFile(StringRef Name) {
43 FEs.push_back(std::make_unique<FileEntry>());
44 return FileEntryRef(
45 *Files.insert({Name, FileEntryRef::MapValue(*FEs.back().get(), DR)})
46 .first);
48 FileEntryRef addFileAlias(StringRef Name, FileEntryRef Base) {
49 return FileEntryRef(
50 *Files
51 .insert(
52 {Name, FileEntryRef::MapValue(
53 const_cast<FileEntry &>(Base.getFileEntry()), DR)})
54 .first);
58 TEST(FileEntryTest, Constructor) {
59 FileEntry FE;
60 EXPECT_EQ(0, FE.getSize());
61 EXPECT_EQ(0, FE.getModificationTime());
62 EXPECT_EQ(nullptr, FE.getDir());
63 EXPECT_EQ(0U, FE.getUniqueID().getDevice());
64 EXPECT_EQ(0U, FE.getUniqueID().getFile());
65 EXPECT_EQ(false, FE.isNamedPipe());
66 EXPECT_EQ(false, FE.isValid());
69 TEST(FileEntryTest, FileEntryRef) {
70 RefMaps Refs;
71 FileEntryRef R1 = Refs.addFile("1");
72 FileEntryRef R2 = Refs.addFile("2");
73 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
75 EXPECT_EQ("1", R1.getName());
76 EXPECT_EQ("2", R2.getName());
77 EXPECT_EQ("1-also", R1Also.getName());
79 EXPECT_NE(&R1.getFileEntry(), &R2.getFileEntry());
80 EXPECT_EQ(&R1.getFileEntry(), &R1Also.getFileEntry());
82 const FileEntry *CE1 = R1;
83 EXPECT_EQ(CE1, &R1.getFileEntry());
86 TEST(FileEntryTest, OptionalFileEntryRefDegradesToFileEntryPtr) {
87 RefMaps Refs;
88 OptionalFileEntryRefDegradesToFileEntryPtr M0;
89 OptionalFileEntryRefDegradesToFileEntryPtr M1 = Refs.addFile("1");
90 OptionalFileEntryRefDegradesToFileEntryPtr M2 = Refs.addFile("2");
91 OptionalFileEntryRefDegradesToFileEntryPtr M0Also = None;
92 OptionalFileEntryRefDegradesToFileEntryPtr M1Also =
93 Refs.addFileAlias("1-also", *M1);
95 EXPECT_EQ(M0, M0Also);
96 EXPECT_EQ(StringRef("1"), M1->getName());
97 EXPECT_EQ(StringRef("2"), M2->getName());
98 EXPECT_EQ(StringRef("1-also"), M1Also->getName());
100 const FileEntry *CE1 = M1;
101 EXPECT_EQ(CE1, &M1->getFileEntry());
104 TEST(FileEntryTest, equals) {
105 RefMaps Refs;
106 FileEntryRef R1 = Refs.addFile("1");
107 FileEntryRef R2 = Refs.addFile("2");
108 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
110 EXPECT_EQ(R1, &R1.getFileEntry());
111 EXPECT_EQ(&R1.getFileEntry(), R1);
112 EXPECT_EQ(R1, R1Also);
113 EXPECT_NE(R1, &R2.getFileEntry());
114 EXPECT_NE(&R2.getFileEntry(), R1);
115 EXPECT_NE(R1, R2);
117 OptionalFileEntryRefDegradesToFileEntryPtr M1 = R1;
119 EXPECT_EQ(M1, &R1.getFileEntry());
120 EXPECT_EQ(&R1.getFileEntry(), M1);
121 EXPECT_NE(M1, &R2.getFileEntry());
122 EXPECT_NE(&R2.getFileEntry(), M1);
125 TEST(FileEntryTest, isSameRef) {
126 RefMaps Refs;
127 FileEntryRef R1 = Refs.addFile("1");
128 FileEntryRef R2 = Refs.addFile("2");
129 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
131 EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1)));
132 EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1.getMapEntry())));
133 EXPECT_FALSE(R1.isSameRef(R2));
134 EXPECT_FALSE(R1.isSameRef(R1Also));
137 TEST(FileEntryTest, DenseMapInfo) {
138 RefMaps Refs;
139 FileEntryRef R1 = Refs.addFile("1");
140 FileEntryRef R2 = Refs.addFile("2");
141 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
143 // Insert R1Also first and confirm it "wins".
145 SmallDenseSet<FileEntryRef, 8> Set;
146 Set.insert(R1Also);
147 Set.insert(R1);
148 Set.insert(R2);
149 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1Also));
150 EXPECT_TRUE(Set.find(R1)->isSameRef(R1Also));
151 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
154 // Insert R1Also second and confirm R1 "wins".
156 SmallDenseSet<FileEntryRef, 8> Set;
157 Set.insert(R1);
158 Set.insert(R1Also);
159 Set.insert(R2);
160 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1));
161 EXPECT_TRUE(Set.find(R1)->isSameRef(R1));
162 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
166 TEST(DirectoryEntryTest, isSameRef) {
167 RefMaps Refs;
168 DirectoryEntryRef R1 = Refs.addDirectory("1");
169 DirectoryEntryRef R2 = Refs.addDirectory("2");
170 DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", R1);
172 EXPECT_TRUE(R1.isSameRef(DirectoryEntryRef(R1)));
173 EXPECT_TRUE(R1.isSameRef(DirectoryEntryRef(R1.getMapEntry())));
174 EXPECT_FALSE(R1.isSameRef(R2));
175 EXPECT_FALSE(R1.isSameRef(R1Also));
178 TEST(DirectoryEntryTest, DenseMapInfo) {
179 RefMaps Refs;
180 DirectoryEntryRef R1 = Refs.addDirectory("1");
181 DirectoryEntryRef R2 = Refs.addDirectory("2");
182 DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", R1);
184 // Insert R1Also first and confirm it "wins".
186 SmallDenseSet<DirectoryEntryRef, 8> Set;
187 Set.insert(R1Also);
188 Set.insert(R1);
189 Set.insert(R2);
190 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1Also));
191 EXPECT_TRUE(Set.find(R1)->isSameRef(R1Also));
192 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
195 // Insert R1Also second and confirm R1 "wins".
197 SmallDenseSet<DirectoryEntryRef, 8> Set;
198 Set.insert(R1);
199 Set.insert(R1Also);
200 Set.insert(R2);
201 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1));
202 EXPECT_TRUE(Set.find(R1)->isSameRef(R1));
203 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
207 } // end namespace