llvm-project/clang-tools-extra/clang-modernize/LoopConvert/LoopActions.h

116 lines
4.3 KiB
C
Raw Normal View History

//===-- LoopConvert/LoopActions.h - 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 declares matchers and callbacks for use in migrating C++
/// for loops.
///
//===----------------------------------------------------------------------===//
#ifndef CLANG_MODERNIZE_LOOP_ACTIONS_H
#define CLANG_MODERNIZE_LOOP_ACTIONS_H
#include "Core/Transform.h"
#include "StmtAncestor.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Tooling/Refactoring.h"
struct Usage;
class Confidence;
// The main computational result of ForLoopIndexUseVisitor.
typedef llvm::SmallVector<Usage, 8> UsageResult;
enum LoopFixerKind {
LFK_Array,
LFK_Iterator,
LFK_PseudoArray
};
struct TUTrackingInfo {
/// \brief Reset and initialize per-TU tracking information.
///
/// Must be called before using container accessors.
void reset() {
ParentFinder.reset(new StmtAncestorASTVisitor);
GeneratedDecls.clear();
ReplacedVars.clear();
}
/// \name Accessors
/// \{
StmtAncestorASTVisitor &getParentFinder() { return *ParentFinder; }
StmtGeneratedVarNameMap &getGeneratedDecls() { return GeneratedDecls; }
ReplacedVarsMap &getReplacedVars() { return ReplacedVars; }
/// \}
private:
llvm::OwningPtr<StmtAncestorASTVisitor> ParentFinder;
StmtGeneratedVarNameMap GeneratedDecls;
ReplacedVarsMap ReplacedVars;
};
/// \brief The callback to be used for loop migration matchers.
///
/// The callback does extra checking not possible in matchers, and attempts to
/// convert the for loop, if possible.
class LoopFixer : public clang::ast_matchers::MatchFinder::MatchCallback {
public:
LoopFixer(TUTrackingInfo &TUInfo, unsigned *AcceptedChanges,
unsigned *DeferredChanges, unsigned *RejectedChanges,
RiskLevel MaxRisk, LoopFixerKind FixerKind, Transform &Owner)
: TUInfo(TUInfo), AcceptedChanges(AcceptedChanges),
DeferredChanges(DeferredChanges), RejectedChanges(RejectedChanges),
MaxRisk(MaxRisk), FixerKind(FixerKind), Owner(Owner) {}
virtual void run(const clang::ast_matchers::MatchFinder::MatchResult &Result);
private:
TUTrackingInfo &TUInfo;
unsigned *AcceptedChanges;
unsigned *DeferredChanges;
unsigned *RejectedChanges;
RiskLevel MaxRisk;
LoopFixerKind FixerKind;
cpp11-migrate: Refactor for driver model of operation Re-commit of r189691 and r189689 now with a proper autoconf fix. Massive simplification of how replacements and file overrides are handled by the migrator: * Sources and headers are all treated the same. * All replacements for a given translation unit are stored in the same TranslationUnitReplacements structure. * Change tracking is updated only from main file; no need for propagating "is tracking" flag around. * Transform base class no longer responsible for applying replacements. They are simply stored and main() looks after deduplication and application. * Renamed -yaml-only to -serialize-replacements. Same restrictions apply: Can only request one transform. New restriction: formatting cannot also be turned on since it's basically a transform. * If -serialize-replacements is requested, changes to files will not be applied on disk. * Changed behaviour of function generating names for serialized replacements: Only the main source file goes into the name of the file since a file may contain changes for multiple different files. * Updated HeaderReplacements LIT test for new serialization behaviour. * Replaced old test that ensures replacements are not serialized if -serialize-replacements is not provided. New version ensures changes are made directly to all files in the translation unit. * Updated unit tests. * Due to major simplification of structures in FileOverrides.h, the FileOverridesTest is quite a bit simpler now. llvm-svn: 189798
2013-09-03 21:16:02 +08:00
Transform &Owner;
/// \brief Computes the changes needed to convert a given for loop, and
/// applies it.
void doConversion(clang::ASTContext *Context, const clang::VarDecl *IndexVar,
const clang::VarDecl *MaybeContainer,
llvm::StringRef ContainerString, const UsageResult &Usages,
const clang::DeclStmt *AliasDecl, bool AliasUseRequired,
bool AliasFromForInit, const clang::ForStmt *TheLoop,
bool ContainerNeedsDereference, bool DerefByValue,
bool DerefByConstRef);
/// \brief Given a loop header that would be convertible, discover all usages
/// of the index variable and convert the loop if possible.
void findAndVerifyUsages(clang::ASTContext *Context,
const clang::VarDecl *LoopVar,
const clang::VarDecl *EndVar,
const clang::Expr *ContainerExpr,
const clang::Expr *BoundExpr,
bool ContainerNeedsDereference, bool DerefByValue,
bool DerefByConstRef, const clang::ForStmt *TheLoop,
Confidence ConfidenceLevel);
/// \brief Determine if the change should be deferred or rejected, returning
/// text which refers to the container iterated over if the change should
/// proceed.
llvm::StringRef checkDeferralsAndRejections(clang::ASTContext *Context,
const clang::Expr *ContainerExpr,
Confidence ConfidenceLevel,
const clang::ForStmt *TheLoop);
};
#endif // CLANG_MODERNIZE_LOOP_ACTIONS_H