2013-07-08 20:17:37 +08:00
|
|
|
//===-- LoopConvert/LoopConvert.cpp - C++11 for-loop migration ------------===//
|
2013-01-05 02:25:18 +08:00
|
|
|
//
|
|
|
|
// 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/Tooling/Refactoring.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
|
|
|
|
using clang::ast_matchers::MatchFinder;
|
|
|
|
using namespace clang::tooling;
|
|
|
|
using namespace clang;
|
|
|
|
|
2013-10-05 20:15:58 +08:00
|
|
|
int LoopConvertTransform::apply(const CompilationDatabase &Database,
|
2013-06-18 23:31:01 +08:00
|
|
|
const std::vector<std::string> &SourcePaths) {
|
|
|
|
ClangTool LoopTool(Database, SourcePaths);
|
2013-01-17 05:11:50 +08:00
|
|
|
|
2013-01-05 02:25:18 +08:00
|
|
|
unsigned AcceptedChanges = 0;
|
|
|
|
unsigned DeferredChanges = 0;
|
|
|
|
unsigned RejectedChanges = 0;
|
|
|
|
|
2013-09-27 03:10:04 +08:00
|
|
|
TUInfo.reset(new TUTrackingInfo);
|
|
|
|
|
2013-01-05 02:25:18 +08:00
|
|
|
MatchFinder Finder;
|
2013-09-27 03:10:04 +08:00
|
|
|
LoopFixer ArrayLoopFixer(*TUInfo, &AcceptedChanges, &DeferredChanges,
|
|
|
|
&RejectedChanges, Options().MaxRiskLevel, LFK_Array,
|
2013-06-18 23:44:58 +08:00
|
|
|
/*Owner=*/ *this);
|
2013-01-05 02:25:18 +08:00
|
|
|
Finder.addMatcher(makeArrayLoopMatcher(), &ArrayLoopFixer);
|
2013-09-27 03:10:04 +08:00
|
|
|
LoopFixer IteratorLoopFixer(*TUInfo, &AcceptedChanges, &DeferredChanges,
|
2013-09-03 21:16:02 +08:00
|
|
|
&RejectedChanges, Options().MaxRiskLevel,
|
|
|
|
LFK_Iterator, /*Owner=*/ *this);
|
2013-01-05 02:25:18 +08:00
|
|
|
Finder.addMatcher(makeIteratorLoopMatcher(), &IteratorLoopFixer);
|
2013-09-27 03:10:04 +08:00
|
|
|
LoopFixer PseudoarrrayLoopFixer(*TUInfo, &AcceptedChanges, &DeferredChanges,
|
2013-09-03 21:16:02 +08:00
|
|
|
&RejectedChanges, Options().MaxRiskLevel,
|
|
|
|
LFK_PseudoArray, /*Owner=*/ *this);
|
2013-01-05 02:25:18 +08:00
|
|
|
Finder.addMatcher(makePseudoArrayLoopMatcher(), &PseudoarrrayLoopFixer);
|
2013-01-17 05:11:50 +08:00
|
|
|
|
2013-06-18 02:18:15 +08:00
|
|
|
if (int result = LoopTool.run(createActionFactory(Finder))) {
|
2013-01-05 02:25:18 +08:00
|
|
|
llvm::errs() << "Error encountered during translation.\n";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-05 08:12:33 +08:00
|
|
|
setAcceptedChanges(AcceptedChanges);
|
|
|
|
setRejectedChanges(RejectedChanges);
|
|
|
|
setDeferredChanges(DeferredChanges);
|
2013-01-05 02:25:18 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-07-24 22:24:33 +08:00
|
|
|
|
2013-09-27 03:10:04 +08:00
|
|
|
bool
|
|
|
|
LoopConvertTransform::handleBeginSource(clang::CompilerInstance &CI,
|
|
|
|
llvm::StringRef Filename) {
|
|
|
|
// Reset and initialize per-TU tracking structures.
|
|
|
|
TUInfo->reset();
|
|
|
|
|
|
|
|
return Transform::handleBeginSource(CI, Filename);
|
|
|
|
}
|
|
|
|
|
2013-07-24 22:24:33 +08:00
|
|
|
struct LoopConvertFactory : TransformFactory {
|
2013-07-29 23:58:47 +08:00
|
|
|
LoopConvertFactory() {
|
|
|
|
Since.Clang = Version(3, 0);
|
|
|
|
Since.Gcc = Version(4, 6);
|
|
|
|
Since.Icc = Version(13);
|
|
|
|
Since.Msvc = Version(11);
|
|
|
|
}
|
|
|
|
|
2013-07-24 22:24:33 +08:00
|
|
|
Transform *createTransform(const TransformOptions &Opts) LLVM_OVERRIDE {
|
|
|
|
return new LoopConvertTransform(Opts);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Register the factory using this statically initialized variable.
|
|
|
|
static TransformFactoryRegistry::Add<LoopConvertFactory>
|
|
|
|
X("loop-convert", "Make use of range-based for loops where possible");
|
|
|
|
|
|
|
|
// This anchor is used to force the linker to link in the generated object file
|
|
|
|
// and thus register the factory.
|
|
|
|
volatile int LoopConvertTransformAnchorSource = 0;
|