Adds the Refactoring library, which is a layer on top of the Tooling library

that allows easy refactoring across translation units.

llvm-svn: 157331
This commit is contained in:
Manuel Klimek 2012-05-23 16:29:20 +00:00
parent c34f776877
commit 3f00134c05
5 changed files with 627 additions and 1 deletions

View File

@ -0,0 +1,142 @@
//===--- Refactoring.h - Framework for clang refactoring tools --*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Interfaces supporting refactorings that span multiple translation units.
// While single translation unit refactorings are supported via the Rewriter,
// when refactoring multiple translation units changes must be stored in a
// SourceManager independent form, duplicate changes need to be removed, and
// all changes must be applied at once at the end of the refactoring so that
// the code is always parseable.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/StringRef.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Tooling/Tooling.h"
#include <set>
#include <string>
namespace clang {
class Rewriter;
class SourceLocation;
namespace tooling {
/// \brief A text replacement.
///
/// Represents a SourceManager independent replacement of a range of text in a
/// specific file.
class Replacement {
public:
/// \brief Creates an invalid (not applicable) replacement.
Replacement();
/// \brief Creates a replacement of the range [Offset, Offset+Length) in
/// FilePath with ReplacementText.
///
/// \param FilePath A source file accessible via a SourceManager.
/// \param Offset The byte offset of the start of the range in the file.
/// \param Length The length of the range in bytes.
Replacement(llvm::StringRef FilePath, unsigned Offset,
unsigned Length, llvm::StringRef ReplacementText);
/// \brief Creates a Replacement of the range [Start, Start+Length) with
/// ReplacementText.
Replacement(SourceManager &Sources, SourceLocation Start, unsigned Length,
llvm::StringRef ReplacementText);
/// \brief Creates a Replacement of the given range with ReplacementText.
Replacement(SourceManager &Sources, const CharSourceRange &Range,
llvm::StringRef ReplacementText);
/// \brief Creates a Replacement of the node with ReplacementText.
template <typename Node>
Replacement(SourceManager &Sources, const Node &NodeToReplace,
llvm::StringRef ReplacementText);
/// \brief Returns whether this replacement can be applied to a file.
///
/// Only replacements that are in a valid file can be applied.
bool isApplicable() const;
/// \brief Accessors.
/// @{
std::string getFilePath() const { return FilePath; }
unsigned getOffset() const { return Offset; }
unsigned getLength() const { return Length; }
/// @}
/// \brief Applies the replacement on the Rewriter.
bool apply(Rewriter &Rewrite) const;
/// \brief Comparator to be able to use Replacement in std::set for uniquing.
class Less {
public:
bool operator()(const Replacement &R1, const Replacement &R2) const;
};
private:
void setFromSourceLocation(SourceManager &Sources, SourceLocation Start,
unsigned Length, llvm::StringRef ReplacementText);
void setFromSourceRange(SourceManager &Sources, const CharSourceRange &Range,
llvm::StringRef ReplacementText);
std::string FilePath;
unsigned Offset;
unsigned Length;
std::string ReplacementText;
};
/// \brief A set of Replacements.
/// FIXME: Change to a vector and deduplicate in the RefactoringTool.
typedef std::set<Replacement, Replacement::Less> Replacements;
/// \brief Apply all replacements on the Rewriter.
///
/// If at least one Apply returns false, ApplyAll returns false. Every
/// Apply will be executed independently of the result of other
/// Apply operations.
bool applyAllReplacements(Replacements &Replaces, Rewriter &Rewrite);
/// \brief A tool to run refactorings.
///
/// This is a refactoring specific version of \see ClangTool.
/// All text replacements added to getReplacements() during the run of the
/// tool will be applied and saved after all translation units have been
/// processed.
class RefactoringTool {
public:
/// \see ClangTool::ClangTool.
RefactoringTool(const CompilationDatabase &Compilations,
ArrayRef<std::string> SourcePaths);
/// \brief Returns a set of replacements. All replacements added during the
/// run of the tool will be applied after all translation units have been
/// processed.
Replacements &getReplacements();
/// \see ClangTool::run.
int run(FrontendActionFactory *ActionFactory);
private:
ClangTool Tool;
Replacements Replace;
};
template <typename Node>
Replacement::Replacement(SourceManager &Sources, const Node &NodeToReplace,
llvm::StringRef ReplacementText) {
const CharSourceRange Range =
CharSourceRange::getTokenRange(NodeToReplace->getSourceRange());
setFromSourceRange(Sources, Range, ReplacementText);
}
} // end namespace tooling
} // end namespace clang

