]> granicus.if.org Git - clang/commitdiff
Allow comments with '#' in dynamic AST Matchers
authorStephen Kelly <steveire@gmail.com>
Wed, 3 Oct 2018 07:56:43 +0000 (07:56 +0000)
committerStephen Kelly <steveire@gmail.com>
Wed, 3 Oct 2018 07:56:43 +0000 (07:56 +0000)
Summary: This is necessary for clang-query to be able to handle comments.

Reviewers: aaron.ballman

Reviewed By: aaron.ballman

Subscribers: cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@343665 91177308-0d34-0410-b5e6-96231b3b80d8

lib/ASTMatchers/Dynamic/Parser.cpp
unittests/ASTMatchers/Dynamic/ParserTest.cpp

index 2f6238b546bf6e8294e3f8aa903eacab3529b541..5db10048fdf844ff0478f6b19cd29e481c0a77b4 100644 (file)
@@ -110,6 +110,10 @@ private:
     }
 
     switch (Code[0]) {
+    case '#':
+      Result.Kind = TokenInfo::TK_Eof;
+      Result.Text = "";
+      return Result;
     case ',':
       Result.Kind = TokenInfo::TK_Comma;
       Result.Text = Code.substr(0, 1);
index fd7bbdde4a117895848b32e30e3901ddba571a30..cda22ef35555743c51c74f838b70416c39c813ba 100644 (file)
@@ -148,8 +148,8 @@ TEST(ParserTest, ParseMatcher) {
   const uint64_t ExpectedBar = Sema.expectMatcher("Bar");
   const uint64_t ExpectedBaz = Sema.expectMatcher("Baz");
   Sema.parse(" Foo ( Bar ( 17), Baz( \n \"B A,Z\") ) .bind( \"Yo!\") ");
-  for (size_t i = 0, e = Sema.Errors.size(); i != e; ++i) {
-    EXPECT_EQ("", Sema.Errors[i]);
+  for (const auto &E : Sema.Errors) {
+    EXPECT_EQ("", E);
   }
 
   EXPECT_NE(ExpectedFoo, ExpectedBar);
@@ -181,6 +181,21 @@ TEST(ParserTest, ParseMatcher) {
   EXPECT_EQ("Yo!", Foo.BoundID);
 }
 
+TEST(ParserTest, ParseComment) {
+  MockSema Sema;
+  const uint64_t ExpectedFoo = Sema.expectMatcher("Foo");
+  Sema.parse(" Foo() # Bar() ");
+  for (const auto &E : Sema.Errors) {
+    EXPECT_EQ("", E);
+  }
+
+  EXPECT_EQ(1ULL, Sema.Matchers.size());
+
+  Sema.parse("Foo(#) ");
+
+  EXPECT_EQ("1:4: Error parsing matcher. Found end-of-code while looking for ')'.", Sema.Errors[1]);
+}
+
 using ast_matchers::internal::Matcher;
 
 Parser::NamedValueMap getTestNamedValues() {