| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifndef TRANSFORMATION_MANAGER_H |
| 12 |
#define TRANSFORMATION_MANAGER_H |
| 13 |
|
| 14 |
#include <string> |
| 15 |
#include <map> |
| 16 |
#include <cassert> |
| 17 |
|
| 18 |
#include "llvm/Support/raw_ostream.h" |
| 19 |
|
| 20 |
class Transformation; |
| 21 |
namespace clang { |
| 22 |
class CompilerInstance; |
| 23 |
class Preprocessor; |
| 24 |
} |
| 25 |
|
| 26 |
class TransformationManager { |
| 27 |
|
| 28 |
public: |
| 29 |
|
| 30 |
static TransformationManager *GetInstance(); |
| 31 |
|
| 32 |
static void Finalize(); |
| 33 |
|
| 34 |
static void registerTransformation(const char *TransName, |
| 35 |
Transformation *TransImpl); |
| 36 |
|
| 37 |
static bool isCXXLangOpt(); |
| 38 |
|
| 39 |
static bool isCLangOpt(); |
| 40 |
|
| 41 |
static bool isOpenCLLangOpt(); |
| 42 |
|
| 43 |
static clang::Preprocessor &getPreprocessor(); |
| 44 |
|
| 45 |
static int ErrorInvalidCounter; |
| 46 |
|
| 47 |
bool doTransformation(std::string &ErrorMsg, int &ErrorCode); |
| 48 |
|
| 49 |
bool verify(std::string &ErrorMsg, int &ErrorCode); |
| 50 |
|
| 51 |
int setTransformation(const std::string &Trans) { |
| 52 |
if (TransformationsMap.find(Trans.c_str()) == TransformationsMap.end()) |
| 53 |
return -1; |
| 54 |
CurrentTransName = Trans; |
| 55 |
CurrentTransformationImpl = TransformationsMap[Trans.c_str()]; |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
void setTransformationCounter(int Counter) { |
| 60 |
assert((Counter > 0) && "Bad Counter value!"); |
| 61 |
TransformationCounter = Counter; |
| 62 |
} |
| 63 |
|
| 64 |
void setToCounter(int Counter) { |
| 65 |
assert((Counter > 0) && "Bad to-counter value!"); |
| 66 |
ToCounter = Counter; |
| 67 |
} |
| 68 |
|
| 69 |
void setSrcFileName(const std::string &FileName) { |
| 70 |
assert(SrcFileName.empty() && "Could only process one file each time"); |
| 71 |
SrcFileName = FileName; |
| 72 |
} |
| 73 |
|
| 74 |
void setOutputFileName(const std::string &FileName) { |
| 75 |
OutputFileName = FileName; |
| 76 |
} |
| 77 |
|
| 78 |
void setReplacement(const std::string &Str) { |
| 79 |
Replacement = Str; |
| 80 |
DoReplacement = true; |
| 81 |
} |
| 82 |
|
| 83 |
void setReferenceValue(const std::string &Str) { |
| 84 |
ReferenceValue = Str; |
| 85 |
CheckReference = true; |
| 86 |
} |
| 87 |
|
| 88 |
void setQueryInstanceFlag(bool Flag) { |
| 89 |
QueryInstanceOnly = Flag; |
| 90 |
} |
| 91 |
|
| 92 |
bool getQueryInstanceFlag() { |
| 93 |
return QueryInstanceOnly; |
| 94 |
} |
| 95 |
|
| 96 |
bool initializeCompilerInstance(std::string &ErrorMsg); |
| 97 |
|
| 98 |
void outputNumTransformationInstances(); |
| 99 |
|
| 100 |
void printTransformations(); |
| 101 |
|
| 102 |
void printTransformationNames(); |
| 103 |
|
| 104 |
private: |
| 105 |
|
| 106 |
TransformationManager(); |
| 107 |
|
| 108 |
~TransformationManager(); |
| 109 |
|
| 110 |
llvm::raw_ostream *getOutStream(); |
| 111 |
|
| 112 |
void closeOutStream(llvm::raw_ostream *OutStream); |
| 113 |
|
| 114 |
static TransformationManager *Instance; |
| 115 |
|
| 116 |
static std::map<std::string, Transformation *> *TransformationsMapPtr; |
| 117 |
|
| 118 |
std::map<std::string, Transformation *> TransformationsMap; |
| 119 |
|
| 120 |
Transformation *CurrentTransformationImpl; |
| 121 |
|
| 122 |
int TransformationCounter; |
| 123 |
|
| 124 |
int ToCounter; |
| 125 |
|
| 126 |
std::string SrcFileName; |
| 127 |
|
| 128 |
std::string OutputFileName; |
| 129 |
|
| 130 |
std::string CurrentTransName; |
| 131 |
|
| 132 |
clang::CompilerInstance *ClangInstance; |
| 133 |
|
| 134 |
bool QueryInstanceOnly; |
| 135 |
|
| 136 |
bool DoReplacement; |
| 137 |
|
| 138 |
std::string Replacement; |
| 139 |
|
| 140 |
bool CheckReference; |
| 141 |
|
| 142 |
std::string ReferenceValue; |
| 143 |
|
| 144 |
|
| 145 |
TransformationManager(const TransformationManager &); |
| 146 |
|
| 147 |
void operator=(const TransformationManager&); |
| 148 |
|
| 149 |
}; |
| 150 |
|
| 151 |
template<typename TransformationClass> |
| 152 |
class RegisterTransformation { |
| 153 |
|
| 154 |
public: |
| 155 |
RegisterTransformation(const char *TransName, const char *Desc) { |
| 156 |
Transformation *TransImpl = new TransformationClass(TransName, Desc); |
| 157 |
assert(TransImpl && "Fail to create TransformationClass"); |
| 158 |
|
| 159 |
TransformationManager::registerTransformation(TransName, TransImpl); |
| 160 |
} |
| 161 |
|
| 162 |
private: |
| 163 |
|
| 164 |
RegisterTransformation(const RegisterTransformation &); |
| 165 |
|
| 166 |
void operator=(const RegisterTransformation &); |
| 167 |
|
| 168 |
}; |
| 169 |
|
| 170 |
#endif |