blob: b2d43e474479b1e458c610f486cc762d20660e6d [file] [log] [blame]
Chris Lattner10e286a2010-11-23 19:19:34 +00001//===--- FileSystemStatCache.cpp - Caching for 'stat' calls ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the FileSystemStatCache interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/FileSystemStatCache.h"
15#include "llvm/System/Path.h"
16using namespace clang;
17
Chris Lattner72f61302010-11-23 20:07:39 +000018#if defined(_MSC_VER)
19#define S_ISDIR(s) (_S_IFDIR & s)
20#endif
21
Chris Lattner898a0612010-11-23 21:17:56 +000022/// FileSystemStatCache::get - Get the 'stat' information for the specified
23/// path, using the cache to accellerate it if possible. This returns true if
24/// the path does not exist or false if it exists.
25///
26/// If FileDescriptor is non-null, then this lookup should only return success
27/// for files (not directories). If it is null this lookup should only return
28/// success for directories (not files). On a successful file lookup, the
29/// implementation can optionally fill in FileDescriptor with a valid
30/// descriptor and the client guarantees that it will close it.
31bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf,
32 int *FileDescriptor, FileSystemStatCache *Cache) {
33 LookupResult R;
34
35 if (Cache)
36 R = Cache->getStat(Path, StatBuf, FileDescriptor);
37 else
38 R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists;
39
40 if (R == CacheMissing) return true;
41
42 bool isForDir = FileDescriptor == 0;
43 return S_ISDIR(StatBuf.st_mode) != isForDir;
44}
45
46
Chris Lattner10e286a2010-11-23 19:19:34 +000047MemorizeStatCalls::LookupResult
Chris Lattner898a0612010-11-23 21:17:56 +000048MemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf,
49 int *FileDescriptor) {
50 LookupResult Result = statChained(Path, StatBuf, FileDescriptor);
Chris Lattner10e286a2010-11-23 19:19:34 +000051
Chris Lattner10e286a2010-11-23 19:19:34 +000052 // Do not cache failed stats, it is easy to construct common inconsistent
53 // situations if we do, and they are not important for PCH performance (which
54 // currently only needs the stats to construct the initial FileManager
55 // entries).
Chris Lattnerd6f61112010-11-23 20:05:15 +000056 if (Result == CacheMissing)
Chris Lattner10e286a2010-11-23 19:19:34 +000057 return Result;
58
59 // Cache file 'stat' results and directories with absolutely paths.
60 if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::Path(Path).isAbsolute())
Chris Lattner74e976b2010-11-23 19:28:12 +000061 StatCalls[Path] = StatBuf;
Chris Lattner10e286a2010-11-23 19:19:34 +000062
63 return Result;
64}