| 1 | //===- unittests/AST/DeclBaseTest.cpp --- Declaration tests----------------===// |
| 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 | // Unit tests for Decl class in the AST. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/AST/DeclBase.h" |
| 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/DeclCXX.h" |
| 16 | #include "clang/AST/Type.h" |
| 17 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 18 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 19 | #include "clang/Basic/LLVM.h" |
| 20 | #include "clang/Tooling/Tooling.h" |
| 21 | #include "gtest/gtest.h" |
| 22 | |
| 23 | using ::clang::BindingDecl; |
| 24 | using ::clang::ast_matchers::bindingDecl; |
| 25 | using ::clang::ast_matchers::hasName; |
| 26 | using ::clang::ast_matchers::match; |
| 27 | using ::clang::ast_matchers::selectFirst; |
| 28 | |
| 29 | TEST(DeclGetFunctionType, BindingDecl) { |
| 30 | llvm::StringRef Code = R"cpp( |
| 31 | template <typename A, typename B> |
| 32 | struct Pair { |
| 33 | A AnA; |
| 34 | B AB; |
| 35 | }; |
| 36 | |
| 37 | void target(int *i) { |
| 38 | Pair<void (*)(int *), bool> P; |
| 39 | auto [FunctionPointer, B] = P; |
| 40 | FunctionPointer(i); |
| 41 | } |
| 42 | )cpp" ; |
| 43 | |
| 44 | auto AST = |
| 45 | clang::tooling::buildASTFromCodeWithArgs(Code, /*Args=*/{"-std=c++20" }); |
| 46 | clang::ASTContext &Ctx = AST->getASTContext(); |
| 47 | |
| 48 | auto *BD = selectFirst<clang::BindingDecl>( |
| 49 | BoundTo: "FunctionPointer" , |
| 50 | Results: match(Matcher: bindingDecl(hasName(Name: "FunctionPointer" )).bind(ID: "FunctionPointer" ), |
| 51 | Context&: Ctx)); |
| 52 | ASSERT_NE(BD, nullptr); |
| 53 | |
| 54 | EXPECT_NE(BD->getFunctionType(), nullptr); |
| 55 | |
| 56 | // Emulate a call before the BindingDecl has a bound type. |
| 57 | const_cast<clang::BindingDecl *>(BD)->setBinding(DeclaredType: clang::QualType(), Binding: nullptr); |
| 58 | EXPECT_EQ(BD->getFunctionType(), nullptr); |
| 59 | } |
| 60 | |