This revision was automatically updated to reflect the committed changes.
Closed by commit rL296337: [clang-move] Extend clang-move to support moving 
global variable. (authored by hokein).

Changed prior to commit:
  https://reviews.llvm.org/D30337?vs=89659&id=89869#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D30337

Files:
  clang-tools-extra/trunk/clang-move/ClangMove.cpp
  clang-tools-extra/trunk/test/clang-move/Inputs/var_test.cpp
  clang-tools-extra/trunk/test/clang-move/Inputs/var_test.h
  clang-tools-extra/trunk/test/clang-move/move-var.cpp
  clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp

Index: clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp
===================================================================
--- clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp
+++ clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp
@@ -539,6 +539,8 @@
                             "enum class E2 { Red };\n"
                             "typedef int Int2;\n"
                             "using Int = int;\n"
+                            "extern int kGlobalInt;\n"
+                            "extern const char* const kGlobalStr;\n"
                             "} // namespace b\n"
                             "} // namespace a\n";
   const char TestCode[] = "#include \"foo.h\"\n";
@@ -553,7 +555,8 @@
       {"A", "Class"},         {"B", "Class"},        {"a::Move1", "Class"},
       {"a::f1", "Function"},  {"a::f2", "Function"}, {"a::b::Move1", "Class"},
       {"a::b::f", "Function"}, {"a::b::E1", "Enum"}, {"a::b::E2", "Enum"},
-      {"a::b::Int2", "TypeAlias"}, {"a::b::Int", "TypeAlias"} };
+      {"a::b::Int2", "TypeAlias"}, {"a::b::Int", "TypeAlias"},
+      {"a::b::kGlobalInt", "Variable"}, {"a::b::kGlobalStr", "Variable"}};
   runClangMoveOnCode(Spec, TestHeader, TestCode, &Reporter);
   std::set<DeclarationReporter::DeclarationPair> Results;
   for (const auto& DelPair : Reporter.getDeclarationList())
Index: clang-tools-extra/trunk/clang-move/ClangMove.cpp
===================================================================
--- clang-tools-extra/trunk/clang-move/ClangMove.cpp
+++ clang-tools-extra/trunk/clang-move/ClangMove.cpp
@@ -168,6 +168,21 @@
   ClangMoveTool *MoveTool;
 };
 
+class VarDeclarationMatch : public MatchFinder::MatchCallback {
+public:
+  explicit VarDeclarationMatch(ClangMoveTool *MoveTool)
+      : MoveTool(MoveTool) {}
+
+  void run(const MatchFinder::MatchResult &Result) override {
+    const auto *VD = Result.Nodes.getNodeAs<clang::VarDecl>("var");
+    assert(VD);
+    MoveDeclFromOldFileToNewFile(MoveTool, VD);
+  }
+
+private:
+  ClangMoveTool *MoveTool;
+};
+
 class TypeAliasMatch : public MatchFinder::MatchCallback {
 public:
   explicit TypeAliasMatch(ClangMoveTool *MoveTool)
@@ -618,6 +633,11 @@
                          .bind("function"),
                      MatchCallbacks.back().get());
 
+  MatchCallbacks.push_back(llvm::make_unique<VarDeclarationMatch>(this));
+  Finder->addMatcher(
+      varDecl(InOldFiles, *HasAnySymbolNames, TopLevelDecl).bind("var"),
+      MatchCallbacks.back().get());
+
   // Match enum definition in old.h. Enum helpers (which are defined in old.cc)
   // will not be moved for now no matter whether they are used or not.
   MatchCallbacks.push_back(llvm::make_unique<EnumDeclarationMatch>(this));
