Skip to content

Commit

Permalink
Changes related to tooling::applyAllReplacements interface change in …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
Eric Liu committed Jul 11, 2016
1 parent 4f8d994 commit a452db4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 27 deletions.
8 changes: 5 additions & 3 deletions clang-tools-extra/include-fixer/IncludeFixer.cpp
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions clang-tools-extra/include-fixer/IncludeFixer.h
Expand Up @@ -68,8 +68,9 @@ class IncludeFixerActionFactory : public clang::tooling::ToolAction {
/// \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());

Expand Down
48 changes: 32 additions & 16 deletions clang-tools-extra/include-fixer/tool/ClangIncludeFixer.cpp
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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();
}

Expand Down
9 changes: 8 additions & 1 deletion clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h
Expand Up @@ -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>

Expand Down Expand Up @@ -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) \
Expand Down
13 changes: 8 additions & 5 deletions clang-tools-extra/unittests/include-fixer/IncludeFixerTest.cpp
Expand Up @@ -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);
}

Expand Down

0 comments on commit a452db4

Please sign in to comment.