diff options
author | Dmitry Polukhin <dmitry.polukhin@gmail.com> | 2021-05-27 01:20:35 -0700 |
---|---|---|
committer | Dmitry Polukhin <dmitry.polukhin@gmail.com> | 2021-06-03 01:37:55 -0700 |
commit | 178ad93e3f1f2381f05baea300873ee5998ac288 (patch) | |
tree | 8a4a842ca478dff178b03ebeb53afbfb32929f30 /clang/lib/Lex/HeaderMap.cpp | |
parent | e237168341ed9688ee70277465a488b6f4b8ba03 (diff) | |
download | llvm-178ad93e3f1f2381f05baea300873ee5998ac288.zip llvm-178ad93e3f1f2381f05baea300873ee5998ac288.tar.gz llvm-178ad93e3f1f2381f05baea300873ee5998ac288.tar.bz2 |
[clang][clangd] Use reverse header map lookup in suggestPathToFileForDiagnostics
Summary:
suggestPathToFileForDiagnostics is actively used in clangd for converting
an absolute path to a header file to a header name as it should be spelled
in the sources. Current approach converts absolute path to relative path.
This diff implements missing logic that makes a reverse lookup from the
relative path to the key in the header map that should be used in the sources.
Prerequisite diff: https://reviews.llvm.org/D103229
Test Plan: check-clang
Reviewers: dexonsmith, bruno, rsmith
Subscribers: cfe-commits
Tasks:
Tags: #clang
Differential Revision: https://reviews.llvm.org/D103142
Diffstat (limited to 'clang/lib/Lex/HeaderMap.cpp')
-rw-r--r-- | clang/lib/Lex/HeaderMap.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/clang/lib/Lex/HeaderMap.cpp b/clang/lib/Lex/HeaderMap.cpp index d44ef29c..4b60cfa 100644 --- a/clang/lib/Lex/HeaderMap.cpp +++ b/clang/lib/Lex/HeaderMap.cpp @@ -240,3 +240,32 @@ StringRef HeaderMapImpl::lookupFilename(StringRef Filename, return StringRef(DestPath.begin(), DestPath.size()); } } + +StringRef HeaderMapImpl::reverseLookupFilename(StringRef DestPath) const { + if (!ReverseMap.empty()) + return ReverseMap.lookup(DestPath); + + const HMapHeader &Hdr = getHeader(); + unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets); + StringRef RetKey; + for (unsigned i = 0; i != NumBuckets; ++i) { + HMapBucket B = getBucket(i); + if (B.Key == HMAP_EmptyBucketKey) + continue; + + Optional<StringRef> Key = getString(B.Key); + Optional<StringRef> Prefix = getString(B.Prefix); + Optional<StringRef> Suffix = getString(B.Suffix); + if (LLVM_LIKELY(Key && Prefix && Suffix)) { + SmallVector<char, 1024> Buf; + Buf.append(Prefix->begin(), Prefix->end()); + Buf.append(Suffix->begin(), Suffix->end()); + StringRef Value(Buf.begin(), Buf.size()); + ReverseMap[Value] = *Key; + + if (DestPath == Value) + RetKey = *Key; + } + } + return RetKey; +} |