Skip to content

Commit

Permalink
Added applyAtomicChanges function.
Browse files Browse the repository at this point in the history
This re-commits r298913.
o See thread http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20170327/189084.html
o Tested with -DLLVM_ENABLE_LOCAL_SUBMODULE_VISIBILITY=0.

Summary: ... which applies a set of `AtomicChange`s on code.

Reviewers: klimek, djasper

Reviewed By: djasper

Subscribers: arphaman, mgorny, chapuni, cfe-commits

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

llvm-svn: 309548
  • Loading branch information
Eric Liu committed Jul 31, 2017
1 parent 4284049 commit 7ef3a19
Show file tree
Hide file tree
Showing 4 changed files with 637 additions and 4 deletions.
34 changes: 34 additions & 0 deletions clang/include/clang/Tooling/Refactoring/AtomicChange.h
Expand Up @@ -16,6 +16,7 @@
#define LLVM_CLANG_TOOLING_REFACTOR_ATOMICCHANGE_H

#include "clang/Basic/SourceManager.h"
#include "clang/Format/Format.h"
#include "clang/Tooling/Core/Replacement.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
Expand Down Expand Up @@ -129,6 +130,39 @@ class AtomicChange {
tooling::Replacements Replaces;
};

// Defines specs for applying changes.
struct ApplyChangesSpec {
// If true, cleans up redundant/erroneous code around changed code with
// clang-format's cleanup functionality, e.g. redundant commas around deleted
// parameter or empty namespaces introduced by deletions.
bool Cleanup = true;

format::FormatStyle Style = format::getNoStyle();

// Options for selectively formatting changes with clang-format:
// kAll: Format all changed lines.
// kNone: Don't format anything.
// kViolations: Format lines exceeding the `ColumnLimit` in `Style`.
enum FormatOption { kAll, kNone, kViolations };

FormatOption Format = kNone;
};

/// \brief Applies all AtomicChanges in \p Changes to the \p Code.
///
/// This completely ignores the file path in each change and replaces them with
/// \p FilePath, i.e. callers are responsible for ensuring all changes are for
/// the same file.
///
/// \returns The changed code if all changes are applied successfully;
/// otherwise, an llvm::Error carrying llvm::StringError is returned (the Error
/// message can be converted to string with `llvm::toString()` and the
/// error_code should be ignored).
llvm::Expected<std::string>
applyAtomicChanges(llvm::StringRef FilePath, llvm::StringRef Code,
llvm::ArrayRef<AtomicChange> Changes,
const ApplyChangesSpec &Spec);

} // end namespace tooling
} // end namespace clang

