Skip to content

Commit e5697d7

Browse files
authored
Return available function types for BindingDecls. (#102196)
Only return nullptr when we don't have an available QualType.
1 parent 2eb6e30 commit e5697d7

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

clang/lib/AST/DeclBase.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,15 +1177,20 @@ int64_t Decl::getID() const {
11771177

11781178
const FunctionType *Decl::getFunctionType(bool BlocksToo) const {
11791179
QualType Ty;
1180-
if (isa<BindingDecl>(this))
1181-
return nullptr;
1182-
else if (const auto *D = dyn_cast<ValueDecl>(this))
1180+
if (const auto *D = dyn_cast<ValueDecl>(this))
11831181
Ty = D->getType();
11841182
else if (const auto *D = dyn_cast<TypedefNameDecl>(this))
11851183
Ty = D->getUnderlyingType();
11861184
else
11871185
return nullptr;
11881186

1187+
if (Ty.isNull()) {
1188+
// BindingDecls do not have types during parsing, so return nullptr. This is
1189+
// the only known case where `Ty` is null.
1190+
assert(isa<BindingDecl>(this));
1191+
return nullptr;
1192+
}
1193+
11891194
if (Ty->isFunctionPointerType())
11901195
Ty = Ty->castAs<PointerType>()->getPointeeType();
11911196
else if (Ty->isFunctionReferenceType())

clang/unittests/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_clang_unittest(ASTTests
2626
CommentTextTest.cpp
2727
ConceptPrinterTest.cpp
2828
DataCollectionTest.cpp
29+
DeclBaseTest.cpp
2930
DeclPrinterTest.cpp
3031
DeclTest.cpp
3132
EvaluateAsRValueTest.cpp

clang/unittests/AST/DeclBaseTest.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}

llvm/utils/gn/secondary/clang/unittests/AST/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ unittest("ASTTests") {
3434
"CommentTextTest.cpp",
3535
"ConceptPrinterTest.cpp",
3636
"DataCollectionTest.cpp",
37+
"DeclBaseTest.cpp",
3738
"DeclPrinterTest.cpp",
3839
"DeclTest.cpp",
3940
"EvaluateAsRValueTest.cpp",

0 commit comments

Comments
 (0)