Skip to content

[clang-include-cleaner] Make cleanup attr report expr location #140233

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

Merged
merged 1 commit into from
May 21, 2025
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
2 changes: 1 addition & 1 deletion clang-tools-extra/include-cleaner/lib/WalkAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
}

bool VisitCleanupAttr(CleanupAttr *attr) {
report(attr->getLocation(), attr->getFunctionDecl());
report(attr->getArgLoc(), attr->getFunctionDecl());
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ TEST(WalkAST, OperatorNewDelete) {

TEST(WalkAST, CleanupAttr) {
testWalk("void* $explicit^freep(void *p);",
"void foo() { __attribute__((^__cleanup__(freep))) char* x = 0; }");
"void foo() { __attribute__((__cleanup__(^freep))) char* x = 0; }");
}

} // namespace
Expand Down
13 changes: 12 additions & 1 deletion clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,17 @@ def Cleanup : InheritableAttr {
let Args = [DeclArgument<Function, "FunctionDecl">];
let Subjects = SubjectList<[LocalVar]>;
let Documentation = [CleanupDocs];
// FIXME: DeclArgument should be reworked to also store the
// Expr instead of adding attr specific hacks like the following.
// See the discussion in https://github.com/llvm/llvm-project/pull/14023.
let AdditionalMembers = [{
private:
SourceLocation ArgLoc;

public:
void setArgLoc(const SourceLocation &Loc) { ArgLoc = Loc; }
auto getArgLoc() const { return ArgLoc; }
}];
}

def CmseNSEntry : InheritableAttr, TargetSpecificAttr<TargetARM> {
Expand Down Expand Up @@ -4815,7 +4826,7 @@ def HLSLResourceBinding: InheritableAttr {
void setImplicitBindingOrderID(uint32_t Value) {
ImplicitBindingOrderID = Value;
}
bool hasImplicitBindingOrderID() const {
bool hasImplicitBindingOrderID() const {
return ImplicitBindingOrderID.has_value();
}
uint32_t getImplicitBindingOrderID() const {
Expand Down
4 changes: 3 additions & 1 deletion clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3620,7 +3620,9 @@ static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
return;
}

D->addAttr(::new (S.Context) CleanupAttr(S.Context, AL, FD));
auto *attr = ::new (S.Context) CleanupAttr(S.Context, AL, FD);
attr->setArgLoc(E->getExprLoc());
D->addAttr(attr);
}

static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Expand Down
Loading