head	1.1;
branch	1.1.1;
access;
symbols
	netbsd-11-0-RC4:1.1.1.11
	netbsd-11-0-RC3:1.1.1.11
	netbsd-11-0-RC2:1.1.1.11
	netbsd-11-0-RC1:1.1.1.11
	perseant-exfatfs-base-20250801:1.1.1.11
	netbsd-11:1.1.1.11.0.10
	netbsd-11-base:1.1.1.11
	netbsd-10-1-RELEASE:1.1.1.11
	perseant-exfatfs-base-20240630:1.1.1.11
	perseant-exfatfs:1.1.1.11.0.8
	perseant-exfatfs-base:1.1.1.11
	netbsd-8-3-RELEASE:1.1.1.8
	netbsd-9-4-RELEASE:1.1.1.10
	netbsd-10-0-RELEASE:1.1.1.11
	netbsd-10-0-RC6:1.1.1.11
	netbsd-10-0-RC5:1.1.1.11
	netbsd-10-0-RC4:1.1.1.11
	netbsd-10-0-RC3:1.1.1.11
	netbsd-10-0-RC2:1.1.1.11
	netbsd-10-0-RC1:1.1.1.11
	netbsd-10:1.1.1.11.0.6
	netbsd-10-base:1.1.1.11
	netbsd-9-3-RELEASE:1.1.1.10
	cjep_sun2x:1.1.1.11.0.4
	cjep_sun2x-base:1.1.1.11
	cjep_staticlib_x-base1:1.1.1.11
	netbsd-9-2-RELEASE:1.1.1.10
	cjep_staticlib_x:1.1.1.11.0.2
	cjep_staticlib_x-base:1.1.1.11
	netbsd-9-1-RELEASE:1.1.1.10
	phil-wifi-20200421:1.1.1.11
	phil-wifi-20200411:1.1.1.11
	phil-wifi-20200406:1.1.1.11
	netbsd-8-2-RELEASE:1.1.1.8
	netbsd-9-0-RELEASE:1.1.1.10
	netbsd-9-0-RC2:1.1.1.10
	netbsd-9-0-RC1:1.1.1.10
	netbsd-9:1.1.1.10.0.2
	netbsd-9-base:1.1.1.10
	phil-wifi-20190609:1.1.1.10
	netbsd-8-1-RELEASE:1.1.1.8
	netbsd-8-1-RC1:1.1.1.8
	pgoyette-compat-merge-20190127:1.1.1.9.2.1
	pgoyette-compat-20190127:1.1.1.10
	pgoyette-compat-20190118:1.1.1.10
	pgoyette-compat-1226:1.1.1.10
	pgoyette-compat-1126:1.1.1.10
	pgoyette-compat-1020:1.1.1.10
	pgoyette-compat-0930:1.1.1.10
	pgoyette-compat-0906:1.1.1.10
	netbsd-7-2-RELEASE:1.1.1.5.2.1
	pgoyette-compat-0728:1.1.1.10
	clang-337282:1.1.1.10
	netbsd-8-0-RELEASE:1.1.1.8
	phil-wifi:1.1.1.9.0.4
	phil-wifi-base:1.1.1.9
	pgoyette-compat-0625:1.1.1.9
	netbsd-8-0-RC2:1.1.1.8
	pgoyette-compat-0521:1.1.1.9
	pgoyette-compat-0502:1.1.1.9
	pgoyette-compat-0422:1.1.1.9
	netbsd-8-0-RC1:1.1.1.8
	pgoyette-compat-0415:1.1.1.9
	pgoyette-compat-0407:1.1.1.9
	pgoyette-compat-0330:1.1.1.9
	pgoyette-compat-0322:1.1.1.9
	pgoyette-compat-0315:1.1.1.9
	netbsd-7-1-2-RELEASE:1.1.1.5.2.1
	pgoyette-compat:1.1.1.9.0.2
	pgoyette-compat-base:1.1.1.9
	netbsd-7-1-1-RELEASE:1.1.1.5.2.1
	clang-319952:1.1.1.9
	matt-nb8-mediatek:1.1.1.8.0.10
	matt-nb8-mediatek-base:1.1.1.8
	clang-309604:1.1.1.9
	perseant-stdc-iso10646:1.1.1.8.0.8
	perseant-stdc-iso10646-base:1.1.1.8
	netbsd-8:1.1.1.8.0.6
	netbsd-8-base:1.1.1.8
	prg-localcount2-base3:1.1.1.8
	prg-localcount2-base2:1.1.1.8
	prg-localcount2-base1:1.1.1.8
	prg-localcount2:1.1.1.8.0.4
	prg-localcount2-base:1.1.1.8
	pgoyette-localcount-20170426:1.1.1.8
	bouyer-socketcan-base1:1.1.1.8
	pgoyette-localcount-20170320:1.1.1.8
	netbsd-7-1:1.1.1.5.2.1.0.6
	netbsd-7-1-RELEASE:1.1.1.5.2.1
	netbsd-7-1-RC2:1.1.1.5.2.1
	clang-294123:1.1.1.8
	netbsd-7-nhusb-base-20170116:1.1.1.5.2.1
	bouyer-socketcan:1.1.1.8.0.2
	bouyer-socketcan-base:1.1.1.8
	clang-291444:1.1.1.8
	pgoyette-localcount-20170107:1.1.1.7
	netbsd-7-1-RC1:1.1.1.5.2.1
	pgoyette-localcount-20161104:1.1.1.7
	netbsd-7-0-2-RELEASE:1.1.1.5.2.1
	localcount-20160914:1.1.1.7
	netbsd-7-nhusb:1.1.1.5.2.1.0.4
	netbsd-7-nhusb-base:1.1.1.5.2.1
	clang-280599:1.1.1.7
	pgoyette-localcount-20160806:1.1.1.7
	pgoyette-localcount-20160726:1.1.1.7
	pgoyette-localcount:1.1.1.7.0.2
	pgoyette-localcount-base:1.1.1.7
	netbsd-7-0-1-RELEASE:1.1.1.5.2.1
	clang-261930:1.1.1.7
	netbsd-7-0:1.1.1.5.2.1.0.2
	netbsd-7-0-RELEASE:1.1.1.5.2.1
	netbsd-7-0-RC3:1.1.1.5.2.1
	netbsd-7-0-RC2:1.1.1.5.2.1
	netbsd-7-0-RC1:1.1.1.5.2.1
	clang-237755:1.1.1.6
	clang-232565:1.1.1.6
	clang-227398:1.1.1.6
	tls-maxphys-base:1.1.1.5
	tls-maxphys:1.1.1.5.0.4
	netbsd-7:1.1.1.5.0.2
	netbsd-7-base:1.1.1.5
	clang-215315:1.1.1.5
	clang-209886:1.1.1.5
	yamt-pagecache:1.1.1.4.0.4
	yamt-pagecache-base9:1.1.1.4
	tls-earlyentropy:1.1.1.4.0.2
	tls-earlyentropy-base:1.1.1.5
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.1.1.4
	riastradh-drm2-base3:1.1.1.4
	clang-202566:1.1.1.4
	clang-201163:1.1.1.4
	clang-199312:1.1.1.3
	clang-198450:1.1.1.2
	clang-196603:1.1.1.1
	clang-195771:1.1.1.1
	LLVM:1.1.1;
locks; strict;
comment	@// @;


1.1
date	2013.11.28.14.14.48;	author joerg;	state Exp;
branches
	1.1.1.1;
next	;
commitid	ow8OybrawrB1f3fx;

1.1.1.1
date	2013.11.28.14.14.48;	author joerg;	state Exp;
branches;
next	1.1.1.2;
commitid	ow8OybrawrB1f3fx;

