1 //===- unittest/AST/SourceLocationTest.cpp - AST source loc 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 // This file contains tests for SourceLocation and SourceRange fields 11 // in AST nodes. 12 // 13 // FIXME: In the long-term, when we test more than source locations, we may 14 // want to have a unit test file for an AST node (or group of related nodes), 15 // rather than a unit test file for source locations for all AST nodes. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #include "clang/AST/ASTContext.h" 20 #include "clang/ASTMatchers/ASTMatchFinder.h" 21 #include "clang/ASTMatchers/ASTMatchers.h" 22 #include "clang/Tooling/Tooling.h" 23 #include "gtest/gtest.h" 24 #include "MatchVerifier.h" 25 26 namespace clang { 27 namespace ast_matchers { 28 29 // FIXME: Pull the *Verifier tests into their own test file. 30 31 TEST(MatchVerifier, ParseError) { 32 LocationVerifier<VarDecl> Verifier; 33 Verifier.expectLocation(1, 1); 34 EXPECT_FALSE(Verifier.match("int i", varDecl())); 35 } 36 37 TEST(MatchVerifier, NoMatch) { 38 LocationVerifier<VarDecl> Verifier; 39 Verifier.expectLocation(1, 1); 40 EXPECT_FALSE(Verifier.match("int i;", recordDecl())); 41 } 42 43 TEST(MatchVerifier, WrongType) { 44 LocationVerifier<RecordDecl> Verifier; 45 Verifier.expectLocation(1, 1); 46 EXPECT_FALSE(Verifier.match("int i;", varDecl())); 47 } 48 49 TEST(LocationVerifier, WrongLocation) { 50 LocationVerifier<VarDecl> Verifier; 51 Verifier.expectLocation(1, 1); 52 EXPECT_FALSE(Verifier.match("int i;", varDecl())); 53 } 54 55 TEST(RangeVerifier, WrongRange) { 56 RangeVerifier<VarDecl> Verifier; 57 Verifier.expectRange(1, 1, 1, 1); 58 EXPECT_FALSE(Verifier.match("int i;", varDecl())); 59 } 60 61 class LabelDeclRangeVerifier : public RangeVerifier<LabelStmt> { 62 protected: 63 virtual SourceRange getRange(const LabelStmt &Node) { 64 return Node.getDecl()->getSourceRange(); 65 } 66 }; 67 68 TEST(LabelDecl, Range) { 69 LabelDeclRangeVerifier Verifier; 70 Verifier.expectRange(1, 12, 1, 12); 71 EXPECT_TRUE(Verifier.match("void f() { l: return; }", labelStmt())); 72 } 73 74 TEST(LabelStmt, Range) { 75 RangeVerifier<LabelStmt> Verifier; 76 Verifier.expectRange(1, 12, 1, 15); 77 EXPECT_TRUE(Verifier.match("void f() { l: return; }", labelStmt())); 78 } 79 80 TEST(ParmVarDecl, KNRLocation) { 81 LocationVerifier<ParmVarDecl> Verifier; 82 Verifier.expectLocation(1, 8); 83 EXPECT_TRUE(Verifier.match("void f(i) {}", varDecl(), Lang_C)); 84 } 85 86 TEST(ParmVarDecl, KNRRange) { 87 RangeVerifier<ParmVarDecl> Verifier; 88 Verifier.expectRange(1, 8, 1, 8); 89 EXPECT_TRUE(Verifier.match("void f(i) {}", varDecl(), Lang_C)); 90 } 91 92 TEST(CXXNewExpr, ArrayRange) { 93 RangeVerifier<CXXNewExpr> Verifier; 94 Verifier.expectRange(1, 12, 1, 22); 95 EXPECT_TRUE(Verifier.match("void f() { new int[10]; }", newExpr())); 96 } 97 98 TEST(CXXNewExpr, ParenRange) { 99 RangeVerifier<CXXNewExpr> Verifier; 100 Verifier.expectRange(1, 12, 1, 20); 101 EXPECT_TRUE(Verifier.match("void f() { new int(); }", newExpr())); 102 } 103 104 TEST(MemberExpr, ImplicitMemberRange) { 105 RangeVerifier<MemberExpr> Verifier; 106 Verifier.expectRange(2, 30, 2, 30); 107 EXPECT_TRUE(Verifier.match("struct S { operator int() const; };\n" 108 "int foo(const S& s) { return s; }", 109 memberExpr())); 110 } 111 112 TEST(VarDecl, VMTypeFixedVarDeclRange) { 113 RangeVerifier<VarDecl> Verifier; 114 Verifier.expectRange(1, 1, 1, 23); 115 EXPECT_TRUE(Verifier.match("int a[(int)(void*)1234];", 116 varDecl(), Lang_C89)); 117 } 118 119 TEST(CXXConstructorDecl, NoRetFunTypeLocRange) { 120 RangeVerifier<CXXConstructorDecl> Verifier; 121 Verifier.expectRange(1, 11, 1, 13); 122 EXPECT_TRUE(Verifier.match("class C { C(); };", functionDecl())); 123 } 124 125 TEST(CompoundLiteralExpr, CompoundVectorLiteralRange) { 126 RangeVerifier<CompoundLiteralExpr> Verifier; 127 Verifier.expectRange(2, 11, 2, 22); 128 EXPECT_TRUE(Verifier.match( 129 "typedef int int2 __attribute__((ext_vector_type(2)));\n" 130 "int2 i2 = (int2){1, 2};", compoundLiteralExpr())); 131 } 132 133 TEST(CompoundLiteralExpr, ParensCompoundVectorLiteralRange) { 134 RangeVerifier<CompoundLiteralExpr> Verifier; 135 Verifier.expectRange(2, 11, 2, 22); 136 EXPECT_TRUE(Verifier.match( 137 "typedef int int2 __attribute__((ext_vector_type(2)));\n" 138 "int2 i2 = (int2)(1, 2);", 139 compoundLiteralExpr(), Lang_OpenCL)); 140 } 141 142 TEST(InitListExpr, VectorLiteralListBraceRange) { 143 RangeVerifier<InitListExpr> Verifier; 144 Verifier.expectRange(2, 17, 2, 22); 145 EXPECT_TRUE(Verifier.match( 146 "typedef int int2 __attribute__((ext_vector_type(2)));\n" 147 "int2 i2 = (int2){1, 2};", initListExpr())); 148 } 149 150 TEST(InitListExpr, VectorLiteralInitListParens) { 151 RangeVerifier<InitListExpr> Verifier; 152 Verifier.expectRange(2, 17, 2, 22); 153 EXPECT_TRUE(Verifier.match( 154 "typedef int int2 __attribute__((ext_vector_type(2)));\n" 155 "int2 i2 = (int2)(1, 2);", initListExpr(), Lang_OpenCL)); 156 } 157 158 } // end namespace ast_matchers 159 } // end namespace clang 160