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

View File

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

View File

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

View File

@ -17,6 +17,7 @@
#include "clang/Frontend/FrontendActions.h" #include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/Refactoring.h" #include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/Tooling.h" #include "clang/Tooling/Tooling.h"
#include "llvm/ADT/Optional.h"
#include <map> #include <map>
#include <memory> #include <memory>
@ -121,7 +122,13 @@ runCheckOnCode(StringRef Code, std::vector<ClangTidyError> *Errors = nullptr,
Fixes.insert(Error.Fix.begin(), Error.Fix.end()); Fixes.insert(Error.Fix.begin(), Error.Fix.end());
if (Errors) if (Errors)
*Errors = Context.getErrors(); *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) \ #define EXPECT_NO_CHANGES(Check, Code) \

View File

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