1.1.1.2
date	2014.01.05.15.37.59;	author joerg;	state Exp;
branches;
next	1.1.1.3;
commitid	wh3aCSIWykURqWjx;

1.1.1.3
date	2014.01.15.21.26.18;	author joerg;	state Exp;
branches;
next	1.1.1.4;
commitid	NQXlzzA0SPkc5glx;

1.1.1.4
date	2014.02.14.20.07.02;	author joerg;	state Exp;
branches
	1.1.1.4.2.1
	1.1.1.4.4.1;
next	1.1.1.5;
commitid	annVkZ1sc17rF6px;

1.1.1.5
date	2014.05.30.18.14.50;	author joerg;	state Exp;
branches
	1.1.1.5.2.1
	1.1.1.5.4.1;
next	1.1.1.6;
commitid	8q0kdlBlCn09GACx;

1.1.1.6
date	2015.01.29.19.57.27;	author joerg;	state Exp;
branches;
next	1.1.1.7;
commitid	mlISSizlPKvepX7y;

1.1.1.7
date	2016.02.27.22.11.46;	author joerg;	state Exp;
branches
	1.1.1.7.2.1;
next	1.1.1.8;
commitid	tIimz3oDlh1NpBWy;

1.1.1.8
date	2017.01.11.10.33.10;	author joerg;	state Exp;
branches;
next	1.1.1.9;
commitid	CNnUNfII1jgNmxBz;

1.1.1.9
date	2017.08.01.19.34.42;	author joerg;	state Exp;
branches
	1.1.1.9.2.1
	1.1.1.9.4.1;
next	1.1.1.10;
commitid	pMuDy65V0VicSx1A;

1.1.1.10
date	2018.07.17.18.32.13;	author joerg;	state Exp;
branches;
next	1.1.1.11;
commitid	wDzL46ALjrCZgwKA;

1.1.1.11
date	2019.11.13.22.23.09;	author joerg;	state dead;
branches;
next	;
commitid	QD8YATxuNG34YJKB;

1.1.1.4.2.1
date	2014.08.10.07.08.25;	author tls;	state Exp;
branches;
next	;
commitid	t01A1TLTYxkpGMLx;

1.1.1.4.4.1
date	2014.02.14.20.07.02;	author yamt;	state dead;
branches;
next	1.1.1.4.4.2;
commitid	WSrDtL5nYAUyiyBx;

1.1.1.4.4.2
date	2014.05.22.16.19.48;	author yamt;	state Exp;
branches;
next	;
commitid	WSrDtL5nYAUyiyBx;

1.1.1.5.2.1
date	2015.06.04.20.04.46;	author snj;	state Exp;
branches;
next	;
commitid	yRnjq9fueSo6n9oy;

1.1.1.5.4.1
date	2014.05.30.18.14.50;	author tls;	state dead;
branches;
next	1.1.1.5.4.2;
commitid	jTnpym9Qu0o4R1Nx;

1.1.1.5.4.2
date	2014.08.19.23.49.27;	author tls;	state Exp;
branches;
next	;
commitid	jTnpym9Qu0o4R1Nx;

1.1.1.7.2.1
date	2017.03.20.06.53.38;	author pgoyette;	state Exp;
branches;
next	;
commitid	jjw7cAwgyKq7RfKz;

1.1.1.9.2.1
date	2018.07.28.04.34.18;	author pgoyette;	state Exp;
branches;
next	;
commitid	1UP1xAIUxv1ZgRLA;

1.1.1.9.4.1
date	2019.06.10.21.46.45;	author christos;	state Exp;
branches;
next	1.1.1.9.4.2;
commitid	jtc8rnCzWiEEHGqB;

1.1.1.9.4.2
date	2020.04.13.07.50.37;	author martin;	state dead;
branches;
next	;
commitid	X01YhRUPVUDaec4C;


desc
@@


1.1
log
@Initial revision
@
text
@//===- unittest/ASTMatchers/Dynamic/ParserTest.cpp - Parser unit tests -===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===-------------------------------------------------------------------===//

#include <string>
#include <vector>

#include "../ASTMatchersTest.h"
#include "clang/ASTMatchers/Dynamic/Parser.h"
#include "clang/ASTMatchers/Dynamic/Registry.h"
#include "gtest/gtest.h"
#include "llvm/ADT/StringMap.h"

namespace clang {
namespace ast_matchers {
namespace dynamic {
namespace {

class MockSema : public Parser::Sema {
public:
  virtual ~MockSema() {}

  uint64_t expectMatcher(StringRef MatcherName) {
    ast_matchers::internal::Matcher<Stmt> M = stmt();
    ExpectedMatchers.insert(std::make_pair(MatcherName, M));
    return M.getID();
  }

  void parse(StringRef Code) {
    Diagnostics Error;
    VariantValue Value;
    Parser::parseExpression(Code, this, &Value, &Error);
    Values.push_back(Value);
    Errors.push_back(Error.toStringFull());
  }

  VariantMatcher actOnMatcherExpression(StringRef MatcherName,
                                        const SourceRange &NameRange,
                                        StringRef BindID,
                                        ArrayRef<ParserValue> Args,
                                        Diagnostics *Error) {
    MatcherInfo ToStore = { MatcherName, NameRange, Args, BindID };
    Matchers.push_back(ToStore);
    return VariantMatcher::SingleMatcher(
        ExpectedMatchers.find(MatcherName)->second);
  }

  struct MatcherInfo {
    StringRef MatcherName;
    SourceRange NameRange;
    std::vector<ParserValue> Args;
    std::string BoundID;
  };

  std::vector<std::string> Errors;
  std::vector<VariantValue> Values;
  std::vector<MatcherInfo> Matchers;
  std::map<std::string, ast_matchers::internal::Matcher<Stmt> >
  ExpectedMatchers;
};

TEST(ParserTest, ParseUnsigned) {
  MockSema Sema;
  Sema.parse("0");
  Sema.parse("123");
  Sema.parse("0x1f");
  Sema.parse("12345678901");
  Sema.parse("1a1");
  EXPECT_EQ(5U, Sema.Values.size());
  EXPECT_EQ(0U, Sema.Values[0].getUnsigned());
  EXPECT_EQ(123U, Sema.Values[1].getUnsigned());
  EXPECT_EQ(31U, Sema.Values[2].getUnsigned());
  EXPECT_EQ("1:1: Error parsing unsigned token: <12345678901>", Sema.Errors[3]);
  EXPECT_EQ("1:1: Error parsing unsigned token: <1a1>", Sema.Errors[4]);
}

TEST(ParserTest, ParseString) {
  MockSema Sema;
  Sema.parse("\"Foo\"");
  Sema.parse("\"\"");
  Sema.parse("\"Baz");
  EXPECT_EQ(3ULL, Sema.Values.size());
  EXPECT_EQ("Foo", Sema.Values[0].getString());
  EXPECT_EQ("", Sema.Values[1].getString());
  EXPECT_EQ("1:1: Error parsing string token: <\"Baz>", Sema.Errors[2]);
}

bool matchesRange(const SourceRange &Range, unsigned StartLine,
                  unsigned EndLine, unsigned StartColumn, unsigned EndColumn) {
  EXPECT_EQ(StartLine, Range.Start.Line);
  EXPECT_EQ(EndLine, Range.End.Line);
  EXPECT_EQ(StartColumn, Range.Start.Column);
  EXPECT_EQ(EndColumn, Range.End.Column);
  return Range.Start.Line == StartLine && Range.End.Line == EndLine &&
         Range.Start.Column == StartColumn && Range.End.Column == EndColumn;
}

llvm::Optional<DynTypedMatcher> getSingleMatcher(const VariantValue &Value) {
  llvm::Optional<DynTypedMatcher> Result =
      Value.getMatcher().getSingleMatcher();
  EXPECT_TRUE(Result.hasValue());
  return Result;
}

TEST(ParserTest, ParseMatcher) {
  MockSema Sema;
  const uint64_t ExpectedFoo = Sema.expectMatcher("Foo");
  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]);
  }

