summaryrefslogtreecommitdiffstats
path: root/lib/Lex/HeaderMap.cpp
diff options
context:
space:
mode:
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>2016-02-23 00:48:16 +0000
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>2016-02-23 00:48:16 +0000
commit6d0c65dab43962dff862d8263333feb385d71c96 (patch)
tree8081ae683d8640f52704ca3d44b742c0be1ada71 /lib/Lex/HeaderMap.cpp
parent006186b25e2e8fc902211cc08bea285c38cc42f2 (diff)
Lex: Return "" when HeaderMap::lookupFilename fails
Change getString() to return Optional<StringRef>, and change lookupFilename() to return an empty string if either one of the prefix and suffix can't be found. This is a more robust follow-up to r261461, but it's still not entirely satisfactory. Ideally we'd report that the header map is corrupt; perhaps something for a follow-up. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@261596 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Lex/HeaderMap.cpp')
-rw-r--r--lib/Lex/HeaderMap.cpp37
1 files changed, 24 insertions, 13 deletions
diff --git a/lib/Lex/HeaderMap.cpp b/lib/Lex/HeaderMap.cpp
index be0d477e8b..4cace5b002 100644
--- a/lib/Lex/HeaderMap.cpp
+++ b/lib/Lex/HeaderMap.cpp
@@ -16,6 +16,7 @@
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/FileManager.h"
#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
@@ -144,15 +145,13 @@ HMapBucket HeaderMapImpl::getBucket(unsigned BucketNo) const {
return Result;
}
-/// getString - Look up the specified string in the string table. If the string
-/// index is not valid, it returns an empty string.
-StringRef HeaderMapImpl::getString(unsigned StrTabIdx) const {
+Optional<StringRef> HeaderMapImpl::getString(unsigned StrTabIdx) const {
// Add the start of the string table to the idx.
StrTabIdx += getEndianAdjustedWord(getHeader().StringsOffset);
// Check for invalid index.
if (StrTabIdx >= FileBuffer->getBufferSize())
- return "";
+ return None;
const char *Data = FileBuffer->getBufferStart() + StrTabIdx;
unsigned MaxLen = FileBuffer->getBufferSize() - StrTabIdx;
@@ -160,7 +159,7 @@ StringRef HeaderMapImpl::getString(unsigned StrTabIdx) const {
// Check whether the buffer is null-terminated.
if (Len == MaxLen && Data[Len - 1])
- return "";
+ return None;
return StringRef(Data, Len);
}
@@ -177,13 +176,19 @@ LLVM_DUMP_METHOD void HeaderMapImpl::dump() const {
llvm::dbgs() << "Header Map " << getFileName() << ":\n " << NumBuckets
<< ", " << getEndianAdjustedWord(Hdr.NumEntries) << "\n";
+ auto getStringOrInvalid = [this](unsigned Id) -> StringRef {
+ if (Optional<StringRef> S = getString(Id))
+ return *S;
+ return "<invalid>";
+ };
+
for (unsigned i = 0; i != NumBuckets; ++i) {
HMapBucket B = getBucket(i);
if (B.Key == HMAP_EmptyBucketKey) continue;
- StringRef Key = getString(B.Key);
- StringRef Prefix = getString(B.Prefix);
- StringRef Suffix = getString(B.Suffix);
+ StringRef Key = getStringOrInvalid(B.Key);
+ StringRef Prefix = getStringOrInvalid(B.Prefix);
+ StringRef Suffix = getStringOrInvalid(B.Suffix);
llvm::dbgs() << " " << i << ". " << Key << " -> '" << Prefix << "' '"
<< Suffix << "'\n";
}
@@ -216,16 +221,22 @@ StringRef HeaderMapImpl::lookupFilename(StringRef Filename,
if (B.Key == HMAP_EmptyBucketKey) return StringRef(); // Hash miss.
// See if the key matches. If not, probe on.
- if (!Filename.equals_lower(getString(B.Key)))
+ Optional<StringRef> Key = getString(B.Key);
+ if (LLVM_UNLIKELY(!Key))
+ continue;
+ if (!Filename.equals_lower(*Key))
continue;
// If so, we have a match in the hash table. Construct the destination
// path.
- StringRef Prefix = getString(B.Prefix);
- StringRef Suffix = getString(B.Suffix);
+ Optional<StringRef> Prefix = getString(B.Prefix);
+ Optional<StringRef> Suffix = getString(B.Suffix);
+
DestPath.clear();
- DestPath.append(Prefix.begin(), Prefix.end());
- DestPath.append(Suffix.begin(), Suffix.end());
+ if (LLVM_LIKELY(Prefix && Suffix)) {
+ DestPath.append(Prefix->begin(), Prefix->end());
+ DestPath.append(Suffix->begin(), Suffix->end());
+ }
return StringRef(DestPath.begin(), DestPath.size());
}
}