Bug Summary

File:clang/lib/Basic/FileManager.cpp
Warning:line 288, column 5
Value stored to 'NamedFileEnt' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name FileManager.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D CLANG_VENDOR="Debian " -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/tools/clang/lib/Basic -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Basic -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/include -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/include -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/tools/clang/lib/Basic -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2019-12-11-181444-25759-1 -x c++ /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Basic/FileManager.cpp
1//===--- FileManager.cpp - File System Probing and Caching ----------------===//
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//===----------------------------------------------------------------------===//
8//
9// This file implements the FileManager interface.
10//
11//===----------------------------------------------------------------------===//
12//
13// TODO: This should index all interesting directories with dirent calls.
14// getdirentries ?
15// opendir/readdir_r/closedir ?
16//
17//===----------------------------------------------------------------------===//
18
19#include "clang/Basic/FileManager.h"
20#include "clang/Basic/FileSystemStatCache.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/Config/llvm-config.h"
25#include "llvm/Support/FileSystem.h"
26#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Support/Path.h"
28#include "llvm/Support/raw_ostream.h"
29#include <algorithm>
30#include <cassert>
31#include <climits>
32#include <cstdint>
33#include <cstdlib>
34#include <string>
35#include <utility>
36
37using namespace clang;
38
39#define DEBUG_TYPE"file-search" "file-search"
40
41ALWAYS_ENABLED_STATISTIC(NumDirLookups, "Number of directory lookups.")static llvm::TrackingStatistic NumDirLookups = {"file-search"
, "NumDirLookups", "Number of directory lookups."}
;
42ALWAYS_ENABLED_STATISTIC(NumFileLookups, "Number of file lookups.")static llvm::TrackingStatistic NumFileLookups = {"file-search"
, "NumFileLookups", "Number of file lookups."}
;
43ALWAYS_ENABLED_STATISTIC(NumDirCacheMisses,static llvm::TrackingStatistic NumDirCacheMisses = {"file-search"
, "NumDirCacheMisses", "Number of directory cache misses."}
44 "Number of directory cache misses.")static llvm::TrackingStatistic NumDirCacheMisses = {"file-search"
, "NumDirCacheMisses", "Number of directory cache misses."}
;
45ALWAYS_ENABLED_STATISTIC(NumFileCacheMisses, "Number of file cache misses.")static llvm::TrackingStatistic NumFileCacheMisses = {"file-search"
, "NumFileCacheMisses", "Number of file cache misses."}
;
46
47//===----------------------------------------------------------------------===//
48// Common logic.
49//===----------------------------------------------------------------------===//
50
51FileManager::FileManager(const FileSystemOptions &FSO,
52 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
53 : FS(std::move(FS)), FileSystemOpts(FSO), SeenDirEntries(64),
54 SeenFileEntries(64), NextFileUID(0) {
55 // If the caller doesn't provide a virtual file system, just grab the real
56 // file system.
57 if (!this->FS)
58 this->FS = llvm::vfs::getRealFileSystem();
59}
60
61FileManager::~FileManager() = default;
62
63void FileManager::setStatCache(std::unique_ptr<FileSystemStatCache> statCache) {
64 assert(statCache && "No stat cache provided?")((statCache && "No stat cache provided?") ? static_cast
<void> (0) : __assert_fail ("statCache && \"No stat cache provided?\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Basic/FileManager.cpp"
, 64, __PRETTY_FUNCTION__))
;
65 StatCache = std::move(statCache);
66}
67
68void FileManager::clearStatCache() { StatCache.reset(); }
69
70/// Retrieve the directory that the given file name resides in.
71/// Filename can point to either a real file or a virtual file.
72static llvm::ErrorOr<const DirectoryEntry *>
73getDirectoryFromFile(FileManager &FileMgr, StringRef Filename,
74 bool CacheFailure) {
75 if (Filename.empty())
76 return std::errc::no_such_file_or_directory;
77
78 if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
79 return std::errc::is_a_directory;
80
81 StringRef DirName = llvm::sys::path::parent_path(Filename);
82 // Use the current directory if file has no path component.
83 if (DirName.empty())
84 DirName = ".";
85
86 return FileMgr.getDirectory(DirName, CacheFailure);
87}
88
89/// Add all ancestors of the given path (pointing to either a file or
90/// a directory) as virtual directories.
91void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
92 StringRef DirName = llvm::sys::path::parent_path(Path);
93 if (DirName.empty())
94 DirName = ".";
95
96 auto &NamedDirEnt = *SeenDirEntries.insert(
97 {DirName, std::errc::no_such_file_or_directory}).first;
98
99 // When caching a virtual directory, we always cache its ancestors
100 // at the same time. Therefore, if DirName is already in the cache,
101 // we don't need to recurse as its ancestors must also already be in
102 // the cache (or it's a known non-virtual directory).
103 if (NamedDirEnt.second)
104 return;
105
106 // Add the virtual directory to the cache.
107 auto UDE = std::make_unique<DirectoryEntry>();
108 UDE->Name = NamedDirEnt.first();
109 NamedDirEnt.second = *UDE.get();
110 VirtualDirectoryEntries.push_back(std::move(UDE));
111
112 // Recursively add the other ancestors.
113 addAncestorsAsVirtualDirs(DirName);
114}
115
116llvm::Expected<DirectoryEntryRef>
117FileManager::getDirectoryRef(StringRef DirName, bool CacheFailure) {
118 // stat doesn't like trailing separators except for root directory.
119 // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
120 // (though it can strip '\\')
121 if (DirName.size() > 1 &&
122 DirName != llvm::sys::path::root_path(DirName) &&
123 llvm::sys::path::is_separator(DirName.back()))
124 DirName = DirName.substr(0, DirName.size()-1);
125#ifdef _WIN32
126 // Fixing a problem with "clang C:test.c" on Windows.
127 // Stat("C:") does not recognize "C:" as a valid directory
128 std::string DirNameStr;
129 if (DirName.size() > 1 && DirName.back() == ':' &&
130 DirName.equals_lower(llvm::sys::path::root_name(DirName))) {
131 DirNameStr = DirName.str() + '.';
132 DirName = DirNameStr;
133 }
134#endif
135
136 ++NumDirLookups;
137
138 // See if there was already an entry in the map. Note that the map
139 // contains both virtual and real directories.
140 auto SeenDirInsertResult =
141 SeenDirEntries.insert({DirName, std::errc::no_such_file_or_directory});
142 if (!SeenDirInsertResult.second) {
143 if (SeenDirInsertResult.first->second)
144 return DirectoryEntryRef(&*SeenDirInsertResult.first);
145 return llvm::errorCodeToError(SeenDirInsertResult.first->second.getError());
146 }
147
148 // We've not seen this before. Fill it in.
149 ++NumDirCacheMisses;
150 auto &NamedDirEnt = *SeenDirInsertResult.first;
151 assert(!NamedDirEnt.second && "should be newly-created")((!NamedDirEnt.second && "should be newly-created") ?
static_cast<void> (0) : __assert_fail ("!NamedDirEnt.second && \"should be newly-created\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Basic/FileManager.cpp"
, 151, __PRETTY_FUNCTION__))
;
152
153 // Get the null-terminated directory name as stored as the key of the
154 // SeenDirEntries map.
155 StringRef InterndDirName = NamedDirEnt.first();
156
157 // Check to see if the directory exists.
158 llvm::vfs::Status Status;
159 auto statError = getStatValue(InterndDirName, Status, false,
160 nullptr /*directory lookup*/);
161 if (statError) {
162 // There's no real directory at the given path.
163 if (CacheFailure)
164 NamedDirEnt.second = statError;
165 else
166 SeenDirEntries.erase(DirName);
167 return llvm::errorCodeToError(statError);
168 }
169
170 // It exists. See if we have already opened a directory with the
171 // same inode (this occurs on Unix-like systems when one dir is
172 // symlinked to another, for example) or the same path (on
173 // Windows).
174 DirectoryEntry &UDE = UniqueRealDirs[Status.getUniqueID()];
175
176 NamedDirEnt.second = UDE;
177 if (UDE.getName().empty()) {
178 // We don't have this directory yet, add it. We use the string
179 // key from the SeenDirEntries map as the string.
180 UDE.Name = InterndDirName;
181 }
182
183 return DirectoryEntryRef(&NamedDirEnt);
184}
185
186llvm::ErrorOr<const DirectoryEntry *>
187FileManager::getDirectory(StringRef DirName, bool CacheFailure) {
188 auto Result = getDirectoryRef(DirName, CacheFailure);
189 if (Result)
190 return &Result->getDirEntry();
191 return llvm::errorToErrorCode(Result.takeError());
192}
193
194llvm::ErrorOr<const FileEntry *>
195FileManager::getFile(StringRef Filename, bool openFile, bool CacheFailure) {
196 auto Result = getFileRef(Filename, openFile, CacheFailure);
197 if (Result)
198 return &Result->getFileEntry();
199 return llvm::errorToErrorCode(Result.takeError());
200}
201
202llvm::Expected<FileEntryRef>
203FileManager::getFileRef(StringRef Filename, bool openFile, bool CacheFailure) {
204 ++NumFileLookups;
205
206 // See if there is already an entry in the map.
207 auto SeenFileInsertResult =
208 SeenFileEntries.insert({Filename, std::errc::no_such_file_or_directory});
209 if (!SeenFileInsertResult.second) {
210 if (!SeenFileInsertResult.first->second)
211 return llvm::errorCodeToError(
212 SeenFileInsertResult.first->second.getError());
213 // Construct and return and FileEntryRef, unless it's a redirect to another
214 // filename.
215 SeenFileEntryOrRedirect Value = *SeenFileInsertResult.first->second;
216 FileEntry *FE;
217 if (LLVM_LIKELY(FE = Value.dyn_cast<FileEntry *>())__builtin_expect((bool)(FE = Value.dyn_cast<FileEntry *>
()), true)
)
218 return FileEntryRef(SeenFileInsertResult.first->first(), *FE);
219 return getFileRef(*Value.get<const StringRef *>(), openFile, CacheFailure);
220 }
221
222 // We've not seen this before. Fill it in.
223 ++NumFileCacheMisses;
224 auto *NamedFileEnt = &*SeenFileInsertResult.first;
225 assert(!NamedFileEnt->second && "should be newly-created")((!NamedFileEnt->second && "should be newly-created"
) ? static_cast<void> (0) : __assert_fail ("!NamedFileEnt->second && \"should be newly-created\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Basic/FileManager.cpp"
, 225, __PRETTY_FUNCTION__))
;
226
227 // Get the null-terminated file name as stored as the key of the
228 // SeenFileEntries map.
229 StringRef InterndFileName = NamedFileEnt->first();
230
231 // Look up the directory for the file. When looking up something like
232 // sys/foo.h we'll discover all of the search directories that have a 'sys'
233 // subdirectory. This will let us avoid having to waste time on known-to-fail
234 // searches when we go to find sys/bar.h, because all the search directories
235 // without a 'sys' subdir will get a cached failure result.
236 auto DirInfoOrErr = getDirectoryFromFile(*this, Filename, CacheFailure);
237 if (!DirInfoOrErr) { // Directory doesn't exist, file can't exist.
238 if (CacheFailure)
239 NamedFileEnt->second = DirInfoOrErr.getError();
240 else
241 SeenFileEntries.erase(Filename);
242
243 return llvm::errorCodeToError(DirInfoOrErr.getError());
244 }
245 const DirectoryEntry *DirInfo = *DirInfoOrErr;
246
247 // FIXME: Use the directory info to prune this, before doing the stat syscall.
248 // FIXME: This will reduce the # syscalls.
249
250 // Check to see if the file exists.
251 std::unique_ptr<llvm::vfs::File> F;
252 llvm::vfs::Status Status;
253 auto statError = getStatValue(InterndFileName, Status, true,
254 openFile ? &F : nullptr);
255 if (statError) {
256 // There's no real file at the given path.
257 if (CacheFailure)
258 NamedFileEnt->second = statError;
259 else
260 SeenFileEntries.erase(Filename);
261
262 return llvm::errorCodeToError(statError);
263 }
264
265 assert((openFile || !F) && "undesired open file")(((openFile || !F) && "undesired open file") ? static_cast
<void> (0) : __assert_fail ("(openFile || !F) && \"undesired open file\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Basic/FileManager.cpp"
, 265, __PRETTY_FUNCTION__))
;
266
267 // It exists. See if we have already opened a file with the same inode.
268 // This occurs when one dir is symlinked to another, for example.
269 FileEntry &UFE = UniqueRealFiles[Status.getUniqueID()];
270
271 NamedFileEnt->second = &UFE;
272
273 // If the name returned by getStatValue is different than Filename, re-intern
274 // the name.
275 if (Status.getName() != Filename) {
276 auto &NewNamedFileEnt =
277 *SeenFileEntries.insert({Status.getName(), &UFE}).first;
278 assert((*NewNamedFileEnt.second).get<FileEntry *>() == &UFE &&(((*NewNamedFileEnt.second).get<FileEntry *>() == &
UFE && "filename from getStatValue() refers to wrong file"
) ? static_cast<void> (0) : __assert_fail ("(*NewNamedFileEnt.second).get<FileEntry *>() == &UFE && \"filename from getStatValue() refers to wrong file\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Basic/FileManager.cpp"
, 279, __PRETTY_FUNCTION__))
279 "filename from getStatValue() refers to wrong file")(((*NewNamedFileEnt.second).get<FileEntry *>() == &
UFE && "filename from getStatValue() refers to wrong file"
) ? static_cast<void> (0) : __assert_fail ("(*NewNamedFileEnt.second).get<FileEntry *>() == &UFE && \"filename from getStatValue() refers to wrong file\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Basic/FileManager.cpp"
, 279, __PRETTY_FUNCTION__))
;
280 InterndFileName = NewNamedFileEnt.first().data();
281 // In addition to re-interning the name, construct a redirecting seen file
282 // entry, that will point to the name the filesystem actually wants to use.
283 StringRef *Redirect = new (CanonicalNameStorage) StringRef(InterndFileName);
284 auto SeenFileInsertResultIt = SeenFileEntries.find(Filename);
285 assert(SeenFileInsertResultIt != SeenFileEntries.end() &&((SeenFileInsertResultIt != SeenFileEntries.end() && "unexpected SeenFileEntries cache miss"
) ? static_cast<void> (0) : __assert_fail ("SeenFileInsertResultIt != SeenFileEntries.end() && \"unexpected SeenFileEntries cache miss\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Basic/FileManager.cpp"
, 286, __PRETTY_FUNCTION__))
286 "unexpected SeenFileEntries cache miss")((SeenFileInsertResultIt != SeenFileEntries.end() && "unexpected SeenFileEntries cache miss"
) ? static_cast<void> (0) : __assert_fail ("SeenFileInsertResultIt != SeenFileEntries.end() && \"unexpected SeenFileEntries cache miss\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Basic/FileManager.cpp"
, 286, __PRETTY_FUNCTION__))
;
287 SeenFileInsertResultIt->second = Redirect;
288 NamedFileEnt = &*SeenFileInsertResultIt;
Value stored to 'NamedFileEnt' is never read
289 }
290
291 if (UFE.isValid()) { // Already have an entry with this inode, return it.
292
293 // FIXME: this hack ensures that if we look up a file by a virtual path in
294 // the VFS that the getDir() will have the virtual path, even if we found
295 // the file by a 'real' path first. This is required in order to find a
296 // module's structure when its headers/module map are mapped in the VFS.
297 // We should remove this as soon as we can properly support a file having
298 // multiple names.
299 if (DirInfo != UFE.Dir && Status.IsVFSMapped)
300 UFE.Dir = DirInfo;
301
302 // Always update the name to use the last name by which a file was accessed.
303 // FIXME: Neither this nor always using the first name is correct; we want
304 // to switch towards a design where we return a FileName object that
305 // encapsulates both the name by which the file was accessed and the
306 // corresponding FileEntry.
307 // FIXME: The Name should be removed from FileEntry once all clients
308 // adopt FileEntryRef.
309 UFE.Name = InterndFileName;
310
311 return FileEntryRef(InterndFileName, UFE);
312 }
313
314 // Otherwise, we don't have this file yet, add it.
315 UFE.Name = InterndFileName;
316 UFE.Size = Status.getSize();
317 UFE.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
318 UFE.Dir = DirInfo;
319 UFE.UID = NextFileUID++;
320 UFE.UniqueID = Status.getUniqueID();
321 UFE.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
322 UFE.File = std::move(F);
323 UFE.IsValid = true;
324
325 if (UFE.File) {
326 if (auto PathName = UFE.File->getName())
327 fillRealPathName(&UFE, *PathName);
328 } else if (!openFile) {
329 // We should still fill the path even if we aren't opening the file.
330 fillRealPathName(&UFE, InterndFileName);
331 }
332 return FileEntryRef(InterndFileName, UFE);
333}
334
335const FileEntry *
336FileManager::getVirtualFile(StringRef Filename, off_t Size,
337 time_t ModificationTime) {
338 ++NumFileLookups;
339
340 // See if there is already an entry in the map for an existing file.
341 auto &NamedFileEnt = *SeenFileEntries.insert(
342 {Filename, std::errc::no_such_file_or_directory}).first;
343 if (NamedFileEnt.second) {
344 SeenFileEntryOrRedirect Value = *NamedFileEnt.second;
345 FileEntry *FE;
346 if (LLVM_LIKELY(FE = Value.dyn_cast<FileEntry *>())__builtin_expect((bool)(FE = Value.dyn_cast<FileEntry *>
()), true)
)
347 return FE;
348 return getVirtualFile(*Value.get<const StringRef *>(), Size,
349 ModificationTime);
350 }
351
352 // We've not seen this before, or the file is cached as non-existent.
353 ++NumFileCacheMisses;
354 addAncestorsAsVirtualDirs(Filename);
355 FileEntry *UFE = nullptr;
356
357 // Now that all ancestors of Filename are in the cache, the
358 // following call is guaranteed to find the DirectoryEntry from the
359 // cache.
360 auto DirInfo = getDirectoryFromFile(*this, Filename, /*CacheFailure=*/true);
361 assert(DirInfo &&((DirInfo && "The directory of a virtual file should already be in the cache."
) ? static_cast<void> (0) : __assert_fail ("DirInfo && \"The directory of a virtual file should already be in the cache.\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Basic/FileManager.cpp"
, 362, __PRETTY_FUNCTION__))
362 "The directory of a virtual file should already be in the cache.")((DirInfo && "The directory of a virtual file should already be in the cache."
) ? static_cast<void> (0) : __assert_fail ("DirInfo && \"The directory of a virtual file should already be in the cache.\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Basic/FileManager.cpp"
, 362, __PRETTY_FUNCTION__))
;
363
364 // Check to see if the file exists. If so, drop the virtual file
365 llvm::vfs::Status Status;
366 const char *InterndFileName = NamedFileEnt.first().data();
367 if (!getStatValue(InterndFileName, Status, true, nullptr)) {
368 UFE = &UniqueRealFiles[Status.getUniqueID()];
369 Status = llvm::vfs::Status(
370 Status.getName(), Status.getUniqueID(),
371 llvm::sys::toTimePoint(ModificationTime),
372 Status.getUser(), Status.getGroup(), Size,
373 Status.getType(), Status.getPermissions());
374
375 NamedFileEnt.second = UFE;
376
377 // If we had already opened this file, close it now so we don't
378 // leak the descriptor. We're not going to use the file
379 // descriptor anyway, since this is a virtual file.
380 if (UFE->File)
381 UFE->closeFile();
382
383 // If we already have an entry with this inode, return it.
384 if (UFE->isValid())
385 return UFE;
386
387 UFE->UniqueID = Status.getUniqueID();
388 UFE->IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
389 fillRealPathName(UFE, Status.getName());
390 } else {
391 VirtualFileEntries.push_back(std::make_unique<FileEntry>());
392 UFE = VirtualFileEntries.back().get();
393 NamedFileEnt.second = UFE;
394 }
395
396 UFE->Name = InterndFileName;
397 UFE->Size = Size;
398 UFE->ModTime = ModificationTime;
399 UFE->Dir = *DirInfo;
400 UFE->UID = NextFileUID++;
401 UFE->IsValid = true;
402 UFE->File.reset();
403 return UFE;
404}
405
406llvm::Optional<FileEntryRef> FileManager::getBypassFile(FileEntryRef VF) {
407 // Stat of the file and return nullptr if it doesn't exist.
408 llvm::vfs::Status Status;
409 if (getStatValue(VF.getName(), Status, /*isFile=*/true, /*F=*/nullptr))
410 return None;
411
412 // Fill it in from the stat.
413 BypassFileEntries.push_back(std::make_unique<FileEntry>());
414 const FileEntry &VFE = VF.getFileEntry();
415 FileEntry &BFE = *BypassFileEntries.back();
416 BFE.Name = VFE.getName();
417 BFE.Size = Status.getSize();
418 BFE.Dir = VFE.Dir;
419 BFE.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
420 BFE.UID = NextFileUID++;
421 BFE.IsValid = true;
422 return FileEntryRef(VF.getName(), BFE);
423}
424
425bool FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
426 StringRef pathRef(path.data(), path.size());
427
428 if (FileSystemOpts.WorkingDir.empty()
429 || llvm::sys::path::is_absolute(pathRef))
430 return false;
431
432 SmallString<128> NewPath(FileSystemOpts.WorkingDir);
433 llvm::sys::path::append(NewPath, pathRef);
434 path = NewPath;
435 return true;
436}
437
438bool FileManager::makeAbsolutePath(SmallVectorImpl<char> &Path) const {
439 bool Changed = FixupRelativePath(Path);
440
441 if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
442 FS->makeAbsolute(Path);
443 Changed = true;
444 }
445
446 return Changed;
447}
448
449void FileManager::fillRealPathName(FileEntry *UFE, llvm::StringRef FileName) {
450 llvm::SmallString<128> AbsPath(FileName);
451 // This is not the same as `VFS::getRealPath()`, which resolves symlinks
452 // but can be very expensive on real file systems.
453 // FIXME: the semantic of RealPathName is unclear, and the name might be
454 // misleading. We need to clean up the interface here.
455 makeAbsolutePath(AbsPath);
456 llvm::sys::path::remove_dots(AbsPath, /*remove_dot_dot=*/true);
457 UFE->RealPathName = AbsPath.str();
458}
459
460llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
461FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile) {
462 uint64_t FileSize = Entry->getSize();
463 // If there's a high enough chance that the file have changed since we
464 // got its size, force a stat before opening it.
465 if (isVolatile)
466 FileSize = -1;
467
468 StringRef Filename = Entry->getName();
469 // If the file is already open, use the open file descriptor.
470 if (Entry->File) {
471 auto Result =
472 Entry->File->getBuffer(Filename, FileSize,
473 /*RequiresNullTerminator=*/true, isVolatile);
474 Entry->closeFile();
475 return Result;
476 }
477
478 // Otherwise, open the file.
479 return getBufferForFileImpl(Filename, FileSize, isVolatile);
480}
481
482llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
483FileManager::getBufferForFileImpl(StringRef Filename, int64_t FileSize,
484 bool isVolatile) {
485 if (FileSystemOpts.WorkingDir.empty())
486 return FS->getBufferForFile(Filename, FileSize,
487 /*RequiresNullTerminator=*/true, isVolatile);
488
489 SmallString<128> FilePath(Filename);
490 FixupRelativePath(FilePath);
491 return FS->getBufferForFile(FilePath, FileSize,
492 /*RequiresNullTerminator=*/true, isVolatile);
493}
494
495/// getStatValue - Get the 'stat' information for the specified path,
496/// using the cache to accelerate it if possible. This returns true
497/// if the path points to a virtual file or does not exist, or returns
498/// false if it's an existent real file. If FileDescriptor is NULL,
499/// do directory look-up instead of file look-up.
500std::error_code
501FileManager::getStatValue(StringRef Path, llvm::vfs::Status &Status,
502 bool isFile, std::unique_ptr<llvm::vfs::File> *F) {
503 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
504 // absolute!
505 if (FileSystemOpts.WorkingDir.empty())
506 return FileSystemStatCache::get(Path, Status, isFile, F,
507 StatCache.get(), *FS);
508
509 SmallString<128> FilePath(Path);
510 FixupRelativePath(FilePath);
511
512 return FileSystemStatCache::get(FilePath.c_str(), Status, isFile, F,
513 StatCache.get(), *FS);
514}
515
516std::error_code
517FileManager::getNoncachedStatValue(StringRef Path,
518 llvm::vfs::Status &Result) {
519 SmallString<128> FilePath(Path);
520 FixupRelativePath(FilePath);
521
522 llvm::ErrorOr<llvm::vfs::Status> S = FS->status(FilePath.c_str());
523 if (!S)
524 return S.getError();
525 Result = *S;
526 return std::error_code();
527}
528
529// For GNU Hurd
530#if defined(__GNU__) && !defined(PATH_MAX4096)
531# define PATH_MAX4096 4096
532#endif
533
534
535void FileManager::GetUniqueIDMapping(
536 SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
537 UIDToFiles.clear();
538 UIDToFiles.resize(NextFileUID);
539
540 // Map file entries
541 for (llvm::StringMap<llvm::ErrorOr<SeenFileEntryOrRedirect>,
542 llvm::BumpPtrAllocator>::const_iterator
543 FE = SeenFileEntries.begin(),
544 FEEnd = SeenFileEntries.end();
545 FE != FEEnd; ++FE)
546 if (llvm::ErrorOr<SeenFileEntryOrRedirect> Entry = FE->getValue()) {
547 if (const auto *FE = (*Entry).dyn_cast<FileEntry *>())
548 UIDToFiles[FE->getUID()] = FE;
549 }
550
551 // Map virtual file entries
552 for (const auto &VFE : VirtualFileEntries)
553 UIDToFiles[VFE->getUID()] = VFE.get();
554}
555
556StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
557 // FIXME: use llvm::sys::fs::canonical() when it gets implemented
558 llvm::DenseMap<const DirectoryEntry *, llvm::StringRef>::iterator Known
559 = CanonicalDirNames.find(Dir);
560 if (Known != CanonicalDirNames.end())
561 return Known->second;
562
563 StringRef CanonicalName(Dir->getName());
564
565 SmallString<4096> CanonicalNameBuf;
566 if (!FS->getRealPath(Dir->getName(), CanonicalNameBuf))
567 CanonicalName = StringRef(CanonicalNameBuf).copy(CanonicalNameStorage);
568
569 CanonicalDirNames.insert({Dir, CanonicalName});
570 return CanonicalName;
571}
572
573void FileManager::PrintStats() const {
574 llvm::errs() << "\n*** File Manager Stats:\n";
575 llvm::errs() << UniqueRealFiles.size() << " real files found, "
576 << UniqueRealDirs.size() << " real dirs found.\n";
577 llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
578 << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
579 llvm::errs() << NumDirLookups << " dir lookups, "
580 << NumDirCacheMisses << " dir cache misses.\n";
581 llvm::errs() << NumFileLookups << " file lookups, "
582 << NumFileCacheMisses << " file cache misses.\n";
583
584 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
585}