[Coverage] Map regions from system headers (#76950)
[llvm-project.git] / clang / unittests / Basic / FileEntryTest.cpp
blobf8a0b4a4edcdaf402ae37aaa273c76a72b25ef19
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 "llvm/Support/Path.h"
13 #include "gtest/gtest.h"
15 using namespace llvm;
17 namespace clang {
19 class FileEntryTestHelper {
20 StringMap<llvm::ErrorOr<FileEntryRef::MapValue>> Files;
21 StringMap<llvm::ErrorOr<DirectoryEntry &>> Dirs;
23 SmallVector<std::unique_ptr<FileEntry>, 5> FEs;
24 SmallVector<std::unique_ptr<DirectoryEntry>, 5> DEs;
25 DirectoryEntryRef DR;
27 public:
28 FileEntryTestHelper() : DR(addDirectory("dir")) {}
30 DirectoryEntryRef addDirectory(StringRef Name) {
31 DEs.emplace_back(new DirectoryEntry());
32 return DirectoryEntryRef(*Dirs.insert({Name, *DEs.back()}).first);
34 DirectoryEntryRef addDirectoryAlias(StringRef Name, DirectoryEntryRef Base) {
35 return DirectoryEntryRef(
36 *Dirs.insert({Name, const_cast<DirectoryEntry &>(Base.getDirEntry())})
37 .first);
40 FileEntryRef addFile(StringRef Name) {
41 FEs.emplace_back(new FileEntry());
42 return FileEntryRef(
43 *Files.insert({Name, FileEntryRef::MapValue(*FEs.back().get(), DR)})
44 .first);
46 FileEntryRef addFileAlias(StringRef Name, FileEntryRef Base) {
47 return FileEntryRef(
48 *Files
49 .insert(
50 {Name, FileEntryRef::MapValue(
51 const_cast<FileEntry &>(Base.getFileEntry()), DR)})
52 .first);
54 FileEntryRef addFileRedirect(StringRef Name, FileEntryRef Base) {
55 auto Dir = addDirectory(llvm::sys::path::parent_path(Name));
57 return FileEntryRef(
58 *Files
59 .insert({Name, FileEntryRef::MapValue(
60 const_cast<FileEntryRef::MapEntry &>(
61 Base.getMapEntry()),
62 Dir)})
63 .first);
67 namespace {
68 TEST(FileEntryTest, FileEntryRef) {
69 FileEntryTestHelper Refs;
70 FileEntryRef R1 = Refs.addFile("1");
71 FileEntryRef R2 = Refs.addFile("2");
72 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
73 FileEntryRef R1Redirect = Refs.addFileRedirect("1-redirect", R1);
74 FileEntryRef R1Redirect2 = Refs.addFileRedirect("1-redirect2", R1Redirect);
76 EXPECT_EQ("1", R1.getName());
77 EXPECT_EQ("2", R2.getName());
78 EXPECT_EQ("1-also", R1Also.getName());
79 EXPECT_EQ("1", R1Redirect.getName());
80 EXPECT_EQ("1", R1Redirect2.getName());
82 EXPECT_EQ("1", R1.getNameAsRequested());
83 EXPECT_EQ("1-redirect", R1Redirect.getNameAsRequested());
84 EXPECT_EQ("1-redirect2", R1Redirect2.getNameAsRequested());
86 EXPECT_NE(&R1.getFileEntry(), &R2.getFileEntry());
87 EXPECT_EQ(&R1.getFileEntry(), &R1Also.getFileEntry());
88 EXPECT_EQ(&R1.getFileEntry(), &R1Redirect.getFileEntry());
89 EXPECT_EQ(&R1Redirect.getFileEntry(), &R1Redirect2.getFileEntry());
91 const FileEntry *CE1 = R1;
92 EXPECT_EQ(CE1, &R1.getFileEntry());
95 TEST(FileEntryTest, equals) {
96 FileEntryTestHelper Refs;
97 FileEntryRef R1 = Refs.addFile("1");
98 FileEntryRef R2 = Refs.addFile("2");
99 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
100 FileEntryRef R1Redirect = Refs.addFileRedirect("1-redirect", R1);
101 FileEntryRef R1Redirect2 = Refs.addFileRedirect("1-redirect2", R1Redirect);
103 EXPECT_EQ(R1, &R1.getFileEntry());
104 EXPECT_EQ(&R1.getFileEntry(), R1);
105 EXPECT_EQ(R1, R1Also);
106 EXPECT_NE(R1, &R2.getFileEntry());
107 EXPECT_NE(&R2.getFileEntry(), R1);
108 EXPECT_NE(R1, R2);
109 EXPECT_EQ(R1, R1Redirect);
110 EXPECT_EQ(R1, R1Redirect2);
113 TEST(FileEntryTest, isSameRef) {
114 FileEntryTestHelper Refs;
115 FileEntryRef R1 = Refs.addFile("1");
116 FileEntryRef R2 = Refs.addFile("2");
117 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
118 FileEntryRef R1Redirect = Refs.addFileRedirect("1-redirect", R1);
119 FileEntryRef R1Redirect2 = Refs.addFileRedirect("1-redirect2", R1Redirect);
121 EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1)));
122 EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1.getMapEntry())));
123 EXPECT_FALSE(R1.isSameRef(R2));
124 EXPECT_FALSE(R1.isSameRef(R1Also));
125 EXPECT_FALSE(R1.isSameRef(R1Redirect));
126 EXPECT_FALSE(R1.isSameRef(R1Redirect2));
127 EXPECT_FALSE(R1Redirect.isSameRef(R1Redirect2));
130 TEST(FileEntryTest, DenseMapInfo) {
131 FileEntryTestHelper Refs;
132 FileEntryRef R1 = Refs.addFile("1");
133 FileEntryRef R2 = Refs.addFile("2");
134 FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
136 // Insert R1Also first and confirm it "wins".
138 SmallDenseSet<FileEntryRef, 8> Set;
139 Set.insert(R1Also);
140 Set.insert(R1);
141 Set.insert(R2);
142 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1Also));
143 EXPECT_TRUE(Set.find(R1)->isSameRef(R1Also));
144 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
147 // Insert R1Also second and confirm R1 "wins".
149 SmallDenseSet<FileEntryRef, 8> Set;
150 Set.insert(R1);
151 Set.insert(R1Also);
152 Set.insert(R2);
153 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1));
154 EXPECT_TRUE(Set.find(R1)->isSameRef(R1));
155 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
158 // Insert R1Also first and confirm it "wins" when looked up as FileEntry.
160 SmallDenseSet<FileEntryRef, 8> Set;
161 Set.insert(R1Also);
162 Set.insert(R1);
163 Set.insert(R2);
165 auto R1AlsoIt = Set.find_as(&R1Also.getFileEntry());
166 ASSERT_TRUE(R1AlsoIt != Set.end());
167 EXPECT_TRUE(R1AlsoIt->isSameRef(R1Also));
169 auto R1It = Set.find_as(&R1.getFileEntry());
170 ASSERT_TRUE(R1It != Set.end());
171 EXPECT_TRUE(R1It->isSameRef(R1Also));
173 auto R2It = Set.find_as(&R2.getFileEntry());
174 ASSERT_TRUE(R2It != Set.end());
175 EXPECT_TRUE(R2It->isSameRef(R2));
178 // Insert R1Also second and confirm R1 "wins" when looked up as FileEntry.
180 SmallDenseSet<FileEntryRef, 8> Set;
181 Set.insert(R1);
182 Set.insert(R1Also);
183 Set.insert(R2);
185 auto R1AlsoIt = Set.find_as(&R1Also.getFileEntry());
186 ASSERT_TRUE(R1AlsoIt != Set.end());
187 EXPECT_TRUE(R1AlsoIt->isSameRef(R1));
189 auto R1It = Set.find_as(&R1.getFileEntry());
190 ASSERT_TRUE(R1It != Set.end());
191 EXPECT_TRUE(R1It->isSameRef(R1));
193 auto R2It = Set.find_as(&R2.getFileEntry());
194 ASSERT_TRUE(R2It != Set.end());
195 EXPECT_TRUE(R2It->isSameRef(R2));
199 TEST(DirectoryEntryTest, isSameRef) {
200 FileEntryTestHelper Refs;
201 DirectoryEntryRef R1 = Refs.addDirectory("1");
202 DirectoryEntryRef R2 = Refs.addDirectory("2");
203 DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", R1);
205 EXPECT_TRUE(R1.isSameRef(DirectoryEntryRef(R1)));
206 EXPECT_TRUE(R1.isSameRef(DirectoryEntryRef(R1.getMapEntry())));
207 EXPECT_FALSE(R1.isSameRef(R2));
208 EXPECT_FALSE(R1.isSameRef(R1Also));
211 TEST(DirectoryEntryTest, DenseMapInfo) {
212 FileEntryTestHelper Refs;
213 DirectoryEntryRef R1 = Refs.addDirectory("1");
214 DirectoryEntryRef R2 = Refs.addDirectory("2");
215 DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", R1);
217 // Insert R1Also first and confirm it "wins".
219 SmallDenseSet<DirectoryEntryRef, 8> Set;
220 Set.insert(R1Also);
221 Set.insert(R1);
222 Set.insert(R2);
223 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1Also));
224 EXPECT_TRUE(Set.find(R1)->isSameRef(R1Also));
225 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
228 // Insert R1Also second and confirm R1 "wins".
230 SmallDenseSet<DirectoryEntryRef, 8> Set;
231 Set.insert(R1);
232 Set.insert(R1Also);
233 Set.insert(R2);
234 EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1));
235 EXPECT_TRUE(Set.find(R1)->isSameRef(R1));
236 EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
240 } // end namespace
241 } // namespace clang