From 46b55558445119d6f2209ec6aa570991c89d8424 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Tue, 18 Sep 2018 19:00:59 +0000 Subject: [PATCH] [clangd] Fix error handling for SymbolID parsing (notably YAML and dexp) llvm-svn: 342505 --- clang-tools-extra/clangd/index/Index.cpp | 9 +++++++-- clang-tools-extra/clangd/index/Index.h | 12 +++--------- clang-tools-extra/clangd/index/SymbolYAML.cpp | 11 +++++++---- clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp | 8 ++++---- clang-tools-extra/clangd/indexer/IndexerMain.cpp | 3 +-- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/clang-tools-extra/clangd/index/Index.cpp b/clang-tools-extra/clangd/index/Index.cpp index cfc49406c1e3..dec95fcf9104 100644 --- a/clang-tools-extra/clangd/index/Index.cpp +++ b/clang-tools-extra/clangd/index/Index.cpp @@ -41,8 +41,13 @@ SymbolID SymbolID::fromRaw(llvm::StringRef Raw) { std::string SymbolID::str() const { return toHex(raw()); } -void operator>>(StringRef Str, SymbolID &ID) { - ID = SymbolID::fromRaw(fromHex(Str)); +llvm::Expected SymbolID::fromStr(llvm::StringRef Str) { + if (Str.size() != RawSize * 2) + return createStringError(llvm::inconvertibleErrorCode(), "Bad ID length"); + for (char C : Str) + if (!isHexDigit(C)) + return createStringError(llvm::inconvertibleErrorCode(), "Bad hex ID"); + return fromRaw(fromHex(Str)); } raw_ostream &operator<<(raw_ostream &OS, SymbolOrigin O) { diff --git a/clang-tools-extra/clangd/index/Index.h b/clang-tools-extra/clangd/index/Index.h index 8a18c619781a..746638229af0 100644 --- a/clang-tools-extra/clangd/index/Index.h +++ b/clang-tools-extra/clangd/index/Index.h @@ -91,12 +91,12 @@ public: return StringRef(reinterpret_cast(HashValue.data()), RawSize); } static SymbolID fromRaw(llvm::StringRef); + // Returns a 40-bytes hex encoded string. std::string str() const; + static llvm::Expected fromStr(llvm::StringRef); private: - friend void operator>>(llvm::StringRef Str, SymbolID &ID); - std::array HashValue; }; @@ -108,15 +108,9 @@ inline llvm::hash_code hash_value(const SymbolID &ID) { return llvm::hash_code(Result); } -// Write SymbolID into the given stream. SymbolID is encoded as a 40-bytes -// hex string. +// Write SymbolID into the given stream. SymbolID is encoded as ID.str(). llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolID &ID); -// Construct SymbolID from a hex string. -// The HexStr is required to be a 40-bytes hex string, which is encoded from the -// "<<" operator. -void operator>>(llvm::StringRef HexStr, SymbolID &ID); - } // namespace clangd } // namespace clang namespace llvm { diff --git a/clang-tools-extra/clangd/index/SymbolYAML.cpp b/clang-tools-extra/clangd/index/SymbolYAML.cpp index 01c7987ac7f3..4dec54782a22 100644 --- a/clang-tools-extra/clangd/index/SymbolYAML.cpp +++ b/clang-tools-extra/clangd/index/SymbolYAML.cpp @@ -40,10 +40,13 @@ struct NormalizedSymbolID { OS << ID; } - SymbolID denormalize(IO &) { - SymbolID ID; - HexString >> ID; - return ID; + SymbolID denormalize(IO &I) { + auto ID = SymbolID::fromStr(HexString); + if (!ID) { + I.setError(llvm::toString(ID.takeError())); + return SymbolID(); + } + return *ID; } std::string HexString; diff --git a/clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp b/clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp index 09c310ff5753..afaace3ac41e 100644 --- a/clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp +++ b/clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp @@ -144,14 +144,14 @@ class Lookup : public Command { }; void run() override { - auto Raw = fromHex(ID); - if (Raw.size() != clang::clangd::SymbolID::RawSize) { - llvm::outs() << "invalid SymbolID\n"; + auto SID = clang::clangd::SymbolID::fromStr(ID); + if (!SID) { + llvm::outs() << llvm::toString(SID.takeError()) << "\n"; return; } clang::clangd::LookupRequest Request; - Request.IDs = {clang::clangd::SymbolID::fromRaw(Raw)}; + Request.IDs = {*SID}; bool FoundSymbol = false; Index->lookup(Request, [&](const Symbol &Sym) { FoundSymbol = true; diff --git a/clang-tools-extra/clangd/indexer/IndexerMain.cpp b/clang-tools-extra/clangd/indexer/IndexerMain.cpp index fc85c54e2270..24551bf61a82 100644 --- a/clang-tools-extra/clangd/indexer/IndexerMain.cpp +++ b/clang-tools-extra/clangd/indexer/IndexerMain.cpp @@ -172,8 +172,7 @@ public: [&](llvm::StringRef Key, llvm::StringRef Value) { llvm::yaml::Input Yin(Value); auto Sym = clang::clangd::SymbolFromYAML(Yin); - clang::clangd::SymbolID ID; - Key >> ID; + auto ID = cantFail(clang::clangd::SymbolID::fromStr(Key)); if (const auto *Existing = UniqueSymbols.find(ID)) UniqueSymbols.insert(mergeSymbol(*Existing, Sym)); else -- GitLab