View File

@ -1,8 +1,9 @@
set(LLVM_LINK_COMPONENTS support)
SET(LLVM_USED_LIBS clangBasic clangFrontend clangAST)
SET(LLVM_USED_LIBS clangBasic clangFrontend clangAST clangRewrite)
add_clang_library(clangTooling
CompilationDatabase.cpp
Refactoring.cpp
Tooling.cpp
ArgumentsAdjusters.cpp
)

View File

@ -0,0 +1,178 @@
//===--- 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.
//
//===----------------------------------------------------------------------===//
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Lex/Lexer.h"
#include "clang/Rewrite/Rewriter.h"
#include "clang/Tooling/Refactoring.h"
#include "llvm/Support/raw_os_ostream.h"
namespace clang {
namespace tooling {
static const char * const InvalidLocation = "";
Replacement::Replacement()
: FilePath(InvalidLocation), Offset(0), Length(0) {}
Replacement::Replacement(llvm::StringRef FilePath, unsigned Offset,
unsigned Length, llvm::StringRef ReplacementText)
: FilePath(FilePath), Offset(Offset),
Length(Length), ReplacementText(ReplacementText) {}
Replacement::Replacement(SourceManager &Sources, SourceLocation Start,
unsigned Length, llvm::StringRef ReplacementText) {
setFromSourceLocation(Sources, Start, Length, ReplacementText);
}
Replacement::Replacement(SourceManager &Sources, const CharSourceRange &Range,
llvm::StringRef ReplacementText) {
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).
getLocWithOffset(Offset);
// 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.
bool RewriteSucceeded = !Rewrite.ReplaceText(Start, Length, ReplacementText);
assert(RewriteSucceeded);
return RewriteSucceeded;
}
bool Replacement::Less::operator()(const Replacement &R1,
const Replacement &R2) const {
if (R1.FilePath != R2.FilePath) return R1.FilePath < R2.FilePath;
if (R1.Offset != R2.Offset) return R1.Offset < R2.Offset;
if (R1.Length != R2.Length) return R1.Length < R2.Length;
return R1.ReplacementText < R2.ReplacementText;
}
void Replacement::setFromSourceLocation(SourceManager &Sources,
SourceLocation Start, unsigned Length,
llvm::StringRef ReplacementText) {
const std::pair<FileID, unsigned> DecomposedLocation =
Sources.getDecomposedLoc(Start);
const FileEntry *Entry = Sources.getFileEntryForID(DecomposedLocation.first);
this->FilePath = Entry != NULL ? Entry->getName() : InvalidLocation;
this->Offset = DecomposedLocation.second;
this->Length = Length;
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,
llvm::StringRef ReplacementText) {
setFromSourceLocation(Sources, Sources.getSpellingLoc(Range.getBegin()),
getRangeSize(Sources, Range), ReplacementText);
}
bool applyAllReplacements(Replacements &Replaces, Rewriter &Rewrite) {
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;
}
bool 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;
llvm::raw_fd_ostream FileStream(
Entry->getName(), ErrorInfo, llvm::raw_fd_ostream::F_Binary);
if (!ErrorInfo.empty())
return false;
I->second.write(FileStream);
FileStream.flush();
}
return true;
}
RefactoringTool::RefactoringTool(const CompilationDatabase &Compilations,
ArrayRef<std::string> SourcePaths)
: Tool(Compilations, SourcePaths) {}
Replacements &RefactoringTool::getReplacements() { return Replace; }
int RefactoringTool::run(FrontendActionFactory *ActionFactory) {
int Result = Tool.run(ActionFactory);
LangOptions DefaultLangOptions;
DiagnosticOptions DefaultDiagnosticOptions;
TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(),
DefaultDiagnosticOptions);
DiagnosticsEngine Diagnostics(
llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
&DiagnosticPrinter, false);
SourceManager Sources(Diagnostics, Tool.getFiles());
Rewriter Rewrite(Sources, DefaultLangOptions);
if (!applyAllReplacements(Replace, Rewrite)) {
llvm::errs() << "Skipped some replacements.\n";
}
if (!saveRewrittenFiles(Rewrite)) {
llvm::errs() << "Could not save rewritten files.\n";
return 1;
}
return Result;
}
} // end namespace tooling
} // end namespace clang

