Files
clang-p2996/clang-tools-extra/cpp11-migrate/UseAuto/UseAuto.cpp
Edwin Vane eeed39a583 Use 'auto' with 'new' expressions
For variable declarations initialized with new expressions, use 'auto' for the
type specifier.

The 'auto' replacement happens only when the type of the VarDecl exactly
matches the type of the initializer and the VarDecl is *not* CV-qualified. The
only case that is currently handled is if the pointer type of the VarDecl is
itself CV qualified.

Some improvements need to be made to Clang's TypeLoc information in order for
other CV qualifier cases to be successfully handled. See the new test suite
new_cv_failing.cpp for examples of usages that could be handled with such an
improvement.

Function pointers are, for now, not transformed until the identifier info can
be extracted.

Reviewer: klimek
llvm-svn: 178575
2013-04-02 20:43:57 +00:00

62 lines
2.1 KiB
C++

//===-- UseAuto/UseAuto.cpp - Use auto type specifier ---------------------===//
//
// 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 UseAutoTransform class.
///
//===----------------------------------------------------------------------===//
#include "UseAuto.h"
#include "UseAutoActions.h"
#include "UseAutoMatchers.h"
using clang::ast_matchers::MatchFinder;
using namespace clang;
using namespace clang::tooling;
int UseAutoTransform::apply(const FileContentsByPath &InputStates,
RiskLevel MaxRisk,
const clang::tooling::CompilationDatabase &Database,
const std::vector<std::string> &SourcePaths,
FileContentsByPath &ResultStates) {
RefactoringTool UseAutoTool(Database, SourcePaths);
for (FileContentsByPath::const_iterator I = InputStates.begin(),
E = InputStates.end();
I != E; ++I)
UseAutoTool.mapVirtualFile(I->first, I->second);
unsigned AcceptedChanges = 0;
MatchFinder Finder;
IteratorReplacer ReplaceIterators(UseAutoTool.getReplacements(),
AcceptedChanges, MaxRisk);
NewReplacer ReplaceNew(UseAutoTool.getReplacements(), AcceptedChanges,
MaxRisk);
Finder.addMatcher(makeIteratorDeclMatcher(), &ReplaceIterators);
Finder.addMatcher(makeDeclWithNewMatcher(), &ReplaceNew);
if (int Result = UseAutoTool.run(newFrontendActionFactory(&Finder))) {
llvm::errs() << "Error encountered during translation.\n";
return Result;
}
RewriterContainer Rewrite(UseAutoTool.getFiles(), InputStates);
// FIXME: Do something if some replacements didn't get applied?
UseAutoTool.applyAllReplacements(Rewrite.getRewriter());
collectResults(Rewrite.getRewriter(), InputStates, ResultStates);
setAcceptedChanges(AcceptedChanges);
return 0;
}