|
| 1 | +//===- unittest/Tooling/RecursiveASTVisitorTests/DeductionGuide.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 | +#include <string> |
| 11 | + |
| 12 | +using namespace clang; |
| 13 | + |
| 14 | +namespace { |
| 15 | + |
| 16 | +class DeductionGuideVisitor |
| 17 | + : public ExpectedLocationVisitor<DeductionGuideVisitor> { |
| 18 | +public: |
| 19 | + DeductionGuideVisitor(bool ShouldVisitImplicitCode) |
| 20 | + : ShouldVisitImplicitCode(ShouldVisitImplicitCode) {} |
| 21 | + bool VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) { |
| 22 | + std::string Storage; |
| 23 | + llvm::raw_string_ostream Stream(Storage); |
| 24 | + D->print(Stream); |
| 25 | + Match(Stream.str(), D->getLocation()); |
| 26 | + return true; |
| 27 | + } |
| 28 | + |
| 29 | + bool shouldVisitTemplateInstantiations() const { return false; } |
| 30 | + |
| 31 | + bool shouldVisitImplicitCode() const { return ShouldVisitImplicitCode; } |
| 32 | + bool ShouldVisitImplicitCode; |
| 33 | +}; |
| 34 | + |
| 35 | +TEST(RecursiveASTVisitor, DeductionGuideNonImplicitMode) { |
| 36 | + DeductionGuideVisitor Visitor(/*ShouldVisitImplicitCode*/ false); |
| 37 | + // Verify that the synthezied deduction guide for alias is not visited in |
| 38 | + // RAV's implicit mode. |
| 39 | + Visitor.ExpectMatch("Foo(T) -> Foo<int>", 11, 1); |
| 40 | + Visitor.DisallowMatch("Bar(type-parameter-0-0) -> Foo<int>", 14, 1); |
| 41 | + EXPECT_TRUE(Visitor.runOver( |
| 42 | + R"cpp( |
| 43 | +template <typename T> |
| 44 | +concept False = true; |
| 45 | +
|
| 46 | +template <typename T> |
| 47 | +struct Foo { |
| 48 | + Foo(T); |
| 49 | +}; |
| 50 | +
|
| 51 | +template<typename T> requires False<T> |
| 52 | +Foo(T) -> Foo<int>; |
| 53 | +
|
| 54 | +template <typename U> |
| 55 | +using Bar = Foo<U>; |
| 56 | +Bar s(1); |
| 57 | + )cpp", |
| 58 | + DeductionGuideVisitor::Lang_CXX2a)); |
| 59 | +} |
| 60 | + |
| 61 | +TEST(RecursiveASTVisitor, DeductionGuideImplicitMode) { |
| 62 | + DeductionGuideVisitor Visitor(/*ShouldVisitImplicitCode*/ true); |
| 63 | + Visitor.ExpectMatch("Foo(T) -> Foo<int>", 11, 1); |
| 64 | + Visitor.ExpectMatch("Bar(type-parameter-0-0) -> Foo<int>", 14, 1); |
| 65 | + EXPECT_TRUE(Visitor.runOver( |
| 66 | + R"cpp( |
| 67 | +template <typename T> |
| 68 | +concept False = true; |
| 69 | +
|
| 70 | +template <typename T> |
| 71 | +struct Foo { |
| 72 | + Foo(T); |
| 73 | +}; |
| 74 | +
|
| 75 | +template<typename T> requires False<T> |
| 76 | +Foo(T) -> Foo<int>; |
| 77 | +
|
| 78 | +template <typename U> |
| 79 | +using Bar = Foo<U>; |
| 80 | +Bar s(1); |
| 81 | + )cpp", |
| 82 | + DeductionGuideVisitor::Lang_CXX2a)); |
| 83 | +} |
| 84 | + |
| 85 | +} // end anonymous namespace |
0 commit comments