blob: 08ae3e0dfbbdb3971d5c9ac49a3615d1b7624660 [file] [log] [blame]
Edwin Vane9bd2e1b2012-12-12 14:30:57 +00001//===-- cpp11-migrate/Cpp11Migrate.cpp - Main file C++11 migration tool ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file implements the C++11 feature migration tool main function
12/// and transformation framework.
13///
14/// Usage:
15/// cpp11-migrate [-p <build-path>] <file1> <file2> ... [-- [compiler-options]]
16///
17/// Where <build-path> is a CMake build directory containing a file named
18/// compile_commands.json which provides compiler options for building each
19/// sourc file. If <build-path> is not provided the compile_commands.json file
20/// is searched for through all parent directories.
21///
22/// Alternatively, one can provide compile options to be applied to every source
23/// file after the optional '--'.
24///
25/// <file1>... specify the paths of files in the CMake source tree, with the
26/// same requirements as other tools built on LibTooling.
27///
28//===----------------------------------------------------------------------===//
29
Edwin Vane9bd2e1b2012-12-12 14:30:57 +000030#include "clang/Frontend/FrontendActions.h"
Chandler Carruth54e147a2013-01-02 10:29:31 +000031#include "clang/Tooling/CommonOptionsParser.h"
Edwin Vane9bd2e1b2012-12-12 14:30:57 +000032#include "clang/Tooling/Tooling.h"
Edwin Vanedde168b2013-01-04 18:25:18 +000033#include "Transforms.h"
34#include "Transform.h"
Edwin Vane9bd2e1b2012-12-12 14:30:57 +000035
36namespace cl = llvm::cl;
37using namespace clang::tooling;
38
Edwin Vanedde168b2013-01-04 18:25:18 +000039static cl::opt<RiskLevel>
40MaxRiskLevel("risk", cl::desc("Select a maximum risk level:"),
41 cl::values(
42 clEnumValN(RL_Safe, "safe", "Only safe transformations"),
43 clEnumValN(RL_Reasonable, "reasonable",
44 "Enable transformations that might change "
45 "semantics (default)"),
46 clEnumValN(RL_Risky, "risky",
47 "Enable transformations that are likely to "
48 "change semantics"),
49 clEnumValEnd),
50 cl::init(RL_Reasonable));
Edwin Vane9bd2e1b2012-12-12 14:30:57 +000051
52int main(int argc, const char **argv) {
Edwin Vanedde168b2013-01-04 18:25:18 +000053 Transforms TransformManager;
54
55 TransformManager.createTransformOpts();
56
57 // This causes options to be parsed.
Edwin Vane9bd2e1b2012-12-12 14:30:57 +000058 CommonOptionsParser OptionsParser(argc, argv);
59
Edwin Vanedde168b2013-01-04 18:25:18 +000060 TransformManager.createSelectedTransforms();
61
62 if (TransformManager.begin() == TransformManager.end()) {
63 llvm::errs() << "No selected transforms\n";
64 return 1;
65 }
66
67 // Initial syntax check.
Edwin Vane9e3c9de2012-12-14 18:59:24 +000068 ClangTool SyntaxTool(OptionsParser.getCompilations(),
69 OptionsParser.getSourcePathList());
Edwin Vane9bd2e1b2012-12-12 14:30:57 +000070
71 // First, let's check to make sure there were no errors.
72 if (SyntaxTool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>()) !=
73 0) {
74 return 1;
75 }
76
Edwin Vanedde168b2013-01-04 18:25:18 +000077 // Apply transforms.
78 for (Transforms::const_iterator I = TransformManager.begin(),
79 E = TransformManager.end(); I != E; ++I) {
80 if ((*I)->apply(MaxRiskLevel, OptionsParser.getCompilations(),
81 OptionsParser.getSourcePathList()) != 0) {
82 return 1;
83 }
84 }
85
86 // Final Syntax check.
87 ClangTool EndSyntaxTool(OptionsParser.getCompilations(),
88 OptionsParser.getSourcePathList());
89 if (EndSyntaxTool.run(
90 newFrontendActionFactory<clang::SyntaxOnlyAction>()) != 0) {
91 // FIXME: Revert changes made to files that fail the syntax test.
92 return 1;
93 }
Edwin Vane9bd2e1b2012-12-12 14:30:57 +000094
95 return 0;
96}