diff options
author | Stephen Kelly <steveire@gmail.com> | 2019-01-08 22:27:08 +0000 |
---|---|---|
committer | Stephen Kelly <steveire@gmail.com> | 2019-01-08 22:27:08 +0000 |
commit | de4533621c50ddea8639b73f1cc57664b313881c (patch) | |
tree | 44eb4bb152314279a86e912da4648b9623c74e74 /clang-tools-extra/clang-query | |
parent | dadbb45f34dbba4f311adecfd484400e8e82d9d0 (diff) | |
download | llvm-de4533621c50ddea8639b73f1cc57664b313881c.zip llvm-de4533621c50ddea8639b73f1cc57664b313881c.tar.gz llvm-de4533621c50ddea8639b73f1cc57664b313881c.tar.bz2 |
[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
llvm-svn: 350660
Diffstat (limited to 'clang-tools-extra/clang-query')
-rw-r--r-- | clang-tools-extra/clang-query/QueryParser.cpp | 43 | ||||
-rw-r--r-- | clang-tools-extra/clang-query/QueryParser.h | 5 |
2 files changed, 19 insertions, 29 deletions
diff --git a/clang-tools-extra/clang-query/QueryParser.cpp b/clang-tools-extra/clang-query/QueryParser.cpp index 7c12e3b..54eb5a0 100644 --- a/clang-tools-extra/clang-query/QueryParser.cpp +++ b/clang-tools-extra/clang-query/QueryParser.cpp @@ -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; - - ++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 @@ template <typename QueryType> QueryRef QueryParser::parseSetOutputKind() { } 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(const Diagnostics &Diag) { 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) { diff --git a/clang-tools-extra/clang-query/QueryParser.h b/clang-tools-extra/clang-query/QueryParser.h index 6730f99..56eb5a9 100644 --- a/clang-tools-extra/clang-query/QueryParser.h +++ b/clang-tools-extra/clang-query/QueryParser.h @@ -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; |