  EXPECT_EQ(1ULL, Sema.Values.size());
  EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID());

  EXPECT_EQ(3ULL, Sema.Matchers.size());
  const MockSema::MatcherInfo Bar = Sema.Matchers[0];
  EXPECT_EQ("Bar", Bar.MatcherName);
  EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 17));
  EXPECT_EQ(1ULL, Bar.Args.size());
  EXPECT_EQ(17U, Bar.Args[0].Value.getUnsigned());

  const MockSema::MatcherInfo Baz = Sema.Matchers[1];
  EXPECT_EQ("Baz", Baz.MatcherName);
  EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 19, 10));
  EXPECT_EQ(1ULL, Baz.Args.size());
  EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString());

  const MockSema::MatcherInfo Foo = Sema.Matchers[2];
  EXPECT_EQ("Foo", Foo.MatcherName);
  EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12));
  EXPECT_EQ(2ULL, Foo.Args.size());
  EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID());
  EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID());
  EXPECT_EQ("Yo!", Foo.BoundID);
}

using ast_matchers::internal::Matcher;

TEST(ParserTest, FullParserTest) {
  Diagnostics Error;
  llvm::Optional<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression(
      "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral()),"
      "                                      hasOperatorName(\"+\"))))",
      &Error));
  EXPECT_EQ("", Error.toStringFull());
  Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>();
  EXPECT_TRUE(matches("int x = 1 + false;", M));
  EXPECT_FALSE(matches("int x = true + 1;", M));
  EXPECT_FALSE(matches("int x = 1 - false;", M));
  EXPECT_FALSE(matches("int x = true - 1;", M));

  llvm::Optional<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression(
      "functionDecl(hasParameter(1, hasName(\"x\")))", &Error));
  EXPECT_EQ("", Error.toStringFull());
  M = HasParameter->unconditionalConvertTo<Decl>();

  EXPECT_TRUE(matches("void f(int a, int x);", M));
  EXPECT_FALSE(matches("void f(int x, int a);", M));

  EXPECT_TRUE(!Parser::parseMatcherExpression(
                   "hasInitializer(\n    binaryOperator(hasLHS(\"A\")))",
                   &Error).hasValue());
  EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n"
            "2:5: Error parsing argument 1 for matcher binaryOperator.\n"
            "2:20: Error building matcher hasLHS.\n"
            "2:27: Incorrect type for arg 1. "
            "(Expected = Matcher<Expr>) != (Actual = String)",
            Error.toStringFull());
}

std::string ParseWithError(StringRef Code) {
  Diagnostics Error;
  VariantValue Value;
  Parser::parseExpression(Code, &Value, &Error);
  return Error.toStringFull();
}

std::string ParseMatcherWithError(StringRef Code) {
  Diagnostics Error;
  Parser::parseMatcherExpression(Code, &Error);
  return Error.toStringFull();
}

TEST(ParserTest, Errors) {
  EXPECT_EQ(
      "1:5: Error parsing matcher. Found token <123> while looking for '('.",
      ParseWithError("Foo 123"));
  EXPECT_EQ(
      "1:9: Error parsing matcher. Found token <123> while looking for ','.",
      ParseWithError("Foo(\"A\" 123)"));
  EXPECT_EQ(
      "1:4: Error parsing matcher. Found end-of-code while looking for ')'.",
      ParseWithError("Foo("));
  EXPECT_EQ("1:1: End of code found while looking for token.",
            ParseWithError(""));
  EXPECT_EQ("Input value is not a matcher expression.",
            ParseMatcherWithError("\"A\""));
  EXPECT_EQ("1:1: Error parsing argument 1 for matcher Foo.\n"
            "1:5: Invalid token <(> found when looking for a value.",
            ParseWithError("Foo(("));
  EXPECT_EQ("1:7: Expected end of code.", ParseWithError("expr()a"));
  EXPECT_EQ("1:11: Malformed bind() expression.",
            ParseWithError("isArrow().biind"));
  EXPECT_EQ("1:15: Malformed bind() expression.",
            ParseWithError("isArrow().bind"));
  EXPECT_EQ("1:16: Malformed bind() expression.",
            ParseWithError("isArrow().bind(foo"));
  EXPECT_EQ("1:21: Malformed bind() expression.",
            ParseWithError("isArrow().bind(\"foo\""));
  EXPECT_EQ("1:1: Error building matcher isArrow.\n"
            "1:1: Matcher does not support binding.",
            ParseWithError("isArrow().bind(\"foo\")"));
  EXPECT_EQ("Input value has unresolved overloaded type: "
            "Matcher<DoStmt|ForStmt|WhileStmt>",
            ParseMatcherWithError("hasBody(stmt())"));
}

TEST(ParserTest, OverloadErrors) {
  EXPECT_EQ("1:1: Error building matcher callee.\n"
            "1:8: Candidate 1: Incorrect type for arg 1. "
            "(Expected = Matcher<Stmt>) != (Actual = String)\n"
            "1:8: Candidate 2: Incorrect type for arg 1. "
            "(Expected = Matcher<Decl>) != (Actual = String)",
            ParseWithError("callee(\"A\")"));
}

}  // end anonymous namespace
}  // end namespace dynamic
}  // end namespace ast_matchers
}  // end namespace clang
@


1.1.1.1
log
@Import Clang 3.4rc1 r195771.
@
text
@@