@@ -852,7 +872,10 @@
     for (const auto *Decl : UnremovedDeclsInOldHeader) {
       auto Kind = Decl->getKind();
       const std::string QualifiedName = Decl->getQualifiedNameAsString();
-      if (Kind == Decl::Kind::Function || Kind == Decl::Kind::FunctionTemplate)
+      if (Kind == Decl::Kind::Var)
+        Reporter->reportDeclaration(QualifiedName, "Variable");
+      else if (Kind == Decl::Kind::Function ||
+               Kind == Decl::Kind::FunctionTemplate)
         Reporter->reportDeclaration(QualifiedName, "Function");
       else if (Kind == Decl::Kind::ClassTemplate ||
                Kind == Decl::Kind::CXXRecord)
@@ -883,6 +906,7 @@
     case Decl::Kind::Typedef:
     case Decl::Kind::TypeAlias:
     case Decl::Kind::TypeAliasTemplate:
+    case Decl::Kind::Var:
       return true;
     default:
       return false;
Index: clang-tools-extra/trunk/test/clang-move/Inputs/var_test.h
===================================================================
--- clang-tools-extra/trunk/test/clang-move/Inputs/var_test.h
+++ clang-tools-extra/trunk/test/clang-move/Inputs/var_test.h
@@ -0,0 +1,11 @@
+namespace a {
+extern int kGlobalInt;
+extern const char *const kGlobalStr;
+}
+
+int kEvilInt = 2;
+
+inline void f1() {
+  int kGlobalInt = 3;
+  const char *const kGlobalStr = "Hello2";
+}
Index: clang-tools-extra/trunk/test/clang-move/Inputs/var_test.cpp
===================================================================
--- clang-tools-extra/trunk/test/clang-move/Inputs/var_test.cpp
+++ clang-tools-extra/trunk/test/clang-move/Inputs/var_test.cpp
@@ -0,0 +1,6 @@
+#include "var_test.h"
+
+namespace a{
+int kGlobalInt = 1;
+const char *const kGlobalStr = "Hello";
+}
Index: clang-tools-extra/trunk/test/clang-move/move-var.cpp
===================================================================
--- clang-tools-extra/trunk/test/clang-move/move-var.cpp
+++ clang-tools-extra/trunk/test/clang-move/move-var.cpp
@@ -0,0 +1,46 @@
+// RUN: mkdir -p %T/move-var
+// RUN: cp %S/Inputs/var_test*  %T/move-var
+// RUN: cd %T/move-var
+// RUN: clang-move -names="a::kGlobalInt" -new_header=%T/move-var/new_var_test.h -old_header=../move-var/var_test.h -old_cc=../move-var/var_test.cpp -new_cc=%T/move-var/new_var_test.cpp %T/move-var/var_test.cpp --
+// RUN: FileCheck -input-file=%T/move-var/var_test.h -check-prefix=CHECK-OLD-VAR-H-CASE1 %s
+// RUN: FileCheck -input-file=%T/move-var/var_test.cpp -check-prefix=CHECK-OLD-VAR-CPP-CASE1 %s
+// RUN: FileCheck -input-file=%T/move-var/new_var_test.h -check-prefix=CHECK-NEW-VAR-H-CASE1 %s
+// RUN: FileCheck -input-file=%T/move-var/new_var_test.cpp -check-prefix=CHECK-NEW-VAR-CPP-CASE1 %s
+
+// CHECK-OLD-VAR-H-CASE1-NOT: extern int kGlobalInt;
+// CHECK-OLD-VAR-H-CASE1: int kGlobalInt = 3;
+
+// CHECK-OLD-VAR-CPP-CASE1-NOT: int kGlobalInt = 1;
+
+// CHECK-NEW-VAR-H-CASE1: extern int kGlobalInt;
+// CHECK-NEW-VAR-H-CASE1-NOT: int kGlobalInt = 3;
+
+// CHECK-NEW-VAR-CPP-CASE1: int kGlobalInt = 1;
+
+
+// RUN: cp %S/Inputs/var_test*  %T/move-var
+// RUN: clang-move -names="a::kGlobalStr" -new_header=%T/move-var/new_var_test.h -old_header=../move-var/var_test.h -old_cc=../move-var/var_test.cpp -new_cc=%T/move-var/new_var_test.cpp %T/move-var/var_test.cpp --
+// RUN: FileCheck -input-file=%T/move-var/var_test.h -check-prefix=CHECK-OLD-VAR-H-CASE2 %s
+// RUN: FileCheck -input-file=%T/move-var/var_test.cpp -check-prefix=CHECK-OLD-VAR-CPP-CASE2 %s
+// RUN: FileCheck -input-file=%T/move-var/new_var_test.h -check-prefix=CHECK-NEW-VAR-H-CASE2 %s
+// RUN: FileCheck -input-file=%T/move-var/new_var_test.cpp -check-prefix=CHECK-NEW-VAR-CPP-CASE2 %s
+
+// CHECK-OLD-VAR-H-CASE2-NOT: extern const char *const kGlobalStr;
+// CHECK-OLD-VAR-H-CASE2: const char *const kGlobalStr = "Hello2";
+
+// CHECK-OLD-VAR-CPP-CASE2-NOT: const char *const kGlobalStr = "Hello";
+
+// CHECK-NEW-VAR-H-CASE2: extern const char *const kGlobalStr;
+// CHECK-NEW-VAR-H-CASE2-NOT: const char *const kGlobalStr = "Hello2";
+
+// CHECK-NEW-VAR-CPP-CASE2: const char *const kGlobalStr = "Hello";
+
+
+// RUN: cp %S/Inputs/var_test*  %T/move-var
+// RUN: clang-move -names="kEvilInt" -new_header=%T/move-var/new_var_test.h -old_header=../move-var/var_test.h -old_cc=../move-var/var_test.cpp -new_cc=%T/move-var/new_var_test.cpp %T/move-var/var_test.cpp --
+// RUN: FileCheck -input-file=%T/move-var/var_test.h -check-prefix=CHECK-OLD-VAR-H-CASE3 %s
+// RUN: FileCheck -input-file=%T/move-var/new_var_test.h -check-prefix=CHECK-NEW-VAR-H-CASE3 %s
+
+// CHECK-OLD-VAR-H-CASE3-NOT: int kEvilInt = 2;
+
+// CHECK-NEW-VAR-H-CASE3: int kEvilInt = 2;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to