Skip to content

Commit

Permalink
[clang-query] Add comment token handling
Browse files Browse the repository at this point in the history
Summary:
It is possible to pass a file of commands to clang-query using the
command line option -f or --preload.  Make it possible to write comments
in such files.

Reviewers: aaron.ballman

Reviewed By: aaron.ballman

Subscribers: mgorny, cfe-commits

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

llvm-svn: 343666
  • Loading branch information
steveire committed Oct 3, 2018
1 parent 7c787b8 commit 2c4079c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions clang-tools-extra/clang-query/QueryParser.cpp
Expand Up @@ -37,6 +37,11 @@ StringRef QueryParser::lexWord() {
++Begin;
}

if (*Begin == '#') {
End = Begin;
return StringRef();
}

const char *WordBegin = Begin;

while (true) {
Expand Down Expand Up @@ -127,6 +132,7 @@ namespace {

enum ParsedQueryKind {
PQK_Invalid,
PQK_Comment,
PQK_NoOp,
PQK_Help,
PQK_Let,
Expand Down Expand Up @@ -161,6 +167,7 @@ QueryRef QueryParser::doParse() {
StringRef CommandStr;
ParsedQueryKind QKind = LexOrCompleteWord<ParsedQueryKind>(this, CommandStr)
.Case("", PQK_NoOp)
.Case("#", PQK_Comment, /*IsCompletion=*/false)
.Case("help", PQK_Help)
.Case("l", PQK_Let, /*IsCompletion=*/false)
.Case("let", PQK_Let)
Expand All @@ -173,6 +180,7 @@ QueryRef QueryParser::doParse() {
.Default(PQK_Invalid);

switch (QKind) {
case PQK_Comment:
case PQK_NoOp:
return new NoOpQuery;

Expand Down
11 changes: 11 additions & 0 deletions clang-tools-extra/unittests/clang-query/QueryParserTest.cpp
Expand Up @@ -146,6 +146,17 @@ TEST_F(QueryParserTest, LetUnlet) {
cast<InvalidQuery>(Q)->ErrStr);
}

TEST_F(QueryParserTest, Comment) {
QueryRef Q = parse("# let foo decl()");
ASSERT_TRUE(isa<NoOpQuery>(Q));

Q = parse("let foo decl() # creates a decl() matcher called foo");
ASSERT_TRUE(isa<LetQuery>(Q));

Q = parse("set bind-root false # reduce noise");
ASSERT_TRUE(isa<SetQuery<bool>>(Q));
}

TEST_F(QueryParserTest, Complete) {
std::vector<llvm::LineEditor::Completion> Comps =
QueryParser::complete("", 0, QS);
Expand Down

0 comments on commit 2c4079c

Please sign in to comment.