1.1.1.2
log
@Import clang 3.5svn r198450.
@
text
@a16 1
#include "llvm/ADT/Optional.h"
d42 1
a42 9
  llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName,
                                                const SourceRange &NameRange,
                                                Diagnostics *Error) {
    const ExpectedMatchersTy::value_type *Matcher =
        &*ExpectedMatchers.find(MatcherName);
    return reinterpret_cast<MatcherCtor>(Matcher);
  }

  VariantMatcher actOnMatcherExpression(MatcherCtor Ctor,
d47 1
a47 3
    const ExpectedMatchersTy::value_type *Matcher =
        reinterpret_cast<const ExpectedMatchersTy::value_type *>(Ctor);
    MatcherInfo ToStore = { Matcher->first, NameRange, Args, BindID };
d49 2
a50 1
    return VariantMatcher::SingleMatcher(Matcher->second);
d63 2
a64 3
  typedef std::map<std::string, ast_matchers::internal::Matcher<Stmt> >
  ExpectedMatchersTy;
  ExpectedMatchersTy ExpectedMatchers;
a196 1
      "1:1: Matcher not found: Foo\n"
a199 1
      "1:1: Matcher not found: Foo\n"
d206 1
a206 2
  EXPECT_EQ("1:1: Matcher not found: Foo\n"
            "1:1: Error parsing argument 1 for matcher Foo.\n"
@


1.1.1.3
log
@Import Clang 3.5svn r199312
@
text
@d10 3
d16 1
a18 3
#include "gtest/gtest.h"
#include <string>
#include <vector>
@


1.1.1.4
log
@Import Clang 3.5svn r201163.
@
text
@a247 14
TEST(ParserTest, Completion) {
  std::vector<MatcherCompletion> Comps =
      Parser::completeExpression("while", 5);
  ASSERT_EQ(1u, Comps.size());
  EXPECT_EQ("Stmt(", Comps[0].TypedText);
  EXPECT_EQ("Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)",
            Comps[0].MatcherDecl);

  Comps = Parser::completeExpression("whileStmt().", 12);
  ASSERT_EQ(1u, Comps.size());
  EXPECT_EQ("bind(\"", Comps[0].TypedText);
  EXPECT_EQ("bind", Comps[0].MatcherDecl);
}

@


1.1.1.4.2.1
log
@Rebase.
@
text
@d42 3
a44 1
  llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName) {
a177 23
  // Test named values.
  struct NamedSema : public Parser::RegistrySema {
   public:
    virtual VariantValue getNamedValue(StringRef Name) {
      if (Name == "nameX")
        return std::string("x");
      if (Name == "param0")
        return VariantMatcher::SingleMatcher(hasParameter(0, hasName("a")));
      return VariantValue();
    }
  };
  NamedSema Sema;
  llvm::Optional<DynTypedMatcher> HasParameterWithNamedValues(
      Parser::parseMatcherExpression(
          "functionDecl(param0, hasParameter(1, hasName(nameX)))", &Sema,
          &Error));
  EXPECT_EQ("", Error.toStringFull());
  M = HasParameterWithNamedValues->unconditionalConvertTo<Decl>();

  EXPECT_TRUE(matches("void f(int a, int x);", M));
  EXPECT_FALSE(matches("void f(int x, int a);", M));


a210 4
      "1:1: Error parsing argument 1 for matcher stmt.\n"
      "1:6: Value not found: someValue",
      ParseWithError("stmt(someValue)"));
  EXPECT_EQ(
d235 1
a235 1
            "Matcher<DoStmt|ForStmt|WhileStmt|CXXForRangeStmt>",
@


1.1.1.5
log
@Import Clang 3.5svn r209886.
@
text
@d42 3
a44 1
  llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName) {
a177 23
  // Test named values.
  struct NamedSema : public Parser::RegistrySema {
   public:
    virtual VariantValue getNamedValue(StringRef Name) {
      if (Name == "nameX")
        return std::string("x");
      if (Name == "param0")
        return VariantMatcher::SingleMatcher(hasParameter(0, hasName("a")));
      return VariantValue();
    }
  };
  NamedSema Sema;
  llvm::Optional<DynTypedMatcher> HasParameterWithNamedValues(
      Parser::parseMatcherExpression(
          "functionDecl(param0, hasParameter(1, hasName(nameX)))", &Sema,
          &Error));
  EXPECT_EQ("", Error.toStringFull());
  M = HasParameterWithNamedValues->unconditionalConvertTo<Decl>();

  EXPECT_TRUE(matches("void f(int a, int x);", M));
  EXPECT_FALSE(matches("void f(int x, int a);", M));


a210 4
      "1:1: Error parsing argument 1 for matcher stmt.\n"
      "1:6: Value not found: someValue",
      ParseWithError("stmt(someValue)"));
  EXPECT_EQ(
d235 1
a235 1
            "Matcher<DoStmt|ForStmt|WhileStmt|CXXForRangeStmt>",
@


1.1.1.5.2.1
log
@Update LLVM to 3.6.1, requested by joerg in ticket 824.
@
text
@d29 1
a29 4
    // Optimizations on the matcher framework make simple matchers like
    // 'stmt()' to be all the same matcher.
    // Use a more complex expression to prevent that.
    ast_matchers::internal::Matcher<Stmt> M = stmt(stmt(), stmt());
d31 1
a31 1
    return M.getID().second;
a127 4
  EXPECT_NE(ExpectedFoo, ExpectedBar);
  EXPECT_NE(ExpectedFoo, ExpectedBaz);
  EXPECT_NE(ExpectedBar, ExpectedBaz);

d129 1
a129 1
  EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID().second);
d148 2
a149 2
  EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID().second);
  EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID().second);
a154 8
Parser::NamedValueMap getTestNamedValues() {
  Parser::NamedValueMap Values;
  Values["nameX"] = std::string("x");
  Values["hasParamA"] =
      VariantMatcher::SingleMatcher(hasParameter(0, hasName("a")));
  return Values;
}

d177 11
a187 1
  auto NamedValues = getTestNamedValues();
d190 2
a191 2
          "functionDecl(hasParamA, hasParameter(1, hasName(nameX)))",
          nullptr, &NamedValues, &Error));
d273 1
a273 1
TEST(ParserTest, CompletionRegistry) {
a286 32
TEST(ParserTest, CompletionNamedValues) {
  // Can complete non-matcher types.
  auto NamedValues = getTestNamedValues();
  StringRef Code = "functionDecl(hasName(";
  std::vector<MatcherCompletion> Comps =
      Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues);
  ASSERT_EQ(1u, Comps.size());
  EXPECT_EQ("nameX", Comps[0].TypedText);
  EXPECT_EQ("String nameX", Comps[0].MatcherDecl);

  // Can complete if there are names in the expression.
  Code = "methodDecl(hasName(nameX), ";
  Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues);
  EXPECT_LT(0u, Comps.size());

  // Can complete names and registry together.
  Code = "methodDecl(hasP";
  Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues);
  ASSERT_EQ(3u, Comps.size());
  EXPECT_EQ("aramA", Comps[0].TypedText);
  EXPECT_EQ("Matcher<FunctionDecl> hasParamA", Comps[0].MatcherDecl);

  EXPECT_EQ("arameter(", Comps[1].TypedText);
  EXPECT_EQ(
      "Matcher<FunctionDecl> hasParameter(unsigned, Matcher<ParmVarDecl>)",
      Comps[1].MatcherDecl);

  EXPECT_EQ("arent(", Comps[2].TypedText);
  EXPECT_EQ("Matcher<Decl> hasParent(Matcher<Decl|Stmt>)",
            Comps[2].MatcherDecl);
}

@


1.1.1.6
log
@Import Clang 3.6RC1 r227398.
@
text
@d29 1
a29 4
    // Optimizations on the matcher framework make simple matchers like
    // 'stmt()' to be all the same matcher.
    // Use a more complex expression to prevent that.
    ast_matchers::internal::Matcher<Stmt> M = stmt(stmt(), stmt());
d31 1
a31 1
    return M.getID().second;
a127 4
  EXPECT_NE(ExpectedFoo, ExpectedBar);
  EXPECT_NE(ExpectedFoo, ExpectedBaz);
  EXPECT_NE(ExpectedBar, ExpectedBaz);

d129 1
a129 1
  EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID().second);
d148 2
a149 2
  EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID().second);
  EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID().second);
a154 8
Parser::NamedValueMap getTestNamedValues() {
  Parser::NamedValueMap Values;
  Values["nameX"] = std::string("x");
  Values["hasParamA"] =
      VariantMatcher::SingleMatcher(hasParameter(0, hasName("a")));
  return Values;
}

d177 11
a187 1
  auto NamedValues = getTestNamedValues();
d190 2
a191 2
          "functionDecl(hasParamA, hasParameter(1, hasName(nameX)))",
          nullptr, &NamedValues, &Error));
