1 //===--- 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"
16 using namespace clang;
17 
18 MemorizeStatCalls::LookupResult
19 MemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf) {
20   LookupResult Result = statChained(Path, StatBuf);
21 
22   // If the chained cache didn't know anything about the file, do the stat now
23   // so we can record the result.
24   if (Result == CacheMiss)
25     Result = ::stat(Path, &StatBuf) ? CacheHitMissing : CacheHitExists;
26 
27 
28   // Do not cache failed stats, it is easy to construct common inconsistent
29   // situations if we do, and they are not important for PCH performance (which
30   // currently only needs the stats to construct the initial FileManager
31   // entries).
32   if (Result == CacheHitMissing)
33     return Result;
34 
35   // Cache file 'stat' results and directories with absolutely paths.
36   if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::Path(Path).isAbsolute())
37     StatCalls[Path] = StatResult(Result, StatBuf);
38 
39   return Result;
40 }
41