2013-07-08 20:17:37 +08:00
|
|
|
//===-- Core/Transform.h - Transform Base Class Def'n -----------*- C++ -*-===//
|
2013-06-06 00:39:24 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2013-07-08 20:17:37 +08:00
|
|
|
/// \brief This file provides the declaration for the base Transform class from
|
2013-06-06 00:39:24 +08:00
|
|
|
/// which all transforms must subclass.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-09-05 03:13:50 +08:00
|
|
|
#ifndef CLANG_MODERNIZE_TRANSFORM_H
|
|
|
|
#define CLANG_MODERNIZE_TRANSFORM_H
|
2013-07-08 20:17:37 +08:00
|
|
|
|
2013-06-14 00:00:46 +08:00
|
|
|
#include "Core/IncludeExcludeInfo.h"
|
2013-09-03 21:16:02 +08:00
|
|
|
#include "Core/Refactoring.h"
|
2013-07-05 01:49:59 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2013-07-24 22:24:33 +08:00
|
|
|
#include "llvm/Support/Registry.h"
|
2013-07-05 01:49:59 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2013-07-08 20:17:37 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2013-06-06 00:39:24 +08:00
|
|
|
|
|
|
|
/// \brief Description of the riskiness of actions that can be taken by
|
|
|
|
/// transforms.
|
|
|
|
enum RiskLevel {
|
|
|
|
/// Transformations that will not change semantics.
|
|
|
|
RL_Safe,
|
|
|
|
|
|
|
|
/// Transformations that might change semantics.
|
|
|
|
RL_Reasonable,
|
|
|
|
|
|
|
|
/// Transformations that are likely to change semantics.
|
|
|
|
RL_Risky
|
|
|
|
};
|
|
|
|
|
|
|
|
// Forward declarations
|
|
|
|
namespace clang {
|
2013-06-18 02:18:15 +08:00
|
|
|
class CompilerInstance;
|
2013-06-06 00:39:24 +08:00
|
|
|
namespace tooling {
|
|
|
|
class CompilationDatabase;
|
2013-06-18 02:18:15 +08:00
|
|
|
class FrontendActionFactory;
|
2013-06-06 00:39:24 +08:00
|
|
|
} // namespace tooling
|
2013-06-13 03:52:13 +08:00
|
|
|
namespace ast_matchers {
|
|
|
|
class MatchFinder;
|
|
|
|
} // namespace ast_matchers
|
2013-06-06 00:39:24 +08:00
|
|
|
} // namespace clang
|
|
|
|
|
2013-09-03 21:16:02 +08:00
|
|
|
// \brief Maps main source file names to a TranslationUnitReplacements
|
|
|
|
// structure storing replacements for that translation unit.
|
|
|
|
typedef llvm::StringMap<clang::tooling::TranslationUnitReplacements>
|
|
|
|
TUReplacementsMap;
|
|
|
|
|
2013-07-05 01:49:59 +08:00
|
|
|
/// \brief To group transforms' options together when printing the help.
|
|
|
|
extern llvm::cl::OptionCategory TransformsOptionsCategory;
|
|
|
|
|
2013-06-07 04:31:52 +08:00
|
|
|
/// \brief Container for global options affecting all transforms.
|
|
|
|
struct TransformOptions {
|
|
|
|
/// \brief Enable the use of performance timers.
|
|
|
|
bool EnableTiming;
|
|
|
|
|
2013-09-13 04:10:59 +08:00
|
|
|
/// \brief Contains information on which files are safe to transform and
|
2013-06-07 04:32:29 +08:00
|
|
|
/// which aren't.
|
2013-09-13 04:10:59 +08:00
|
|
|
IncludeExcludeInfo ModifiableFiles;
|
2013-06-07 04:32:29 +08:00
|
|
|
|
2013-06-07 04:31:52 +08:00
|
|
|
/// \brief Maximum allowed level of risk.
|
|
|
|
RiskLevel MaxRiskLevel;
|
|
|
|
};
|
|
|
|
|
2013-06-06 00:39:24 +08:00
|
|
|
/// \brief Abstract base class for all C++11 migration transforms.
|
|
|
|
///
|
2013-06-18 02:18:15 +08:00
|
|
|
/// Subclasses must call createActionFactory() to create a
|
|
|
|
/// FrontendActionFactory to pass to ClangTool::run(). Subclasses are also
|
|
|
|
/// responsible for calling setOverrides() before calling ClangTool::run().
|
|
|
|
///
|
|
|
|
/// If timing is enabled (see TransformOptions), per-source performance timing
|
|
|
|
/// is recorded and stored in a TimingVec for later access with timing_begin()
|
|
|
|
/// and timing_end().
|
|
|
|
class Transform {
|
2013-06-06 00:39:24 +08:00
|
|
|
public:
|
|
|
|
/// \brief Constructor
|
|
|
|
/// \param Name Name of the transform for human-readable purposes (e.g. -help
|
|
|
|
/// text)
|
2013-06-26 03:01:18 +08:00
|
|
|
/// \param Options Global options that affect all Transforms.
|
2013-06-18 23:31:01 +08:00
|
|
|
Transform(llvm::StringRef Name, const TransformOptions &Options);
|
2013-06-06 00:39:24 +08:00
|
|
|
|
2013-06-18 23:31:01 +08:00
|
|
|
virtual ~Transform();
|
2013-06-06 00:39:24 +08:00
|
|
|
|
|
|
|
/// \brief Apply a transform to all files listed in \p SourcePaths.
|
|
|
|
///
|
2013-10-05 20:15:58 +08:00
|
|
|
/// \param[in] Database Contains information for how to compile all files in
|
|
|
|
/// \p SourcePaths.
|
|
|
|
/// \param[in] SourcePaths list of sources to transform.
|
|
|
|
///
|
|
|
|
/// \returns \li 0 if successful
|
|
|
|
/// \li 1 otherwise
|
|
|
|
virtual int apply(const clang::tooling::CompilationDatabase &Database,
|
2013-06-18 23:31:01 +08:00
|
|
|
const std::vector<std::string> &SourcePaths) = 0;
|
2013-06-06 00:39:24 +08:00
|
|
|
|
|
|
|
/// \brief Query if changes were made during the last call to apply().
|
|
|
|
bool getChangesMade() const { return AcceptedChanges > 0; }
|
|
|
|
|
|
|
|
/// \brief Query if changes were not made due to conflicts with other changes
|
|
|
|
/// made during the last call to apply() or if changes were too risky for the
|
|
|
|
/// requested risk level.
|
|
|
|
bool getChangesNotMade() const {
|
|
|
|
return RejectedChanges > 0 || DeferredChanges > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Query the number of accepted changes.
|
|
|
|
unsigned getAcceptedChanges() const { return AcceptedChanges; }
|
|
|
|
/// \brief Query the number of changes considered too risky.
|
|
|
|
unsigned getRejectedChanges() const { return RejectedChanges; }
|
|
|
|
/// \brief Query the number of changes not made because they conflicted with
|
|
|
|
/// early changes.
|
|
|
|
unsigned getDeferredChanges() const { return DeferredChanges; }
|
|
|
|
|
|
|
|
/// \brief Query transform name.
|
|
|
|
llvm::StringRef getName() const { return Name; }
|
|
|
|
|
|
|
|
/// \brief Reset internal state of the transform.
|
|
|
|
///
|
|
|
|
/// Useful if calling apply() several times with one instantiation of a
|
|
|
|
/// transform.
|
|
|
|
void Reset() {
|
|
|
|
AcceptedChanges = 0;
|
|
|
|
RejectedChanges = 0;
|
|
|
|
DeferredChanges = 0;
|
|
|
|
}
|
|
|
|
|
2013-06-18 23:44:58 +08:00
|
|
|
/// \brief Tests if the file containing \a Loc is allowed to be modified by
|
2013-09-05 01:35:07 +08:00
|
|
|
/// the Modernizer.
|
2013-06-18 23:44:58 +08:00
|
|
|
bool isFileModifiable(const clang::SourceManager &SM,
|
|
|
|
const clang::SourceLocation &Loc) const;
|
|
|
|
|
2013-08-29 21:42:13 +08:00
|
|
|
/// \brief Whether a transformation with a risk level of \p RiskLevel is
|
|
|
|
/// acceptable or not.
|
|
|
|
bool isAcceptableRiskLevel(RiskLevel RiskLevel) const {
|
|
|
|
return RiskLevel <= GlobalOptions.MaxRiskLevel;
|
|
|
|
}
|
|
|
|
|
2013-06-18 02:18:15 +08:00
|
|
|
/// \brief Called before parsing a translation unit for a FrontendAction.
|
|
|
|
///
|
|
|
|
/// Transform uses this function to apply file overrides and start
|
|
|
|
/// performance timers. Subclasses overriding this function must call it
|
|
|
|
/// before returning.
|
2013-06-06 00:39:24 +08:00
|
|
|
virtual bool handleBeginSource(clang::CompilerInstance &CI,
|
2013-06-18 02:18:15 +08:00
|
|
|
llvm::StringRef Filename);
|
2013-06-06 00:39:24 +08:00
|
|
|
|
2013-06-18 02:18:15 +08:00
|
|
|
/// \brief Called after FrontendAction has been run over a translation unit.
|
|
|
|
///
|
|
|
|
/// Transform uses this function to stop performance timers. Subclasses
|
|
|
|
/// overriding this function must call it before returning. A call to
|
|
|
|
/// handleEndSource() for a given translation unit is expected to be called
|
|
|
|
/// immediately after the corresponding handleBeginSource() call.
|
|
|
|
virtual void handleEndSource();
|
2013-06-06 00:39:24 +08:00
|
|
|
|
|
|
|
/// \brief Performance timing data is stored as a vector of pairs. Pairs are
|
|
|
|
/// formed of:
|
|
|
|
/// \li Name of source file.
|
|
|
|
/// \li Elapsed time.
|
|
|
|
typedef std::vector<std::pair<std::string, llvm::TimeRecord> > TimingVec;
|
|
|
|
|
|
|
|
/// \brief Return an iterator to the start of collected timing data.
|
|
|
|
TimingVec::const_iterator timing_begin() const { return Timings.begin(); }
|
|
|
|
/// \brief Return an iterator to the start of collected timing data.
|
|
|
|
TimingVec::const_iterator timing_end() const { return Timings.end(); }
|
|
|
|
|
2013-09-03 21:16:02 +08:00
|
|
|
/// \brief Add a Replacement to the list for the current translation unit.
|
|
|
|
///
|
|
|
|
/// \returns \li true on success
|
|
|
|
/// \li false if there is no current translation unit
|
|
|
|
bool addReplacementForCurrentTU(const clang::tooling::Replacement &R);
|
|
|
|
|
|
|
|
/// \brief Accessor to Replacements across all transformed translation units.
|
|
|
|
const TUReplacementsMap &getAllReplacements() const {
|
|
|
|
return Replacements;
|
|
|
|
}
|
|
|
|
|
2013-06-06 00:39:24 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
void setAcceptedChanges(unsigned Changes) {
|
|
|
|
AcceptedChanges = Changes;
|
|
|
|
}
|
|
|
|
void setRejectedChanges(unsigned Changes) {
|
|
|
|
RejectedChanges = Changes;
|
|
|
|
}
|
|
|
|
void setDeferredChanges(unsigned Changes) {
|
|
|
|
DeferredChanges = Changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Allows subclasses to manually add performance timer data.
|
|
|
|
///
|
|
|
|
/// \p Label should probably include the source file name somehow as the
|
|
|
|
/// duration info is simply added to the vector of timing data which holds
|
|
|
|
/// data for all sources processed by this transform.
|
|
|
|
void addTiming(llvm::StringRef Label, llvm::TimeRecord Duration);
|
|
|
|
|
2013-06-18 23:31:01 +08:00
|
|
|
/// \brief Provide access for subclasses to the TransformOptions they were
|
|
|
|
/// created with.
|
2013-06-07 04:31:52 +08:00
|
|
|
const TransformOptions &Options() { return GlobalOptions; }
|
|
|
|
|
2013-06-18 02:18:15 +08:00
|
|
|
/// \brief Subclasses must call this function to create a
|
|
|
|
/// FrontendActionFactory to pass to ClangTool.
|
|
|
|
///
|
|
|
|
/// The factory returned by this function is responsible for calling back to
|
|
|
|
/// Transform to call handleBeginSource() and handleEndSource().
|
2013-06-13 03:52:13 +08:00
|
|
|
clang::tooling::FrontendActionFactory *
|
2013-06-18 02:18:15 +08:00
|
|
|
createActionFactory(clang::ast_matchers::MatchFinder &Finder);
|
2013-06-13 03:52:13 +08:00
|
|
|
|
2013-06-06 00:39:24 +08:00
|
|
|
private:
|
|
|
|
const std::string Name;
|
2013-06-07 04:31:52 +08:00
|
|
|
const TransformOptions &GlobalOptions;
|
2013-09-03 21:16:02 +08:00
|
|
|
TUReplacementsMap Replacements;
|
2013-06-18 23:31:01 +08:00
|
|
|
std::string CurrentSource;
|
2013-06-06 00:39:24 +08:00
|
|
|
TimingVec Timings;
|
|
|
|
unsigned AcceptedChanges;
|
|
|
|
unsigned RejectedChanges;
|
|
|
|
unsigned DeferredChanges;
|
|
|
|
};
|
|
|
|
|
2013-07-29 23:58:47 +08:00
|
|
|
/// \brief Describes a version number of the form major[.minor] (minor being
|
|
|
|
/// optional).
|
|
|
|
struct Version {
|
|
|
|
explicit Version(unsigned Major = 0, unsigned Minor = 0)
|
|
|
|
: Major(Major), Minor(Minor) {}
|
|
|
|
|
|
|
|
bool operator<(Version RHS) const {
|
|
|
|
if (Major < RHS.Major)
|
|
|
|
return true;
|
|
|
|
if (Major == RHS.Major)
|
|
|
|
return Minor < RHS.Minor;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(Version RHS) const {
|
|
|
|
return Major == RHS.Major && Minor == RHS.Minor;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(Version RHS) const { return !(*this == RHS); }
|
|
|
|
bool operator>(Version RHS) const { return RHS < *this; }
|
|
|
|
bool operator<=(Version RHS) const { return !(*this > RHS); }
|
|
|
|
bool operator>=(Version RHS) const { return !(*this < RHS); }
|
|
|
|
|
|
|
|
bool isNull() const { return Minor == 0 && Major == 0; }
|
|
|
|
unsigned getMajor() const { return Major; }
|
|
|
|
unsigned getMinor() const { return Minor; }
|
|
|
|
|
|
|
|
/// \brief Creates a version from a string of the form \c major[.minor].
|
|
|
|
///
|
|
|
|
/// Note that any version component after \c minor is ignored.
|
|
|
|
///
|
|
|
|
/// \return A null version is returned on error.
|
|
|
|
static Version getFromString(llvm::StringRef VersionStr);
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned Major;
|
|
|
|
unsigned Minor;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Convenience structure to store the version of some compilers.
|
|
|
|
struct CompilerVersions {
|
|
|
|
Version Clang, Gcc, Icc, Msvc;
|
|
|
|
};
|
|
|
|
|
2013-07-24 22:24:33 +08:00
|
|
|
/// \brief A factory that can instantiate a specific transform.
|
|
|
|
///
|
2013-07-29 23:58:47 +08:00
|
|
|
/// Each transform should subclass this class and implement
|
|
|
|
/// \c createTransform().
|
|
|
|
///
|
|
|
|
/// In the sub-classed factory constructor, specify the earliest versions since
|
|
|
|
/// the compilers in \c CompilerVersions support the feature introduced by the
|
|
|
|
/// transform. See the example below.
|
|
|
|
///
|
|
|
|
/// Note that you should use \c TransformFactoryRegistry to register the
|
|
|
|
/// transform globally.
|
2013-07-24 22:24:33 +08:00
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
/// \code
|
|
|
|
/// class MyTransform : public Transform { ... };
|
|
|
|
///
|
|
|
|
/// struct MyFactory : TransformFactory {
|
2013-07-29 23:58:47 +08:00
|
|
|
/// MyFactory() {
|
|
|
|
/// Since.Clang = Version(3, 0);
|
|
|
|
/// Since.Gcc = Version(4, 7);
|
|
|
|
/// Since.Icc = Version(12);
|
|
|
|
/// 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 MyTransform(Opts);
|
|
|
|
/// }
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// // Register the factory using this statically initialized variable.
|
|
|
|
/// static TransformFactoryRegistry::Add<MyFactory>
|
|
|
|
/// X("my-transform", "<Short description of my transform>");
|
|
|
|
///
|
|
|
|
/// // This anchor is used to force the linker to link in the generated object
|
|
|
|
/// // file and thus register the factory.
|
|
|
|
/// volatile int MyTransformAnchorSource = 0;
|
|
|
|
/// \endcode
|
|
|
|
class TransformFactory {
|
|
|
|
public:
|
|
|
|
virtual ~TransformFactory();
|
|
|
|
virtual Transform *createTransform(const TransformOptions &) = 0;
|
2013-07-29 23:58:47 +08:00
|
|
|
|
|
|
|
/// \brief Whether the transform is supported by the required compilers or
|
|
|
|
/// not.
|
|
|
|
bool supportsCompilers(CompilerVersions Required) const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/// \brief Since when the C++11 feature introduced by this transform has been
|
|
|
|
/// available.
|
|
|
|
///
|
|
|
|
/// Can be set by the sub-class in the constructor body.
|
|
|
|
CompilerVersions Since;
|
2013-07-24 22:24:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef llvm::Registry<TransformFactory> TransformFactoryRegistry;
|
|
|
|
|
2014-07-03 22:12:37 +08:00
|
|
|
extern template class llvm::Registry<TransformFactory>;
|
|
|
|
|
2013-09-05 03:13:50 +08:00
|
|
|
#endif // CLANG_MODERNIZE_TRANSFORM_H
|