d273 1
a273 1
TEST(ParserTest, CompletionRegistry) {
a286 32
TEST(ParserTest, CompletionNamedValues) {
  // Can complete non-matcher types.
  auto NamedValues = getTestNamedValues();
  StringRef Code = "functionDecl(hasName(";
  std::vector<MatcherCompletion> Comps =
      Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues);
  ASSERT_EQ(1u, Comps.size());
  EXPECT_EQ("nameX", Comps[0].TypedText);
  EXPECT_EQ("String nameX", Comps[0].MatcherDecl);

  // Can complete if there are names in the expression.
  Code = "methodDecl(hasName(nameX), ";
  Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues);
  EXPECT_LT(0u, Comps.size());

  // Can complete names and registry together.
  Code = "methodDecl(hasP";
  Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues);
  ASSERT_EQ(3u, Comps.size());
  EXPECT_EQ("aramA", Comps[0].TypedText);
  EXPECT_EQ("Matcher<FunctionDecl> hasParamA", Comps[0].MatcherDecl);

  EXPECT_EQ("arameter(", Comps[1].TypedText);
  EXPECT_EQ(
      "Matcher<FunctionDecl> hasParameter(unsigned, Matcher<ParmVarDecl>)",
      Comps[1].MatcherDecl);

  EXPECT_EQ("arent(", Comps[2].TypedText);
  EXPECT_EQ("Matcher<Decl> hasParent(Matcher<Decl|Stmt>)",
            Comps[2].MatcherDecl);
}

@


1.1.1.7
log
@Import Clang 3.8.0rc3 r261930.
@
text
@d26 1
a26 1
  ~MockSema() override {}
d45 1
a45 2
  llvm::Optional<MatcherCtor>
  lookupMatcherCtor(StringRef MatcherName) override {
d52 1
a52 1
                                        SourceRange NameRange,
d55 1
a55 1
                                        Diagnostics *Error) override {
d104 1
a104 1
bool matchesRange(SourceRange Range, unsigned StartLine,
d164 1
a164 1
  Values["nameX"] = llvm::StringRef("x");
d303 1
a303 1
  Code = "cxxMethodDecl(hasName(nameX), ";
d308 1
a308 1
  Code = "cxxMethodDecl(hasP";
d320 2
a321 4
  EXPECT_EQ(
      "Matcher<Decl> "
      "hasParent(Matcher<TemplateArgument|NestedNameSpecifierLoc|Decl|...>)",
      Comps[2].MatcherDecl);
@


1.1.1.7.2.1
log
@Sync with HEAD
@
text
@d14 1
d266 1
a266 1
            "Matcher<DoStmt|ForStmt|WhileStmt|CXXForRangeStmt|FunctionDecl>",
d323 1
a323 1
      "hasParent(Matcher<NestedNameSpecifierLoc|TypeLoc|Decl|...>)",
@


1.1.1.8
log
@Import Clang pre-4.0.0 r291444.
@
text
@d14 1
d266 1
a266 1
            "Matcher<DoStmt|ForStmt|WhileStmt|CXXForRangeStmt|FunctionDecl>",
d323 1
a323 1
      "hasParent(Matcher<NestedNameSpecifierLoc|TypeLoc|Decl|...>)",
@


1.1.1.9
log
@Import clang r309604 from branches/release_50
@
text
@a77 24
TEST(ParserTest, ParseBoolean) {
  MockSema Sema;
  Sema.parse("true");
  Sema.parse("false");
  EXPECT_EQ(2U, Sema.Values.size());
  EXPECT_TRUE(Sema.Values[0].getBoolean());
  EXPECT_FALSE(Sema.Values[1].getBoolean());
}

TEST(ParserTest, ParseDouble) {
  MockSema Sema;
  Sema.parse("1.0");
  Sema.parse("2.0f");
  Sema.parse("34.56e-78");
  Sema.parse("4.E+6");
  Sema.parse("1");
  EXPECT_EQ(5U, Sema.Values.size());
  EXPECT_EQ(1.0, Sema.Values[0].getDouble());
  EXPECT_EQ("1:1: Error parsing numeric literal: <2.0f>", Sema.Errors[1]);
  EXPECT_EQ(34.56e-78, Sema.Values[2].getDouble());
  EXPECT_EQ(4e+6, Sema.Values[3].getDouble());
  EXPECT_FALSE(Sema.Values[4].isDouble());
}

d89 2
a90 2
  EXPECT_EQ("1:1: Error parsing numeric literal: <12345678901>", Sema.Errors[3]);
  EXPECT_EQ("1:1: Error parsing numeric literal: <1a1>", Sema.Errors[4]);
@


1.1.1.9.4.1
log
@Sync with HEAD
@
text
@d189 2
a190 2
  Values["hasParamA"] = VariantMatcher::SingleMatcher(
      functionDecl(hasParameter(0, hasName("a"))));
a238 11
TEST(ParserTest, VariadicMatchTest) {
  Diagnostics Error;
  llvm::Optional<DynTypedMatcher> OM(Parser::parseMatcherExpression(
      "stmt(objcMessageExpr(hasAnySelector(\"methodA\", \"methodB:\")))",
      &Error));
  EXPECT_EQ("", Error.toStringFull());
  auto M = OM->unconditionalConvertTo<Stmt>();
  EXPECT_TRUE(matchesObjC("@@interface I @@end "
                          "void foo(I* i) { [i methodA]; }", M));
}

d332 1
a332 1
  Code = "functionDecl(hasP";
d335 2
d338 1
a338 1
  EXPECT_EQ("arameter(", Comps[0].TypedText);
d341 1
a341 4
      Comps[0].MatcherDecl);

  EXPECT_EQ("aramA", Comps[1].TypedText);
  EXPECT_EQ("Matcher<Decl> hasParamA", Comps[1].MatcherDecl);
@


1.1.1.9.4.2
log
@Mostly merge changes from HEAD upto 20200411
@
text
@@


1.1.1.9.2.1
log
@Sync with HEAD
@
text
@d189 2
a190 2
  Values["hasParamA"] = VariantMatcher::SingleMatcher(
      functionDecl(hasParameter(0, hasName("a"))));
a238 11
TEST(ParserTest, VariadicMatchTest) {
  Diagnostics Error;
  llvm::Optional<DynTypedMatcher> OM(Parser::parseMatcherExpression(
      "stmt(objcMessageExpr(hasAnySelector(\"methodA\", \"methodB:\")))",
      &Error));
  EXPECT_EQ("", Error.toStringFull());
  auto M = OM->unconditionalConvertTo<Stmt>();
  EXPECT_TRUE(matchesObjC("@@interface I @@end "
                          "void foo(I* i) { [i methodA]; }", M));
}

d332 1
a332 1
  Code = "functionDecl(hasP";
d335 2
d338 1
a338 1
  EXPECT_EQ("arameter(", Comps[0].TypedText);
d341 1
a341 4
      Comps[0].MatcherDecl);

  EXPECT_EQ("aramA", Comps[1].TypedText);
  EXPECT_EQ("Matcher<Decl> hasParamA", Comps[1].MatcherDecl);
@


1.1.1.10
log
@Import clang r337282 from trunk
@
text
@d189 2
a190 2
  Values["hasParamA"] = VariantMatcher::SingleMatcher(
      functionDecl(hasParameter(0, hasName("a"))));
a238 11
TEST(ParserTest, VariadicMatchTest) {
  Diagnostics Error;
  llvm::Optional<DynTypedMatcher> OM(Parser::parseMatcherExpression(
      "stmt(objcMessageExpr(hasAnySelector(\"methodA\", \"methodB:\")))",
      &Error));
  EXPECT_EQ("", Error.toStringFull());
  auto M = OM->unconditionalConvertTo<Stmt>();
  EXPECT_TRUE(matchesObjC("@@interface I @@end "
                          "void foo(I* i) { [i methodA]; }", M));
}

d332 1
a332 1
  Code = "functionDecl(hasP";
d335 2
d338 1
a338 1
  EXPECT_EQ("arameter(", Comps[0].TypedText);
d341 1
a341 4
      Comps[0].MatcherDecl);

  EXPECT_EQ("aramA", Comps[1].TypedText);
  EXPECT_EQ("Matcher<Decl> hasParamA", Comps[1].MatcherDecl);
