1 //===- unittest/Tooling/RecursiveASTVisitorTests/Concept.cpp----------------==//
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
7 //===----------------------------------------------------------------------===//
9 #include "TestVisitor.h"
10 #include "clang/AST/ASTConcept.h"
11 #include "clang/AST/DeclTemplate.h"
12 #include "clang/AST/ExprConcepts.h"
13 #include "clang/AST/Type.h"
15 using namespace clang
;
19 struct ConceptVisitor
: ExpectedLocationVisitor
<ConceptVisitor
> {
20 bool VisitConceptSpecializationExpr(ConceptSpecializationExpr
*E
) {
21 ++ConceptSpecializationExprsVisited
;
24 bool TraverseTypeConstraint(const TypeConstraint
*C
) {
25 ++TypeConstraintsTraversed
;
26 return ExpectedLocationVisitor::TraverseTypeConstraint(C
);
28 bool TraverseConceptRequirement(concepts::Requirement
*R
) {
29 ++ConceptRequirementsTraversed
;
30 return ExpectedLocationVisitor::TraverseConceptRequirement(R
);
32 bool TraverseConceptReference(ConceptReference
*CR
) {
33 ++ConceptReferencesTraversed
;
34 return ExpectedLocationVisitor::TraverseConceptReference(CR
);
36 bool VisitConceptReference(ConceptReference
*CR
) {
37 ++ConceptReferencesVisited
;
41 bool shouldVisitImplicitCode() { return ShouldVisitImplicitCode
; }
43 int ConceptSpecializationExprsVisited
= 0;
44 int TypeConstraintsTraversed
= 0;
45 int ConceptRequirementsTraversed
= 0;
46 int ConceptReferencesTraversed
= 0;
47 int ConceptReferencesVisited
= 0;
48 bool ShouldVisitImplicitCode
= false;
51 TEST(RecursiveASTVisitor
, Concepts
) {
52 ConceptVisitor Visitor
;
53 Visitor
.ShouldVisitImplicitCode
= true;
54 EXPECT_TRUE(Visitor
.runOver("template <typename T> concept Fooable = true;\n"
55 "template <Fooable T> void bar(T);",
56 ConceptVisitor::Lang_CXX2a
));
57 // Check that we traverse the "Fooable T" template parameter's
58 // TypeConstraint's ImmediatelyDeclaredConstraint, which is a
59 // ConceptSpecializationExpr.
60 EXPECT_EQ(1, Visitor
.ConceptSpecializationExprsVisited
);
61 // Also check we traversed the TypeConstraint that produced the expr.
62 EXPECT_EQ(1, Visitor
.TypeConstraintsTraversed
);
63 EXPECT_EQ(1, Visitor
.ConceptReferencesTraversed
);
64 EXPECT_EQ(1, Visitor
.ConceptReferencesVisited
);
66 Visitor
= {}; // Don't visit implicit code now.
67 EXPECT_TRUE(Visitor
.runOver("template <typename T> concept Fooable = true;\n"
68 "template <Fooable T> void bar(T);",
69 ConceptVisitor::Lang_CXX2a
));
70 // Check that we only visit the TypeConstraint, but not the implicitly
71 // generated immediately declared expression.
72 EXPECT_EQ(0, Visitor
.ConceptSpecializationExprsVisited
);
73 EXPECT_EQ(1, Visitor
.TypeConstraintsTraversed
);
74 EXPECT_EQ(1, Visitor
.ConceptReferencesTraversed
);
75 EXPECT_EQ(1, Visitor
.ConceptReferencesVisited
);
78 EXPECT_TRUE(Visitor
.runOver("template <class T> concept A = true;\n"
79 "template <class T> struct vector {};\n"
80 "template <class T> concept B = requires(T x) {\n"
81 " typename vector<T*>;\n"
85 ConceptVisitor::Lang_CXX2a
));
86 EXPECT_EQ(3, Visitor
.ConceptRequirementsTraversed
);
87 EXPECT_EQ(1, Visitor
.ConceptReferencesTraversed
);
88 EXPECT_EQ(1, Visitor
.ConceptReferencesVisited
);
91 struct VisitDeclOnlyOnce
: ExpectedLocationVisitor
<VisitDeclOnlyOnce
> {
92 bool VisitConceptDecl(ConceptDecl
*D
) {
93 ++ConceptDeclsVisited
;
97 bool VisitAutoType(AutoType
*) {
101 bool VisitAutoTypeLoc(AutoTypeLoc
) {
102 ++AutoTypeLocVisited
;
105 bool VisitConceptReference(ConceptReference
*) {
106 ++ConceptReferencesVisited
;
110 bool TraverseVarDecl(VarDecl
*V
) {
111 // The base traversal visits only the `TypeLoc`.
112 // However, in the test we also validate the underlying `QualType`.
113 TraverseType(V
->getType());
114 return ExpectedLocationVisitor::TraverseVarDecl(V
);
117 bool shouldWalkTypesOfTypeLocs() { return false; }
119 int ConceptDeclsVisited
= 0;
120 int AutoTypeVisited
= 0;
121 int AutoTypeLocVisited
= 0;
122 int ConceptReferencesVisited
= 0;
125 TEST(RecursiveASTVisitor
, ConceptDeclInAutoType
) {
126 // Check `AutoType` and `AutoTypeLoc` do not repeatedly traverse the
127 // underlying concept.
128 VisitDeclOnlyOnce Visitor
;
129 Visitor
.runOver("template <class T> concept A = true;\n"
131 VisitDeclOnlyOnce::Lang_CXX2a
);
132 EXPECT_EQ(1, Visitor
.AutoTypeVisited
);
133 EXPECT_EQ(1, Visitor
.AutoTypeLocVisited
);
134 EXPECT_EQ(1, Visitor
.ConceptDeclsVisited
);
135 EXPECT_EQ(1, Visitor
.ConceptReferencesVisited
);
138 } // end anonymous namespace