Expand Down
179 changes: 179 additions & 0 deletions clang/lib/Tooling/Refactoring/AtomicChange.cpp
Expand Up @@ -83,6 +83,116 @@ template <> struct MappingTraits<clang::tooling::AtomicChange> {

namespace clang {
namespace tooling {
namespace {

// Returns true if there is any line that violates \p ColumnLimit in range
// [Start, End].
bool violatesColumnLimit(llvm::StringRef Code, unsigned ColumnLimit,
unsigned Start, unsigned End) {
auto StartPos = Code.rfind('\n', Start);
StartPos = (StartPos == llvm::StringRef::npos) ? 0 : StartPos + 1;

auto EndPos = Code.find("\n", End);
if (EndPos == llvm::StringRef::npos)
EndPos = Code.size();

llvm::SmallVector<llvm::StringRef, 8> Lines;
Code.substr(StartPos, EndPos - StartPos).split(Lines, '\n');
for (llvm::StringRef Line : Lines)
if (Line.size() > ColumnLimit)
return true;
return false;
}

std::vector<Range>
getRangesForFormating(llvm::StringRef Code, unsigned ColumnLimit,
ApplyChangesSpec::FormatOption Format,
const clang::tooling::Replacements &Replaces) {
// kNone suppresses formatting entirely.
if (Format == ApplyChangesSpec::kNone)
return {};
std::vector<clang::tooling::Range> Ranges;
// This works assuming that replacements are ordered by offset.
// FIXME: use `getAffectedRanges()` to calculate when it does not include '\n'
// at the end of an insertion in affected ranges.
int Offset = 0;
for (const clang::tooling::Replacement &R : Replaces) {
int Start = R.getOffset() + Offset;
int End = Start + R.getReplacementText().size();
if (!R.getReplacementText().empty() &&
R.getReplacementText().back() == '\n' && R.getLength() == 0 &&
R.getOffset() > 0 && R.getOffset() <= Code.size() &&
Code[R.getOffset() - 1] == '\n')
// If we are inserting at the start of a line and the replacement ends in
// a newline, we don't need to format the subsequent line.
--End;
Offset += R.getReplacementText().size() - R.getLength();

if (Format == ApplyChangesSpec::kAll ||
violatesColumnLimit(Code, ColumnLimit, Start, End))
Ranges.emplace_back(Start, End - Start);
}
return Ranges;
}

inline llvm::Error make_string_error(const llvm::Twine &Message) {
return llvm::make_error<llvm::StringError>(Message,
llvm::inconvertibleErrorCode());
}

// Creates replacements for inserting/deleting #include headers.
llvm::Expected<Replacements>
createReplacementsForHeaders(llvm::StringRef FilePath, llvm::StringRef Code,
llvm::ArrayRef<AtomicChange> Changes,
const format::FormatStyle &Style) {
// Create header insertion/deletion replacements to be cleaned up
// (i.e. converted to real insertion/deletion replacements).
Replacements HeaderReplacements;
for (const auto &Change : Changes) {
for (llvm::StringRef Header : Change.getInsertedHeaders()) {
std::string EscapedHeader =
Header.startswith("<") || Header.startswith("\"")
? Header.str()
: ("\"" + Header + "\"").str();
std::string ReplacementText = "#include " + EscapedHeader;
// Offset UINT_MAX and length 0 indicate that the replacement is a header
// insertion.
llvm::Error Err = HeaderReplacements.add(
tooling::Replacement(FilePath, UINT_MAX, 0, ReplacementText));
if (Err)
return std::move(Err);
}
for (const std::string &Header : Change.getRemovedHeaders()) {
// Offset UINT_MAX and length 1 indicate that the replacement is a header
// deletion.
llvm::Error Err =
HeaderReplacements.add(Replacement(FilePath, UINT_MAX, 1, Header));
if (Err)
return std::move(Err);
}
}

// cleanupAroundReplacements() converts header insertions/deletions into
// actual replacements that add/remove headers at the right location.
return clang::format::cleanupAroundReplacements(Code, HeaderReplacements,
Style);
}

// Combine replacements in all Changes as a `Replacements`. This ignores the
// file path in all replacements and replaces them with \p FilePath.
llvm::Expected<Replacements>
combineReplacementsInChanges(llvm::StringRef FilePath,
llvm::ArrayRef<AtomicChange> Changes) {
Replacements Replaces;
for (const auto &Change : Changes)
for (const auto &R : Change.getReplacements())
if (auto Err = Replaces.add(Replacement(
FilePath, R.getOffset(), R.getLength(), R.getReplacementText())))
return std::move(Err);
return Replaces;
}

} // end namespace

AtomicChange::AtomicChange(const SourceManager &SM,
SourceLocation KeyPosition) {
Expand Down Expand Up @@ -173,5 +283,74 @@ void AtomicChange::removeHeader(llvm::StringRef Header) {
RemovedHeaders.push_back(Header);
}

llvm::Expected<std::string>
applyAtomicChanges(llvm::StringRef FilePath, llvm::StringRef Code,
llvm::ArrayRef<AtomicChange> Changes,
const ApplyChangesSpec &Spec) {
llvm::Expected<Replacements> HeaderReplacements =
createReplacementsForHeaders(FilePath, Code, Changes, Spec.Style);
if (!HeaderReplacements)
return make_string_error(
"Failed to create replacements for header changes: " +
llvm::toString(HeaderReplacements.takeError()));

llvm::Expected<Replacements> Replaces =
combineReplacementsInChanges(FilePath, Changes);
if (!Replaces)
return make_string_error("Failed to combine replacements in all changes: " +
llvm::toString(Replaces.takeError()));

Replacements AllReplaces = std::move(*Replaces);
for (const auto &R : *HeaderReplacements) {
llvm::Error Err = AllReplaces.add(R);
if (Err)
return make_string_error(
"Failed to combine existing replacements with header replacements: " +
llvm::toString(std::move(Err)));
}

if (Spec.Cleanup) {
llvm::Expected<Replacements> CleanReplaces =
format::cleanupAroundReplacements(Code, AllReplaces, Spec.Style);
if (!CleanReplaces)
return make_string_error("Failed to cleanup around replacements: " +
llvm::toString(CleanReplaces.takeError()));
AllReplaces = std::move(*CleanReplaces);
}

// Apply all replacements.
llvm::Expected<std::string> ChangedCode =
applyAllReplacements(Code, AllReplaces);
if (!ChangedCode)
return make_string_error("Failed to apply all replacements: " +
llvm::toString(ChangedCode.takeError()));

// Sort inserted headers. This is done even if other formatting is turned off
// as incorrectly sorted headers are always just wrong, it's not a matter of
// taste.
Replacements HeaderSortingReplacements = format::sortIncludes(
Spec.Style, *ChangedCode, AllReplaces.getAffectedRanges(), FilePath);
ChangedCode = applyAllReplacements(*ChangedCode, HeaderSortingReplacements);
if (!ChangedCode)
return make_string_error(
"Failed to apply replacements for sorting includes: " +
llvm::toString(ChangedCode.takeError()));

AllReplaces = AllReplaces.merge(HeaderSortingReplacements);

std::vector<Range> FormatRanges = getRangesForFormating(
*ChangedCode, Spec.Style.ColumnLimit, Spec.Format, AllReplaces);
if (!FormatRanges.empty()) {
Replacements FormatReplacements =
format::reformat(Spec.Style, *ChangedCode, FormatRanges, FilePath);
ChangedCode = applyAllReplacements(*ChangedCode, FormatReplacements);
if (!ChangedCode)
return make_string_error(
"Failed to apply replacements for formatting changed code: " +
llvm::toString(ChangedCode.takeError()));
}
return ChangedCode;
}

} // end namespace tooling
} // end namespace clang
6 changes: 2 additions & 4 deletions clang/lib/Tooling/Refactoring/CMakeLists.txt
@@ -1,7 +1,4 @@
set(LLVM_LINK_COMPONENTS
Option
Support
)
set(LLVM_LINK_COMPONENTS Support)

add_clang_library(clangToolingRefactor
AtomicChange.cpp
Expand All @@ -14,6 +11,7 @@ add_clang_library(clangToolingRefactor
clangAST
clangASTMatchers
clangBasic
clangFormat
clangIndex
clangLex
clangToolingCore
Expand Down

0 comments on commit 7ef3a19

Please sign in to comment.