@


1.1.1.11
log
@Mark old LLVM instance as dead.
@
text
@@


1.1.1.5.4.1
log
@file ParserTest.cpp was added on branch tls-maxphys on 2014-08-19 23:49:27 +0000
@
text
@d1 290
@


1.1.1.5.4.2
log
@Rebase to HEAD as of a few days ago.
@
text
@a0 290
//===- unittest/ASTMatchers/Dynamic/ParserTest.cpp - Parser unit tests -===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===-------------------------------------------------------------------===//

#include "../ASTMatchersTest.h"
#include "clang/ASTMatchers/Dynamic/Parser.h"
#include "clang/ASTMatchers/Dynamic/Registry.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringMap.h"
#include "gtest/gtest.h"
#include <string>
#include <vector>

namespace clang {
namespace ast_matchers {
namespace dynamic {
namespace {

class MockSema : public Parser::Sema {
public:
  virtual ~MockSema() {}

  uint64_t expectMatcher(StringRef MatcherName) {
    ast_matchers::internal::Matcher<Stmt> M = stmt();
    ExpectedMatchers.insert(std::make_pair(MatcherName, M));
    return M.getID();
  }

  void parse(StringRef Code) {
    Diagnostics Error;
    VariantValue Value;
    Parser::parseExpression(Code, this, &Value, &Error);
    Values.push_back(Value);
    Errors.push_back(Error.toStringFull());
  }

  llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName) {
    const ExpectedMatchersTy::value_type *Matcher =
        &*ExpectedMatchers.find(MatcherName);
    return reinterpret_cast<MatcherCtor>(Matcher);
  }

  VariantMatcher actOnMatcherExpression(MatcherCtor Ctor,
                                        const SourceRange &NameRange,
                                        StringRef BindID,
                                        ArrayRef<ParserValue> Args,
                                        Diagnostics *Error) {
    const ExpectedMatchersTy::value_type *Matcher =
        reinterpret_cast<const ExpectedMatchersTy::value_type *>(Ctor);
    MatcherInfo ToStore = { Matcher->first, NameRange, Args, BindID };
    Matchers.push_back(ToStore);
    return VariantMatcher::SingleMatcher(Matcher->second);
  }

  struct MatcherInfo {
    StringRef MatcherName;
    SourceRange NameRange;
    std::vector<ParserValue> Args;
    std::string BoundID;
  };

  std::vector<std::string> Errors;
  std::vector<VariantValue> Values;
  std::vector<MatcherInfo> Matchers;
  typedef std::map<std::string, ast_matchers::internal::Matcher<Stmt> >
  ExpectedMatchersTy;
  ExpectedMatchersTy ExpectedMatchers;
};

TEST(ParserTest, ParseUnsigned) {
  MockSema Sema;
  Sema.parse("0");
  Sema.parse("123");
  Sema.parse("0x1f");
  Sema.parse("12345678901");
  Sema.parse("1a1");
  EXPECT_EQ(5U, Sema.Values.size());
  EXPECT_EQ(0U, Sema.Values[0].getUnsigned());
  EXPECT_EQ(123U, Sema.Values[1].getUnsigned());
  EXPECT_EQ(31U, Sema.Values[2].getUnsigned());
  EXPECT_EQ("1:1: Error parsing unsigned token: <12345678901>", Sema.Errors[3]);
  EXPECT_EQ("1:1: Error parsing unsigned token: <1a1>", Sema.Errors[4]);
}

TEST(ParserTest, ParseString) {
  MockSema Sema;
  Sema.parse("\"Foo\"");
  Sema.parse("\"\"");
  Sema.parse("\"Baz");
  EXPECT_EQ(3ULL, Sema.Values.size());
  EXPECT_EQ("Foo", Sema.Values[0].getString());
  EXPECT_EQ("", Sema.Values[1].getString());
  EXPECT_EQ("1:1: Error parsing string token: <\"Baz>", Sema.Errors[2]);
}

bool matchesRange(const SourceRange &Range, unsigned StartLine,
                  unsigned EndLine, unsigned StartColumn, unsigned EndColumn) {
  EXPECT_EQ(StartLine, Range.Start.Line);
  EXPECT_EQ(EndLine, Range.End.Line);
  EXPECT_EQ(StartColumn, Range.Start.Column);
  EXPECT_EQ(EndColumn, Range.End.Column);
  return Range.Start.Line == StartLine && Range.End.Line == EndLine &&
         Range.Start.Column == StartColumn && Range.End.Column == EndColumn;
}

llvm::Optional<DynTypedMatcher> getSingleMatcher(const VariantValue &Value) {
  llvm::Optional<DynTypedMatcher> Result =
      Value.getMatcher().getSingleMatcher();
  EXPECT_TRUE(Result.hasValue());
  return Result;
}

TEST(ParserTest, ParseMatcher) {
  MockSema Sema;
  const uint64_t ExpectedFoo = Sema.expectMatcher("Foo");
  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]);
  }

  EXPECT_EQ(1ULL, Sema.Values.size());
  EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID());

  EXPECT_EQ(3ULL, Sema.Matchers.size());
  const MockSema::MatcherInfo Bar = Sema.Matchers[0];
  EXPECT_EQ("Bar", Bar.MatcherName);
  EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 17));
  EXPECT_EQ(1ULL, Bar.Args.size());
  EXPECT_EQ(17U, Bar.Args[0].Value.getUnsigned());

  const MockSema::MatcherInfo Baz = Sema.Matchers[1];
  EXPECT_EQ("Baz", Baz.MatcherName);
  EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 19, 10));
  EXPECT_EQ(1ULL, Baz.Args.size());
  EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString());

  const MockSema::MatcherInfo Foo = Sema.Matchers[2];
  EXPECT_EQ("Foo", Foo.MatcherName);
  EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12));
  EXPECT_EQ(2ULL, Foo.Args.size());
  EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID());
  EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID());
  EXPECT_EQ("Yo!", Foo.BoundID);
}

using ast_matchers::internal::Matcher;

TEST(ParserTest, FullParserTest) {
  Diagnostics Error;
  llvm::Optional<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression(
      "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral()),"
      "                                      hasOperatorName(\"+\"))))",
      &Error));
  EXPECT_EQ("", Error.toStringFull());
  Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>();
  EXPECT_TRUE(matches("int x = 1 + false;", M));
  EXPECT_FALSE(matches("int x = true + 1;", M));
  EXPECT_FALSE(matches("int x = 1 - false;", M));
  EXPECT_FALSE(matches("int x = true - 1;", M));

  llvm::Optional<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression(
      "functionDecl(hasParameter(1, hasName(\"x\")))", &Error));
  EXPECT_EQ("", Error.toStringFull());
  M = HasParameter->unconditionalConvertTo<Decl>();

  EXPECT_TRUE(matches("void f(int a, int x);", M));
  EXPECT_FALSE(matches("void f(int x, int a);", M));

  // Test named values.
  struct NamedSema : public Parser::RegistrySema {
   public:
    virtual VariantValue getNamedValue(StringRef Name) {
      if (Name == "nameX")
        return std::string("x");
      if (Name == "param0")
        return VariantMatcher::SingleMatcher(hasParameter(0, hasName("a")));
      return VariantValue();
    }
  };
  NamedSema Sema;
  llvm::Optional<DynTypedMatcher> HasParameterWithNamedValues(
      Parser::parseMatcherExpression(
          "functionDecl(param0, hasParameter(1, hasName(nameX)))", &Sema,
          &Error));
  EXPECT_EQ("", Error.toStringFull());
  M = HasParameterWithNamedValues->unconditionalConvertTo<Decl>();

  EXPECT_TRUE(matches("void f(int a, int x);", M));
  EXPECT_FALSE(matches("void f(int x, int a);", M));


  EXPECT_TRUE(!Parser::parseMatcherExpression(
                   "hasInitializer(\n    binaryOperator(hasLHS(\"A\")))",
                   &Error).hasValue());
  EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n"
            "2:5: Error parsing argument 1 for matcher binaryOperator.\n"
            "2:20: Error building matcher hasLHS.\n"
            "2:27: Incorrect type for arg 1. "
            "(Expected = Matcher<Expr>) != (Actual = String)",
            Error.toStringFull());
}

