1f4a2713aSLionel Sambuc //===- unittest/AST/SourceLocationTest.cpp - AST source loc unit tests ----===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file contains tests for SourceLocation and SourceRange fields
11f4a2713aSLionel Sambuc // in AST nodes.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc // FIXME: In the long-term, when we test more than source locations, we may
14f4a2713aSLionel Sambuc // want to have a unit test file for an AST node (or group of related nodes),
15f4a2713aSLionel Sambuc // rather than a unit test file for source locations for all AST nodes.
16f4a2713aSLionel Sambuc //
17f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
18f4a2713aSLionel Sambuc
19f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
20*0a6a1f1dSLionel Sambuc #include "MatchVerifier.h"
21f4a2713aSLionel Sambuc #include "clang/ASTMatchers/ASTMatchFinder.h"
22f4a2713aSLionel Sambuc #include "clang/ASTMatchers/ASTMatchers.h"
23f4a2713aSLionel Sambuc #include "clang/Tooling/Tooling.h"
24f4a2713aSLionel Sambuc #include "gtest/gtest.h"
25f4a2713aSLionel Sambuc
26f4a2713aSLionel Sambuc namespace clang {
27f4a2713aSLionel Sambuc namespace ast_matchers {
28f4a2713aSLionel Sambuc
29f4a2713aSLionel Sambuc // FIXME: Pull the *Verifier tests into their own test file.
30f4a2713aSLionel Sambuc
TEST(MatchVerifier,ParseError)31f4a2713aSLionel Sambuc TEST(MatchVerifier, ParseError) {
32f4a2713aSLionel Sambuc LocationVerifier<VarDecl> Verifier;
33f4a2713aSLionel Sambuc Verifier.expectLocation(1, 1);
34f4a2713aSLionel Sambuc EXPECT_FALSE(Verifier.match("int i", varDecl()));
35f4a2713aSLionel Sambuc }
36f4a2713aSLionel Sambuc
TEST(MatchVerifier,NoMatch)37f4a2713aSLionel Sambuc TEST(MatchVerifier, NoMatch) {
38f4a2713aSLionel Sambuc LocationVerifier<VarDecl> Verifier;
39f4a2713aSLionel Sambuc Verifier.expectLocation(1, 1);
40f4a2713aSLionel Sambuc EXPECT_FALSE(Verifier.match("int i;", recordDecl()));
41f4a2713aSLionel Sambuc }
42f4a2713aSLionel Sambuc
TEST(MatchVerifier,WrongType)43f4a2713aSLionel Sambuc TEST(MatchVerifier, WrongType) {
44f4a2713aSLionel Sambuc LocationVerifier<RecordDecl> Verifier;
45f4a2713aSLionel Sambuc Verifier.expectLocation(1, 1);
46f4a2713aSLionel Sambuc EXPECT_FALSE(Verifier.match("int i;", varDecl()));
47f4a2713aSLionel Sambuc }
48f4a2713aSLionel Sambuc
TEST(LocationVerifier,WrongLocation)49f4a2713aSLionel Sambuc TEST(LocationVerifier, WrongLocation) {
50f4a2713aSLionel Sambuc LocationVerifier<VarDecl> Verifier;
51f4a2713aSLionel Sambuc Verifier.expectLocation(1, 1);
52f4a2713aSLionel Sambuc EXPECT_FALSE(Verifier.match("int i;", varDecl()));
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc
TEST(RangeVerifier,WrongRange)55f4a2713aSLionel Sambuc TEST(RangeVerifier, WrongRange) {
56f4a2713aSLionel Sambuc RangeVerifier<VarDecl> Verifier;
57f4a2713aSLionel Sambuc Verifier.expectRange(1, 1, 1, 1);
58f4a2713aSLionel Sambuc EXPECT_FALSE(Verifier.match("int i;", varDecl()));
59f4a2713aSLionel Sambuc }
60f4a2713aSLionel Sambuc
61f4a2713aSLionel Sambuc class LabelDeclRangeVerifier : public RangeVerifier<LabelStmt> {
62f4a2713aSLionel Sambuc protected:
getRange(const LabelStmt & Node)63f4a2713aSLionel Sambuc virtual SourceRange getRange(const LabelStmt &Node) {
64f4a2713aSLionel Sambuc return Node.getDecl()->getSourceRange();
65f4a2713aSLionel Sambuc }
66f4a2713aSLionel Sambuc };
67f4a2713aSLionel Sambuc
TEST(LabelDecl,Range)68f4a2713aSLionel Sambuc TEST(LabelDecl, Range) {
69f4a2713aSLionel Sambuc LabelDeclRangeVerifier Verifier;
70f4a2713aSLionel Sambuc Verifier.expectRange(1, 12, 1, 12);
71f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match("void f() { l: return; }", labelStmt()));
72f4a2713aSLionel Sambuc }
73f4a2713aSLionel Sambuc
TEST(LabelStmt,Range)74f4a2713aSLionel Sambuc TEST(LabelStmt, Range) {
75f4a2713aSLionel Sambuc RangeVerifier<LabelStmt> Verifier;
76f4a2713aSLionel Sambuc Verifier.expectRange(1, 12, 1, 15);
77f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match("void f() { l: return; }", labelStmt()));
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc
TEST(ParmVarDecl,KNRLocation)80f4a2713aSLionel Sambuc TEST(ParmVarDecl, KNRLocation) {
81f4a2713aSLionel Sambuc LocationVerifier<ParmVarDecl> Verifier;
82f4a2713aSLionel Sambuc Verifier.expectLocation(1, 8);
83f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match("void f(i) {}", varDecl(), Lang_C));
84f4a2713aSLionel Sambuc }
85f4a2713aSLionel Sambuc
TEST(ParmVarDecl,KNRRange)86f4a2713aSLionel Sambuc TEST(ParmVarDecl, KNRRange) {
87f4a2713aSLionel Sambuc RangeVerifier<ParmVarDecl> Verifier;
88f4a2713aSLionel Sambuc Verifier.expectRange(1, 8, 1, 8);
89f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match("void f(i) {}", varDecl(), Lang_C));
90f4a2713aSLionel Sambuc }
91f4a2713aSLionel Sambuc
TEST(CXXNewExpr,ArrayRange)92f4a2713aSLionel Sambuc TEST(CXXNewExpr, ArrayRange) {
93f4a2713aSLionel Sambuc RangeVerifier<CXXNewExpr> Verifier;
94f4a2713aSLionel Sambuc Verifier.expectRange(1, 12, 1, 22);
95f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match("void f() { new int[10]; }", newExpr()));
96f4a2713aSLionel Sambuc }
97f4a2713aSLionel Sambuc
TEST(CXXNewExpr,ParenRange)98f4a2713aSLionel Sambuc TEST(CXXNewExpr, ParenRange) {
99f4a2713aSLionel Sambuc RangeVerifier<CXXNewExpr> Verifier;
100f4a2713aSLionel Sambuc Verifier.expectRange(1, 12, 1, 20);
101f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match("void f() { new int(); }", newExpr()));
102f4a2713aSLionel Sambuc }
103f4a2713aSLionel Sambuc
TEST(MemberExpr,ImplicitMemberRange)104f4a2713aSLionel Sambuc TEST(MemberExpr, ImplicitMemberRange) {
105f4a2713aSLionel Sambuc RangeVerifier<MemberExpr> Verifier;
106f4a2713aSLionel Sambuc Verifier.expectRange(2, 30, 2, 30);
107f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match("struct S { operator int() const; };\n"
108f4a2713aSLionel Sambuc "int foo(const S& s) { return s; }",
109f4a2713aSLionel Sambuc memberExpr()));
110f4a2713aSLionel Sambuc }
111f4a2713aSLionel Sambuc
TEST(VarDecl,VMTypeFixedVarDeclRange)112f4a2713aSLionel Sambuc TEST(VarDecl, VMTypeFixedVarDeclRange) {
113f4a2713aSLionel Sambuc RangeVerifier<VarDecl> Verifier;
114f4a2713aSLionel Sambuc Verifier.expectRange(1, 1, 1, 23);
115f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match("int a[(int)(void*)1234];",
116f4a2713aSLionel Sambuc varDecl(), Lang_C89));
117f4a2713aSLionel Sambuc }
118f4a2713aSLionel Sambuc
TEST(CXXConstructorDecl,NoRetFunTypeLocRange)119f4a2713aSLionel Sambuc TEST(CXXConstructorDecl, NoRetFunTypeLocRange) {
120f4a2713aSLionel Sambuc RangeVerifier<CXXConstructorDecl> Verifier;
121f4a2713aSLionel Sambuc Verifier.expectRange(1, 11, 1, 13);
122f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match("class C { C(); };", functionDecl()));
123f4a2713aSLionel Sambuc }
124f4a2713aSLionel Sambuc
TEST(CompoundLiteralExpr,CompoundVectorLiteralRange)125f4a2713aSLionel Sambuc TEST(CompoundLiteralExpr, CompoundVectorLiteralRange) {
126f4a2713aSLionel Sambuc RangeVerifier<CompoundLiteralExpr> Verifier;
127f4a2713aSLionel Sambuc Verifier.expectRange(2, 11, 2, 22);
128f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match(
129f4a2713aSLionel Sambuc "typedef int int2 __attribute__((ext_vector_type(2)));\n"
130f4a2713aSLionel Sambuc "int2 i2 = (int2){1, 2};", compoundLiteralExpr()));
131f4a2713aSLionel Sambuc }
132f4a2713aSLionel Sambuc
TEST(CompoundLiteralExpr,ParensCompoundVectorLiteralRange)133f4a2713aSLionel Sambuc TEST(CompoundLiteralExpr, ParensCompoundVectorLiteralRange) {
134f4a2713aSLionel Sambuc RangeVerifier<CompoundLiteralExpr> Verifier;
135f4a2713aSLionel Sambuc Verifier.expectRange(2, 20, 2, 31);
136f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match(
137f4a2713aSLionel Sambuc "typedef int int2 __attribute__((ext_vector_type(2)));\n"
138f4a2713aSLionel Sambuc "constant int2 i2 = (int2)(1, 2);",
139f4a2713aSLionel Sambuc compoundLiteralExpr(), Lang_OpenCL));
140f4a2713aSLionel Sambuc }
141f4a2713aSLionel Sambuc
TEST(InitListExpr,VectorLiteralListBraceRange)142f4a2713aSLionel Sambuc TEST(InitListExpr, VectorLiteralListBraceRange) {
143f4a2713aSLionel Sambuc RangeVerifier<InitListExpr> Verifier;
144f4a2713aSLionel Sambuc Verifier.expectRange(2, 17, 2, 22);
145f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match(
146f4a2713aSLionel Sambuc "typedef int int2 __attribute__((ext_vector_type(2)));\n"
147f4a2713aSLionel Sambuc "int2 i2 = (int2){1, 2};", initListExpr()));
148f4a2713aSLionel Sambuc }
149f4a2713aSLionel Sambuc
TEST(InitListExpr,VectorLiteralInitListParens)150f4a2713aSLionel Sambuc TEST(InitListExpr, VectorLiteralInitListParens) {
151f4a2713aSLionel Sambuc RangeVerifier<InitListExpr> Verifier;
152f4a2713aSLionel Sambuc Verifier.expectRange(2, 26, 2, 31);
153f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match(
154f4a2713aSLionel Sambuc "typedef int int2 __attribute__((ext_vector_type(2)));\n"
155f4a2713aSLionel Sambuc "constant int2 i2 = (int2)(1, 2);", initListExpr(), Lang_OpenCL));
156f4a2713aSLionel Sambuc }
157f4a2713aSLionel Sambuc
158f4a2713aSLionel Sambuc class TemplateAngleBracketLocRangeVerifier : public RangeVerifier<TypeLoc> {
159f4a2713aSLionel Sambuc protected:
getRange(const TypeLoc & Node)160f4a2713aSLionel Sambuc virtual SourceRange getRange(const TypeLoc &Node) {
161f4a2713aSLionel Sambuc TemplateSpecializationTypeLoc T =
162f4a2713aSLionel Sambuc Node.getUnqualifiedLoc().castAs<TemplateSpecializationTypeLoc>();
163f4a2713aSLionel Sambuc assert(!T.isNull());
164f4a2713aSLionel Sambuc return SourceRange(T.getLAngleLoc(), T.getRAngleLoc());
165f4a2713aSLionel Sambuc }
166f4a2713aSLionel Sambuc };
167f4a2713aSLionel Sambuc
TEST(TemplateSpecializationTypeLoc,AngleBracketLocations)168f4a2713aSLionel Sambuc TEST(TemplateSpecializationTypeLoc, AngleBracketLocations) {
169f4a2713aSLionel Sambuc TemplateAngleBracketLocRangeVerifier Verifier;
170f4a2713aSLionel Sambuc Verifier.expectRange(2, 8, 2, 10);
171f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match(
172f4a2713aSLionel Sambuc "template<typename T> struct A {}; struct B{}; void f(\n"
173f4a2713aSLionel Sambuc "const A<B>&);",
174f4a2713aSLionel Sambuc loc(templateSpecializationType())));
175f4a2713aSLionel Sambuc }
176f4a2713aSLionel Sambuc
TEST(CXXNewExpr,TypeParenRange)177f4a2713aSLionel Sambuc TEST(CXXNewExpr, TypeParenRange) {
178f4a2713aSLionel Sambuc RangeVerifier<CXXNewExpr> Verifier;
179f4a2713aSLionel Sambuc Verifier.expectRange(1, 10, 1, 18);
180f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match("int* a = new (int);", newExpr()));
181f4a2713aSLionel Sambuc }
182f4a2713aSLionel Sambuc
183f4a2713aSLionel Sambuc class UnaryTransformTypeLocParensRangeVerifier : public RangeVerifier<TypeLoc> {
184f4a2713aSLionel Sambuc protected:
getRange(const TypeLoc & Node)185f4a2713aSLionel Sambuc virtual SourceRange getRange(const TypeLoc &Node) {
186f4a2713aSLionel Sambuc UnaryTransformTypeLoc T =
187f4a2713aSLionel Sambuc Node.getUnqualifiedLoc().castAs<UnaryTransformTypeLoc>();
188f4a2713aSLionel Sambuc assert(!T.isNull());
189f4a2713aSLionel Sambuc return SourceRange(T.getLParenLoc(), T.getRParenLoc());
190f4a2713aSLionel Sambuc }
191f4a2713aSLionel Sambuc };
192f4a2713aSLionel Sambuc
TEST(UnaryTransformTypeLoc,ParensRange)193f4a2713aSLionel Sambuc TEST(UnaryTransformTypeLoc, ParensRange) {
194f4a2713aSLionel Sambuc UnaryTransformTypeLocParensRangeVerifier Verifier;
195f4a2713aSLionel Sambuc Verifier.expectRange(3, 26, 3, 28);
196f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match(
197f4a2713aSLionel Sambuc "template <typename T>\n"
198f4a2713aSLionel Sambuc "struct S {\n"
199f4a2713aSLionel Sambuc "typedef __underlying_type(T) type;\n"
200f4a2713aSLionel Sambuc "};",
201f4a2713aSLionel Sambuc loc(unaryTransformType())));
202f4a2713aSLionel Sambuc }
203f4a2713aSLionel Sambuc
TEST(CXXFunctionalCastExpr,SourceRange)204f4a2713aSLionel Sambuc TEST(CXXFunctionalCastExpr, SourceRange) {
205f4a2713aSLionel Sambuc RangeVerifier<CXXFunctionalCastExpr> Verifier;
206f4a2713aSLionel Sambuc Verifier.expectRange(2, 10, 2, 14);
207f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match(
208f4a2713aSLionel Sambuc "int foo() {\n"
209f4a2713aSLionel Sambuc " return int{};\n"
210f4a2713aSLionel Sambuc "}",
211f4a2713aSLionel Sambuc functionalCastExpr(), Lang_CXX11));
212f4a2713aSLionel Sambuc }
213f4a2713aSLionel Sambuc
TEST(CXXConstructExpr,SourceRange)214*0a6a1f1dSLionel Sambuc TEST(CXXConstructExpr, SourceRange) {
215*0a6a1f1dSLionel Sambuc RangeVerifier<CXXConstructExpr> Verifier;
216*0a6a1f1dSLionel Sambuc Verifier.expectRange(3, 14, 3, 19);
217*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match(
218*0a6a1f1dSLionel Sambuc "struct A { A(int, int); };\n"
219*0a6a1f1dSLionel Sambuc "void f(A a);\n"
220*0a6a1f1dSLionel Sambuc "void g() { f({0, 0}); }",
221*0a6a1f1dSLionel Sambuc constructExpr(), Lang_CXX11));
222*0a6a1f1dSLionel Sambuc }
223*0a6a1f1dSLionel Sambuc
TEST(CXXTemporaryObjectExpr,SourceRange)224f4a2713aSLionel Sambuc TEST(CXXTemporaryObjectExpr, SourceRange) {
225f4a2713aSLionel Sambuc RangeVerifier<CXXTemporaryObjectExpr> Verifier;
226f4a2713aSLionel Sambuc Verifier.expectRange(2, 6, 2, 12);
227f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match(
228f4a2713aSLionel Sambuc "struct A { A(int, int); };\n"
229f4a2713aSLionel Sambuc "A a( A{0, 0} );",
230f4a2713aSLionel Sambuc temporaryObjectExpr(), Lang_CXX11));
231f4a2713aSLionel Sambuc }
232f4a2713aSLionel Sambuc
TEST(CXXUnresolvedConstructExpr,SourceRange)233f4a2713aSLionel Sambuc TEST(CXXUnresolvedConstructExpr, SourceRange) {
234f4a2713aSLionel Sambuc RangeVerifier<CXXUnresolvedConstructExpr> Verifier;
235f4a2713aSLionel Sambuc Verifier.expectRange(3, 10, 3, 12);
236f4a2713aSLionel Sambuc std::vector<std::string> Args;
237f4a2713aSLionel Sambuc Args.push_back("-fno-delayed-template-parsing");
238f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match(
239f4a2713aSLionel Sambuc "template <typename U>\n"
240f4a2713aSLionel Sambuc "U foo() {\n"
241f4a2713aSLionel Sambuc " return U{};\n"
242f4a2713aSLionel Sambuc "}",
243f4a2713aSLionel Sambuc unresolvedConstructExpr(), Args, Lang_CXX11));
244f4a2713aSLionel Sambuc }
245f4a2713aSLionel Sambuc
TEST(UsingDecl,SourceRange)246f4a2713aSLionel Sambuc TEST(UsingDecl, SourceRange) {
247f4a2713aSLionel Sambuc RangeVerifier<UsingDecl> Verifier;
248f4a2713aSLionel Sambuc Verifier.expectRange(2, 22, 2, 25);
249f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match(
250f4a2713aSLionel Sambuc "class B { protected: int i; };\n"
251f4a2713aSLionel Sambuc "class D : public B { B::i; };",
252f4a2713aSLionel Sambuc usingDecl()));
253f4a2713aSLionel Sambuc }
254f4a2713aSLionel Sambuc
TEST(UnresolvedUsingValueDecl,SourceRange)255f4a2713aSLionel Sambuc TEST(UnresolvedUsingValueDecl, SourceRange) {
256f4a2713aSLionel Sambuc RangeVerifier<UnresolvedUsingValueDecl> Verifier;
257f4a2713aSLionel Sambuc Verifier.expectRange(3, 3, 3, 6);
258f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match(
259f4a2713aSLionel Sambuc "template <typename B>\n"
260f4a2713aSLionel Sambuc "class D : public B {\n"
261f4a2713aSLionel Sambuc " B::i;\n"
262f4a2713aSLionel Sambuc "};",
263f4a2713aSLionel Sambuc unresolvedUsingValueDecl()));
264f4a2713aSLionel Sambuc }
265f4a2713aSLionel Sambuc
TEST(FriendDecl,FriendNonMemberFunctionLocation)266*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendNonMemberFunctionLocation) {
267*0a6a1f1dSLionel Sambuc LocationVerifier<FriendDecl> Verifier;
268*0a6a1f1dSLionel Sambuc Verifier.expectLocation(2, 13);
269*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("struct A {\n"
270*0a6a1f1dSLionel Sambuc "friend void f();\n"
271*0a6a1f1dSLionel Sambuc "};\n",
272*0a6a1f1dSLionel Sambuc friendDecl()));
273*0a6a1f1dSLionel Sambuc }
274*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendNonMemberFunctionRange)275*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendNonMemberFunctionRange) {
276*0a6a1f1dSLionel Sambuc RangeVerifier<FriendDecl> Verifier;
277*0a6a1f1dSLionel Sambuc Verifier.expectRange(2, 1, 2, 15);
278*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("struct A {\n"
279*0a6a1f1dSLionel Sambuc "friend void f();\n"
280*0a6a1f1dSLionel Sambuc "};\n",
281*0a6a1f1dSLionel Sambuc friendDecl()));
282*0a6a1f1dSLionel Sambuc }
283*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendNonMemberFunctionDefinitionLocation)284*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendNonMemberFunctionDefinitionLocation) {
285*0a6a1f1dSLionel Sambuc LocationVerifier<FriendDecl> Verifier;
286*0a6a1f1dSLionel Sambuc Verifier.expectLocation(2, 12);
287*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("struct A {\n"
288*0a6a1f1dSLionel Sambuc "friend int f() { return 0; }\n"
289*0a6a1f1dSLionel Sambuc "};\n",
290*0a6a1f1dSLionel Sambuc friendDecl()));
291*0a6a1f1dSLionel Sambuc }
292*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendNonMemberFunctionDefinitionRange)293*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendNonMemberFunctionDefinitionRange) {
294*0a6a1f1dSLionel Sambuc RangeVerifier<FriendDecl> Verifier;
295*0a6a1f1dSLionel Sambuc Verifier.expectRange(2, 1, 2, 28);
296*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("struct A {\n"
297*0a6a1f1dSLionel Sambuc "friend int f() { return 0; }\n"
298*0a6a1f1dSLionel Sambuc "};\n",
299*0a6a1f1dSLionel Sambuc friendDecl()));
300*0a6a1f1dSLionel Sambuc }
301*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendElaboratedTypeLocation)302*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendElaboratedTypeLocation) {
303*0a6a1f1dSLionel Sambuc LocationVerifier<FriendDecl> Verifier;
304*0a6a1f1dSLionel Sambuc Verifier.expectLocation(2, 8);
305*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("struct A {\n"
306*0a6a1f1dSLionel Sambuc "friend class B;\n"
307*0a6a1f1dSLionel Sambuc "};\n",
308*0a6a1f1dSLionel Sambuc friendDecl()));
309*0a6a1f1dSLionel Sambuc }
310*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendElaboratedTypeRange)311*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendElaboratedTypeRange) {
312*0a6a1f1dSLionel Sambuc RangeVerifier<FriendDecl> Verifier;
313*0a6a1f1dSLionel Sambuc Verifier.expectRange(2, 1, 2, 14);
314*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("struct A {\n"
315*0a6a1f1dSLionel Sambuc "friend class B;\n"
316*0a6a1f1dSLionel Sambuc "};\n",
317*0a6a1f1dSLionel Sambuc friendDecl()));
318*0a6a1f1dSLionel Sambuc }
319*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendSimpleTypeLocation)320*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendSimpleTypeLocation) {
321*0a6a1f1dSLionel Sambuc LocationVerifier<FriendDecl> Verifier;
322*0a6a1f1dSLionel Sambuc Verifier.expectLocation(3, 8);
323*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("class B;\n"
324*0a6a1f1dSLionel Sambuc "struct A {\n"
325*0a6a1f1dSLionel Sambuc "friend B;\n"
326*0a6a1f1dSLionel Sambuc "};\n",
327*0a6a1f1dSLionel Sambuc friendDecl(), Lang_CXX11));
328*0a6a1f1dSLionel Sambuc }
329*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendSimpleTypeRange)330*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendSimpleTypeRange) {
331*0a6a1f1dSLionel Sambuc RangeVerifier<FriendDecl> Verifier;
332*0a6a1f1dSLionel Sambuc Verifier.expectRange(3, 1, 3, 8);
333*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("class B;\n"
334*0a6a1f1dSLionel Sambuc "struct A {\n"
335*0a6a1f1dSLionel Sambuc "friend B;\n"
336*0a6a1f1dSLionel Sambuc "};\n",
337*0a6a1f1dSLionel Sambuc friendDecl(), Lang_CXX11));
338*0a6a1f1dSLionel Sambuc }
339*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendTemplateParameterLocation)340*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendTemplateParameterLocation) {
341*0a6a1f1dSLionel Sambuc LocationVerifier<FriendDecl> Verifier;
342*0a6a1f1dSLionel Sambuc Verifier.expectLocation(3, 8);
343*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("template <typename T>\n"
344*0a6a1f1dSLionel Sambuc "struct A {\n"
345*0a6a1f1dSLionel Sambuc "friend T;\n"
346*0a6a1f1dSLionel Sambuc "};\n",
347*0a6a1f1dSLionel Sambuc friendDecl(), Lang_CXX11));
348*0a6a1f1dSLionel Sambuc }
349*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendTemplateParameterRange)350*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendTemplateParameterRange) {
351*0a6a1f1dSLionel Sambuc RangeVerifier<FriendDecl> Verifier;
352*0a6a1f1dSLionel Sambuc Verifier.expectRange(3, 1, 3, 8);
353*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("template <typename T>\n"
354*0a6a1f1dSLionel Sambuc "struct A {\n"
355*0a6a1f1dSLionel Sambuc "friend T;\n"
356*0a6a1f1dSLionel Sambuc "};\n",
357*0a6a1f1dSLionel Sambuc friendDecl(), Lang_CXX11));
358*0a6a1f1dSLionel Sambuc }
359*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendDecltypeLocation)360*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendDecltypeLocation) {
361*0a6a1f1dSLionel Sambuc LocationVerifier<FriendDecl> Verifier;
362*0a6a1f1dSLionel Sambuc Verifier.expectLocation(4, 8);
363*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("struct A;\n"
364*0a6a1f1dSLionel Sambuc "A foo();\n"
365*0a6a1f1dSLionel Sambuc "struct A {\n"
366*0a6a1f1dSLionel Sambuc "friend decltype(foo());\n"
367*0a6a1f1dSLionel Sambuc "};\n",
368*0a6a1f1dSLionel Sambuc friendDecl(), Lang_CXX11));
369*0a6a1f1dSLionel Sambuc }
370*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendDecltypeRange)371*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendDecltypeRange) {
372*0a6a1f1dSLionel Sambuc RangeVerifier<FriendDecl> Verifier;
373*0a6a1f1dSLionel Sambuc Verifier.expectRange(4, 1, 4, 8);
374*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("struct A;\n"
375*0a6a1f1dSLionel Sambuc "A foo();\n"
376*0a6a1f1dSLionel Sambuc "struct A {\n"
377*0a6a1f1dSLionel Sambuc "friend decltype(foo());\n"
378*0a6a1f1dSLionel Sambuc "};\n",
379*0a6a1f1dSLionel Sambuc friendDecl(), Lang_CXX11));
380*0a6a1f1dSLionel Sambuc }
381*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendConstructorDestructorLocation)382*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendConstructorDestructorLocation) {
383*0a6a1f1dSLionel Sambuc const std::string Code = "struct B {\n"
384*0a6a1f1dSLionel Sambuc "B();\n"
385*0a6a1f1dSLionel Sambuc "~B();\n"
386*0a6a1f1dSLionel Sambuc "};\n"
387*0a6a1f1dSLionel Sambuc "struct A {\n"
388*0a6a1f1dSLionel Sambuc "friend B::B(), B::~B();\n"
389*0a6a1f1dSLionel Sambuc "};\n";
390*0a6a1f1dSLionel Sambuc LocationVerifier<FriendDecl> ConstructorVerifier;
391*0a6a1f1dSLionel Sambuc ConstructorVerifier.expectLocation(6, 11);
392*0a6a1f1dSLionel Sambuc EXPECT_TRUE(ConstructorVerifier.match(
393*0a6a1f1dSLionel Sambuc Code, friendDecl(has(constructorDecl(ofClass(hasName("B")))))));
394*0a6a1f1dSLionel Sambuc LocationVerifier<FriendDecl> DestructorVerifier;
395*0a6a1f1dSLionel Sambuc DestructorVerifier.expectLocation(6, 19);
396*0a6a1f1dSLionel Sambuc EXPECT_TRUE(DestructorVerifier.match(
397*0a6a1f1dSLionel Sambuc Code, friendDecl(has(destructorDecl(ofClass(hasName("B")))))));
398*0a6a1f1dSLionel Sambuc }
399*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendConstructorDestructorRange)400*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendConstructorDestructorRange) {
401*0a6a1f1dSLionel Sambuc const std::string Code = "struct B {\n"
402*0a6a1f1dSLionel Sambuc "B();\n"
403*0a6a1f1dSLionel Sambuc "~B();\n"
404*0a6a1f1dSLionel Sambuc "};\n"
405*0a6a1f1dSLionel Sambuc "struct A {\n"
406*0a6a1f1dSLionel Sambuc "friend B::B(), B::~B();\n"
407*0a6a1f1dSLionel Sambuc "};\n";
408*0a6a1f1dSLionel Sambuc RangeVerifier<FriendDecl> ConstructorVerifier;
409*0a6a1f1dSLionel Sambuc ConstructorVerifier.expectRange(6, 1, 6, 13);
410*0a6a1f1dSLionel Sambuc EXPECT_TRUE(ConstructorVerifier.match(
411*0a6a1f1dSLionel Sambuc Code, friendDecl(has(constructorDecl(ofClass(hasName("B")))))));
412*0a6a1f1dSLionel Sambuc RangeVerifier<FriendDecl> DestructorVerifier;
413*0a6a1f1dSLionel Sambuc DestructorVerifier.expectRange(6, 1, 6, 22);
414*0a6a1f1dSLionel Sambuc EXPECT_TRUE(DestructorVerifier.match(
415*0a6a1f1dSLionel Sambuc Code, friendDecl(has(destructorDecl(ofClass(hasName("B")))))));
416*0a6a1f1dSLionel Sambuc }
417*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendTemplateFunctionLocation)418*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendTemplateFunctionLocation) {
419*0a6a1f1dSLionel Sambuc LocationVerifier<FriendDecl> Verifier;
420*0a6a1f1dSLionel Sambuc Verifier.expectLocation(3, 13);
421*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("struct A {\n"
422*0a6a1f1dSLionel Sambuc "template <typename T>\n"
423*0a6a1f1dSLionel Sambuc "friend void f();\n"
424*0a6a1f1dSLionel Sambuc "};\n",
425*0a6a1f1dSLionel Sambuc friendDecl()));
426*0a6a1f1dSLionel Sambuc }
427*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendTemplateFunctionRange)428*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendTemplateFunctionRange) {
429*0a6a1f1dSLionel Sambuc RangeVerifier<FriendDecl> Verifier;
430*0a6a1f1dSLionel Sambuc Verifier.expectRange(2, 1, 3, 15);
431*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("struct A {\n"
432*0a6a1f1dSLionel Sambuc "template <typename T>\n"
433*0a6a1f1dSLionel Sambuc "friend void f();\n"
434*0a6a1f1dSLionel Sambuc "};\n",
435*0a6a1f1dSLionel Sambuc friendDecl()));
436*0a6a1f1dSLionel Sambuc }
437*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendTemplateClassLocation)438*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendTemplateClassLocation) {
439*0a6a1f1dSLionel Sambuc LocationVerifier<FriendDecl> Verifier;
440*0a6a1f1dSLionel Sambuc Verifier.expectLocation(3, 14);
441*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("struct A {\n"
442*0a6a1f1dSLionel Sambuc "template <typename T>\n"
443*0a6a1f1dSLionel Sambuc "friend class B;\n"
444*0a6a1f1dSLionel Sambuc "};\n",
445*0a6a1f1dSLionel Sambuc friendDecl()));
446*0a6a1f1dSLionel Sambuc }
447*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendTemplateClassRange)448*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendTemplateClassRange) {
449*0a6a1f1dSLionel Sambuc RangeVerifier<FriendDecl> Verifier;
450*0a6a1f1dSLionel Sambuc Verifier.expectRange(2, 1, 3, 14);
451*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("struct A {\n"
452*0a6a1f1dSLionel Sambuc "template <typename T>\n"
453*0a6a1f1dSLionel Sambuc "friend class B;\n"
454*0a6a1f1dSLionel Sambuc "};\n",
455*0a6a1f1dSLionel Sambuc friendDecl()));
456*0a6a1f1dSLionel Sambuc }
457*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendInlineFunctionLocation)458*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendInlineFunctionLocation) {
459*0a6a1f1dSLionel Sambuc LocationVerifier<FriendDecl> Verifier;
460*0a6a1f1dSLionel Sambuc Verifier.expectLocation(2, 19);
461*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("struct A {\n"
462*0a6a1f1dSLionel Sambuc "int inline friend f() { return 0; }"
463*0a6a1f1dSLionel Sambuc "};\n",
464*0a6a1f1dSLionel Sambuc friendDecl()));
465*0a6a1f1dSLionel Sambuc }
466*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,FriendInlineFunctionRange)467*0a6a1f1dSLionel Sambuc TEST(FriendDecl, FriendInlineFunctionRange) {
468*0a6a1f1dSLionel Sambuc RangeVerifier<FriendDecl> Verifier;
469*0a6a1f1dSLionel Sambuc Verifier.expectRange(2, 1, 2, 35);
470*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match("struct A {\n"
471*0a6a1f1dSLionel Sambuc "int inline friend f() { return 0; }"
472*0a6a1f1dSLionel Sambuc "};\n",
473*0a6a1f1dSLionel Sambuc friendDecl(), Lang_CXX11));
474*0a6a1f1dSLionel Sambuc }
475*0a6a1f1dSLionel Sambuc
TEST(FriendDecl,InstantiationSourceRange)476f4a2713aSLionel Sambuc TEST(FriendDecl, InstantiationSourceRange) {
477f4a2713aSLionel Sambuc RangeVerifier<FriendDecl> Verifier;
478f4a2713aSLionel Sambuc Verifier.expectRange(4, 3, 4, 35);
479f4a2713aSLionel Sambuc EXPECT_TRUE(Verifier.match(
480f4a2713aSLionel Sambuc "template <typename T> class S;\n"
481f4a2713aSLionel Sambuc "template<class T> void operator+(S<T> x);\n"
482f4a2713aSLionel Sambuc "template<class T> struct S {\n"
483f4a2713aSLionel Sambuc " friend void operator+<>(S<T> src);\n"
484f4a2713aSLionel Sambuc "};\n"
485f4a2713aSLionel Sambuc "void test(S<double> s) { +s; }",
486f4a2713aSLionel Sambuc friendDecl(hasParent(recordDecl(isTemplateInstantiation())))));
487f4a2713aSLionel Sambuc }
488f4a2713aSLionel Sambuc
TEST(ObjCMessageExpr,CXXConstructExprRange)489*0a6a1f1dSLionel Sambuc TEST(ObjCMessageExpr, CXXConstructExprRange) {
490*0a6a1f1dSLionel Sambuc RangeVerifier<CXXConstructExpr> Verifier;
491*0a6a1f1dSLionel Sambuc Verifier.expectRange(5, 25, 5, 27);
492*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Verifier.match(
493*0a6a1f1dSLionel Sambuc "struct A { int a; };\n"
494*0a6a1f1dSLionel Sambuc "@interface B {}\n"
495*0a6a1f1dSLionel Sambuc "+ (void) f1: (A)arg;\n"
496*0a6a1f1dSLionel Sambuc "@end\n"
497*0a6a1f1dSLionel Sambuc "void f2() { A a; [B f1: (a)]; }\n",
498*0a6a1f1dSLionel Sambuc constructExpr(), Lang_OBJCXX));
499*0a6a1f1dSLionel Sambuc }
500*0a6a1f1dSLionel Sambuc
501f4a2713aSLionel Sambuc } // end namespace ast_matchers
502f4a2713aSLionel Sambuc } // end namespace clang
503