void expectRewritten(const std::string &Code,
                     const std::string &Expected,
                     const T &AMatcher,
                     RefactoringCallback &Callback) {
  MatchFinder Finder;
  Finder.addMatcher(AMatcher, &Callback);
  std::unique_ptr<tooling::FrontendActionFactory> Factory(
      tooling::newFrontendActionFactory(&Finder));
  ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), Code))
      << "Parsing error in \"" << Code << "\"";
  RewriterTestContext Context;
  FileID ID = Context.createInMemoryFile("input.cc", Code);
  EXPECT_TRUE(tooling::applyAllReplacements(Callback.getReplacements(),
                                            Context.Rewrite));
  EXPECT_EQ(Expected, Context.getRewrittenText(ID));
}
Exemple #2
0
 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
                    const FormatStyle &Style) {
   RewriterTestContext Context;
   FileID ID = Context.createInMemoryFile("input.cc", Code);
   SourceLocation Start =
       Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset);
   std::vector<CharSourceRange> Ranges(
       1,
       CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length)));
   LangOptions LangOpts;
   LangOpts.CPlusPlus = 1;
   Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts);
   tooling::Replacements Replace =
       reformat(Style, Lex, Context.Sources, Ranges);
   EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
   return Context.getRewrittenText(ID);
 }