1//===- unittest/Tooling/RecursiveASTVisitorTestTypeLocVisitor.cpp ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "TestVisitor.h"
10
11using namespace clang;
12
13namespace {
14
15class TypeLocVisitor : public ExpectedLocationVisitor<TypeLocVisitor> {
16public:
17 bool VisitTypeLoc(TypeLoc TypeLocation) {
18 Match(Name: TypeLocation.getType().getAsString(), Location: TypeLocation.getBeginLoc());
19 return true;
20 }
21};
22
23TEST(RecursiveASTVisitor, VisitsBaseClassDeclarations) {
24 TypeLocVisitor Visitor;
25 Visitor.ExpectMatch(Match: "class X", Line: 1, Column: 30);
26 EXPECT_TRUE(Visitor.runOver("class X {}; class Y : public X {};"));
27}
28
29TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersOfForwardDeclaredClass) {
30 TypeLocVisitor Visitor;
31 Visitor.ExpectMatch(Match: "class X", Line: 3, Column: 18);
32 EXPECT_TRUE(Visitor.runOver(
33 "class Y;\n"
34 "class X {};\n"
35 "class Y : public X {};"));
36}
37
38TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersWithIncompleteInnerClass) {
39 TypeLocVisitor Visitor;
40 Visitor.ExpectMatch(Match: "class X", Line: 2, Column: 18);
41 EXPECT_TRUE(Visitor.runOver(
42 "class X {};\n"
43 "class Y : public X { class Z; };"));
44}
45
46TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersOfSelfReferentialType) {
47 TypeLocVisitor Visitor;
48 Visitor.ExpectMatch(Match: "X<Y>", Line: 2, Column: 18, Times: 2);
49 EXPECT_TRUE(Visitor.runOver(
50 "template<typename T> class X {};\n"
51 "class Y : public X<Y> {};"));
52}
53
54TEST(RecursiveASTVisitor, VisitsClassTemplateTypeParmDefaultArgument) {
55 TypeLocVisitor Visitor;
56 Visitor.ExpectMatch(Match: "class X", Line: 2, Column: 23);
57 EXPECT_TRUE(Visitor.runOver(
58 "class X;\n"
59 "template<typename T = X> class Y;\n"
60 "template<typename T> class Y {};\n"));
61}
62
63TEST(RecursiveASTVisitor, VisitsCompoundLiteralType) {
64 TypeLocVisitor Visitor;
65 Visitor.ExpectMatch(Match: "struct S", Line: 1, Column: 26);
66 EXPECT_TRUE(Visitor.runOver(
67 "int f() { return (struct S { int a; }){.a = 0}.a; }",
68 TypeLocVisitor::Lang_C));
69}
70
71TEST(RecursiveASTVisitor, VisitsObjCPropertyType) {
72 TypeLocVisitor Visitor;
73 Visitor.ExpectMatch(Match: "NSNumber", Line: 2, Column: 33);
74 EXPECT_TRUE(Visitor.runOver(
75 "@class NSNumber; \n"
76 "@interface A @property (retain) NSNumber *x; @end\n",
77 TypeLocVisitor::Lang_OBJC));
78}
79
80TEST(RecursiveASTVisitor, VisitInvalidType) {
81 TypeLocVisitor Visitor;
82 // FIXME: It would be nice to have information about subtypes of invalid type
83 //Visitor.ExpectMatch("typeof(struct F *) []", 1, 1);
84 // Even if the full type is invalid, it should still find sub types
85 //Visitor.ExpectMatch("struct F", 1, 19);
86 EXPECT_FALSE(Visitor.runOver(
87 "__typeof__(struct F*) var[invalid];\n",
88 TypeLocVisitor::Lang_C));
89}
90
91TEST(RecursiveASTVisitor, VisitsUsingEnumType) {
92 TypeLocVisitor Visitor;
93 Visitor.ExpectMatch(Match: "::A", Line: 2, Column: 12);
94 EXPECT_TRUE(Visitor.runOver("enum class A {}; \n"
95 "using enum ::A;\n",
96 TypeLocVisitor::Lang_CXX2a));
97}
98
99} // end anonymous namespace
100

source code of clang/unittests/Tooling/RecursiveASTVisitorTestTypeLocVisitor.cpp