steveire created this revision.
steveire added a reviewer: aaron.ballman.
Herald added a subscriber: cfe-commits.

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


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D56415

Files:
  clang-query/QueryParser.cpp
  clang-query/QueryParser.h

Index: clang-query/QueryParser.h
===================================================================
--- clang-query/QueryParser.h
+++ clang-query/QueryParser.h
@@ -37,7 +37,7 @@
 
 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 @@
   /// \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;
Index: clang-query/QueryParser.cpp
===================================================================
--- clang-query/QueryParser.cpp
+++ clang-query/QueryParser.cpp
@@ -27,29 +27,19 @@
 // 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 (Line.empty())
+    return StringRef(Line.begin(), 0);
 
-    if (!isWhitespace(*Begin))
-      break;
-
-    ++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_while([](char c){ return !isWhitespace(c); });
+  Line = Line.drop_front(Word.size());
+  return Word;
 }
 
 // This is the StringSwitch-alike used by lexOrCompleteWord below. See that
@@ -133,10 +123,9 @@
 }
 
 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,7 +163,7 @@
 
 QueryRef QueryParser::completeMatcherExpression() {
   std::vector<MatcherCompletion> Comps = Parser::completeExpression(
-      StringRef(Begin, End - Begin), CompletionPos - Begin, nullptr,
+      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,7 +211,7 @@
 
     Diagnostics Diag;
     ast_matchers::dynamic::VariantValue Value;
-    if (!Parser::parseExpression(StringRef(Begin, End - Begin), nullptr,
+    if (!Parser::parseExpression(Line, nullptr,
                                  &QS.NamedValues, &Value, &Diag)) {
       return makeInvalidQueryFromDiagnostics(Diag);
     }
@@ -235,7 +224,7 @@
       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) {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to