//===- unittest/AST/SourceLocationTest.cpp - AST source loc unit tests ----===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // This file contains tests for SourceLocation and SourceRange fields // in AST nodes. // // FIXME: In the long-term, when we test more than source locations, we may // want to have a unit test file for an AST node (or group of related nodes), // rather than a unit test file for source locations for all AST nodes. // //===----------------------------------------------------------------------===// #include "clang/AST/ASTContext.h" #include "MatchVerifier.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" #include "clang/Tooling/Tooling.h" #include "gtest/gtest.h" namespace clang { namespace ast_matchers { // FIXME: Pull the *Verifier tests into their own test file. TEST(MatchVerifier, ParseError) { LocationVerifier Verifier; Verifier.expectLocation(1, 1); EXPECT_FALSE(Verifier.match("int i", varDecl())); } TEST(MatchVerifier, NoMatch) { LocationVerifier Verifier; Verifier.expectLocation(1, 1); EXPECT_FALSE(Verifier.match("int i;", recordDecl())); } TEST(MatchVerifier, WrongType) { LocationVerifier Verifier; Verifier.expectLocation(1, 1); EXPECT_FALSE(Verifier.match("int i;", varDecl())); } TEST(LocationVerifier, WrongLocation) { LocationVerifier Verifier; Verifier.expectLocation(1, 1); EXPECT_FALSE(Verifier.match("int i;", varDecl())); } TEST(RangeVerifier, WrongRange) { RangeVerifier Verifier; Verifier.expectRange(1, 1, 1, 1); EXPECT_FALSE(Verifier.match("int i;", varDecl())); } class LabelDeclRangeVerifier : public RangeVerifier { protected: SourceRange getRange(const LabelStmt &Node) override { return Node.getDecl()->getSourceRange(); } }; TEST(LabelDecl, Range) { LabelDeclRangeVerifier Verifier; Verifier.expectRange(1, 12, 1, 12); EXPECT_TRUE(Verifier.match("void f() { l: return; }", labelStmt())); } TEST(LabelStmt, Range) { RangeVerifier Verifier; Verifier.expectRange(1, 12, 1, 15); EXPECT_TRUE(Verifier.match("void f() { l: return; }", labelStmt())); } TEST(ParmVarDecl, KNRLocation) { LocationVerifier Verifier; Verifier.expectLocation(1, 8); EXPECT_TRUE(Verifier.match("void f(i) {}", varDecl(), Lang_C)); } TEST(ParmVarDecl, KNRRange) { RangeVerifier Verifier; Verifier.expectRange(1, 8, 1, 8); EXPECT_TRUE(Verifier.match("void f(i) {}", varDecl(), Lang_C)); } TEST(CXXNewExpr, ArrayRange) { RangeVerifier Verifier; Verifier.expectRange(1, 12, 1, 22); EXPECT_TRUE(Verifier.match("void f() { new int[10]; }", cxxNewExpr())); } TEST(CXXNewExpr, ParenRange) { RangeVerifier Verifier; Verifier.expectRange(1, 12, 1, 20); EXPECT_TRUE(Verifier.match("void f() { new int(); }", cxxNewExpr())); } TEST(MemberExpr, ImplicitMemberRange) { RangeVerifier Verifier; Verifier.expectRange(2, 30, 2, 30); EXPECT_TRUE(Verifier.match("struct S { operator int() const; };\n" "int foo(const S& s) { return s; }", memberExpr())); } class MemberExprArrowLocVerifier : public RangeVerifier { protected: SourceRange getRange(const MemberExpr &Node) override { return Node.getOperatorLoc(); } }; TEST(MemberExpr, ArrowRange) { MemberExprArrowLocVerifier Verifier; Verifier.expectRange(2, 19, 2, 19); EXPECT_TRUE(Verifier.match("struct S { int x; };\n" "void foo(S *s) { s->x = 0; }", memberExpr())); } TEST(MemberExpr, MacroArrowRange) { MemberExprArrowLocVerifier Verifier; Verifier.expectRange(1, 24, 1, 24); EXPECT_TRUE(Verifier.match("#define MEMBER(a, b) (a->b)\n" "struct S { int x; };\n" "void foo(S *s) { MEMBER(s, x) = 0; }", memberExpr())); } TEST(MemberExpr, ImplicitArrowRange) { MemberExprArrowLocVerifier Verifier; Verifier.expectRange(0, 0, 0, 0); EXPECT_TRUE(Verifier.match("struct S { int x; void Test(); };\n" "void S::Test() { x = 1; }", memberExpr())); } TEST(VarDecl, VMTypeFixedVarDeclRange) { RangeVerifier Verifier; Verifier.expectRange(1, 1, 1, 23); EXPECT_TRUE(Verifier.match("int a[(int)(void*)1234];", varDecl(), Lang_C89)); } TEST(CXXConstructorDecl, NoRetFunTypeLocRange) { RangeVerifier Verifier; Verifier.expectRange(1, 11, 1, 13); EXPECT_TRUE(Verifier.match("class C { C(); };", functionDecl())); } TEST(CXXConstructorDecl, DefaultedCtorLocRange) { RangeVerifier Verifier; Verifier.expectRange(1, 11, 1, 23); EXPECT_TRUE(Verifier.match("class C { C() = default; };", functionDecl())); } TEST(CXXConstructorDecl, DeletedCtorLocRange) { RangeVerifier Verifier; Verifier.expectRange(1, 11, 1, 22); EXPECT_TRUE(Verifier.match("class C { C() = delete; };", functionDecl())); } TEST(CompoundLiteralExpr, CompoundVectorLiteralRange) { RangeVerifier Verifier; Verifier.expectRange(2, 11, 2, 22); EXPECT_TRUE(Verifier.match( "typedef int int2 __attribute__((ext_vector_type(2)));\n" "int2 i2 = (int2){1, 2};", compoundLiteralExpr())); } TEST(CompoundLiteralExpr, ParensCompoundVectorLiteralRange) { RangeVerifier Verifier; Verifier.expectRange(2, 20, 2, 31); EXPECT_TRUE(Verifier.match( "typedef int int2 __attribute__((ext_vector_type(2)));\n" "constant int2 i2 = (int2)(1, 2);", compoundLiteralExpr(), Lang_OpenCL)); } TEST(InitListExpr, VectorLiteralListBraceRange) { RangeVerifier Verifier; Verifier.expectRange(2, 17, 2, 22); EXPECT_TRUE(Verifier.match( "typedef int int2 __attribute__((ext_vector_type(2)));\n" "int2 i2 = (int2){1, 2};", initListExpr())); } TEST(InitListExpr, VectorLiteralInitListParens) { RangeVerifier Verifier; Verifier.expectRange(2, 26, 2, 31); EXPECT_TRUE(Verifier.match( "typedef int int2 __attribute__((ext_vector_type(2)));\n" "constant int2 i2 = (int2)(1, 2);", initListExpr(), Lang_OpenCL)); } class TemplateAngleBracketLocRangeVerifier : public RangeVerifier { protected: SourceRange getRange(const TypeLoc &Node) override { TemplateSpecializationTypeLoc T = Node.getUnqualifiedLoc().castAs(); assert(!T.isNull()); return SourceRange(T.getLAngleLoc(), T.getRAngleLoc()); } }; TEST(TemplateSpecializationTypeLoc, AngleBracketLocations) { TemplateAngleBracketLocRangeVerifier Verifier; Verifier.expectRange(2, 8, 2, 10); EXPECT_TRUE(Verifier.match( "template struct A {}; struct B{}; void f(\n" "const A&);", loc(templateSpecializationType()))); } TEST(CXXNewExpr, TypeParenRange) { RangeVerifier Verifier; Verifier.expectRange(1, 10, 1, 18); EXPECT_TRUE(Verifier.match("int* a = new (int);", cxxNewExpr())); } class UnaryTransformTypeLocParensRangeVerifier : public RangeVerifier { protected: SourceRange getRange(const TypeLoc &Node) override { UnaryTransformTypeLoc T = Node.getUnqualifiedLoc().castAs(); assert(!T.isNull()); return SourceRange(T.getLParenLoc(), T.getRParenLoc()); } }; TEST(UnaryTransformTypeLoc, ParensRange) { UnaryTransformTypeLocParensRangeVerifier Verifier; Verifier.expectRange(3, 26, 3, 28); EXPECT_TRUE(Verifier.match( "template \n" "struct S {\n" "typedef __underlying_type(T) type;\n" "};", loc(unaryTransformType()))); } TEST(CXXFunctionalCastExpr, SourceRange) { RangeVerifier Verifier; Verifier.expectRange(2, 10, 2, 14); EXPECT_TRUE(Verifier.match( "int foo() {\n" " return int{};\n" "}", cxxFunctionalCastExpr(), Lang_CXX11)); } TEST(CXXConstructExpr, SourceRange) { RangeVerifier Verifier; Verifier.expectRange(3, 14, 3, 19); EXPECT_TRUE(Verifier.match( "struct A { A(int, int); };\n" "void f(A a);\n" "void g() { f({0, 0}); }", cxxConstructExpr(), Lang_CXX11)); } TEST(CXXTemporaryObjectExpr, SourceRange) { RangeVerifier Verifier; Verifier.expectRange(2, 6, 2, 12); EXPECT_TRUE(Verifier.match( "struct A { A(int, int); };\n" "A a( A{0, 0} );", cxxTemporaryObjectExpr(), Lang_CXX11)); } TEST(CXXUnresolvedConstructExpr, SourceRange) { RangeVerifier Verifier; Verifier.expectRange(3, 10, 3, 12); std::vector Args; Args.push_back("-fno-delayed-template-parsing"); EXPECT_TRUE(Verifier.match( "template \n" "U foo() {\n" " return U{};\n" "}", cxxUnresolvedConstructExpr(), Args, Lang_CXX11)); } TEST(UsingDecl, SourceRange) { RangeVerifier Verifier; Verifier.expectRange(2, 22, 2, 25); EXPECT_TRUE(Verifier.match( "class B { protected: int i; };\n" "class D : public B { B::i; };", usingDecl())); } TEST(UnresolvedUsingValueDecl, SourceRange) { RangeVerifier Verifier; Verifier.expectRange(3, 3, 3, 6); EXPECT_TRUE(Verifier.match( "template \n" "class D : public B {\n" " B::i;\n" "};", unresolvedUsingValueDecl())); } TEST(FriendDecl, FriendNonMemberFunctionLocation) { LocationVerifier Verifier; Verifier.expectLocation(2, 13); EXPECT_TRUE(Verifier.match("struct A {\n" "friend void f();\n" "};\n", friendDecl())); } TEST(FriendDecl, FriendNonMemberFunctionRange) { RangeVerifier Verifier; Verifier.expectRange(2, 1, 2, 15); EXPECT_TRUE(Verifier.match("struct A {\n" "friend void f();\n" "};\n", friendDecl())); } TEST(FriendDecl, FriendNonMemberFunctionDefinitionLocation) { LocationVerifier Verifier; Verifier.expectLocation(2, 12); EXPECT_TRUE(Verifier.match("struct A {\n" "friend int f() { return 0; }\n" "};\n", friendDecl())); } TEST(FriendDecl, FriendNonMemberFunctionDefinitionRange) { RangeVerifier Verifier; Verifier.expectRange(2, 1, 2, 28); EXPECT_TRUE(Verifier.match("struct A {\n" "friend int f() { return 0; }\n" "};\n", friendDecl())); } TEST(FriendDecl, FriendElaboratedTypeLocation) { LocationVerifier Verifier; Verifier.expectLocation(2, 8); EXPECT_TRUE(Verifier.match("struct A {\n" "friend class B;\n" "};\n", friendDecl())); } TEST(FriendDecl, FriendElaboratedTypeRange) { RangeVerifier Verifier; Verifier.expectRange(2, 1, 2, 14); EXPECT_TRUE(Verifier.match("struct A {\n" "friend class B;\n" "};\n", friendDecl())); } TEST(FriendDecl, FriendSimpleTypeLocation) { LocationVerifier Verifier; Verifier.expectLocation(3, 8); EXPECT_TRUE(Verifier.match("class B;\n" "struct A {\n" "friend B;\n" "};\n", friendDecl(), Lang_CXX11)); } TEST(FriendDecl, FriendSimpleTypeRange) { RangeVerifier Verifier; Verifier.expectRange(3, 1, 3, 8); EXPECT_TRUE(Verifier.match("class B;\n" "struct A {\n" "friend B;\n" "};\n", friendDecl(), Lang_CXX11)); } TEST(FriendDecl, FriendTemplateParameterLocation) { LocationVerifier Verifier; Verifier.expectLocation(3, 8); EXPECT_TRUE(Verifier.match("template \n" "struct A {\n" "friend T;\n" "};\n", friendDecl(), Lang_CXX11)); } TEST(FriendDecl, FriendTemplateParameterRange) { RangeVerifier Verifier; Verifier.expectRange(3, 1, 3, 8); EXPECT_TRUE(Verifier.match("template \n" "struct A {\n" "friend T;\n" "};\n", friendDecl(), Lang_CXX11)); } TEST(FriendDecl, FriendDecltypeLocation) { LocationVerifier Verifier; Verifier.expectLocation(4, 8); EXPECT_TRUE(Verifier.match("struct A;\n" "A foo();\n" "struct A {\n" "friend decltype(foo());\n" "};\n", friendDecl(), Lang_CXX11)); } TEST(FriendDecl, FriendDecltypeRange) { RangeVerifier Verifier; Verifier.expectRange(4, 1, 4, 8); EXPECT_TRUE(Verifier.match("struct A;\n" "A foo();\n" "struct A {\n" "friend decltype(foo());\n" "};\n", friendDecl(), Lang_CXX11)); } TEST(FriendDecl, FriendConstructorDestructorLocation) { const std::string Code = "struct B {\n" "B();\n" "~B();\n" "};\n" "struct A {\n" "friend B::B(), B::~B();\n" "};\n"; LocationVerifier ConstructorVerifier; ConstructorVerifier.expectLocation(6, 11); EXPECT_TRUE(ConstructorVerifier.match( Code, friendDecl(has(cxxConstructorDecl(ofClass(hasName("B"))))))); LocationVerifier DestructorVerifier; DestructorVerifier.expectLocation(6, 19); EXPECT_TRUE(DestructorVerifier.match( Code, friendDecl(has(cxxDestructorDecl(ofClass(hasName("B"))))))); } TEST(FriendDecl, FriendConstructorDestructorRange) { const std::string Code = "struct B {\n" "B();\n" "~B();\n" "};\n" "struct A {\n" "friend B::B(), B::~B();\n" "};\n"; RangeVerifier ConstructorVerifier; ConstructorVerifier.expectRange(6, 1, 6, 13); EXPECT_TRUE(ConstructorVerifier.match( Code, friendDecl(has(cxxConstructorDecl(ofClass(hasName("B"))))))); RangeVerifier DestructorVerifier; DestructorVerifier.expectRange(6, 1, 6, 22); EXPECT_TRUE(DestructorVerifier.match( Code, friendDecl(has(cxxDestructorDecl(ofClass(hasName("B"))))))); } TEST(FriendDecl, FriendTemplateFunctionLocation) { LocationVerifier Verifier; Verifier.expectLocation(3, 13); EXPECT_TRUE(Verifier.match("struct A {\n" "template \n" "friend void f();\n" "};\n", friendDecl())); } TEST(FriendDecl, FriendTemplateFunctionRange) { RangeVerifier Verifier; Verifier.expectRange(2, 1, 3, 15); EXPECT_TRUE(Verifier.match("struct A {\n" "template \n" "friend void f();\n" "};\n", friendDecl())); } TEST(FriendDecl, FriendTemplateClassLocation) { LocationVerifier Verifier; Verifier.expectLocation(3, 14); EXPECT_TRUE(Verifier.match("struct A {\n" "template \n" "friend class B;\n" "};\n", friendDecl())); } TEST(FriendDecl, FriendTemplateClassRange) { RangeVerifier Verifier; Verifier.expectRange(2, 1, 3, 14); EXPECT_TRUE(Verifier.match("struct A {\n" "template \n" "friend class B;\n" "};\n", friendDecl())); } TEST(FriendDecl, FriendInlineFunctionLocation) { LocationVerifier Verifier; Verifier.expectLocation(2, 19); EXPECT_TRUE(Verifier.match("struct A {\n" "int inline friend f() { return 0; }" "};\n", friendDecl())); } TEST(FriendDecl, FriendInlineFunctionRange) { RangeVerifier Verifier; Verifier.expectRange(2, 1, 2, 35); EXPECT_TRUE(Verifier.match("struct A {\n" "int inline friend f() { return 0; }" "};\n", friendDecl(), Lang_CXX11)); } TEST(FriendDecl, InstantiationSourceRange) { RangeVerifier Verifier; Verifier.expectRange(4, 3, 4, 35); EXPECT_TRUE(Verifier.match( "template class S;\n" "template void operator+(S x);\n" "template struct S {\n" " friend void operator+<>(S src);\n" "};\n" "void test(S s) { +s; }", friendDecl(hasParent(cxxRecordDecl(isTemplateInstantiation()))))); } TEST(ObjCMessageExpr, CXXConstructExprRange) { RangeVerifier Verifier; Verifier.expectRange(5, 25, 5, 27); EXPECT_TRUE(Verifier.match( "struct A { int a; };\n" "@interface B {}\n" "+ (void) f1: (A)arg;\n" "@end\n" "void f2() { A a; [B f1: (a)]; }\n", cxxConstructExpr(), Lang_OBJCXX)); } } // end namespace ast_matchers } // end namespace clang