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 Completions; Index: clang-query/QueryParser.cpp =================================================================== --- clang-query/QueryParser.cpp +++ clang-query/QueryParser.cpp @@ -27,29 +27,22 @@ // 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; - - ++Begin; - } + 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; - 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 @@ } 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 QueryParser::completeMatcherExpression() { std::vector 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 @@ 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 @@ return completeMatcherExpression(); Diagnostics Diag; - auto MatcherSource = StringRef(Begin, End - Begin).trim(); + auto MatcherSource = Line.trim(); Optional Matcher = Parser::parseMatcherExpression( MatcherSource, nullptr, &QS.NamedValues, &Diag); if (!Matcher) {