diff options
| author | Edwin Vane <edwin.vane@intel.com> | 2013-01-22 18:31:49 +0000 |
|---|---|---|
| committer | Edwin Vane <edwin.vane@intel.com> | 2013-01-22 18:31:49 +0000 |
| commit | 44c3145938573088d5f89b7557bd32cb70359e26 (patch) | |
| tree | b30a4a88a251e6cbfe0445afa62042aa294f7364 /clang-tools-extra/cpp11-migrate/UseNullptr/UseNullptr.cpp | |
| parent | 33a6e11c21a6cea79bdf18d53366742f5d37e480 (diff) | |
| download | bcm5719-llvm-44c3145938573088d5f89b7557bd32cb70359e26.tar.gz bcm5719-llvm-44c3145938573088d5f89b7557bd32cb70359e26.zip | |
Add use-nullptr transform to cpp11-migrate
This transform converts the usage of null pointer constants (e.g. NULL, 0,
etc.) in legacy C++ code and converts them to use the new C++11 nullptr
keyword.
- Added use-nullptr transform.
- Added C++11 support to the final syntax check. Used ArgumentAdjuster class to
add -std=c++11 option to the command line options.
- Added tests for use-nullptr transform.
- Added tests that exercises both loop-convert and use-nullptr in the source
file.
TODO: There's a known bug when using both -loop-convert and -use-nullptr at the
same time.
Author: Tareq A Siraj <tareq.a.siraj@intel.com>
Reviewers: klimek, gribozavr
llvm-svn: 173178
Diffstat (limited to 'clang-tools-extra/cpp11-migrate/UseNullptr/UseNullptr.cpp')
| -rw-r--r-- | clang-tools-extra/cpp11-migrate/UseNullptr/UseNullptr.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/clang-tools-extra/cpp11-migrate/UseNullptr/UseNullptr.cpp b/clang-tools-extra/cpp11-migrate/UseNullptr/UseNullptr.cpp new file mode 100644 index 00000000000..aaf5bf1a3f0 --- /dev/null +++ b/clang-tools-extra/cpp11-migrate/UseNullptr/UseNullptr.cpp @@ -0,0 +1,68 @@ +//===-- 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 UseNullptrTransform +/// class. +/// +//===----------------------------------------------------------------------===// + +#include "UseNullptr.h" +#include "NullptrActions.h" +#include "NullptrMatchers.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 UseNullptrTransform::apply(const FileContentsByPath &InputStates, + RiskLevel MaxRisk, + const CompilationDatabase &Database, + const std::vector<std::string> &SourcePaths, + FileContentsByPath &ResultStates) { + RefactoringTool UseNullptrTool(Database, SourcePaths); + + for (FileContentsByPath::const_iterator I = InputStates.begin(), + E = InputStates.end(); + I != E; ++I) { + UseNullptrTool.mapVirtualFile(I->first, I->second); + } + + unsigned AcceptedChanges = 0; + + MatchFinder Finder; + NullptrFixer Fixer(UseNullptrTool.getReplacements(), + AcceptedChanges, + MaxRisk); + + Finder.addMatcher(makeImplicitCastMatcher(), &Fixer); + Finder.addMatcher(makeCastSequenceMatcher(), &Fixer); + + if (int result = UseNullptrTool.run(newFrontendActionFactory(&Finder))) { + llvm::errs() << "Error encountered during translation.\n"; + return result; + } + + RewriterContainer Rewrite(UseNullptrTool.getFiles()); + + // FIXME: Do something if some replacements didn't get applied? + UseNullptrTool.applyAllReplacements(Rewrite.getRewriter()); + + collectResults(Rewrite.getRewriter(), ResultStates); + + if (AcceptedChanges > 0) { + setChangesMade(); + } + + return 0; +} |

