[clang-tools-extra] [include-cleaner] Support ObjC @compatibility_alias in WalkAST (PR #220401)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 1 14:57:37 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tools-extra
Author: dmaclach (dmaclach)
<details>
<summary>Changes</summary>
When visiting an ObjCInterfaceTypeLoc, check if the spelled name matches a @<!-- -->compatibility_alias. If so, report the alias declaration instead of the underlying interface declaration. This ensures that include-cleaner attributes the usage to the header defining the alias.
---
Full diff: https://github.com/llvm/llvm-project/pull/220401.diff
2 Files Affected:
- (modified) clang-tools-extra/include-cleaner/lib/WalkAST.cpp (+21-1)
- (modified) clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp (+2-2)
``````````diff
diff --git a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
index 13978b0462acd..0f02ed3de76d8 100644
--- a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
+++ b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
@@ -441,7 +441,27 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
// Objective-C support
bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
- reportType(TL.getNameLoc(), TL.getIFaceDecl());
+ ObjCInterfaceDecl *IFace = TL.getIFaceDecl();
+ if (!IFace)
+ return true;
+
+ SourceLocation Loc = TL.getNameLoc();
+ ASTContext &Ctx = IFace->getASTContext();
+ StringRef SpelledName =
+ Lexer::getSourceText(CharSourceRange::getTokenRange(Loc),
+ Ctx.getSourceManager(), Ctx.getLangOpts());
+ if (!SpelledName.empty() && SpelledName != IFace->getName()) {
+ // We may have a @compatibility_alias.
+ for (auto *D : Ctx.getTranslationUnitDecl()->decls()) {
+ if (auto *Alias = dyn_cast<ObjCCompatibleAliasDecl>(D)) {
+ if (Alias->getName() == SpelledName) {
+ report(Loc, Alias);
+ return true;
+ }
+ }
+ }
+ }
+ reportType(Loc, IFace);
return true;
}
diff --git a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
index 1e2ff594ef87a..3c82fd447ab99 100644
--- a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
@@ -1120,9 +1120,9 @@ TEST(WalkAST, ObjCCompatibleAliasDecl) {
TEST(WalkAST, ObjCCompatibleAliasUsage) {
testWalk(R"objc(
- @interface $explicit^MyClass
+ @interface MyClass
@end
- @compatibility_alias AliasName MyClass;
+ $explicit^@compatibility_alias AliasName MyClass;
)objc",
R"objc(
void test() {
``````````
</details>
https://github.com/llvm/llvm-project/pull/220401
More information about the cfe-commits
mailing list