summaryrefslogtreecommitdiffstats
path: root/clang/lib/Basic
diff options
context:
space:
mode:
authorDavid Blaikie <dblaikie@gmail.com>2014-07-08 15:46:02 +0000
committerDavid Blaikie <dblaikie@gmail.com>2014-07-08 15:46:02 +0000
commit326ffb3683fa5198a9408e8c201149a7dde55fe9 (patch)
tree0a329cc6630428626c1ecfc73072e2b244e7cdea /clang/lib/Basic
parentee5872187571906c4430e181184142112742468c (diff)
downloadbcm5719-llvm-326ffb3683fa5198a9408e8c201149a7dde55fe9.tar.gz
bcm5719-llvm-326ffb3683fa5198a9408e8c201149a7dde55fe9.zip
Improve memory ownership of vfs::Files in the FileSystemStatCache by using std::unique_ptr
Spotted after a memory leak (due to the complexities of manual memory management) was fixed in 212466. llvm-svn: 212541
Diffstat (limited to 'clang/lib/Basic')
-rw-r--r--clang/lib/Basic/FileManager.cpp10
-rw-r--r--clang/lib/Basic/FileSystemStatCache.cpp13
2 files changed, 8 insertions, 15 deletions
diff --git a/clang/lib/Basic/FileManager.cpp b/clang/lib/Basic/FileManager.cpp
index 1faa370e82b..22beed7dcce 100644
--- a/clang/lib/Basic/FileManager.cpp
+++ b/clang/lib/Basic/FileManager.cpp
@@ -253,7 +253,7 @@ const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
// FIXME: This will reduce the # syscalls.
// Nope, there isn't. Check to see if the file exists.
- vfs::File *F = nullptr;
+ std::unique_ptr<vfs::File> F;
FileData Data;
if (getStatValue(InterndFileName, Data, true, openFile ? &F : nullptr)) {
// There's no real file at the given path.
@@ -281,10 +281,6 @@ const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
if (DirInfo != UFE.Dir && Data.IsVFSMapped)
UFE.Dir = DirInfo;
- // If the stat process opened the file, close it to avoid a FD leak.
- if (F)
- delete F;
-
return &UFE;
}
@@ -297,7 +293,7 @@ const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
UFE.UniqueID = Data.UniqueID;
UFE.IsNamedPipe = Data.IsNamedPipe;
UFE.InPCH = Data.InPCH;
- UFE.File.reset(F);
+ UFE.File = std::move(F);
UFE.IsValid = true;
return &UFE;
}
@@ -453,7 +449,7 @@ getBufferForFile(StringRef Filename, std::string *ErrorStr) {
/// false if it's an existent real file. If FileDescriptor is NULL,
/// do directory look-up instead of file look-up.
bool FileManager::getStatValue(const char *Path, FileData &Data, bool isFile,
- vfs::File **F) {
+ std::unique_ptr<vfs::File> *F) {
// FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
// absolute!
if (FileSystemOpts.WorkingDir.empty())
diff --git a/clang/lib/Basic/FileSystemStatCache.cpp b/clang/lib/Basic/FileSystemStatCache.cpp
index 4952ef49ef8..7515cfb440a 100644
--- a/clang/lib/Basic/FileSystemStatCache.cpp
+++ b/clang/lib/Basic/FileSystemStatCache.cpp
@@ -52,8 +52,8 @@ static void copyStatusToFileData(const vfs::Status &Status,
/// implementation can optionally fill in FileDescriptor with a valid
/// descriptor and the client guarantees that it will close it.
bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile,
- vfs::File **F, FileSystemStatCache *Cache,
- vfs::FileSystem &FS) {
+ std::unique_ptr<vfs::File> *F,
+ FileSystemStatCache *Cache, vfs::FileSystem &FS) {
LookupResult R;
bool isForDir = !isFile;
@@ -92,7 +92,7 @@ bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile,
if (Status) {
R = CacheExists;
copyStatusToFileData(*Status, Data);
- *F = OwnedFile.release();
+ *F = std::move(OwnedFile);
} else {
// fstat rarely fails. If it does, claim the initial open didn't
// succeed.
@@ -109,11 +109,8 @@ bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile,
// demands.
if (Data.IsDirectory != isForDir) {
// If not, close the file if opened.
- if (F && *F) {
- (*F)->close();
- delete *F;
+ if (F)
*F = nullptr;
- }
return true;
}
@@ -123,7 +120,7 @@ bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile,
MemorizeStatCalls::LookupResult
MemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile,
- vfs::File **F, vfs::FileSystem &FS) {
+ std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) {
LookupResult Result = statChained(Path, Data, isFile, F, FS);
// Do not cache failed stats, it is easy to construct common inconsistent
OpenPOWER on IntegriCloud