[clang-tools-extra] r286833 - [clang-move] Fix an incorrect range for the functions whose returned value is a macro

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 14 06:46:49 PST 2016


Author: hokein
Date: Mon Nov 14 08:46:48 2016
New Revision: 286833

URL: http://llvm.org/viewvc/llvm-project?rev=286833&view=rev
Log:
[clang-move] Fix an incorrect range for the functions whose returned value is a macro

Summary:
Fix an incorrect range for the functions whose returned value is a macro
(e.g. `bool`). This incorrect range can lead to modifications of an unexpected
file where the macro is in.

We should use expansion location instead of spelling location.

Reviewers: ioeric

Subscribers: cfe-commits

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

Modified:
    clang-tools-extra/trunk/clang-move/ClangMove.cpp
    clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp

Modified: clang-tools-extra/trunk/clang-move/ClangMove.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-move/ClangMove.cpp?rev=286833&r1=286832&r2=286833&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-move/ClangMove.cpp (original)
+++ clang-tools-extra/trunk/clang-move/ClangMove.cpp Mon Nov 14 08:46:48 2016
@@ -227,8 +227,8 @@ getLocForEndOfDecl(const clang::Decl *D,
 clang::CharSourceRange
 GetFullRange(const clang::SourceManager *SM, const clang::Decl *D,
              const clang::LangOptions &options = clang::LangOptions()) {
-  clang::SourceRange Full = D->getSourceRange();
-  Full.setEnd(getLocForEndOfDecl(D, SM));
+  clang::SourceRange Full(SM->getExpansionLoc(D->getLocStart()),
+                          getLocForEndOfDecl(D, SM));
   // Expand to comments that are associated with the Decl.
   if (const auto *Comment = D->getASTContext().getRawCommentForDeclNoCache(D)) {
     if (SM->isBeforeInTranslationUnit(Full.getEnd(), Comment->getLocEnd()))

Modified: clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp?rev=286833&r1=286832&r2=286833&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp Mon Nov 14 08:46:48 2016
@@ -337,6 +337,24 @@ TEST(ClangMove, DontMoveAll) {
   }
 }
 
+TEST(ClangMove, MacroInFunction) {
+  const char TestHeader[] = "#define INT int\n"
+                            "class A {\npublic:\n  int f();\n};\n"
+                            "class B {};\n";
+  const char TestCode[] = "#include \"foo.h\"\n"
+                          "INT A::f() { return 0; }\n";
+  const char ExpectedNewCode[] = "#include \"new_foo.h\"\n\n"
+                                 "INT A::f() { return 0; }\n";
+  move::ClangMoveTool::MoveDefinitionSpec Spec;
+  Spec.Names.push_back("A");
+  Spec.OldHeader = "foo.h";
+  Spec.OldCC = "foo.cc";
+  Spec.NewHeader = "new_foo.h";
+  Spec.NewCC = "new_foo.cc";
+  auto Results = runClangMoveOnCode(Spec, TestHeader, TestCode);
+  EXPECT_EQ(ExpectedNewCode, Results[Spec.NewCC]);
+}
+
 } // namespace
 } // namespce move
 } // namespace clang




More information about the cfe-commits mailing list