Changes related to tooling::applyAllReplacements interface change in D21601.

Summary:
this patch contains changes related to the interface change from
http://reviews.llvm.org/D21601. Only submit this patch after D21601 is
submitted.

Reviewers: djasper, klimek

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D21602

llvm-svn: 275063
This commit is contained in:
Eric Liu 2016-07-11 13:53:21 +00:00
parent 4f8d99433d
commit a452db4d0f
5 changed files with 56 additions and 27 deletions

View File

@ -349,7 +349,7 @@ bool IncludeFixerActionFactory::runInvocation(
return !Compiler.getDiagnostics().hasFatalErrorOccurred();
}
tooling::Replacements
llvm::Expected<tooling::Replacements>
createInsertHeaderReplacements(StringRef Code, StringRef FilePath,
StringRef Header,
const clang::format::FormatStyle &Style) {
@ -360,8 +360,10 @@ createInsertHeaderReplacements(StringRef Code, StringRef FilePath,
clang::tooling::Replacements Insertions = {
tooling::Replacement(FilePath, UINT_MAX, 0, IncludeName)};
return formatReplacements(
Code, cleanupAroundReplacements(Code, Insertions, Style), Style);
auto CleanReplaces = cleanupAroundReplacements(Code, Insertions, Style);
if (!CleanReplaces)
return CleanReplaces;
return formatReplacements(Code, *CleanReplaces, Style);
}
} // namespace include_fixer

View File

@ -68,8 +68,9 @@ private:
/// \param Header The header being inserted.
/// \param Style clang-format style being used.
///
/// \return Replacements for inserting and sorting headers.
tooling::Replacements createInsertHeaderReplacements(
/// \return Replacements for inserting and sorting headers on success;
/// otherwise, an llvm::Error carrying llvm::StringError is returned.
llvm::Expected<tooling::Replacements> createInsertHeaderReplacements(
StringRef Code, StringRef FilePath, StringRef Header,
const clang::format::FormatStyle &Style = clang::format::getLLVMStyle());

View File

@ -209,13 +209,20 @@ int includeFixerMain(int argc, const char **argv) {
return 1;
}
tooling::Replacements Replacements =
clang::include_fixer::createInsertHeaderReplacements(
Code->getBuffer(), FilePath, Context.getHeaders().front(),
InsertStyle);
std::string ChangedCode =
tooling::applyAllReplacements(Code->getBuffer(), Replacements);
llvm::outs() << ChangedCode;
auto Replacements = clang::include_fixer::createInsertHeaderReplacements(
Code->getBuffer(), FilePath, Context.getHeaders().front(), InsertStyle);
if (!Replacements) {
errs() << "Failed to create header insertion replacement: "
<< llvm::toString(Replacements.takeError()) << "\n";
return 1;
}
auto ChangedCode =
tooling::applyAllReplacements(Code->getBuffer(), *Replacements);
if (!ChangedCode) {
llvm::errs() << llvm::toString(ChangedCode.takeError()) << "\n";
return 1;
}
llvm::outs() << *ChangedCode;
return 0;
}
@ -250,17 +257,22 @@ int includeFixerMain(int argc, const char **argv) {
return 1;
}
tooling::Replacements Replacements =
clang::include_fixer::createInsertHeaderReplacements(
/*Code=*/Buffer.get()->getBuffer(), FilePath,
Context.getHeaders().front(), InsertStyle);
// FIXME: Rank the results and pick the best one instead of the first one.
auto Replacements = clang::include_fixer::createInsertHeaderReplacements(
/*Code=*/Buffer.get()->getBuffer(), FilePath,
Context.getHeaders().front(), InsertStyle);
if (!Replacements) {
errs() << "Failed to create header insertion replacement: "
<< llvm::toString(Replacements.takeError()) << "\n";
return 1;
}
if (!Quiet)
llvm::errs() << "Added #include" << Context.getHeaders().front();
// Add missing namespace qualifiers to the unidentified symbol.
if (Context.getSymbolRange().getLength() > 0)
Replacements.insert(Context.createSymbolReplacement(FilePath, 0));
Replacements->insert(Context.createSymbolReplacement(FilePath, 0));
// Set up a new source manager for applying the resulting replacements.
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
@ -270,15 +282,19 @@ int includeFixerMain(int argc, const char **argv) {
Diagnostics.setClient(&DiagnosticPrinter, false);
if (STDINMode) {
std::string ChangedCode =
tooling::applyAllReplacements(Code->getBuffer(), Replacements);
llvm::outs() << ChangedCode;
auto ChangedCode =
tooling::applyAllReplacements(Code->getBuffer(), *Replacements);
if (!ChangedCode) {
llvm::errs() << llvm::toString(ChangedCode.takeError()) << "\n";
return 1;
}
llvm::outs() << *ChangedCode;
return 0;
}
// Write replacements to disk.
Rewriter Rewrites(SM, LangOptions());
tooling::applyAllReplacements(Replacements, Rewrites);
tooling::applyAllReplacements(*Replacements, Rewrites);
return Rewrites.overwriteChangedFiles();
}

View File

@ -17,6 +17,7 @@
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/Optional.h"
#include <map>
#include <memory>
@ -121,7 +122,13 @@ runCheckOnCode(StringRef Code, std::vector<ClangTidyError> *Errors = nullptr,
Fixes.insert(Error.Fix.begin(), Error.Fix.end());
if (Errors)
*Errors = Context.getErrors();
return tooling::applyAllReplacements(Code, Fixes);
auto Result = tooling::applyAllReplacements(Code, Fixes);
if (!Result) {
// FIXME: propogate the error.
llvm::consumeError(Result.takeError());
return "";
}
return *Result;
}
#define EXPECT_NO_CHANGES(Check, Code) \

View File

@ -86,14 +86,17 @@ static std::string runIncludeFixer(
runOnCode(&Factory, Code, FakeFileName, ExtraArgs);
if (FixerContext.getMatchedSymbols().empty())
return Code;
tooling::Replacements Replacements =
clang::include_fixer::createInsertHeaderReplacements(
Code, FakeFileName, FixerContext.getHeaders().front());
auto Replaces = clang::include_fixer::createInsertHeaderReplacements(
Code, FakeFileName, FixerContext.getHeaders().front());
EXPECT_TRUE(static_cast<bool>(Replaces))
<< llvm::toString(Replaces.takeError()) << "\n";
if (!Replaces)
return "";
clang::RewriterTestContext Context;
clang::FileID ID = Context.createInMemoryFile(FakeFileName, Code);
if (FixerContext.getSymbolRange().getLength() > 0)
Replacements.insert(FixerContext.createSymbolReplacement(FakeFileName, 0));
clang::tooling::applyAllReplacements(Replacements, Context.Rewrite);
Replaces->insert(FixerContext.createSymbolReplacement(FakeFileName, 0));
clang::tooling::applyAllReplacements(*Replaces, Context.Rewrite);
return Context.getRewrittenText(ID);
}