Skip to content

Commit 19c708c

Browse files
v01dXYZv01dxyzresistor
authored
FunctionDecl::getFunctionTypeLoc: ignore function type attributes (#118420)
Related to #118290. --------- Co-authored-by: v01dxyz <[email protected]> Co-authored-by: Owen Anderson <[email protected]>
1 parent e9280a1 commit 19c708c

File tree

3 files changed

+105
-8
lines changed

3 files changed

+105
-8
lines changed

clang-tools-extra/clangd/unittests/InlayHintTests.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,19 +1577,21 @@ TEST(TypeHints, Aliased) {
15771577
}
15781578

15791579
TEST(TypeHints, CallingConvention) {
1580-
// Check that we don't crash for lambdas without a FunctionTypeLoc
1580+
// Check that we don't crash for lambdas with an annotation
15811581
// https://github.com/clangd/clangd/issues/2223
1582-
std::string Code = R"cpp(
1582+
Annotations Source(R"cpp(
15831583
void test() {
1584-
[]() __cdecl {};
1584+
[]($lambda[[)]]__cdecl {};
15851585
}
1586-
)cpp";
1587-
TestTU TU = TestTU::withCode(Code);
1586+
)cpp");
1587+
TestTU TU = TestTU::withCode(Source.code());
15881588
TU.ExtraArgs.push_back("--target=x86_64-w64-mingw32");
15891589
TU.PredefineMacros = true; // for the __cdecl
15901590
auto AST = TU.build();
15911591

1592-
EXPECT_THAT(hintsOfKind(AST, InlayHintKind::Type), IsEmpty());
1592+
EXPECT_THAT(
1593+
hintsOfKind(AST, InlayHintKind::Type),
1594+
ElementsAre(HintMatcher(ExpectedHint{"-> void", "lambda"}, Source)));
15931595
}
15941596

15951597
TEST(TypeHints, Decltype) {

clang/lib/AST/Decl.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3910,8 +3910,25 @@ bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
39103910

39113911
FunctionTypeLoc FunctionDecl::getFunctionTypeLoc() const {
39123912
const TypeSourceInfo *TSI = getTypeSourceInfo();
3913-
return TSI ? TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>()
3914-
: FunctionTypeLoc();
3913+
3914+
if (!TSI)
3915+
return FunctionTypeLoc();
3916+
3917+
TypeLoc TL = TSI->getTypeLoc();
3918+
FunctionTypeLoc FTL;
3919+
3920+
while (!(FTL = TL.getAs<FunctionTypeLoc>())) {
3921+
if (const auto PTL = TL.getAs<ParenTypeLoc>())
3922+
TL = PTL.getInnerLoc();
3923+
else if (const auto ATL = TL.getAs<AttributedTypeLoc>())
3924+
TL = ATL.getEquivalentTypeLoc();
3925+
else if (const auto MQTL = TL.getAs<MacroQualifiedTypeLoc>())
3926+
TL = MQTL.getInnerLoc();
3927+
else
3928+
break;
3929+
}
3930+
3931+
return FTL;
39153932
}
39163933

39173934
SourceRange FunctionDecl::getReturnTypeSourceRange() const {

clang/unittests/AST/AttrTest.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ TEST(Attr, AnnotateType) {
8686
struct S { int mem; };
8787
int [[clang::annotate_type("int")]]
8888
S::* [[clang::annotate_type("ptr_to_mem")]] ptr_to_member = &S::mem;
89+
90+
// Function Type Attributes
91+
__attribute__((noreturn)) int f_noreturn();
92+
__attribute__((preserve_most)) int f_cc_preserve_most();
93+
94+
#define PRESERVE_MOST __attribute__((preserve_most))
95+
PRESERVE_MOST int f_macro_attribue();
96+
97+
int (__attribute__((preserve_most)) f_paren_attribute)();
98+
99+
int (
100+
PRESERVE_MOST
101+
(
102+
__attribute__((warn_unused_result))
103+
(f_w_paren_and_attr)
104+
)
105+
) ();
89106
)cpp");
90107

91108
{
@@ -153,6 +170,67 @@ TEST(Attr, AnnotateType) {
153170
EXPECT_EQ(IntTL.getType(), AST->getASTContext().IntTy);
154171
}
155172

173+
{
174+
const FunctionDecl *Func = getFunctionNode(AST.get(), "f_noreturn");
175+
const FunctionTypeLoc FTL = Func->getFunctionTypeLoc();
176+
const FunctionType *FT = FTL.getTypePtr();
177+
178+
EXPECT_TRUE(FT->getNoReturnAttr());
179+
}
180+
181+
{
182+
const FunctionDecl *Func = getFunctionNode(AST.get(), "f_cc_preserve_most");
183+
const FunctionTypeLoc FTL = Func->getFunctionTypeLoc();
184+
const FunctionType *FT = FTL.getTypePtr();
185+
186+
EXPECT_TRUE(FT->getCallConv() == CC_PreserveMost);
187+
}
188+
189+
{
190+
for (auto should_have_func_type_loc : {
191+
"f_macro_attribue",
192+
"f_paren_attribute",
193+
"f_w_paren_and_attr",
194+
}) {
195+
llvm::errs() << "O: " << should_have_func_type_loc << "\n";
196+
const FunctionDecl *Func =
197+
getFunctionNode(AST.get(), should_have_func_type_loc);
198+
199+
EXPECT_TRUE(Func->getFunctionTypeLoc());
200+
}
201+
}
202+
203+
// The following test verifies getFunctionTypeLoc returns a type
204+
// which takes into account the attribute (instead of only the nake
205+
// type).
206+
//
207+
// This is hard to do with C/C++ because it seems using a function
208+
// type attribute with a C/C++ function declaration only results
209+
// with either:
210+
//
211+
// 1. It does NOT produce any AttributedType (for example it only
212+
// sets one flag of the FunctionType's ExtInfo, e.g. NoReturn).
213+
// 2. It produces an AttributedType with modified type and
214+
// equivalent type that are equal (for example, that's what
215+
// happens with Calling Convention attributes).
216+
//
217+
// Fortunately, ObjC has one specific function type attribute that
218+
// creates an AttributedType with different modified type and
219+
// equivalent type.
220+
auto AST_ObjC = buildASTFromCodeWithArgs(
221+
R"objc(
222+
__attribute__((ns_returns_retained)) id f();
223+
)objc",
224+
{"-fobjc-arc", "-fsyntax-only", "-fobjc-runtime=macosx-10.7"},
225+
"input.mm");
226+
{
227+
const FunctionDecl *f = getFunctionNode(AST_ObjC.get(), "f");
228+
const FunctionTypeLoc FTL = f->getFunctionTypeLoc();
229+
230+
const FunctionType *FT = FTL.getTypePtr();
231+
EXPECT_TRUE(FT->getExtInfo().getProducesResult());
232+
}
233+
156234
// Test type annotation on an `__auto_type` type in C mode.
157235
AST = buildASTFromCodeWithArgs(R"c(
158236
__auto_type [[clang::annotate_type("auto")]] auto_var = 1;

0 commit comments

Comments
 (0)