std::string ParseWithError(StringRef Code) {
  Diagnostics Error;
  VariantValue Value;
  Parser::parseExpression(Code, &Value, &Error);
  return Error.toStringFull();
}

std::string ParseMatcherWithError(StringRef Code) {
  Diagnostics Error;
  Parser::parseMatcherExpression(Code, &Error);
  return Error.toStringFull();
}

TEST(ParserTest, Errors) {
  EXPECT_EQ(
      "1:5: Error parsing matcher. Found token <123> while looking for '('.",
      ParseWithError("Foo 123"));
  EXPECT_EQ(
      "1:1: Matcher not found: Foo\n"
      "1:9: Error parsing matcher. Found token <123> while looking for ','.",
      ParseWithError("Foo(\"A\" 123)"));
  EXPECT_EQ(
      "1:1: Error parsing argument 1 for matcher stmt.\n"
      "1:6: Value not found: someValue",
      ParseWithError("stmt(someValue)"));
  EXPECT_EQ(
      "1:1: Matcher not found: Foo\n"
      "1:4: Error parsing matcher. Found end-of-code while looking for ')'.",
      ParseWithError("Foo("));
  EXPECT_EQ("1:1: End of code found while looking for token.",
            ParseWithError(""));
  EXPECT_EQ("Input value is not a matcher expression.",
            ParseMatcherWithError("\"A\""));
  EXPECT_EQ("1:1: Matcher not found: Foo\n"
            "1:1: Error parsing argument 1 for matcher Foo.\n"
            "1:5: Invalid token <(> found when looking for a value.",
            ParseWithError("Foo(("));
  EXPECT_EQ("1:7: Expected end of code.", ParseWithError("expr()a"));
  EXPECT_EQ("1:11: Malformed bind() expression.",
            ParseWithError("isArrow().biind"));
  EXPECT_EQ("1:15: Malformed bind() expression.",
            ParseWithError("isArrow().bind"));
  EXPECT_EQ("1:16: Malformed bind() expression.",
            ParseWithError("isArrow().bind(foo"));
  EXPECT_EQ("1:21: Malformed bind() expression.",
            ParseWithError("isArrow().bind(\"foo\""));
  EXPECT_EQ("1:1: Error building matcher isArrow.\n"
            "1:1: Matcher does not support binding.",
            ParseWithError("isArrow().bind(\"foo\")"));
  EXPECT_EQ("Input value has unresolved overloaded type: "
            "Matcher<DoStmt|ForStmt|WhileStmt|CXXForRangeStmt>",
            ParseMatcherWithError("hasBody(stmt())"));
}

TEST(ParserTest, OverloadErrors) {
  EXPECT_EQ("1:1: Error building matcher callee.\n"
            "1:8: Candidate 1: Incorrect type for arg 1. "
            "(Expected = Matcher<Stmt>) != (Actual = String)\n"
            "1:8: Candidate 2: Incorrect type for arg 1. "
            "(Expected = Matcher<Decl>) != (Actual = String)",
            ParseWithError("callee(\"A\")"));
}

TEST(ParserTest, Completion) {
  std::vector<MatcherCompletion> Comps =
      Parser::completeExpression("while", 5);
  ASSERT_EQ(1u, Comps.size());
  EXPECT_EQ("Stmt(", Comps[0].TypedText);
  EXPECT_EQ("Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)",
            Comps[0].MatcherDecl);

  Comps = Parser::completeExpression("whileStmt().", 12);
  ASSERT_EQ(1u, Comps.size());
  EXPECT_EQ("bind(\"", Comps[0].TypedText);
  EXPECT_EQ("bind", Comps[0].MatcherDecl);
}

}  // end anonymous namespace
}  // end namespace dynamic
}  // end namespace ast_matchers
}  // end namespace clang
@


1.1.1.4.4.1
log
@file ParserTest.cpp was added on branch yamt-pagecache on 2014-05-22 16:19:48 +0000
@
text
@d1 265
@


1.1.1.4.4.2
log
@sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs.  ("Protocol error: too many arguments")
@
text
@a0 265
//===- unittest/ASTMatchers/Dynamic/ParserTest.cpp - Parser unit tests -===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===-------------------------------------------------------------------===//

#include "../ASTMatchersTest.h"
#include "clang/ASTMatchers/Dynamic/Parser.h"
#include "clang/ASTMatchers/Dynamic/Registry.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringMap.h"
#include "gtest/gtest.h"
#include <string>
#include <vector>

namespace clang {
namespace ast_matchers {
namespace dynamic {
namespace {

class MockSema : public Parser::Sema {
public:
  virtual ~MockSema() {}

  uint64_t expectMatcher(StringRef MatcherName) {
    ast_matchers::internal::Matcher<Stmt> M = stmt();
    ExpectedMatchers.insert(std::make_pair(MatcherName, M));
    return M.getID();
  }

  void parse(StringRef Code) {
    Diagnostics Error;
    VariantValue Value;
    Parser::parseExpression(Code, this, &Value, &Error);
    Values.push_back(Value);
    Errors.push_back(Error.toStringFull());
  }

  llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName,
                                                const SourceRange &NameRange,
                                                Diagnostics *Error) {
    const ExpectedMatchersTy::value_type *Matcher =
        &*ExpectedMatchers.find(MatcherName);
    return reinterpret_cast<MatcherCtor>(Matcher);
  }

  VariantMatcher actOnMatcherExpression(MatcherCtor Ctor,
                                        const SourceRange &NameRange,
                                        StringRef BindID,
                                        ArrayRef<ParserValue> Args,
                                        Diagnostics *Error) {
    const ExpectedMatchersTy::value_type *Matcher =
        reinterpret_cast<const ExpectedMatchersTy::value_type *>(Ctor);
    MatcherInfo ToStore = { Matcher->first, NameRange, Args, BindID };
    Matchers.push_back(ToStore);
    return VariantMatcher::SingleMatcher(Matcher->second);
  }

  struct MatcherInfo {
    StringRef MatcherName;
    SourceRange NameRange;
    std::vector<ParserValue> Args;
    std::string BoundID;
  };

