Skip to content

Commit

Permalink
Make getFunctionTypeLoc support AttributedType
Browse files Browse the repository at this point in the history
  • Loading branch information
v01dxyz committed Dec 12, 2024
1 parent d6572d9 commit f007235
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
4 changes: 4 additions & 0 deletions clang/include/clang/AST/TypeLoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,10 @@ class AttributedTypeLoc : public ConcreteTypeLoc<UnqualTypeLoc,
return getInnerTypeLoc();
}

TypeLoc getEquivalentTypeLoc() const {
return TypeLoc(getTypePtr()->getEquivalentType(), getNonLocalData());
}

/// The type attribute.
const Attr *getAttr() const {
return getLocalData()->TypeAttr;
Expand Down
19 changes: 17 additions & 2 deletions clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3745,8 +3745,23 @@ bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {

FunctionTypeLoc FunctionDecl::getFunctionTypeLoc() const {
const TypeSourceInfo *TSI = getTypeSourceInfo();
return TSI ? TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>()
: FunctionTypeLoc();

if (!TSI)
return FunctionTypeLoc();

TypeLoc TL = TSI->getTypeLoc();
FunctionTypeLoc FTL;

while (!(FTL = TL.getAs<FunctionTypeLoc>())) {
if (auto PTL = TL.getAs<ParenTypeLoc>())
TL = PTL.getInnerLoc();
else if (auto ATL = TL.getAs<AttributedTypeLoc>())
TL = ATL.getEquivalentTypeLoc();
else
break;
}

return FTL;
}

SourceRange FunctionDecl::getReturnTypeSourceRange() const {
Expand Down
15 changes: 1 addition & 14 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2413,23 +2413,10 @@ static void handleCHERICompartmentName(Sema &S, Decl *D, const ParsedAttr &Attr,
if (FD && FD->getReturnType()->isVoidType()) {
S.Diag(Attr.getLoc(), diag::warn_cheri_compartment_void_return_type);

const TypeSourceInfo *TSI = FD->getTypeSourceInfo();
TypeLoc TL = TSI->getTypeLoc().IgnoreParens();

// ignore function type attributes
while (auto ATL = TL.getAs<AttributedTypeLoc>())
TL = ATL.getModifiedLoc();

if (auto FTL = TL.getAs<FunctionTypeLoc>()) {
SourceRange SR = FTL.getReturnLoc().getSourceRange();
if (SourceRange SR = FD->getReturnTypeSourceRange(); SR.isValid()) {
S.Diag(SR.getBegin(), diag::note_cheri_compartment_void_return_type)
<< FixItHint::CreateReplacement(SR, "int");
}
// FunctionTypeLoc FTL = FD->getFunctionTypeLoc();

// TypeLoc RL = FTL.getReturnLoc();
// SourceRange SR = FD->getReturnTypeSourceRange();

} else
D->addAttr(::new (S.Context) WarnUnusedResultAttr(
S.Context, Attr, "CHERI compartment call"));
Expand Down

0 comments on commit f007235

Please sign in to comment.