[PATCH] D139087: [include-cleaner] Handle base class member access from derived class.

Viktoriia Bakalova via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 2 01:42:31 PST 2022


VitaNuo updated this revision to Diff 479556.
VitaNuo added a comment.

Add more comments. Add unimplemented VisitCXXDependentScopeMemberExpr method with a FIXME comment.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D139087/new/

https://reviews.llvm.org/D139087

Files:
  clang-tools-extra/include-cleaner/lib/WalkAST.cpp
  clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp


Index: clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
===================================================================
--- clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
@@ -88,12 +88,10 @@
     auto RTStr = llvm::to_string(RT);
     for (auto Expected : Target.points(RTStr))
       if (!llvm::is_contained(ReferencedOffsets[RT], Expected))
-        DiagnosePoint("location not marked used with type " + RTStr,
-                      Expected);
+        DiagnosePoint("location not marked used with type " + RTStr, Expected);
     for (auto Actual : ReferencedOffsets[RT])
       if (!llvm::is_contained(Target.points(RTStr), Actual))
-        DiagnosePoint("location unexpectedly used with type " + RTStr,
-                      Actual);
+        DiagnosePoint("location unexpectedly used with type " + RTStr, Actual);
   }
 
   // If there were any differences, we print the entire referencing code once.
@@ -172,10 +170,16 @@
 }
 
 TEST(WalkAST, MemberExprs) {
-  testWalk("struct S { void $explicit^foo(); };", "void foo() { S{}.^foo(); }");
+  testWalk("struct $explicit^S { void foo(); };", "void foo() { S{}.^foo(); }");
   testWalk(
-      "struct S { void foo(); }; struct X : S { using S::$explicit^foo; };",
+      "struct S { void foo(); }; struct $explicit^X : S { using S::foo; };",
       "void foo() { X{}.^foo(); }");
+  testWalk("struct Base { int a; }; struct $explicit^Derived : public Base {};",
+           "void fun(Derived d) { d.^a; }");
+  testWalk("struct Base { int a; }; struct $explicit^Derived : public Base {};",
+           "void fun(Derived* d) { d->^a; }");
+  testWalk("struct Base { int a; }; struct $explicit^Derived : public Base {};",
+           "void fun(Derived& d) { d.^a; }");
 }
 
 TEST(WalkAST, ConstructExprs) {
Index: clang-tools-extra/include-cleaner/lib/WalkAST.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/WalkAST.cpp
+++ clang-tools-extra/include-cleaner/lib/WalkAST.cpp
@@ -59,10 +59,29 @@
   }
 
   bool VisitMemberExpr(MemberExpr *E) {
-    report(E->getMemberLoc(), E->getFoundDecl().getDecl());
+    // Instead of the FieldDecl for MemberExpr, we report the Decl of
+    // the corresponding record. This is done in order to report
+    // the usage of most specific type (e.g., derived class, when
+    // using members from the base).
+    QualType Type = E->getBase()->IgnoreImpCasts()->getType();
+    RecordDecl *RD = getDeclFromType(Type);
+    report(E->getMemberLoc(), RD);
     return true;
   }
 
+  bool VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
+    // FIXME: implement this
+    return true;
+  }
+
+  RecordDecl *getDeclFromType(QualType Type) {
+    if (Type->isPointerType()) {
+      Type = Type->getPointeeType();
+    }
+    RecordDecl *RecordDecl = Type->getAsRecordDecl();
+    return RecordDecl;
+  }
+
   bool VisitCXXConstructExpr(CXXConstructExpr *E) {
     report(E->getLocation(), E->getConstructor(),
            E->getParenOrBraceRange().isValid() ? RefType::Explicit


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139087.479556.patch
Type: text/x-patch
Size: 3149 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221202/8dfd4fff/attachment-0001.bin>


More information about the cfe-commits mailing list