Files
clang-p2996/clang-tools-extra/cpp11-migrate/LoopConvert/LoopConvert.cpp
Edwin Vane a6bbcdd707 cpp11-migrate: New mechanism for overriding file contents
Next step toward supporting migrating headers. Instead of using ClangTool's
ability to override all files at once, use a custom FrontendAction and override
only the source (and eventually headers) the action is about to parse.

Use of newFrontendActionFactory() is replaced with a new factory maker provided
by Transform.

llvm-svn: 183855
2013-06-12 19:52:13 +00:00

78 lines
3.0 KiB
C++

//===-- LoopConvert/LoopConvert.cpp - C++11 for-loop migration --*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file provides the implementation of the LoopConvertTransform
/// class.
///
//===----------------------------------------------------------------------===//
#include "LoopConvert.h"
#include "LoopActions.h"
#include "LoopMatchers.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/Tooling.h"
using clang::ast_matchers::MatchFinder;
using namespace clang::tooling;
using namespace clang;
int LoopConvertTransform::apply(const FileContentsByPath &InputStates,
const CompilationDatabase &Database,
const std::vector<std::string> &SourcePaths,
FileContentsByPath &ResultStates) {
RefactoringTool LoopTool(Database, SourcePaths);
StmtAncestorASTVisitor ParentFinder;
StmtGeneratedVarNameMap GeneratedDecls;
ReplacedVarsMap ReplacedVars;
unsigned AcceptedChanges = 0;
unsigned DeferredChanges = 0;
unsigned RejectedChanges = 0;
MatchFinder Finder;
LoopFixer ArrayLoopFixer(&ParentFinder, &LoopTool.getReplacements(),
&GeneratedDecls, &ReplacedVars, &AcceptedChanges,
&DeferredChanges, &RejectedChanges,
Options().MaxRiskLevel, LFK_Array);
Finder.addMatcher(makeArrayLoopMatcher(), &ArrayLoopFixer);
LoopFixer IteratorLoopFixer(&ParentFinder, &LoopTool.getReplacements(),
&GeneratedDecls, &ReplacedVars,
&AcceptedChanges, &DeferredChanges,
&RejectedChanges,
Options().MaxRiskLevel, LFK_Iterator);
Finder.addMatcher(makeIteratorLoopMatcher(), &IteratorLoopFixer);
LoopFixer PseudoarrrayLoopFixer(&ParentFinder, &LoopTool.getReplacements(),
&GeneratedDecls, &ReplacedVars,
&AcceptedChanges, &DeferredChanges,
&RejectedChanges,
Options().MaxRiskLevel, LFK_PseudoArray);
Finder.addMatcher(makePseudoArrayLoopMatcher(), &PseudoarrrayLoopFixer);
if (int result = LoopTool.run(createActionFactory(Finder, InputStates))) {
llvm::errs() << "Error encountered during translation.\n";
return result;
}
RewriterContainer Rewrite(LoopTool.getFiles(), InputStates);
// FIXME: Do something if some replacements didn't get applied?
LoopTool.applyAllReplacements(Rewrite.getRewriter());
collectResults(Rewrite.getRewriter(), InputStates, ResultStates);
setAcceptedChanges(AcceptedChanges);
setRejectedChanges(RejectedChanges);
setDeferredChanges(DeferredChanges);
return 0;
}