2013-07-08 20:17:37 +08:00
|
|
|
//===-- UseNullptr/UseNullptr.cpp - C++11 nullptr migration ---------------===//
|
2013-01-23 02:31:49 +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 UseNullptrTransform
|
|
|
|
/// class.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "UseNullptr.h"
|
|
|
|
#include "NullptrActions.h"
|
|
|
|
#include "NullptrMatchers.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-18 01:57:36 +08:00
|
|
|
namespace cl = llvm::cl;
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
UserNullMacroNames("user-null-macros",
|
|
|
|
cl::desc("Comma-separated list of user-defined "
|
|
|
|
"macro names that behave like NULL"),
|
|
|
|
cl::cat(TransformsOptionsCategory), cl::init(""));
|
2013-01-23 02:31:49 +08:00
|
|
|
|
2013-10-05 20:15:58 +08:00
|
|
|
int UseNullptrTransform::apply(const CompilationDatabase &Database,
|
2013-06-18 23:31:01 +08:00
|
|
|
const std::vector<std::string> &SourcePaths) {
|
|
|
|
ClangTool UseNullptrTool(Database, SourcePaths);
|
2013-01-23 02:31:49 +08:00
|
|
|
|
|
|
|
unsigned AcceptedChanges = 0;
|
|
|
|
|
2013-10-18 01:57:36 +08:00
|
|
|
llvm::SmallVector<llvm::StringRef, 1> MacroNames;
|
|
|
|
if (!UserNullMacroNames.empty()) {
|
|
|
|
llvm::StringRef S = UserNullMacroNames;
|
|
|
|
S.split(MacroNames, ",");
|
|
|
|
}
|
2013-01-23 02:31:49 +08:00
|
|
|
MatchFinder Finder;
|
2013-10-18 01:57:36 +08:00
|
|
|
NullptrFixer Fixer(AcceptedChanges, MacroNames, /*Owner=*/ *this);
|
2013-06-18 23:44:58 +08:00
|
|
|
|
2013-01-23 02:31:49 +08:00
|
|
|
Finder.addMatcher(makeCastSequenceMatcher(), &Fixer);
|
2014-08-09 00:06:07 +08:00
|
|
|
if (int result = UseNullptrTool.run(createActionFactory(Finder).get())) {
|
2013-01-23 02:31:49 +08:00
|
|
|
llvm::errs() << "Error encountered during translation.\n";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-05 08:12:33 +08:00
|
|
|
setAcceptedChanges(AcceptedChanges);
|
2013-01-23 02:31:49 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-07-24 22:24:33 +08:00
|
|
|
|
2015-03-23 20:49:15 +08:00
|
|
|
namespace {
|
2013-07-24 22:24:33 +08:00
|
|
|
struct UseNullptrFactory : TransformFactory {
|
2013-07-29 23:58:47 +08:00
|
|
|
UseNullptrFactory() {
|
|
|
|
Since.Clang = Version(3, 0);
|
|
|
|
Since.Gcc = Version(4, 6);
|
|
|
|
Since.Icc = Version(12, 1);
|
|
|
|
Since.Msvc = Version(10);
|
|
|
|
}
|
|
|
|
|
2014-03-02 18:20:11 +08:00
|
|
|
Transform *createTransform(const TransformOptions &Opts) override {
|
2013-07-24 22:24:33 +08:00
|
|
|
return new UseNullptrTransform(Opts);
|
|
|
|
}
|
|
|
|
};
|
2015-03-23 20:49:15 +08:00
|
|
|
} // namespace
|
2013-07-24 22:24:33 +08:00
|
|
|
|
|
|
|
// Register the factory using this statically initialized variable.
|
|
|
|
static TransformFactoryRegistry::Add<UseNullptrFactory>
|
|
|
|
X("use-nullptr", "Make use of nullptr keyword where possible");
|
|
|
|
|
|
|
|
// This anchor is used to force the linker to link in the generated object file
|
|
|
|
// and thus register the factory.
|
|
|
|
volatile int UseNullptrTransformAnchorSource = 0;
|