Skip to content

Commit 75b6d33

Browse files
committed
[clang-include-cleaner] Make cleanup attr report expr location
Instead of reporting the location of the attribute, let's report the location of the function reference that's passed to the cleanup attribute as the first argument. This is required as the attribute might be coming from a macro which means clang-include-cleaner skips the use as it gets attributed to the header file declaringt the macro and not to the main file. To make this work, we have to add a fake argument to the CleanupAttr constructor so we can pass in the original Expr alongside the function declaration. Fixes #140212
1 parent bca39f4 commit 75b6d33

File tree

4 files changed

+7
-5
lines changed

4 files changed

+7
-5
lines changed

clang-tools-extra/include-cleaner/lib/WalkAST.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
322322
}
323323

324324
bool VisitCleanupAttr(CleanupAttr *attr) {
325-
report(attr->getLocation(), attr->getFunctionDecl());
325+
report(attr->getExpr()->getExprLoc(), attr->getFunctionDecl());
326326
return true;
327327
}
328328

clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ TEST(WalkAST, OperatorNewDelete) {
573573

574574
TEST(WalkAST, CleanupAttr) {
575575
testWalk("void* $explicit^freep(void *p);",
576-
"void foo() { __attribute__((^__cleanup__(freep))) char* x = 0; }");
576+
"void foo() { __attribute__((__cleanup__(^freep))) char* x = 0; }");
577577
}
578578

579579
} // namespace

clang/include/clang/Basic/Attr.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ class BoolArgument<string name, bit opt = 0, bit fake = 0> : Argument<name, opt,
226226
class IdentifierArgument<string name, bit opt = 0> : Argument<name, opt>;
227227
class IntArgument<string name, bit opt = 0> : Argument<name, opt>;
228228
class StringArgument<string name, bit opt = 0> : Argument<name, opt>;
229-
class ExprArgument<string name, bit opt = 0> : Argument<name, opt>;
229+
class ExprArgument<string name, bit opt = 0, bit fake = 0> : Argument<name, opt,
230+
fake>;
230231
class DeclArgument<DeclNode kind, string name, bit opt = 0, bit fake = 0>
231232
: Argument<name, opt, fake> {
232233
DeclNode Kind = kind;
@@ -1351,7 +1352,8 @@ def OSConsumesThis : InheritableAttr {
13511352

13521353
def Cleanup : InheritableAttr {
13531354
let Spellings = [GCC<"cleanup">];
1354-
let Args = [DeclArgument<Function, "FunctionDecl">];
1355+
let Args = [DeclArgument<Function, "FunctionDecl">,
1356+
ExprArgument<"Expr", /*opt=*/0, /*fake=*/1>];
13551357
let Subjects = SubjectList<[LocalVar]>;
13561358
let Documentation = [CleanupDocs];
13571359
}

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3620,7 +3620,7 @@ static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
36203620
return;
36213621
}
36223622

3623-
D->addAttr(::new (S.Context) CleanupAttr(S.Context, AL, FD));
3623+
D->addAttr(::new (S.Context) CleanupAttr(S.Context, AL, FD, E));
36243624
}
36253625

36263626
static void handleEnumExtensibilityAttr(Sema &S, Decl *D,

0 commit comments

Comments
 (0)