2013-01-05 02:25:18 +08:00
|
|
|
//===-- 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.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-07-08 20:17:37 +08:00
|
|
|
|
2013-09-05 03:13:50 +08:00
|
|
|
#ifndef CLANG_MODERNIZE_LOOP_ACTIONS_H
|
|
|
|
#define CLANG_MODERNIZE_LOOP_ACTIONS_H
|
2013-01-05 02:25:18 +08:00
|
|
|
|
2013-04-05 04:19:58 +08:00
|
|
|
#include "Core/Transform.h"
|
2014-01-08 04:05:01 +08:00
|
|
|
#include "StmtAncestor.h"
|
2013-01-05 02:25:18 +08:00
|
|
|
#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
|
|
|
|
};
|
|
|
|
|
2013-09-27 03:10:04 +08:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2013-01-05 02:25:18 +08:00
|
|
|
/// \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 {
|
2013-09-27 03:10:04 +08:00
|
|
|
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) {}
|
2013-06-18 23:44:58 +08:00
|
|
|
|
2013-09-27 03:10:04 +08:00
|
|
|
virtual void run(const clang::ast_matchers::MatchFinder::MatchResult &Result);
|
2013-01-05 02:25:18 +08:00
|
|
|
|
2013-09-27 03:10:04 +08:00
|
|
|
private:
|
|
|
|
TUTrackingInfo &TUInfo;
|
2013-01-05 02:25:18 +08:00
|
|
|
unsigned *AcceptedChanges;
|
|
|
|
unsigned *DeferredChanges;
|
|
|
|
unsigned *RejectedChanges;
|
|
|
|
RiskLevel MaxRisk;
|
|
|
|
LoopFixerKind FixerKind;
|
2013-09-03 21:16:02 +08:00
|
|
|
Transform &Owner;
|
2013-01-05 02:25:18 +08:00
|
|
|
|
|
|
|
/// \brief Computes the changes needed to convert a given for loop, and
|
|
|
|
/// applies it.
|
2013-09-27 03:10:04 +08:00
|
|
|
void doConversion(clang::ASTContext *Context, const clang::VarDecl *IndexVar,
|
2013-01-05 02:25:18 +08:00
|
|
|
const clang::VarDecl *MaybeContainer,
|
2013-09-27 03:10:04 +08:00
|
|
|
llvm::StringRef ContainerString, const UsageResult &Usages,
|
|
|
|
const clang::DeclStmt *AliasDecl, bool AliasUseRequired,
|
|
|
|
bool AliasFromForInit, const clang::ForStmt *TheLoop,
|
|
|
|
bool ContainerNeedsDereference, bool DerefByValue,
|
2013-05-10 01:46:20 +08:00
|
|
|
bool DerefByConstRef);
|
2013-01-05 02:25:18 +08:00
|
|
|
|
|
|
|
/// \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,
|
2013-09-27 03:10:04 +08:00
|
|
|
bool ContainerNeedsDereference, bool DerefByValue,
|
|
|
|
bool DerefByConstRef, const clang::ForStmt *TheLoop,
|
2013-01-05 02:25:18 +08:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2013-09-05 03:13:50 +08:00
|
|
|
#endif // CLANG_MODERNIZE_LOOP_ACTIONS_H
|