Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return available function types for BindingDecls. #102196

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions clang/lib/AST/DeclBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1161,15 +1161,20 @@ int64_t Decl::getID() const {

const FunctionType *Decl::getFunctionType(bool BlocksToo) const {
QualType Ty;
if (isa<BindingDecl>(this))
return nullptr;
else if (const auto *D = dyn_cast<ValueDecl>(this))
if (const auto *D = dyn_cast<ValueDecl>(this))
Ty = D->getType();
else if (const auto *D = dyn_cast<TypedefNameDecl>(this))
Ty = D->getUnderlyingType();
else
return nullptr;

if (Ty.isNull()) {
// BindingDecls do not have types during parsing, so return nullptr. This is
// the only known case where `Ty` is null.
assert(isa<BindingDecl>(this));
return nullptr;
}

if (Ty->isFunctionPointerType())
Ty = Ty->castAs<PointerType>()->getPointeeType();
else if (Ty->isFunctionReferenceType())
Expand Down
1 change: 1 addition & 0 deletions clang/unittests/AST/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ add_clang_unittest(ASTTests
CommentTextTest.cpp
ConceptPrinterTest.cpp
DataCollectionTest.cpp
DeclBaseTest.cpp
DeclPrinterTest.cpp
DeclTest.cpp
EvaluateAsRValueTest.cpp
Expand Down
59 changes: 59 additions & 0 deletions clang/unittests/AST/DeclBaseTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//===- unittests/AST/DeclBaseTest.cpp --- Declaration tests----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Unit tests for Decl class in the AST.
//
//===----------------------------------------------------------------------===//

#include "clang/AST/DeclBase.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/Type.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Basic/LLVM.h"
#include "clang/Tooling/Tooling.h"
#include "gtest/gtest.h"

using ::clang::BindingDecl;
using ::clang::ast_matchers::bindingDecl;
using ::clang::ast_matchers::hasName;
using ::clang::ast_matchers::match;
using ::clang::ast_matchers::selectFirst;

TEST(DeclGetFunctionType, BindingDecl) {
llvm::StringRef Code = R"cpp(
template <typename A, typename B>
struct Pair {
A AnA;
B AB;
};

void target(int *i) {
Pair<void (*)(int *), bool> P;
auto [FunctionPointer, B] = P;
FunctionPointer(i);
}
)cpp";

auto AST =
clang::tooling::buildASTFromCodeWithArgs(Code, /*Args=*/{"-std=c++20"});
clang::ASTContext &Ctx = AST->getASTContext();

auto *BD = selectFirst<clang::BindingDecl>(
"FunctionPointer",
match(bindingDecl(hasName("FunctionPointer")).bind("FunctionPointer"),
Ctx));
ASSERT_NE(BD, nullptr);

EXPECT_NE(BD->getFunctionType(), nullptr);

// Emulate a call before the BindingDecl has a bound type.
const_cast<clang::BindingDecl *>(BD)->setBinding(clang::QualType(), nullptr);
EXPECT_EQ(BD->getFunctionType(), nullptr);
}
1 change: 1 addition & 0 deletions llvm/utils/gn/secondary/clang/unittests/AST/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ unittest("ASTTests") {
"CommentTextTest.cpp",
"ConceptPrinterTest.cpp",
"DataCollectionTest.cpp",
"DeclBaseTest.cpp",
"DeclPrinterTest.cpp",
"DeclTest.cpp",
"EvaluateAsRValueTest.cpp",
Expand Down
Loading