[clang-tools-extra] r350660 - [Query] NFC: Port QueryParser to StringRef

Stephen Kelly via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 8 14:27:08 PST 2019


Author: steveire
Date: Tue Jan  8 14:27:08 2019
New Revision: 350660

URL: http://llvm.org/viewvc/llvm-project?rev=350660&view=rev
Log:
[Query] NFC: Port QueryParser to StringRef

Summary:
There is no reason for it to not be a StringRef.  Making it one
simplifies existing code, and makes follow-up features easier.

Reviewers: aaron.ballman

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D56415

Modified:
    clang-tools-extra/trunk/clang-query/QueryParser.cpp
    clang-tools-extra/trunk/clang-query/QueryParser.h

Modified: clang-tools-extra/trunk/clang-query/QueryParser.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-query/QueryParser.cpp?rev=350660&r1=350659&r2=350660&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-query/QueryParser.cpp (original)
+++ clang-tools-extra/trunk/clang-query/QueryParser.cpp Tue Jan  8 14:27:08 2019
@@ -27,29 +27,22 @@ namespace query {
 // is found before End, return StringRef().  Begin is adjusted to exclude the
 // lexed region.
 StringRef QueryParser::lexWord() {
-  while (true) {
-    if (Begin == End)
-      return StringRef(Begin, 0);
+  Line = Line.ltrim();
 
-    if (!isWhitespace(*Begin))
-      break;
+  if (Line.empty())
+    // Even though the Line is empty, it contains a pointer and
+    // a (zero) length. The pointer is used in the LexOrCompleteWord
+    // code completion.
+    return Line;
 
-    ++Begin;
-  }
-
-  if (*Begin == '#') {
-    End = Begin;
+  if (Line.front() == '#') {
+    Line = {};
     return StringRef();
   }
 
-  const char *WordBegin = Begin;
-
-  while (true) {
-    ++Begin;
-
-    if (Begin == End || isWhitespace(*Begin))
-      return StringRef(WordBegin, Begin - WordBegin);
-  }
+  StringRef Word = Line.take_until(isWhitespace);
+  Line = Line.drop_front(Word.size());
+  return Word;
 }
 
 // This is the StringSwitch-alike used by lexOrCompleteWord below. See that
@@ -133,10 +126,9 @@ template <typename QueryType> QueryRef Q
 }
 
 QueryRef QueryParser::endQuery(QueryRef Q) {
-  const char *Extra = Begin;
+  const StringRef Extra = Line;
   if (!lexWord().empty())
-    return new InvalidQuery("unexpected extra input: '" +
-                            StringRef(Extra, End - Extra) + "'");
+    return new InvalidQuery("unexpected extra input: '" + Extra + "'");
   return Q;
 }
 
@@ -174,8 +166,7 @@ QueryRef makeInvalidQueryFromDiagnostics
 
 QueryRef QueryParser::completeMatcherExpression() {
   std::vector<MatcherCompletion> Comps = Parser::completeExpression(
-      StringRef(Begin, End - Begin), CompletionPos - Begin, nullptr,
-      &QS.NamedValues);
+      Line, CompletionPos - Line.begin(), nullptr, &QS.NamedValues);
   for (auto I = Comps.begin(), E = Comps.end(); I != E; ++I) {
     Completions.push_back(LineEditor::Completion(I->TypedText, I->MatcherDecl));
   }
@@ -222,8 +213,8 @@ QueryRef QueryParser::doParse() {
 
     Diagnostics Diag;
     ast_matchers::dynamic::VariantValue Value;
-    if (!Parser::parseExpression(StringRef(Begin, End - Begin), nullptr,
-                                 &QS.NamedValues, &Value, &Diag)) {
+    if (!Parser::parseExpression(Line, nullptr, &QS.NamedValues, &Value,
+                                 &Diag)) {
       return makeInvalidQueryFromDiagnostics(Diag);
     }
 
@@ -235,7 +226,7 @@ QueryRef QueryParser::doParse() {
       return completeMatcherExpression();
 
     Diagnostics Diag;
-    auto MatcherSource = StringRef(Begin, End - Begin).trim();
+    auto MatcherSource = Line.trim();
     Optional<DynTypedMatcher> Matcher = Parser::parseMatcherExpression(
         MatcherSource, nullptr, &QS.NamedValues, &Diag);
     if (!Matcher) {

Modified: clang-tools-extra/trunk/clang-query/QueryParser.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-query/QueryParser.h?rev=350660&r1=350659&r2=350660&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-query/QueryParser.h (original)
+++ clang-tools-extra/trunk/clang-query/QueryParser.h Tue Jan  8 14:27:08 2019
@@ -37,7 +37,7 @@ public:
 
 private:
   QueryParser(StringRef Line, const QuerySession &QS)
-      : Begin(Line.begin()), End(Line.end()), CompletionPos(nullptr), QS(QS) {}
+      : Line(Line), CompletionPos(nullptr), QS(QS) {}
 
   StringRef lexWord();
 
@@ -55,8 +55,7 @@ private:
   /// \c InvalidQuery if a parse error occurs.
   QueryRef doParse();
 
-  const char *Begin;
-  const char *End;
+  StringRef Line;
 
   const char *CompletionPos;
   std::vector<llvm::LineEditor::Completion> Completions;




More information about the cfe-commits mailing list