aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Basic/FileEntryTest.cpp
blob: de3b9dd6aa6e781dddc75faaf976d96c00cb71dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//===- unittests/Basic/FileEntryTest.cpp - Test FileEntry/FileEntryRef ----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "clang/Basic/FileEntry.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Path.h"
#include "gtest/gtest.h"

using namespace llvm;

namespace clang {

class FileEntryTestHelper {
  StringMap<llvm::ErrorOr<FileEntryRef::MapValue>> Files;
  StringMap<llvm::ErrorOr<DirectoryEntry &>> Dirs;

  SmallVector<std::unique_ptr<FileEntry>, 5> FEs;
  SmallVector<std::unique_ptr<DirectoryEntry>, 5> DEs;
  DirectoryEntryRef DR;

public:
  FileEntryTestHelper() : DR(addDirectory("dir")) {}

  DirectoryEntryRef addDirectory(StringRef Name) {
    DEs.emplace_back(new DirectoryEntry());
    return DirectoryEntryRef(*Dirs.insert({Name, *DEs.back()}).first);
  }
  DirectoryEntryRef addDirectoryAlias(StringRef Name, DirectoryEntryRef Base) {
    return DirectoryEntryRef(
        *Dirs.insert({Name, const_cast<DirectoryEntry &>(Base.getDirEntry())})
             .first);
  }

  FileEntryRef addFile(StringRef Name) {
    FEs.emplace_back(new FileEntry());
    return FileEntryRef(
        *Files.insert({Name, FileEntryRef::MapValue(*FEs.back(), DR)}).first);
  }
  FileEntryRef addFileAlias(StringRef Name, FileEntryRef Base) {
    return FileEntryRef(
        *Files
             .insert(
                 {Name, FileEntryRef::MapValue(
                            const_cast<FileEntry &>(Base.getFileEntry()), DR)})
             .first);
  }
  FileEntryRef addFileRedirect(StringRef Name, FileEntryRef Base) {
    auto Dir = addDirectory(llvm::sys::path::parent_path(Name));

    return FileEntryRef(
        *Files
             .insert({Name, FileEntryRef::MapValue(
                                const_cast<FileEntryRef::MapEntry &>(
                                    Base.getMapEntry()),
                                Dir)})
             .first);
  }
};

namespace {
TEST(FileEntryTest, FileEntryRef) {
  FileEntryTestHelper Refs;
  FileEntryRef R1 = Refs.addFile("1");
  FileEntryRef R2 = Refs.addFile("2");
  FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
  FileEntryRef R1Redirect = Refs.addFileRedirect("1-redirect", R1);
  FileEntryRef R1Redirect2 = Refs.addFileRedirect("1-redirect2", R1Redirect);

  EXPECT_EQ("1", R1.getName());
  EXPECT_EQ("2", R2.getName());
  EXPECT_EQ("1-also", R1Also.getName());
  EXPECT_EQ("1", R1Redirect.getName());
  EXPECT_EQ("1", R1Redirect2.getName());

  EXPECT_EQ("1", R1.getNameAsRequested());
  EXPECT_EQ("1-redirect", R1Redirect.getNameAsRequested());
  EXPECT_EQ("1-redirect2", R1Redirect2.getNameAsRequested());

  EXPECT_NE(&R1.getFileEntry(), &R2.getFileEntry());
  EXPECT_EQ(&R1.getFileEntry(), &R1Also.getFileEntry());
  EXPECT_EQ(&R1.getFileEntry(), &R1Redirect.getFileEntry());
  EXPECT_EQ(&R1Redirect.getFileEntry(), &R1Redirect2.getFileEntry());

  const FileEntry *CE1 = R1;
  EXPECT_EQ(CE1, &R1.getFileEntry());
}

TEST(FileEntryTest, equals) {
  FileEntryTestHelper Refs;
  FileEntryRef R1 = Refs.addFile("1");
  FileEntryRef R2 = Refs.addFile("2");
  FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
  FileEntryRef R1Redirect = Refs.addFileRedirect("1-redirect", R1);
  FileEntryRef R1Redirect2 = Refs.addFileRedirect("1-redirect2", R1Redirect);

  EXPECT_EQ(R1, &R1.getFileEntry());
  EXPECT_EQ(&R1.getFileEntry(), R1);
  EXPECT_EQ(R1, R1Also);
  EXPECT_NE(R1, &R2.getFileEntry());
  EXPECT_NE(&R2.getFileEntry(), R1);
  EXPECT_NE(R1, R2);
  EXPECT_EQ(R1, R1Redirect);
  EXPECT_EQ(R1, R1Redirect2);
}

TEST(FileEntryTest, isSameRef) {
  FileEntryTestHelper Refs;
  FileEntryRef R1 = Refs.addFile("1");
  FileEntryRef R2 = Refs.addFile("2");
  FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);
  FileEntryRef R1Redirect = Refs.addFileRedirect("1-redirect", R1);
  FileEntryRef R1Redirect2 = Refs.addFileRedirect("1-redirect2", R1Redirect);

  EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1)));
  EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1.getMapEntry())));
  EXPECT_FALSE(R1.isSameRef(R2));
  EXPECT_FALSE(R1.isSameRef(R1Also));
  EXPECT_FALSE(R1.isSameRef(R1Redirect));
  EXPECT_FALSE(R1.isSameRef(R1Redirect2));
  EXPECT_FALSE(R1Redirect.isSameRef(R1Redirect2));
}

TEST(FileEntryTest, DenseMapInfo) {
  FileEntryTestHelper Refs;
  FileEntryRef R1 = Refs.addFile("1");
  FileEntryRef R2 = Refs.addFile("2");
  FileEntryRef R1Also = Refs.addFileAlias("1-also", R1);

  // Insert R1Also first and confirm it "wins".
  {
    SmallDenseSet<FileEntryRef, 8> Set;
    Set.insert(R1Also);
    Set.insert(R1);
    Set.insert(R2);
    EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1Also));
    EXPECT_TRUE(Set.find(R1)->isSameRef(R1Also));
    EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
  }

  // Insert R1Also second and confirm R1 "wins".
  {
    SmallDenseSet<FileEntryRef, 8> Set;
    Set.insert(R1);
    Set.insert(R1Also);
    Set.insert(R2);
    EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1));
    EXPECT_TRUE(Set.find(R1)->isSameRef(R1));
    EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
  }

  // Insert R1Also first and confirm it "wins" when looked up as FileEntry.
  {
    SmallDenseSet<FileEntryRef, 8> Set;
    Set.insert(R1Also);
    Set.insert(R1);
    Set.insert(R2);

    auto R1AlsoIt = Set.find_as(&R1Also.getFileEntry());
    ASSERT_TRUE(R1AlsoIt != Set.end());
    EXPECT_TRUE(R1AlsoIt->isSameRef(R1Also));

    auto R1It = Set.find_as(&R1.getFileEntry());
    ASSERT_TRUE(R1It != Set.end());
    EXPECT_TRUE(R1It->isSameRef(R1Also));

    auto R2It = Set.find_as(&R2.getFileEntry());
    ASSERT_TRUE(R2It != Set.end());
    EXPECT_TRUE(R2It->isSameRef(R2));
  }

  // Insert R1Also second and confirm R1 "wins" when looked up as FileEntry.
  {
    SmallDenseSet<FileEntryRef, 8> Set;
    Set.insert(R1);
    Set.insert(R1Also);
    Set.insert(R2);

    auto R1AlsoIt = Set.find_as(&R1Also.getFileEntry());
    ASSERT_TRUE(R1AlsoIt != Set.end());
    EXPECT_TRUE(R1AlsoIt->isSameRef(R1));

    auto R1It = Set.find_as(&R1.getFileEntry());
    ASSERT_TRUE(R1It != Set.end());
    EXPECT_TRUE(R1It->isSameRef(R1));

    auto R2It = Set.find_as(&R2.getFileEntry());
    ASSERT_TRUE(R2It != Set.end());
    EXPECT_TRUE(R2It->isSameRef(R2));
  }
}

TEST(DirectoryEntryTest, isSameRef) {
  FileEntryTestHelper Refs;
  DirectoryEntryRef R1 = Refs.addDirectory("1");
  DirectoryEntryRef R2 = Refs.addDirectory("2");
  DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", R1);

  EXPECT_TRUE(R1.isSameRef(DirectoryEntryRef(R1)));
  EXPECT_TRUE(R1.isSameRef(DirectoryEntryRef(R1.getMapEntry())));
  EXPECT_FALSE(R1.isSameRef(R2));
  EXPECT_FALSE(R1.isSameRef(R1Also));
}

TEST(DirectoryEntryTest, DenseMapInfo) {
  FileEntryTestHelper Refs;
  DirectoryEntryRef R1 = Refs.addDirectory("1");
  DirectoryEntryRef R2 = Refs.addDirectory("2");
  DirectoryEntryRef R1Also = Refs.addDirectoryAlias("1-also", R1);

  // Insert R1Also first and confirm it "wins".
  {
    SmallDenseSet<DirectoryEntryRef, 8> Set;
    Set.insert(R1Also);
    Set.insert(R1);
    Set.insert(R2);
    EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1Also));
    EXPECT_TRUE(Set.find(R1)->isSameRef(R1Also));
    EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
  }

  // Insert R1Also second and confirm R1 "wins".
  {
    SmallDenseSet<DirectoryEntryRef, 8> Set;
    Set.insert(R1);
    Set.insert(R1Also);
    Set.insert(R2);
    EXPECT_TRUE(Set.find(R1Also)->isSameRef(R1));
    EXPECT_TRUE(Set.find(R1)->isSameRef(R1));
    EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
  }
}

} // end namespace
} // namespace clang