Skip to content

Commit bdc3ce9

Browse files
committed
[clang] Make FileEntryRef::getDir() return the as-requested DirectoryEntryRef
For redirected file entries, `FileEntryRef::getDir()` returns the parent directory entry of the target file entry. This differs from `FileEntry::getDir()` that always returns the parent directory that was last used to look up that file. After switching from `FileEntry` to `FileEntryRef` for umbrella headers in D142113, this discrepancy became observable and caused Clang to emit incorrect diagnostics. This patch changes Clang so that it always associates `FileEntryRef` with the parent directory that was used to look it up. This brings its behavior closer to `FileEntry`, but without the hacky mutation. This also ensures that `llvm::sys::path::parent_path(FileRef->getNameAsRequested()) == FileRef->getDir()->getName()`. Previously, `FileRef->getDir()` would fall underneath the redirecting VFS into the world of on-disk paths. Reviewed By: benlangmuir, rmaz Differential Revision: https://reviews.llvm.org/D151398
1 parent 9762854 commit bdc3ce9

File tree

5 files changed

+83
-6
lines changed

5 files changed

+83
-6
lines changed

Diff for: clang/include/clang/Basic/FileEntry.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class FileEntryRef {
7070
const FileEntry &getFileEntry() const {
7171
return *getBaseMapEntry().second->V.get<FileEntry *>();
7272
}
73-
DirectoryEntryRef getDir() const { return *getBaseMapEntry().second->Dir; }
73+
DirectoryEntryRef getDir() const { return ME->second->Dir; }
7474

7575
inline off_t getSize() const;
7676
inline unsigned getUID() const;
@@ -120,12 +120,12 @@ class FileEntryRef {
120120
/// name that was used to lookup the file.
121121
llvm::PointerUnion<FileEntry *, const MapEntry *> V;
122122

123-
/// Directory the file was found in. Set if and only if V is a FileEntry.
124-
OptionalDirectoryEntryRef Dir;
123+
/// Directory the file was found in.
124+
DirectoryEntryRef Dir;
125125

126126
MapValue() = delete;
127127
MapValue(FileEntry &FE, DirectoryEntryRef Dir) : V(&FE), Dir(Dir) {}
128-
MapValue(MapEntry &ME) : V(&ME) {}
128+
MapValue(MapEntry &ME, DirectoryEntryRef Dir) : V(&ME), Dir(Dir) {}
129129
};
130130

131131
/// Check if RHS referenced the file in exactly the same way.

Diff for: clang/lib/Basic/FileManager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ FileManager::getFileRef(StringRef Filename, bool openFile, bool CacheFailure) {
319319

320320
// Cache the redirection in the previously-inserted entry, still available
321321
// in the tentative return value.
322-
NamedFileEnt->second = FileEntryRef::MapValue(Redirection);
322+
NamedFileEnt->second = FileEntryRef::MapValue(Redirection, DirInfo);
323323
}
324324

325325
FileEntryRef ReturnedRef(*NamedFileEnt);

Diff for: clang/test/Modules/vfs-umbrella-same-dir.m

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
4+
//--- sources/FW/Private.h
5+
#include <FW/PrivateUnmapped.h>
6+
//--- sources/FW/PrivateUnmapped.h
7+
#include <FW/Public.h>
8+
//--- sources/FW/Public.h
9+
#include <FW/PublicPresent.h>
10+
//--- frameworks/FW.framework/Headers/PublicPresent.h
11+
// empty
12+
//--- frameworks/FW.framework/Modules/module.modulemap
13+
framework module FW { umbrella header "Public.h" }
14+
//--- frameworks/FW.framework/Modules/module.private.modulemap
15+
framework module FW_Private { umbrella header "Private.h" }
16+
//--- vfs.json.in
17+
{
18+
"case-sensitive": "false",
19+
"version": 0,
20+
"roots": [
21+
{
22+
"contents": [
23+
{
24+
"external-contents": "DIR/sources/FW/Public.h",
25+
"name": "Public.h",
26+
"type": "file"
27+
}
28+
],
29+
"name": "DIR/frameworks/FW.framework/Headers",
30+
"type": "directory"
31+
},
32+
{
33+
"contents": [
34+
{
35+
"external-contents": "DIR/sources/FW/Private.h",
36+
"name": "Private.h",
37+
"type": "file"
38+
}
39+
],
40+
"name": "DIR/frameworks/FW.framework/PrivateHeaders",
41+
"type": "directory"
42+
}
43+
]
44+
}
45+
46+
//--- tu.m
47+
#import <FW/Private.h>
48+
// expected-no-diagnostics
49+
50+
// RUN: sed -e "s|DIR|%/t|g" %t/vfs.json.in > %t/vfs.json
51+
52+
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache \
53+
// RUN: -ivfsoverlay %t/vfs.json -I %t/sources -F %t/frameworks -fsyntax-only %t/tu.m -verify

Diff for: clang/unittests/Basic/FileEntryTest.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "clang/Basic/FileEntry.h"
1010
#include "llvm/ADT/DenseSet.h"
1111
#include "llvm/ADT/StringMap.h"
12+
#include "llvm/Support/Path.h"
1213
#include "gtest/gtest.h"
1314

1415
using namespace llvm;
@@ -51,11 +52,14 @@ class FileEntryTestHelper {
5152
.first);
5253
}
5354
FileEntryRef addFileRedirect(StringRef Name, FileEntryRef Base) {
55+
auto Dir = addDirectory(llvm::sys::path::parent_path(Name));
56+
5457
return FileEntryRef(
5558
*Files
5659
.insert({Name, FileEntryRef::MapValue(
5760
const_cast<FileEntryRef::MapEntry &>(
58-
Base.getMapEntry()))})
61+
Base.getMapEntry()),
62+
Dir)})
5963
.first);
6064
}
6165
};

Diff for: clang/unittests/Basic/FileManagerTest.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,26 @@ TEST_F(FileManagerTest, getFileRefReturnsCorrectNameForDifferentStatPath) {
310310
EXPECT_EQ(&F2->getFileEntry(), &F2Alias2->getFileEntry());
311311
}
312312

313+
TEST_F(FileManagerTest, getFileRefReturnsCorrectDirNameForDifferentStatPath) {
314+
// Inject files with the same inode into distinct directories (name & inode).
315+
auto StatCache = std::make_unique<FakeStatCache>();
316+
StatCache->InjectDirectory("dir1", 40);
317+
StatCache->InjectDirectory("dir2", 41);
318+
StatCache->InjectFile("dir1/f.cpp", 42);
319+
StatCache->InjectFile("dir2/f.cpp", 42, "dir1/f.cpp");
320+
321+
manager.setStatCache(std::move(StatCache));
322+
auto Dir1F = manager.getFileRef("dir1/f.cpp");
323+
auto Dir2F = manager.getFileRef("dir2/f.cpp");
324+
325+
ASSERT_FALSE(!Dir1F);
326+
ASSERT_FALSE(!Dir2F);
327+
EXPECT_EQ("dir1", Dir1F->getDir().getName());
328+
EXPECT_EQ("dir2", Dir2F->getDir().getName());
329+
EXPECT_EQ("dir1/f.cpp", Dir1F->getNameAsRequested());
330+
EXPECT_EQ("dir2/f.cpp", Dir2F->getNameAsRequested());
331+
}
332+
313333
// getFile() returns the same FileEntry for virtual files that have
314334
// corresponding real files that are aliases.
315335
TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) {

0 commit comments

Comments
 (0)