clang-modernize: Transform::createActionFactory return ownership by unique_ptr instead of raw pointer.

Follow up to r213851 to simplify code and reduce the chance of future
leaks.

llvm-svn: 215214
This commit is contained in:
David Blaikie 2014-08-08 16:06:07 +00:00
parent 99d2df956d
commit 590e5ff473
10 changed files with 22 additions and 27 deletions

View File

@ -40,8 +40,7 @@ int AddOverrideTransform::apply(const CompilationDatabase &Database,
// Make Fixer available to handleBeginSource().
this->Fixer = &Fixer;
std::unique_ptr<FrontendActionFactory> Factory(createActionFactory(Finder));
if (int result = AddOverrideTool.run(Factory.get())) {
if (int result = AddOverrideTool.run(createActionFactory(Finder).get())) {
llvm::errs() << "Error encountered during translation.\n";
return result;
}

View File

@ -128,8 +128,9 @@ Transform::addReplacementForCurrentTU(const clang::tooling::Replacement &R) {
return true;
}
FrontendActionFactory *Transform::createActionFactory(MatchFinder &Finder) {
return new ActionFactory(Finder, /*Owner=*/ *this);
std::unique_ptr<FrontendActionFactory>
Transform::createActionFactory(MatchFinder &Finder) {
return llvm::make_unique<ActionFactory>(Finder, /*Owner=*/*this);
}
Version Version::getFromString(llvm::StringRef VersionStr) {

View File

@ -208,8 +208,8 @@ protected:
///
/// The factory returned by this function is responsible for calling back to
/// Transform to call handleBeginSource() and handleEndSource().
clang::tooling::FrontendActionFactory *
createActionFactory(clang::ast_matchers::MatchFinder &Finder);
std::unique_ptr<clang::tooling::FrontendActionFactory>
createActionFactory(clang::ast_matchers::MatchFinder &Finder);
private:
const std::string Name;

View File

@ -48,8 +48,7 @@ int LoopConvertTransform::apply(const CompilationDatabase &Database,
LFK_PseudoArray, /*Owner=*/ *this);
Finder.addMatcher(makePseudoArrayLoopMatcher(), &PseudoarrrayLoopFixer);
std::unique_ptr<FrontendActionFactory> Factory(createActionFactory(Finder));
if (int result = LoopTool.run(Factory.get())) {
if (int result = LoopTool.run(createActionFactory(Finder).get())) {
llvm::errs() << "Error encountered during translation.\n";
return result;
}

View File

@ -35,8 +35,7 @@ int PassByValueTransform::apply(const tooling::CompilationDatabase &Database,
// make the replacer available to handleBeginSource()
this->Replacer = &Replacer;
std::unique_ptr<FrontendActionFactory> Factory(createActionFactory(Finder));
if (Tool.run(Factory.get())) {
if (Tool.run(createActionFactory(Finder).get())) {
llvm::errs() << "Error encountered during translation.\n";
return 1;
}

View File

@ -34,8 +34,7 @@ ReplaceAutoPtrTransform::apply(const CompilationDatabase &Database,
Finder.addMatcher(makeAutoPtrUsingDeclMatcher(), &Replacer);
Finder.addMatcher(makeTransferOwnershipExprMatcher(), &Fixer);
std::unique_ptr<FrontendActionFactory> Factory(createActionFactory(Finder));
if (Tool.run(Factory.get())) {
if (Tool.run(createActionFactory(Finder).get())) {
llvm::errs() << "Error encountered during translation.\n";
return 1;
}

View File

@ -36,8 +36,7 @@ int UseAutoTransform::apply(const clang::tooling::CompilationDatabase &Database,
Finder.addMatcher(makeIteratorDeclMatcher(), &ReplaceIterators);
Finder.addMatcher(makeDeclWithNewMatcher(), &ReplaceNew);
std::unique_ptr<FrontendActionFactory> Factory(createActionFactory(Finder));
if (int Result = UseAutoTool.run(Factory.get())) {
if (int Result = UseAutoTool.run(createActionFactory(Finder).get())) {
llvm::errs() << "Error encountered during translation.\n";
return Result;
}

View File

@ -46,8 +46,7 @@ int UseNullptrTransform::apply(const CompilationDatabase &Database,
NullptrFixer Fixer(AcceptedChanges, MacroNames, /*Owner=*/ *this);
Finder.addMatcher(makeCastSequenceMatcher(), &Fixer);
std::unique_ptr<FrontendActionFactory> Factory(createActionFactory(Finder));
if (int result = UseNullptrTool.run(Factory.get())) {
if (int result = UseNullptrTool.run(createActionFactory(Finder).get())) {
llvm::errs() << "Error encountered during translation.\n";
return result;
}

View File

@ -248,17 +248,17 @@ static CompilerVersions handleSupportedCompilers(const char *ProgName,
return RequiredVersions;
}
CompilationDatabase *autoDetectCompilations(std::string &ErrorMessage) {
std::unique_ptr<CompilationDatabase>
autoDetectCompilations(std::string &ErrorMessage) {
// Auto-detect a compilation database from BuildPath.
if (BuildPath.getNumOccurrences() > 0)
return CompilationDatabase::autoDetectFromDirectory(BuildPath,
ErrorMessage);
// Try to auto-detect a compilation database from the first source.
if (!SourcePaths.empty()) {
std::unique_ptr<CompilationDatabase> Compilations(
CompilationDatabase::autoDetectFromSource(SourcePaths[0],
ErrorMessage));
if (Compilations) {
if (std::unique_ptr<CompilationDatabase> Compilations =
CompilationDatabase::autoDetectFromSource(SourcePaths[0],
ErrorMessage)) {
// FIXME: just pass SourcePaths[0] once getCompileCommands supports
// non-absolute paths.
SmallString<64> Path(SourcePaths[0]);
@ -268,7 +268,7 @@ CompilationDatabase *autoDetectCompilations(std::string &ErrorMessage) {
// Ignore a detected compilation database that doesn't contain source0
// since it is probably an unrelated compilation database.
if (!Commands.empty())
return Compilations.release();
return Compilations;
}
// Reset ErrorMessage since a fix compilation database will be created if
// it fails to detect one from source.
@ -276,7 +276,7 @@ CompilationDatabase *autoDetectCompilations(std::string &ErrorMessage) {
// If no compilation database can be detected from source then we create a
// fixed compilation database with c++11 support.
std::string CommandLine[] = { "-std=c++11" };
return new FixedCompilationDatabase(".", CommandLine);
return llvm::make_unique<FixedCompilationDatabase>(".", CommandLine);
}
ErrorMessage = "Could not determine sources to transform";
@ -335,7 +335,7 @@ int main(int argc, const char **argv) {
if (!Compilations) {
std::string ErrorMessage;
Compilations.reset(autoDetectCompilations(ErrorMessage));
Compilations = autoDetectCompilations(ErrorMessage);
if (!Compilations) {
llvm::errs() << llvm::sys::path::filename(argv[0]) << ": " << ErrorMessage
<< "\n";

View File

@ -183,11 +183,11 @@ int main(int argc, const char **argv) {
cl::ParseCommandLineOptions(argc, argv);
if (!Compilations) {
std::string ErrorMessage;
Compilations.reset(
CompilationDatabase::loadFromDirectory(BuildPath, ErrorMessage));
Compilations =
CompilationDatabase::loadFromDirectory(BuildPath, ErrorMessage);
if (!Compilations)
llvm::report_fatal_error(ErrorMessage);
}
}
tooling::RefactoringTool Tool(*Compilations, SourcePaths);
ast_matchers::MatchFinder Finder;
FixCStrCall Callback(&Tool.getReplacements());