xref: /llvm-project/clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp (revision c31b3524cb60481f1746c1faa1cb5eb31c04c0df)
1 //===- unittest/ASTMatchers/Dynamic/RegistryTest.cpp - Registry unit tests -===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===-----------------------------------------------------------------------===//
9 
10 #include <vector>
11 
12 #include "../ASTMatchersTest.h"
13 #include "clang/ASTMatchers/Dynamic/Registry.h"
14 #include "gtest/gtest.h"
15 
16 namespace clang {
17 namespace ast_matchers {
18 namespace dynamic {
19 namespace {
20 
21 using ast_matchers::internal::Matcher;
22 
23 DynTypedMatcher *constructMatcher(StringRef MatcherName, Diagnostics *Error) {
24   const std::vector<ParserValue> Args;
25   return Registry::constructMatcher(MatcherName, SourceRange(), Args, Error);
26 }
27 
28 DynTypedMatcher *constructMatcher(StringRef MatcherName,
29                                   const VariantValue &Arg1,
30                                   Diagnostics *Error) {
31   std::vector<ParserValue> Args(1);
32   Args[0].Value = Arg1;
33   return Registry::constructMatcher(MatcherName, SourceRange(), Args, Error);
34 }
35 
36 DynTypedMatcher *constructMatcher(StringRef MatcherName,
37                                   const VariantValue &Arg1,
38                                   const VariantValue &Arg2,
39                                   Diagnostics *Error) {
40   std::vector<ParserValue> Args(2);
41   Args[0].Value = Arg1;
42   Args[1].Value = Arg2;
43   return Registry::constructMatcher(MatcherName, SourceRange(), Args, Error);
44 }
45 
46 TEST(RegistryTest, CanConstructNoArgs) {
47   OwningPtr<DynTypedMatcher> IsArrowValue(constructMatcher("isArrow", NULL));
48   OwningPtr<DynTypedMatcher> BoolValue(constructMatcher("boolLiteral", NULL));
49 
50   const std::string ClassSnippet = "struct Foo { int x; };\n"
51                                    "Foo *foo = new Foo;\n"
52                                    "int i = foo->x;\n";
53   const std::string BoolSnippet = "bool Foo = true;\n";
54 
55   EXPECT_TRUE(matchesDynamic(ClassSnippet, *IsArrowValue));
56   EXPECT_TRUE(matchesDynamic(BoolSnippet, *BoolValue));
57   EXPECT_FALSE(matchesDynamic(ClassSnippet, *BoolValue));
58   EXPECT_FALSE(matchesDynamic(BoolSnippet, *IsArrowValue));
59 }
60 
61 TEST(RegistryTest, ConstructWithSimpleArgs) {
62   OwningPtr<DynTypedMatcher> Value(
63       constructMatcher("hasName", std::string("X"), NULL));
64   EXPECT_TRUE(matchesDynamic("class X {};", *Value));
65   EXPECT_FALSE(matchesDynamic("int x;", *Value));
66 
67   Value.reset(constructMatcher("parameterCountIs", 2, NULL));
68   EXPECT_TRUE(matchesDynamic("void foo(int,int);", *Value));
69   EXPECT_FALSE(matchesDynamic("void foo(int);", *Value));
70 }
71 
72 TEST(RegistryTest, ConstructWithMatcherArgs) {
73   OwningPtr<DynTypedMatcher> HasInitializerSimple(
74       constructMatcher("hasInitializer", stmt(), NULL));
75   OwningPtr<DynTypedMatcher> HasInitializerComplex(
76       constructMatcher("hasInitializer", callExpr(), NULL));
77 
78   std::string code = "int i;";
79   EXPECT_FALSE(matchesDynamic(code, *HasInitializerSimple));
80   EXPECT_FALSE(matchesDynamic(code, *HasInitializerComplex));
81 
82   code = "int i = 1;";
83   EXPECT_TRUE(matchesDynamic(code, *HasInitializerSimple));
84   EXPECT_FALSE(matchesDynamic(code, *HasInitializerComplex));
85 
86   code = "int y(); int i = y();";
87   EXPECT_TRUE(matchesDynamic(code, *HasInitializerSimple));
88   EXPECT_TRUE(matchesDynamic(code, *HasInitializerComplex));
89 
90   OwningPtr<DynTypedMatcher> HasParameter(
91       constructMatcher("hasParameter", 1, hasName("x"), NULL));
92   EXPECT_TRUE(matchesDynamic("void f(int a, int x);", *HasParameter));
93   EXPECT_FALSE(matchesDynamic("void f(int x, int a);", *HasParameter));
94 }
95 
96 TEST(RegistryTest, Errors) {
97   // Incorrect argument count.
98   OwningPtr<Diagnostics> Error(new Diagnostics());
99   EXPECT_TRUE(NULL == constructMatcher("hasInitializer", Error.get()));
100   EXPECT_EQ("Incorrect argument count. (Expected = 1) != (Actual = 0)",
101             Error->ToString());
102   Error.reset(new Diagnostics());
103   EXPECT_TRUE(NULL == constructMatcher("isArrow", std::string(), Error.get()));
104   EXPECT_EQ("Incorrect argument count. (Expected = 0) != (Actual = 1)",
105             Error->ToString());
106 
107   // Bad argument type
108   Error.reset(new Diagnostics());
109   EXPECT_TRUE(NULL == constructMatcher("ofClass", std::string(), Error.get()));
110   EXPECT_EQ("Incorrect type on function ofClass for arg 1.", Error->ToString());
111   Error.reset(new Diagnostics());
112   EXPECT_TRUE(NULL == constructMatcher("recordDecl", recordDecl(),
113                                        ::std::string(), Error.get()));
114   EXPECT_EQ("Incorrect type on function recordDecl for arg 2.",
115             Error->ToString());
116 }
117 
118 } // end anonymous namespace
119 } // end namespace dynamic
120 } // end namespace ast_matchers
121 } // end namespace clang
122