  std::vector<std::string> Errors;
  std::vector<VariantValue> Values;
  std::vector<MatcherInfo> Matchers;
  typedef std::map<std::string, ast_matchers::internal::Matcher<Stmt> >
  ExpectedMatchersTy;
  ExpectedMatchersTy ExpectedMatchers;
};

TEST(ParserTest, ParseUnsigned) {
  MockSema Sema;
  Sema.parse("0");
  Sema.parse("123");
  Sema.parse("0x1f");
  Sema.parse("12345678901");
  Sema.parse("1a1");
  EXPECT_EQ(5U, Sema.Values.size());
  EXPECT_EQ(0U, Sema.Values[0].getUnsigned());
  EXPECT_EQ(123U, Sema.Values[1].getUnsigned());
  EXPECT_EQ(31U, Sema.Values[2].getUnsigned());
  EXPECT_EQ("1:1: Error parsing unsigned token: <12345678901>", Sema.Errors[3]);
  EXPECT_EQ("1:1: Error parsing unsigned token: <1a1>", Sema.Errors[4]);
}

TEST(ParserTest, ParseString) {
  MockSema Sema;
  Sema.parse("\"Foo\"");
  Sema.parse("\"\"");
  Sema.parse("\"Baz");
  EXPECT_EQ(3ULL, Sema.Values.size());
  EXPECT_EQ("Foo", Sema.Values[0].getString());
  EXPECT_EQ("", Sema.Values[1].getString());
  EXPECT_EQ("1:1: Error parsing string token: <\"Baz>", Sema.Errors[2]);
}

bool matchesRange(const SourceRange &Range, unsigned StartLine,
                  unsigned EndLine, unsigned StartColumn, unsigned EndColumn) {
  EXPECT_EQ(StartLine, Range.Start.Line);
  EXPECT_EQ(EndLine, Range.End.Line);
  EXPECT_EQ(StartColumn, Range.Start.Column);
  EXPECT_EQ(EndColumn, Range.End.Column);
  return Range.Start.Line == StartLine && Range.End.Line == EndLine &&
         Range.Start.Column == StartColumn && Range.End.Column == EndColumn;
}

llvm::Optional<DynTypedMatcher> getSingleMatcher(const VariantValue &Value) {
  llvm::Optional<DynTypedMatcher> Result =
      Value.getMatcher().getSingleMatcher();
  EXPECT_TRUE(Result.hasValue());
  return Result;
}

TEST(ParserTest, ParseMatcher) {
  MockSema Sema;
  const uint64_t ExpectedFoo = Sema.expectMatcher("Foo");
  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]);
  }

  EXPECT_EQ(1ULL, Sema.Values.size());
  EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID());

  EXPECT_EQ(3ULL, Sema.Matchers.size());
  const MockSema::MatcherInfo Bar = Sema.Matchers[0];
  EXPECT_EQ("Bar", Bar.MatcherName);
  EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 17));
  EXPECT_EQ(1ULL, Bar.Args.size());
  EXPECT_EQ(17U, Bar.Args[0].Value.getUnsigned());

  const MockSema::MatcherInfo Baz = Sema.Matchers[1];
  EXPECT_EQ("Baz", Baz.MatcherName);
  EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 19, 10));
  EXPECT_EQ(1ULL, Baz.Args.size());
  EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString());

  const MockSema::MatcherInfo Foo = Sema.Matchers[2];
  EXPECT_EQ("Foo", Foo.MatcherName);
  EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12));
  EXPECT_EQ(2ULL, Foo.Args.size());
  EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID());
  EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID());
  EXPECT_EQ("Yo!", Foo.BoundID);
}

using ast_matchers::internal::Matcher;

TEST(ParserTest, FullParserTest) {
  Diagnostics Error;
  llvm::Optional<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression(
      "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral()),"
      "                                      hasOperatorName(\"+\"))))",
      &Error));
  EXPECT_EQ("", Error.toStringFull());
  Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>();
  EXPECT_TRUE(matches("int x = 1 + false;", M));
  EXPECT_FALSE(matches("int x = true + 1;", M));
  EXPECT_FALSE(matches("int x = 1 - false;", M));
  EXPECT_FALSE(matches("int x = true - 1;", M));

  llvm::Optional<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression(
      "functionDecl(hasParameter(1, hasName(\"x\")))", &Error));
  EXPECT_EQ("", Error.toStringFull());
  M = HasParameter->unconditionalConvertTo<Decl>();

  EXPECT_TRUE(matches("void f(int a, int x);", M));
  EXPECT_FALSE(matches("void f(int x, int a);", M));

  EXPECT_TRUE(!Parser::parseMatcherExpression(
                   "hasInitializer(\n    binaryOperator(hasLHS(\"A\")))",
                   &Error).hasValue());
  EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n"
            "2:5: Error parsing argument 1 for matcher binaryOperator.\n"
            "2:20: Error building matcher hasLHS.\n"
            "2:27: Incorrect type for arg 1. "
            "(Expected = Matcher<Expr>) != (Actual = String)",
            Error.toStringFull());
}

std::string ParseWithError(StringRef Code) {
  Diagnostics Error;
  VariantValue Value;
  Parser::parseExpression(Code, &Value, &Error);
  return Error.toStringFull();
}

std::string ParseMatcherWithError(StringRef Code) {
  Diagnostics Error;
  Parser::parseMatcherExpression(Code, &Error);
  return Error.toStringFull();
}

TEST(ParserTest, Errors) {
  EXPECT_EQ(
      "1:5: Error parsing matcher. Found token <123> while looking for '('.",
      ParseWithError("Foo 123"));
  EXPECT_EQ(
      "1:1: Matcher not found: Foo\n"
      "1:9: Error parsing matcher. Found token <123> while looking for ','.",
      ParseWithError("Foo(\"A\" 123)"));
  EXPECT_EQ(
      "1:1: Matcher not found: Foo\n"
      "1:4: Error parsing matcher. Found end-of-code while looking for ')'.",
      ParseWithError("Foo("));
  EXPECT_EQ("1:1: End of code found while looking for token.",
            ParseWithError(""));
  EXPECT_EQ("Input value is not a matcher expression.",
            ParseMatcherWithError("\"A\""));
  EXPECT_EQ("1:1: Matcher not found: Foo\n"
            "1:1: Error parsing argument 1 for matcher Foo.\n"
            "1:5: Invalid token <(> found when looking for a value.",
            ParseWithError("Foo(("));
  EXPECT_EQ("1:7: Expected end of code.", ParseWithError("expr()a"));
  EXPECT_EQ("1:11: Malformed bind() expression.",
            ParseWithError("isArrow().biind"));
  EXPECT_EQ("1:15: Malformed bind() expression.",
            ParseWithError("isArrow().bind"));
  EXPECT_EQ("1:16: Malformed bind() expression.",
            ParseWithError("isArrow().bind(foo"));
  EXPECT_EQ("1:21: Malformed bind() expression.",
            ParseWithError("isArrow().bind(\"foo\""));
  EXPECT_EQ("1:1: Error building matcher isArrow.\n"
            "1:1: Matcher does not support binding.",
            ParseWithError("isArrow().bind(\"foo\")"));
  EXPECT_EQ("Input value has unresolved overloaded type: "
            "Matcher<DoStmt|ForStmt|WhileStmt>",
            ParseMatcherWithError("hasBody(stmt())"));
}

TEST(ParserTest, OverloadErrors) {
  EXPECT_EQ("1:1: Error building matcher callee.\n"
            "1:8: Candidate 1: Incorrect type for arg 1. "
            "(Expected = Matcher<Stmt>) != (Actual = String)\n"
            "1:8: Candidate 2: Incorrect type for arg 1. "
            "(Expected = Matcher<Decl>) != (Actual = String)",
            ParseWithError("callee(\"A\")"));
}

TEST(ParserTest, Completion) {
  std::vector<MatcherCompletion> Comps =
      Parser::completeExpression("while", 5);
  ASSERT_EQ(1u, Comps.size());
  EXPECT_EQ("Stmt(", Comps[0].TypedText);
  EXPECT_EQ("Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)",
            Comps[0].MatcherDecl);

  Comps = Parser::completeExpression("whileStmt().", 12);
  ASSERT_EQ(1u, Comps.size());
  EXPECT_EQ("bind(\"", Comps[0].TypedText);
  EXPECT_EQ("bind", Comps[0].MatcherDecl);
}

}  // end anonymous namespace
}  // end namespace dynamic
}  // end namespace ast_matchers
}  // end namespace clang
@


