Skip to content

Commit

Permalink
[clang-move] Fix incorrect EndLoc for declarations in macros.
Browse files Browse the repository at this point in the history
Reviewers: ioeric

Subscribers: cfe-commits

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

llvm-svn: 289541
  • Loading branch information
hokein committed Dec 13, 2016
1 parent 79d1255 commit dc4edba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions clang-tools-extra/clang-move/ClangMove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ SourceLocation
getLocForEndOfDecl(const clang::Decl *D,
const LangOptions &LangOpts = clang::LangOptions()) {
const auto &SM = D->getASTContext().getSourceManager();
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(D->getLocEnd());
auto EndExpansionLoc = SM.getExpansionLoc(D->getLocEnd());
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(EndExpansionLoc);
// Try to load the file buffer.
bool InvalidTemp = false;
llvm::StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp);
Expand All @@ -235,7 +236,7 @@ getLocForEndOfDecl(const clang::Decl *D,
// FIXME: this is a bit hacky to get ReadToEndOfLine work.
Lex.setParsingPreprocessorDirective(true);
Lex.ReadToEndOfLine(&Line);
SourceLocation EndLoc = D->getLocEnd().getLocWithOffset(Line.size());
SourceLocation EndLoc = EndExpansionLoc.getLocWithOffset(Line.size());
// If we already reach EOF, just return the EOF SourceLocation;
// otherwise, move 1 offset ahead to include the trailing newline character
// '\n'.
Expand Down
18 changes: 18 additions & 0 deletions clang-tools-extra/unittests/clang-move/ClangMoveTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,24 @@ TEST(ClangMove, MacroInFunction) {
EXPECT_EQ(ExpectedNewCode, Results[Spec.NewCC]);
}

TEST(ClangMove, DefinitionInMacro) {
const char TestHeader[] = "#define DEF(CLASS) void CLASS##_::f() {}\n"
"class A_ {\nvoid f();\n};\n"
"class B {};\n";
const char TestCode[] = "#include \"foo.h\"\n"
"DEF(A)\n";
const char ExpectedNewCode[] = "#include \"new_foo.h\"\n\n"
"DEF(A)\n";
move::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]);
}

TEST(ClangMove, WellFormattedCode) {
const std::string CommonHeader =
"namespace a {\n"
Expand Down

0 comments on commit dc4edba

Please sign in to comment.