2012-05-24 00:29:20 +08:00
|
|
|
//===--- Refactoring.cpp - Framework for clang refactoring tools ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implements tools to support refactorings.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-10-24 06:26:28 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2012-05-24 00:29:20 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
2012-09-01 13:09:24 +08:00
|
|
|
#include "clang/Rewrite/Core/Rewriter.h"
|
2012-05-24 00:29:20 +08:00
|
|
|
#include "clang/Tooling/Refactoring.h"
|
|
|
|
#include "llvm/Support/raw_os_ostream.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tooling {
|
|
|
|
|
|
|
|
static const char * const InvalidLocation = "";
|
|
|
|
|
|
|
|
Replacement::Replacement()
|
2013-05-16 18:40:07 +08:00
|
|
|
: FilePath(InvalidLocation) {}
|
2012-05-24 00:29:20 +08:00
|
|
|
|
2013-05-16 18:40:07 +08:00
|
|
|
Replacement::Replacement(StringRef FilePath, unsigned Offset, unsigned Length,
|
|
|
|
StringRef ReplacementText)
|
|
|
|
: FilePath(FilePath), ReplacementRange(Offset, Length),
|
|
|
|
ReplacementText(ReplacementText) {}
|
2012-05-24 00:29:20 +08:00
|
|
|
|
|
|
|
Replacement::Replacement(SourceManager &Sources, SourceLocation Start,
|
2013-01-13 03:30:44 +08:00
|
|
|
unsigned Length, StringRef ReplacementText) {
|
2012-05-24 00:29:20 +08:00
|
|
|
setFromSourceLocation(Sources, Start, Length, ReplacementText);
|
|
|
|
}
|
|
|
|
|
|
|
|
Replacement::Replacement(SourceManager &Sources, const CharSourceRange &Range,
|
2013-01-13 03:30:44 +08:00
|
|
|
StringRef ReplacementText) {
|
2012-05-24 00:29:20 +08:00
|
|
|
setFromSourceRange(Sources, Range, ReplacementText);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Replacement::isApplicable() const {
|
|
|
|
return FilePath != InvalidLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Replacement::apply(Rewriter &Rewrite) const {
|
|
|
|
SourceManager &SM = Rewrite.getSourceMgr();
|
|
|
|
const FileEntry *Entry = SM.getFileManager().getFile(FilePath);
|
|
|
|
if (Entry == NULL)
|
|
|
|
return false;
|
|
|
|
FileID ID;
|
|
|
|
// FIXME: Use SM.translateFile directly.
|
|
|
|
SourceLocation Location = SM.translateFileLineCol(Entry, 1, 1);
|
|
|
|
ID = Location.isValid() ?
|
|
|
|
SM.getFileID(Location) :
|
|
|
|
SM.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
|
|
|
|
// FIXME: We cannot check whether Offset + Length is in the file, as
|
|
|
|
// the remapping API is not public in the RewriteBuffer.
|
|
|
|
const SourceLocation Start =
|
|
|
|
SM.getLocForStartOfFile(ID).
|
2013-05-16 18:40:07 +08:00
|
|
|
getLocWithOffset(ReplacementRange.getOffset());
|
2012-05-24 00:29:20 +08:00
|
|
|
// ReplaceText returns false on success.
|
|
|
|
// ReplaceText only fails if the source location is not a file location, in
|
|
|
|
// which case we already returned false earlier.
|
2013-05-16 18:40:07 +08:00
|
|
|
bool RewriteSucceeded = !Rewrite.ReplaceText(
|
|
|
|
Start, ReplacementRange.getLength(), ReplacementText);
|
2012-05-24 00:29:20 +08:00
|
|
|
assert(RewriteSucceeded);
|
|
|
|
return RewriteSucceeded;
|
|
|
|
}
|
|
|
|
|
2012-05-31 00:04:29 +08:00
|
|
|
std::string Replacement::toString() const {
|
|
|
|
std::string result;
|
|
|
|
llvm::raw_string_ostream stream(result);
|
2013-05-16 18:40:07 +08:00
|
|
|
stream << FilePath << ": " << ReplacementRange.getOffset() << ":+"
|
|
|
|
<< ReplacementRange.getLength() << ":\"" << ReplacementText << "\"";
|
2012-05-31 00:04:29 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-05-24 00:29:20 +08:00
|
|
|
bool Replacement::Less::operator()(const Replacement &R1,
|
|
|
|
const Replacement &R2) const {
|
|
|
|
if (R1.FilePath != R2.FilePath) return R1.FilePath < R2.FilePath;
|
2013-05-16 18:40:07 +08:00
|
|
|
if (R1.ReplacementRange.getOffset() != R2.ReplacementRange.getOffset())
|
|
|
|
return R1.ReplacementRange.getOffset() < R2.ReplacementRange.getOffset();
|
|
|
|
if (R1.ReplacementRange.getLength() != R2.ReplacementRange.getLength())
|
|
|
|
return R1.ReplacementRange.getLength() < R2.ReplacementRange.getLength();
|
2012-05-24 00:29:20 +08:00
|
|
|
return R1.ReplacementText < R2.ReplacementText;
|
|
|
|
}
|
|
|
|
|
2013-08-08 21:31:14 +08:00
|
|
|
bool Replacement::operator==(const Replacement &Other) const {
|
|
|
|
return ReplacementRange.getOffset() == Other.ReplacementRange.getOffset() &&
|
|
|
|
ReplacementRange.getLength() == Other.ReplacementRange.getLength() &&
|
|
|
|
FilePath == Other.FilePath && ReplacementText == Other.ReplacementText;
|
|
|
|
}
|
|
|
|
|
2012-05-24 00:29:20 +08:00
|
|
|
void Replacement::setFromSourceLocation(SourceManager &Sources,
|
|
|
|
SourceLocation Start, unsigned Length,
|
2013-01-13 03:30:44 +08:00
|
|
|
StringRef ReplacementText) {
|
2012-05-24 00:29:20 +08:00
|
|
|
const std::pair<FileID, unsigned> DecomposedLocation =
|
|
|
|
Sources.getDecomposedLoc(Start);
|
|
|
|
const FileEntry *Entry = Sources.getFileEntryForID(DecomposedLocation.first);
|
|
|
|
this->FilePath = Entry != NULL ? Entry->getName() : InvalidLocation;
|
2013-05-16 18:40:07 +08:00
|
|
|
this->ReplacementRange = Range(DecomposedLocation.second, Length);
|
2012-05-24 00:29:20 +08:00
|
|
|
this->ReplacementText = ReplacementText;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: This should go into the Lexer, but we need to figure out how
|
|
|
|
// to handle ranges for refactoring in general first - there is no obvious
|
|
|
|
// good way how to integrate this into the Lexer yet.
|
|
|
|
static int getRangeSize(SourceManager &Sources, const CharSourceRange &Range) {
|
|
|
|
SourceLocation SpellingBegin = Sources.getSpellingLoc(Range.getBegin());
|
|
|
|
SourceLocation SpellingEnd = Sources.getSpellingLoc(Range.getEnd());
|
|
|
|
std::pair<FileID, unsigned> Start = Sources.getDecomposedLoc(SpellingBegin);
|
|
|
|
std::pair<FileID, unsigned> End = Sources.getDecomposedLoc(SpellingEnd);
|
|
|
|
if (Start.first != End.first) return -1;
|
|
|
|
if (Range.isTokenRange())
|
|
|
|
End.second += Lexer::MeasureTokenLength(SpellingEnd, Sources,
|
|
|
|
LangOptions());
|
|
|
|
return End.second - Start.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Replacement::setFromSourceRange(SourceManager &Sources,
|
|
|
|
const CharSourceRange &Range,
|
2013-01-13 03:30:44 +08:00
|
|
|
StringRef ReplacementText) {
|
2012-05-24 00:29:20 +08:00
|
|
|
setFromSourceLocation(Sources, Sources.getSpellingLoc(Range.getBegin()),
|
|
|
|
getRangeSize(Sources, Range), ReplacementText);
|
|
|
|
}
|
|
|
|
|
2013-07-18 02:29:58 +08:00
|
|
|
bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite) {
|
2012-05-24 00:29:20 +08:00
|
|
|
bool Result = true;
|
|
|
|
for (Replacements::const_iterator I = Replaces.begin(),
|
|
|
|
E = Replaces.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I->isApplicable()) {
|
|
|
|
Result = I->apply(Rewrite) && Result;
|
|
|
|
} else {
|
|
|
|
Result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2013-07-18 02:29:58 +08:00
|
|
|
std::string applyAllReplacements(StringRef Code, const Replacements &Replaces) {
|
2013-05-16 18:40:07 +08:00
|
|
|
FileManager Files((FileSystemOptions()));
|
|
|
|
DiagnosticsEngine Diagnostics(
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
|
|
|
|
new DiagnosticOptions);
|
|
|
|
Diagnostics.setClient(new TextDiagnosticPrinter(
|
|
|
|
llvm::outs(), &Diagnostics.getDiagnosticOptions()));
|
|
|
|
SourceManager SourceMgr(Diagnostics, Files);
|
|
|
|
Rewriter Rewrite(SourceMgr, LangOptions());
|
|
|
|
llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, "<stdin>");
|
|
|
|
const clang::FileEntry *Entry =
|
|
|
|
Files.getVirtualFile("<stdin>", Buf->getBufferSize(), 0);
|
|
|
|
SourceMgr.overrideFileContents(Entry, Buf);
|
|
|
|
FileID ID =
|
|
|
|
SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
|
2013-07-18 02:29:58 +08:00
|
|
|
for (Replacements::const_iterator I = Replaces.begin(), E = Replaces.end();
|
|
|
|
I != E; ++I) {
|
2013-05-16 18:40:07 +08:00
|
|
|
Replacement Replace("<stdin>", I->getOffset(), I->getLength(),
|
|
|
|
I->getReplacementText());
|
|
|
|
if (!Replace.apply(Rewrite))
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
std::string Result;
|
|
|
|
llvm::raw_string_ostream OS(Result);
|
|
|
|
Rewrite.getEditBuffer(ID).write(OS);
|
|
|
|
OS.flush();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2013-05-21 20:21:39 +08:00
|
|
|
unsigned shiftedCodePosition(const Replacements &Replaces, unsigned Position) {
|
|
|
|
unsigned NewPosition = Position;
|
|
|
|
for (Replacements::iterator I = Replaces.begin(), E = Replaces.end(); I != E;
|
|
|
|
++I) {
|
|
|
|
if (I->getOffset() >= Position)
|
|
|
|
break;
|
|
|
|
if (I->getOffset() + I->getLength() > Position)
|
|
|
|
NewPosition += I->getOffset() + I->getLength() - Position;
|
|
|
|
NewPosition += I->getReplacementText().size() - I->getLength();
|
|
|
|
}
|
|
|
|
return NewPosition;
|
|
|
|
}
|
|
|
|
|
2013-08-08 21:31:14 +08:00
|
|
|
void deduplicate(std::vector<Replacement> &Replaces,
|
|
|
|
std::vector<Range> &Conflicts) {
|
|
|
|
if (Replaces.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Deduplicate
|
|
|
|
std::sort(Replaces.begin(), Replaces.end(), Replacement::Less());
|
|
|
|
std::vector<Replacement>::iterator End =
|
|
|
|
std::unique(Replaces.begin(), Replaces.end());
|
|
|
|
Replaces.erase(End, Replaces.end());
|
|
|
|
|
|
|
|
// Detect conflicts
|
|
|
|
Range ConflictRange(Replaces.front().getOffset(),
|
|
|
|
Replaces.front().getLength());
|
|
|
|
unsigned ConflictStart = 0;
|
|
|
|
unsigned ConflictLength = 1;
|
|
|
|
for (unsigned i = 1; i < Replaces.size(); ++i) {
|
|
|
|
Range Current(Replaces[i].getOffset(), Replaces[i].getLength());
|
|
|
|
if (ConflictRange.overlapsWith(Current)) {
|
|
|
|
// Extend conflicted range
|
|
|
|
ConflictRange = Range(ConflictRange.getOffset(),
|
2013-08-14 00:26:44 +08:00
|
|
|
std::max(ConflictRange.getLength(),
|
|
|
|
Current.getOffset() + Current.getLength() -
|
|
|
|
ConflictRange.getOffset()));
|
2013-08-08 21:31:14 +08:00
|
|
|
++ConflictLength;
|
|
|
|
} else {
|
|
|
|
if (ConflictLength > 1)
|
|
|
|
Conflicts.push_back(Range(ConflictStart, ConflictLength));
|
|
|
|
ConflictRange = Current;
|
|
|
|
ConflictStart = i;
|
|
|
|
ConflictLength = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ConflictLength > 1)
|
|
|
|
Conflicts.push_back(Range(ConflictStart, ConflictLength));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-24 00:29:20 +08:00
|
|
|
RefactoringTool::RefactoringTool(const CompilationDatabase &Compilations,
|
|
|
|
ArrayRef<std::string> SourcePaths)
|
2013-01-12 01:04:55 +08:00
|
|
|
: ClangTool(Compilations, SourcePaths) {}
|
2012-05-24 00:29:20 +08:00
|
|
|
|
|
|
|
Replacements &RefactoringTool::getReplacements() { return Replace; }
|
|
|
|
|
2013-01-12 01:04:55 +08:00
|
|
|
int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {
|
|
|
|
if (int Result = run(ActionFactory)) {
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2012-05-24 00:29:20 +08:00
|
|
|
LangOptions DefaultLangOptions;
|
2012-10-24 06:26:28 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
|
|
|
TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
|
2012-05-24 00:29:20 +08:00
|
|
|
DiagnosticsEngine Diagnostics(
|
2013-01-13 03:30:44 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
|
2012-10-24 06:26:28 +08:00
|
|
|
&*DiagOpts, &DiagnosticPrinter, false);
|
2013-01-12 01:04:55 +08:00
|
|
|
SourceManager Sources(Diagnostics, getFiles());
|
2012-05-24 00:29:20 +08:00
|
|
|
Rewriter Rewrite(Sources, DefaultLangOptions);
|
2013-01-12 01:04:55 +08:00
|
|
|
|
|
|
|
if (!applyAllReplacements(Rewrite)) {
|
2012-05-24 00:29:20 +08:00
|
|
|
llvm::errs() << "Skipped some replacements.\n";
|
|
|
|
}
|
2013-01-12 01:04:55 +08:00
|
|
|
|
|
|
|
return saveRewrittenFiles(Rewrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
|
|
|
|
return tooling::applyAllReplacements(Replace, Rewrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
|
|
|
|
for (Rewriter::buffer_iterator I = Rewrite.buffer_begin(),
|
|
|
|
E = Rewrite.buffer_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
// FIXME: This code is copied from the FixItRewriter.cpp - I think it should
|
|
|
|
// go into directly into Rewriter (there we also have the Diagnostics to
|
|
|
|
// handle the error cases better).
|
|
|
|
const FileEntry *Entry =
|
|
|
|
Rewrite.getSourceMgr().getFileEntryForID(I->first);
|
|
|
|
std::string ErrorInfo;
|
2013-07-17 03:44:23 +08:00
|
|
|
llvm::raw_fd_ostream FileStream(Entry->getName(), ErrorInfo,
|
|
|
|
llvm::sys::fs::F_Binary);
|
2013-01-12 01:04:55 +08:00
|
|
|
if (!ErrorInfo.empty())
|
|
|
|
return 1;
|
|
|
|
I->second.write(FileStream);
|
|
|
|
FileStream.flush();
|
2012-05-24 00:29:20 +08:00
|
|
|
}
|
2013-01-12 01:04:55 +08:00
|
|
|
return 0;
|
2012-05-24 00:29:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace tooling
|
|
|
|
} // end namespace clang
|