Skip to content

Commit 22e6282

Browse files
committed
[clang-query] Allow for trailing comma in matchers
1 parent 122afae commit 22e6282

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ Improvements to clang-doc
8888
Improvements to clang-query
8989
---------------------------
9090

91+
- Matcher queries interpreted by clang-query are now support trailing comma (,)
92+
in matcher arguments. Note that C++ still doesn't allow this in function
93+
arguments. So when porting a query to C++, remove all instances of trailing
94+
comma (otherwise C++ compiler will just complain about "expected expression").
95+
9196
Improvements to include-cleaner
9297
-------------------------------
9398
- Deprecated the ``-insert`` and ``-remove`` command line options, and added
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
void foo(void) {}
2+
// CHECK-OK: trailing-comma.c:1:1: note: "root" binds here
3+
// CHECK-ERR-COMMA: Invalid token <,> found when looking for a value.
4+
5+
// RUN: clang-query -c "match \
6+
// RUN: functionDecl( \
7+
// RUN: hasName( \
8+
// RUN: \"foo\", \
9+
// RUN: ), \
10+
// RUN: ) \
11+
// RUN: " %s | FileCheck --check-prefix=CHECK-OK %s
12+
13+
// Same with \n tokens
14+
// RUN: echo "match functionDecl( hasName( \"foo\" , ) , )" | sed "s/ /\n/g" >%t
15+
// RUN: clang-query -f %t %s | FileCheck --check-prefix=CHECK-OK %s
16+
17+
// RUN: not clang-query -c "match functionDecl(hasName(\"foo\"),,)" %s \
18+
// RUN: | FileCheck --check-prefix=CHECK-ERR-COMMA %s
19+
20+
// RUN: not clang-query -c "match functionDecl(,)" %s \
21+
// RUN: | FileCheck --check-prefix=CHECK-ERR-COMMA %s

clang/lib/ASTMatchers/Dynamic/Parser.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,11 @@ bool Parser::parseMatcherBuilder(MatcherCtor Ctor, const TokenInfo &NameToken,
490490
<< CommaToken.Text;
491491
return false;
492492
}
493+
// Allow for a trailing , token and possibly a new line.
494+
Tokenizer->SkipNewlines();
495+
if (Tokenizer->nextTokenKind() == TokenInfo::TK_CloseParen) {
496+
continue;
497+
}
493498
}
494499

495500
Diagnostics::Context Ctx(Diagnostics::Context::MatcherArg, Error,
@@ -658,6 +663,11 @@ bool Parser::parseMatcherExpressionImpl(const TokenInfo &NameToken,
658663
<< CommaToken.Text;
659664
return false;
660665
}
666+
// Allow for a trailing , token and possibly a new line.
667+
Tokenizer->SkipNewlines();
668+
if (Tokenizer->nextTokenKind() == TokenInfo::TK_CloseParen) {
669+
continue;
670+
}
661671
}
662672

663673
Diagnostics::Context Ctx(Diagnostics::Context::MatcherArg, Error,

0 commit comments

Comments
 (0)