[clang-tools-extra] r342505 - [clangd] Fix error handling for SymbolID parsing (notably YAML and dexp)

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 18 12:01:00 PDT 2018


Author: sammccall
Date: Tue Sep 18 12:00:59 2018
New Revision: 342505

URL: http://llvm.org/viewvc/llvm-project?rev=342505&view=rev
Log:
[clangd] Fix error handling for SymbolID parsing (notably YAML and dexp)

Modified:
    clang-tools-extra/trunk/clangd/index/Index.cpp
    clang-tools-extra/trunk/clangd/index/Index.h
    clang-tools-extra/trunk/clangd/index/SymbolYAML.cpp
    clang-tools-extra/trunk/clangd/index/dex/dexp/Dexp.cpp
    clang-tools-extra/trunk/clangd/indexer/IndexerMain.cpp

Modified: clang-tools-extra/trunk/clangd/index/Index.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/index/Index.cpp?rev=342505&r1=342504&r2=342505&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/index/Index.cpp (original)
+++ clang-tools-extra/trunk/clangd/index/Index.cpp Tue Sep 18 12:00:59 2018
@@ -41,8 +41,13 @@ SymbolID SymbolID::fromRaw(llvm::StringR
 
 std::string SymbolID::str() const { return toHex(raw()); }
 
-void operator>>(StringRef Str, SymbolID &ID) {
-  ID = SymbolID::fromRaw(fromHex(Str));
+llvm::Expected<SymbolID> 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) {

Modified: clang-tools-extra/trunk/clangd/index/Index.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/index/Index.h?rev=342505&r1=342504&r2=342505&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/index/Index.h (original)
+++ clang-tools-extra/trunk/clangd/index/Index.h Tue Sep 18 12:00:59 2018
@@ -91,12 +91,12 @@ public:
     return StringRef(reinterpret_cast<const char *>(HashValue.data()), RawSize);
   }
   static SymbolID fromRaw(llvm::StringRef);
+
   // Returns a 40-bytes hex encoded string.
   std::string str() const;
+  static llvm::Expected<SymbolID> fromStr(llvm::StringRef);
 
 private:
-  friend void operator>>(llvm::StringRef Str, SymbolID &ID);
-
   std::array<uint8_t, RawSize> HashValue;
 };
 
@@ -108,15 +108,9 @@ inline llvm::hash_code hash_value(const
   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 {

Modified: clang-tools-extra/trunk/clangd/index/SymbolYAML.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/index/SymbolYAML.cpp?rev=342505&r1=342504&r2=342505&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/index/SymbolYAML.cpp (original)
+++ clang-tools-extra/trunk/clangd/index/SymbolYAML.cpp Tue Sep 18 12:00:59 2018
@@ -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;

Modified: clang-tools-extra/trunk/clangd/index/dex/dexp/Dexp.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/index/dex/dexp/Dexp.cpp?rev=342505&r1=342504&r2=342505&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/index/dex/dexp/Dexp.cpp (original)
+++ clang-tools-extra/trunk/clangd/index/dex/dexp/Dexp.cpp Tue Sep 18 12:00:59 2018
@@ -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;

Modified: clang-tools-extra/trunk/clangd/indexer/IndexerMain.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/indexer/IndexerMain.cpp?rev=342505&r1=342504&r2=342505&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/indexer/IndexerMain.cpp (original)
+++ clang-tools-extra/trunk/clangd/indexer/IndexerMain.cpp Tue Sep 18 12:00:59 2018
@@ -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




More information about the cfe-commits mailing list