Skip to content

Commit 4876977

Browse files
committed
[Lex] Make HeaderMaps a unique_ptr vector
Summary: unique_ptr makes the ownership clearer than a raw pointer container. Reviewers: Eugene.Zelenko, dblaikie Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D50945 llvm-svn: 340198
1 parent 2a08285 commit 4876977

File tree

4 files changed

+12
-18
lines changed

4 files changed

+12
-18
lines changed

clang/include/clang/Lex/HeaderMap.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ class HeaderMap : private HeaderMapImpl {
7171
public:
7272
/// This attempts to load the specified file as a header map. If it doesn't
7373
/// look like a HeaderMap, it gives up and returns null.
74-
static const HeaderMap *Create(const FileEntry *FE, FileManager &FM);
74+
static std::unique_ptr<HeaderMap> Create(const FileEntry *FE,
75+
FileManager &FM);
7576

7677
/// Check to see if the specified relative filename is located in this
7778
/// HeaderMap. If so, open it and return its FileEntry. If RawPath is not

clang/include/clang/Lex/HeaderSearch.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/Basic/SourceLocation.h"
1818
#include "clang/Basic/SourceManager.h"
1919
#include "clang/Lex/DirectoryLookup.h"
20+
#include "clang/Lex/HeaderMap.h"
2021
#include "clang/Lex/ModuleMap.h"
2122
#include "llvm/ADT/ArrayRef.h"
2223
#include "llvm/ADT/DenseMap.h"
@@ -38,7 +39,6 @@ class DirectoryEntry;
3839
class ExternalPreprocessorSource;
3940
class FileEntry;
4041
class FileManager;
41-
class HeaderMap;
4242
class HeaderSearchOptions;
4343
class IdentifierInfo;
4444
class LangOptions;
@@ -226,9 +226,8 @@ class HeaderSearch {
226226
llvm::StringMap<std::string, llvm::BumpPtrAllocator>;
227227
std::unique_ptr<IncludeAliasMap> IncludeAliases;
228228

229-
/// This is a mapping from FileEntry -> HeaderMap, uniquing
230-
/// headermaps. This vector owns the headermap.
231-
std::vector<std::pair<const FileEntry *, const HeaderMap *>> HeaderMaps;
229+
/// This is a mapping from FileEntry -> HeaderMap, uniquing headermaps.
230+
std::vector<std::pair<const FileEntry *, std::unique_ptr<HeaderMap>>> HeaderMaps;
232231

233232
/// The mapping between modules and headers.
234233
mutable ModuleMap ModMap;
@@ -264,7 +263,6 @@ class HeaderSearch {
264263
const LangOptions &LangOpts, const TargetInfo *Target);
265264
HeaderSearch(const HeaderSearch &) = delete;
266265
HeaderSearch &operator=(const HeaderSearch &) = delete;
267-
~HeaderSearch();
268266

269267
/// Retrieve the header-search options with which this header search
270268
/// was initialized.

clang/lib/Lex/HeaderMap.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ static inline unsigned HashHMapKey(StringRef Str) {
4848
/// map. If it doesn't look like a HeaderMap, it gives up and returns null.
4949
/// If it looks like a HeaderMap but is obviously corrupted, it puts a reason
5050
/// into the string error argument and returns null.
51-
const HeaderMap *HeaderMap::Create(const FileEntry *FE, FileManager &FM) {
51+
std::unique_ptr<HeaderMap> HeaderMap::Create(const FileEntry *FE,
52+
FileManager &FM) {
5253
// If the file is too small to be a header map, ignore it.
5354
unsigned FileSize = FE->getSize();
5455
if (FileSize <= sizeof(HMapHeader)) return nullptr;
@@ -59,7 +60,7 @@ const HeaderMap *HeaderMap::Create(const FileEntry *FE, FileManager &FM) {
5960
bool NeedsByteSwap;
6061
if (!checkHeader(**FileBuffer, NeedsByteSwap))
6162
return nullptr;
62-
return new HeaderMap(std::move(*FileBuffer), NeedsByteSwap);
63+
return std::unique_ptr<HeaderMap>(new HeaderMap(std::move(*FileBuffer), NeedsByteSwap));
6364
}
6465

6566
bool HeaderMapImpl::checkHeader(const llvm::MemoryBuffer &File,

clang/lib/Lex/HeaderSearch.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,6 @@ HeaderSearch::HeaderSearch(std::shared_ptr<HeaderSearchOptions> HSOpts,
7575
FileMgr(SourceMgr.getFileManager()), FrameworkMap(64),
7676
ModMap(SourceMgr, Diags, LangOpts, Target, *this) {}
7777

78-
HeaderSearch::~HeaderSearch() {
79-
// Delete headermaps.
80-
for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
81-
delete HeaderMaps[i].second;
82-
}
83-
8478
void HeaderSearch::PrintStats() {
8579
fprintf(stderr, "\n*** HeaderSearch Stats:\n");
8680
fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
@@ -113,12 +107,12 @@ const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
113107
// Pointer equality comparison of FileEntries works because they are
114108
// already uniqued by inode.
115109
if (HeaderMaps[i].first == FE)
116-
return HeaderMaps[i].second;
110+
return HeaderMaps[i].second.get();
117111
}
118112

119-
if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
120-
HeaderMaps.push_back(std::make_pair(FE, HM));
121-
return HM;
113+
if (std::unique_ptr<HeaderMap> HM = HeaderMap::Create(FE, FileMgr)) {
114+
HeaderMaps.emplace_back(FE, std::move(HM));
115+
return HeaderMaps.back().second.get();
122116
}
123117

124118
return nullptr;

0 commit comments

Comments
 (0)