View File

@ -70,6 +70,7 @@ add_clang_unittest(Tooling
Tooling/CompilationDatabaseTest.cpp
Tooling/ToolingTest.cpp
Tooling/RecursiveASTVisitorTest.cpp
Tooling/RefactoringTest.cpp
Tooling/RewriterTest.cpp
USED_LIBS gtest gtest_main clangAST clangTooling clangRewrite
)

View File

@ -0,0 +1,304 @@
//===- unittest/Tooling/RefactoringTest.cpp - Refactoring unit tests ------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "RewriterTestContext.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclGroup.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/DiagnosticOptions.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Rewrite/Rewriter.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Path.h"
#include "gtest/gtest.h"
namespace clang {
namespace tooling {
class ReplacementTest : public ::testing::Test {
protected:
Replacement createReplacement(SourceLocation Start, unsigned Length,
llvm::StringRef ReplacementText) {
return Replacement(Context.Sources, Start, Length, ReplacementText);
}
RewriterTestContext Context;
};
TEST_F(ReplacementTest, CanDeleteAllText) {
FileID ID = Context.createInMemoryFile("input.cpp", "text");
SourceLocation Location = Context.getLocation(ID, 1, 1);
Replacement Replace(createReplacement(Location, 4, ""));
EXPECT_TRUE(Replace.apply(Context.Rewrite));
EXPECT_EQ("", Context.getRewrittenText(ID));
}
TEST_F(ReplacementTest, CanDeleteAllTextInTextWithNewlines) {
FileID ID = Context.createInMemoryFile("input.cpp", "line1\nline2\nline3");
SourceLocation Location = Context.getLocation(ID, 1, 1);
Replacement Replace(createReplacement(Location, 17, ""));
EXPECT_TRUE(Replace.apply(Context.Rewrite));
EXPECT_EQ("", Context.getRewrittenText(ID));
}
TEST_F(ReplacementTest, CanAddText) {
FileID ID = Context.createInMemoryFile("input.cpp", "");
SourceLocation Location = Context.getLocation(ID, 1, 1);
Replacement Replace(createReplacement(Location, 0, "result"));
EXPECT_TRUE(Replace.apply(Context.Rewrite));
EXPECT_EQ("result", Context.getRewrittenText(ID));
}
TEST_F(ReplacementTest, CanReplaceTextAtPosition) {
FileID ID = Context.createInMemoryFile("input.cpp",
"line1\nline2\nline3\nline4");
SourceLocation Location = Context.getLocation(ID, 2, 3);
Replacement Replace(createReplacement(Location, 12, "x"));
EXPECT_TRUE(Replace.apply(Context.Rewrite));
EXPECT_EQ("line1\nlixne4", Context.getRewrittenText(ID));
}
TEST_F(ReplacementTest, CanReplaceTextAtPositionMultipleTimes) {
FileID ID = Context.createInMemoryFile("input.cpp",
"line1\nline2\nline3\nline4");
SourceLocation Location1 = Context.getLocation(ID, 2, 3);
Replacement Replace1(createReplacement(Location1, 12, "x\ny\n"));
EXPECT_TRUE(Replace1.apply(Context.Rewrite));
EXPECT_EQ("line1\nlix\ny\nne4", Context.getRewrittenText(ID));
// Since the original source has not been modified, the (4, 4) points to the
// 'e' in the original content.
SourceLocation Location2 = Context.getLocation(ID, 4, 4);
Replacement Replace2(createReplacement(Location2, 1, "f"));
EXPECT_TRUE(Replace2.apply(Context.Rewrite));
EXPECT_EQ("line1\nlix\ny\nnf4", Context.getRewrittenText(ID));
}
TEST_F(ReplacementTest, ApplyFailsForNonExistentLocation) {
Replacement Replace("nonexistent-file.cpp", 0, 1, "");
EXPECT_FALSE(Replace.apply(Context.Rewrite));
}
TEST_F(ReplacementTest, CanRetrivePath) {
Replacement Replace("/path/to/file.cpp", 0, 1, "");
EXPECT_EQ("/path/to/file.cpp", Replace.getFilePath());
}
TEST_F(ReplacementTest, ReturnsInvalidPath) {
Replacement Replace1(Context.Sources, SourceLocation(), 0, "");
EXPECT_TRUE(Replace1.getFilePath().empty());
Replacement Replace2;
EXPECT_TRUE(Replace2.getFilePath().empty());
}
TEST_F(ReplacementTest, CanApplyReplacements) {
FileID ID = Context.createInMemoryFile("input.cpp",
"line1\nline2\nline3\nline4");
Replacements Replaces;
Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 2, 1),
5, "replaced"));
Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 3, 1),
5, "other"));
EXPECT_TRUE(applyAllReplacements(Replaces, Context.Rewrite));
EXPECT_EQ("line1\nreplaced\nother\nline4", Context.getRewrittenText(ID));
}
TEST_F(ReplacementTest, SkipsDuplicateReplacements) {
FileID ID = Context.createInMemoryFile("input.cpp",
"line1\nline2\nline3\nline4");
Replacements Replaces;
Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 2, 1),
5, "replaced"));
Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 2, 1),
5, "replaced"));
Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 2, 1),
5, "replaced"));
EXPECT_TRUE(applyAllReplacements(Replaces, Context.Rewrite));
EXPECT_EQ("line1\nreplaced\nline3\nline4", Context.getRewrittenText(ID));
}
TEST_F(ReplacementTest, ApplyAllFailsIfOneApplyFails) {
// This test depends on the value of the file name of an invalid source
// location being in the range ]a, z[.
FileID IDa = Context.createInMemoryFile("a.cpp", "text");
FileID IDz = Context.createInMemoryFile("z.cpp", "text");
Replacements Replaces;
Replaces.insert(Replacement(Context.Sources, Context.getLocation(IDa, 1, 1),
4, "a"));
Replaces.insert(Replacement(Context.Sources, SourceLocation(),
5, "2"));
Replaces.insert(Replacement(Context.Sources, Context.getLocation(IDz, 1, 1),
4, "z"));
EXPECT_FALSE(applyAllReplacements(Replaces, Context.Rewrite));
EXPECT_EQ("a", Context.getRewrittenText(IDa));
EXPECT_EQ("z", Context.getRewrittenText(IDz));
}
class FlushRewrittenFilesTest : public ::testing::Test {
public:
FlushRewrittenFilesTest() {
std::string ErrorInfo;
TemporaryDirectory = llvm::sys::Path::GetTemporaryDirectory(&ErrorInfo);
assert(ErrorInfo.empty());
}
~FlushRewrittenFilesTest() {
std::string ErrorInfo;
TemporaryDirectory.eraseFromDisk(true, &ErrorInfo);
assert(ErrorInfo.empty());
}
FileID createFile(llvm::StringRef Name, llvm::StringRef Content) {
llvm::SmallString<1024> Path(TemporaryDirectory.str());
llvm::sys::path::append(Path, Name);
std::string ErrorInfo;
llvm::raw_fd_ostream OutStream(Path.c_str(),
ErrorInfo, llvm::raw_fd_ostream::F_Binary);
assert(ErrorInfo.empty());
OutStream << Content;
OutStream.close();
const FileEntry *File = Context.Files.getFile(Path);
assert(File != NULL);
return Context.Sources.createFileID(File, SourceLocation(), SrcMgr::C_User);
}
std::string getFileContentFromDisk(llvm::StringRef Name) {
llvm::SmallString<1024> Path(TemporaryDirectory.str());
llvm::sys::path::append(Path, Name);
// We need to read directly from the FileManager without relaying through
// a FileEntry, as otherwise we'd read through an already opened file
// descriptor, which might not see the changes made.
// FIXME: Figure out whether there is a way to get the SourceManger to
// reopen the file.
return Context.Files.getBufferForFile(Path, NULL)->getBuffer();
}
llvm::sys::Path TemporaryDirectory;
RewriterTestContext Context;
};
TEST_F(FlushRewrittenFilesTest, StoresChangesOnDisk) {
FileID ID = createFile("input.cpp", "line1\nline2\nline3\nline4");
Replacements Replaces;
Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 2, 1),
5, "replaced"));
EXPECT_TRUE(applyAllReplacements(Replaces, Context.Rewrite));
EXPECT_FALSE(Context.Rewrite.overwriteChangedFiles());
EXPECT_EQ("line1\nreplaced\nline3\nline4",
getFileContentFromDisk("input.cpp"));
}
namespace {
template <typename T>
class TestVisitor : public clang::RecursiveASTVisitor<T> {
public:
bool runOver(StringRef Code) {
return runToolOnCode(new TestAction(this), Code);
}
protected:
clang::SourceManager *SM;
private:
class FindConsumer : public clang::ASTConsumer {
public:
FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
virtual void HandleTranslationUnit(clang::ASTContext &Context) {
Visitor->TraverseDecl(Context.getTranslationUnitDecl());
}
private:
TestVisitor *Visitor;
};
class TestAction : public clang::ASTFrontendAction {
public:
TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
virtual clang::ASTConsumer* CreateASTConsumer(
clang::CompilerInstance& compiler, llvm::StringRef dummy) {
Visitor->SM = &compiler.getSourceManager();
/// TestConsumer will be deleted by the framework calling us.
return new FindConsumer(Visitor);
}
private:
TestVisitor *Visitor;
};
};
} // end namespace
void expectReplacementAt(const Replacement &Replace,
StringRef File, unsigned Offset, unsigned Length) {
ASSERT_TRUE(Replace.isApplicable());
EXPECT_EQ(File, Replace.getFilePath());
EXPECT_EQ(Offset, Replace.getOffset());
EXPECT_EQ(Length, Replace.getLength());
}
class ClassDeclXVisitor : public TestVisitor<ClassDeclXVisitor> {
public:
bool VisitCXXRecordDecl(CXXRecordDecl *Record) {
if (Record->getName() == "X") {
Replace = Replacement(*SM, Record, "");
}
return true;
}
Replacement Replace;
};
TEST(Replacement, CanBeConstructedFromNode) {
ClassDeclXVisitor ClassDeclX;
EXPECT_TRUE(ClassDeclX.runOver(" class X;"));
expectReplacementAt(ClassDeclX.Replace, "input.cc", 5, 7);
}
TEST(Replacement, ReplacesAtSpellingLocation) {
ClassDeclXVisitor ClassDeclX;
EXPECT_TRUE(ClassDeclX.runOver("#define A(Y) Y\nA(class X);"));
expectReplacementAt(ClassDeclX.Replace, "input.cc", 17, 7);
}
class CallToFVisitor : public TestVisitor<CallToFVisitor> {
public:
bool VisitCallExpr(CallExpr *Call) {
if (Call->getDirectCallee()->getName() == "F") {
Replace = Replacement(*SM, Call, "");
}
return true;
}
Replacement Replace;
};
TEST(Replacement, FunctionCall) {
CallToFVisitor CallToF;
EXPECT_TRUE(CallToF.runOver("void F(); void G() { F(); }"));
expectReplacementAt(CallToF.Replace, "input.cc", 21, 3);
}
TEST(Replacement, TemplatedFunctionCall) {
CallToFVisitor CallToF;
EXPECT_TRUE(CallToF.runOver(
"template <typename T> void F(); void G() { F<int>(); }"));
expectReplacementAt(CallToF.Replace, "input.cc", 43, 8);
}
} // end namespace tooling
} // end namespace clang