dcastagna updated this revision to Diff 336912.
dcastagna added a comment.

clang-format again


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D100310

Files:
  clang/include/clang/Tooling/Refactoring/RecursiveSymbolVisitor.h
  clang/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp
  clang/unittests/Rename/RenameClassTest.cpp


Index: clang/unittests/Rename/RenameClassTest.cpp
===================================================================
--- clang/unittests/Rename/RenameClassTest.cpp
+++ clang/unittests/Rename/RenameClassTest.cpp
@@ -780,6 +780,27 @@
   CompareSnippets(Expected, After);
 }
 
+TEST_F(ClangRenameTest, FieldDesignatedInitializers) {
+  std::string Before = R"(
+      struct S {
+        int a;
+      };
+      void foo() {
+        S s = { .a = 10 };
+        s.a = 20;
+      })";
+  std::string Expected = R"(
+      struct S {
+        int b;
+      };
+      void foo() {
+        S s = { .b = 10 };
+        s.b = 20;
+      })";
+  std::string After = runClangRenameOnCode(Before, "S::a", "S::b");
+  CompareSnippets(Expected, After);
+}
+
 // FIXME: investigate why the test fails when adding a new USR to the USRSet.
 TEST_F(ClangRenameTest, DISABLED_NestedTemplates) {
   std::string Before = R"(
Index: clang/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp
===================================================================
--- clang/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp
+++ clang/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp
@@ -226,6 +226,24 @@
     return true;
   }
 
+  bool VisitDesignatedInitExpr(const DesignatedInitExpr *E) {
+    for (const DesignatedInitExpr::Designator &D : E->designators()) {
+      if (D.isFieldDesignator() && D.getField()) {
+        const FieldDecl *Decl = D.getField();
+        if (isInUSRSet(Decl)) {
+          auto StartLoc = D.getFieldLoc();
+          auto EndLoc = D.getFieldLoc();
+          RenameInfos.push_back({StartLoc, EndLoc,
+                                 /*FromDecl=*/nullptr,
+                                 /*Context=*/nullptr,
+                                 /*Specifier=*/nullptr,
+                                 /*IgnorePrefixQualifiers=*/true});
+        }
+      }
+    }
+    return true;
+  }
+
   bool VisitCXXConstructorDecl(const CXXConstructorDecl *CD) {
     // Fix the constructor initializer when renaming class members.
     for (const auto *Initializer : CD->inits()) {
Index: clang/include/clang/Tooling/Refactoring/RecursiveSymbolVisitor.h
===================================================================
--- clang/include/clang/Tooling/Refactoring/RecursiveSymbolVisitor.h
+++ clang/include/clang/Tooling/Refactoring/RecursiveSymbolVisitor.h
@@ -122,6 +122,17 @@
     return BaseType::TraverseNestedNameSpecifierLoc(NNS);
   }
 
+  bool VisitDesignatedInitExpr(const DesignatedInitExpr *E) {
+    for (const DesignatedInitExpr::Designator &D : E->designators()) {
+      if (D.isFieldDesignator() && D.getField()) {
+        const FieldDecl *Decl = D.getField();
+        if (!visit(Decl, D.getFieldLoc(), D.getFieldLoc()))
+          return false;
+      }
+    }
+    return true;
+  }
+
 private:
   const SourceManager &SM;
   const LangOptions &LangOpts;


Index: clang/unittests/Rename/RenameClassTest.cpp
===================================================================
--- clang/unittests/Rename/RenameClassTest.cpp
+++ clang/unittests/Rename/RenameClassTest.cpp
@@ -780,6 +780,27 @@
   CompareSnippets(Expected, After);
 }
 
+TEST_F(ClangRenameTest, FieldDesignatedInitializers) {
+  std::string Before = R"(
+      struct S {
+        int a;
+      };
+      void foo() {
+        S s = { .a = 10 };
+        s.a = 20;
+      })";
+  std::string Expected = R"(
+      struct S {
+        int b;
+      };
+      void foo() {
+        S s = { .b = 10 };
+        s.b = 20;
+      })";
+  std::string After = runClangRenameOnCode(Before, "S::a", "S::b");
+  CompareSnippets(Expected, After);
+}
+
 // FIXME: investigate why the test fails when adding a new USR to the USRSet.
 TEST_F(ClangRenameTest, DISABLED_NestedTemplates) {
   std::string Before = R"(
Index: clang/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp
===================================================================
--- clang/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp
+++ clang/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp
@@ -226,6 +226,24 @@
     return true;
   }
 
+  bool VisitDesignatedInitExpr(const DesignatedInitExpr *E) {
+    for (const DesignatedInitExpr::Designator &D : E->designators()) {
+      if (D.isFieldDesignator() && D.getField()) {
+        const FieldDecl *Decl = D.getField();
+        if (isInUSRSet(Decl)) {
+          auto StartLoc = D.getFieldLoc();
+          auto EndLoc = D.getFieldLoc();
+          RenameInfos.push_back({StartLoc, EndLoc,
+                                 /*FromDecl=*/nullptr,
+                                 /*Context=*/nullptr,
+                                 /*Specifier=*/nullptr,
+                                 /*IgnorePrefixQualifiers=*/true});
+        }
+      }
+    }
+    return true;
+  }
+
   bool VisitCXXConstructorDecl(const CXXConstructorDecl *CD) {
     // Fix the constructor initializer when renaming class members.
     for (const auto *Initializer : CD->inits()) {
Index: clang/include/clang/Tooling/Refactoring/RecursiveSymbolVisitor.h
===================================================================
--- clang/include/clang/Tooling/Refactoring/RecursiveSymbolVisitor.h
+++ clang/include/clang/Tooling/Refactoring/RecursiveSymbolVisitor.h
@@ -122,6 +122,17 @@
     return BaseType::TraverseNestedNameSpecifierLoc(NNS);
   }
 
+  bool VisitDesignatedInitExpr(const DesignatedInitExpr *E) {
+    for (const DesignatedInitExpr::Designator &D : E->designators()) {
+      if (D.isFieldDesignator() && D.getField()) {
+        const FieldDecl *Decl = D.getField();
+        if (!visit(Decl, D.getFieldLoc(), D.getFieldLoc()))
+          return false;
+      }
+    }
+    return true;
+  }
+
 private:
   const SourceManager &SM;
   const LangOptions &LangOpts;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to