Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ Improvements to clang-doc
Improvements to clang-query
---------------------------

- Matcher queries interpreted by clang-query are now support trailing comma (,)
in matcher arguments. Note that C++ still doesn't allow this in function
arguments. So when porting a query to C++, remove all instances of trailing
comma (otherwise C++ compiler will just complain about "expected expression").

Improvements to include-cleaner
-------------------------------
- Deprecated the ``-insert`` and ``-remove`` command line options, and added
Expand Down
21 changes: 21 additions & 0 deletions clang-tools-extra/test/clang-query/trailing-comma.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
void foo(void) {}
// CHECK-OK: trailing-comma.c:1:1: note: "root" binds here
// CHECK-ERR-COMMA: Invalid token <,> found when looking for a value.

// RUN: clang-query -c "match \
// RUN: functionDecl( \
// RUN: hasName( \
// RUN: \"foo\", \
// RUN: ), \
// RUN: ) \
// RUN: " %s | FileCheck --check-prefix=CHECK-OK %s

// Same with \n tokens
// RUN: echo "match functionDecl( hasName( \"foo\" , ) , )" | sed "s/ /\n/g" >%t
// RUN: clang-query -f %t %s | FileCheck --check-prefix=CHECK-OK %s

// RUN: not clang-query -c "match functionDecl(hasName(\"foo\"),,)" %s \
// RUN: | FileCheck --check-prefix=CHECK-ERR-COMMA %s

// RUN: not clang-query -c "match functionDecl(,)" %s \
// RUN: | FileCheck --check-prefix=CHECK-ERR-COMMA %s
10 changes: 10 additions & 0 deletions clang/lib/ASTMatchers/Dynamic/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@ bool Parser::parseMatcherBuilder(MatcherCtor Ctor, const TokenInfo &NameToken,
<< CommaToken.Text;
return false;
}
// Allow for a trailing , token and possibly a new line.
Tokenizer->SkipNewlines();
if (Tokenizer->nextTokenKind() == TokenInfo::TK_CloseParen) {
continue;
}
}

Diagnostics::Context Ctx(Diagnostics::Context::MatcherArg, Error,
Expand Down Expand Up @@ -658,6 +663,11 @@ bool Parser::parseMatcherExpressionImpl(const TokenInfo &NameToken,
<< CommaToken.Text;
return false;
}
// Allow for a trailing , token and possibly a new line.
Tokenizer->SkipNewlines();
if (Tokenizer->nextTokenKind() == TokenInfo::TK_CloseParen) {
continue;
}
}

Diagnostics::Context Ctx(Diagnostics::Context::MatcherArg, Error,
Expand Down