[clang-tools-extra] r278099 - [clang-rename] fix bug with initializer lists

Kirill Bobyrev via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 9 00:14:49 PDT 2016


Author: omtcyfz
Date: Tue Aug  9 02:14:48 2016
New Revision: 278099

URL: http://llvm.org/viewvc/llvm-project?rev=278099&view=rev
Log:
[clang-rename] fix bug with initializer lists

Clang-rename is currently not able to find a symbol in initializer list. This
patch fixes described issue.

Reviewers: alexfh

Differential Revision: https://reviews.llvm.org/D23193

Modified:
    clang-tools-extra/trunk/clang-rename/USRFinder.cpp
    clang-tools-extra/trunk/clang-rename/USRLocFinder.cpp
    clang-tools-extra/trunk/test/clang-rename/Field.cpp

Modified: clang-tools-extra/trunk/clang-rename/USRFinder.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-rename/USRFinder.cpp?rev=278099&r1=278098&r2=278099&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/USRFinder.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/USRFinder.cpp Tue Aug  9 02:14:48 2016
@@ -90,6 +90,25 @@ public:
                      TypeEndLoc);
   }
 
+  bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) {
+    for (auto &Initializer : ConstructorDecl->inits()) {
+      if (Initializer->getSourceOrder() == -1) {
+        // Ignore implicit initializers.
+        continue;
+      }
+      if (const clang::FieldDecl *FieldDecl = Initializer->getMember()) {
+        const SourceLocation InitBeginLoc = Initializer->getSourceLocation(),
+                             InitEndLoc = Lexer::getLocForEndOfToken(
+                                 InitBeginLoc, 0, Context.getSourceManager(),
+                                 Context.getLangOpts());
+        if (!setResult(FieldDecl, InitBeginLoc, InitEndLoc)) {
+          return false;
+        }
+      }
+    }
+    return true;
+  }
+
   // Other:
 
   const NamedDecl *getNamedDecl() { return Result; }

Modified: clang-tools-extra/trunk/clang-rename/USRLocFinder.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-rename/USRLocFinder.cpp?rev=278099&r1=278098&r2=278099&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/USRLocFinder.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/USRLocFinder.cpp Tue Aug  9 02:14:48 2016
@@ -48,18 +48,9 @@ public:
         // Ignore implicit initializers.
         continue;
       }
-      if (const clang::FieldDecl *FieldDecl = Initializer->getAnyMember()) {
+      if (const clang::FieldDecl *FieldDecl = Initializer->getMember()) {
         if (USRSet.find(getUSRForDecl(FieldDecl)) != USRSet.end()) {
-          // The initializer refers to a field that is to be renamed.
-          SourceLocation Location = Initializer->getSourceLocation();
-          StringRef TokenName = Lexer::getSourceText(
-              CharSourceRange::getTokenRange(Location),
-              Context.getSourceManager(), Context.getLangOpts());
-          if (TokenName == PrevName) {
-            // The token of the source location we find actually has the old
-            // name.
-            LocationsFound.push_back(Initializer->getSourceLocation());
-          }
+          LocationsFound.push_back(Initializer->getSourceLocation());
         }
       }
     }

Modified: clang-tools-extra/trunk/test/clang-rename/Field.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-rename/Field.cpp?rev=278099&r1=278098&r2=278099&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-rename/Field.cpp (original)
+++ clang-tools-extra/trunk/test/clang-rename/Field.cpp Tue Aug  9 02:14:48 2016
@@ -1,14 +1,15 @@
-// RUN: cat %s > %t.cpp
-// RUN: clang-rename -offset=148 -new-name=Bar %t.cpp -i --
-// RUN: sed 's,//.*,,' %t.cpp | FileCheck %s
-
 class Baz {
-  int Foo;              // CHECK: Bar;
+  int Foo; /* Test 1 */ // CHECK: int Bar;
 public:
   Baz();
 };
 
-Baz::Baz() : Foo(0) {}  // CHECK: Baz::Baz() : Bar(0) {}
+Baz::Baz() : Foo(0) /* Test 2 */ {}  // CHECK: Baz::Baz() : Bar(0)
+
+// Test 1.
+// RUN: clang-rename -offset=18 -new-name=Bar %s -- | sed 's,//.*,,' | FileCheck %s
+// Test 2.
+// RUN: clang-rename -offset=89 -new-name=Bar %s -- | sed 's,//.*,,' | FileCheck %s
 
-// Use grep -FUbo 'Foo' <file> to get the correct offset of foo when changing
-// this file.
+// To find offsets after modifying the file, use:
+//   grep -Ubo 'Foo.*' <file>




More information about the cfe-commits mailing list