Edwin Vane | 9bd2e1b | 2012-12-12 14:30:57 +0000 | [diff] [blame] | 1 | //===-- 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 Vane | 9bd2e1b | 2012-12-12 14:30:57 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/FrontendActions.h" |
Chandler Carruth | 54e147a | 2013-01-02 10:29:31 +0000 | [diff] [blame] | 31 | #include "clang/Tooling/CommonOptionsParser.h" |
Edwin Vane | 9bd2e1b | 2012-12-12 14:30:57 +0000 | [diff] [blame] | 32 | #include "clang/Tooling/Tooling.h" |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame^] | 33 | #include "Transforms.h" |
| 34 | #include "Transform.h" |
Edwin Vane | 9bd2e1b | 2012-12-12 14:30:57 +0000 | [diff] [blame] | 35 | |
| 36 | namespace cl = llvm::cl; |
| 37 | using namespace clang::tooling; |
| 38 | |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame^] | 39 | static cl::opt<RiskLevel> |
| 40 | MaxRiskLevel("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 Vane | 9bd2e1b | 2012-12-12 14:30:57 +0000 | [diff] [blame] | 51 | |
| 52 | int main(int argc, const char **argv) { |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame^] | 53 | Transforms TransformManager; |
| 54 | |
| 55 | TransformManager.createTransformOpts(); |
| 56 | |
| 57 | // This causes options to be parsed. |
Edwin Vane | 9bd2e1b | 2012-12-12 14:30:57 +0000 | [diff] [blame] | 58 | CommonOptionsParser OptionsParser(argc, argv); |
| 59 | |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame^] | 60 | 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 Vane | 9e3c9de | 2012-12-14 18:59:24 +0000 | [diff] [blame] | 68 | ClangTool SyntaxTool(OptionsParser.getCompilations(), |
| 69 | OptionsParser.getSourcePathList()); |
Edwin Vane | 9bd2e1b | 2012-12-12 14:30:57 +0000 | [diff] [blame] | 70 | |
| 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 Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame^] | 77 | // 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 Vane | 9bd2e1b | 2012-12-12 14:30:57 +0000 | [diff] [blame] | 94 | |
| 95 | return 0; |
| 96 | } |