xref: /llvm-project/clang/unittests/AST/DeclBaseTest.cpp (revision e5697d7f99b441064a4e4c3c27c2fc8e5d2784c0)
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       "FunctionPointer",
50       match(bindingDecl(hasName("FunctionPointer")).bind("FunctionPointer"),
51             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(clang::QualType(), nullptr);
58   EXPECT_EQ(BD->getFunctionType(), nullptr);
59 }
60