| 1 | //===----------------------------------------------------------------------===// |
| 2 | // Copyright (c) 2016, 2019 The University of Utah |
| 3 | // Copyright (c) 2016 Ori Brostovski <[email protected]> |
| 4 | // Copyright (c) 2016 Ceemple Software Ltd |
| 5 | // All rights reserved. |
| 6 | // |
| 7 | // This file is distributed under the University of Illinois Open Source |
| 8 | // License. See the file COPYING for details. |
| 9 | // |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | // Here is a quick command line for testing: |
| 13 | // ./clang_delta --transformation=vector-to-array ~/empty.cpp |
| 14 | // --counter=1./clang_delta --transformation=vector-to-array ~/empty.cpp |
| 15 | // --counter=1 |
| 16 | |
| 17 | // TODO: |
| 18 | // pointer/array of vector |
| 19 | // dependent type vector |
| 20 | // vector used before class definition (lesser priotity, not common) |
| 21 | // vector not inside std:: |
| 22 | // all scopes, global, static, member, local, threadsafe, whatever |
| 23 | // vector class without params |
| 24 | // other templates not changed |
| 25 | |
| 26 | #if HAVE_CONFIG_H |
| 27 | # include <config.h> |
| 28 | #endif |
| 29 | |
| 30 | #include "VectorToArray.h" |
| 31 | |
| 32 | #include "clang/AST/ASTContext.h" |
| 33 | #include "clang/AST/RecursiveASTVisitor.h" |
| 34 | #include "clang/Basic/SourceManager.h" |
| 35 | |
| 36 | #include "TransformationManager.h" |
| 37 | |
| 38 | #include <cctype> |
| 39 | |
| 40 | #define ARRAY_SUFFIX "[10]" |
| 41 | |
| 42 | using namespace clang; |
| 43 | |
| 44 | static const char *DescriptionMsg = |
| 45 | "Try replacing vectors with arrays. It transforms the following code: \n\ |
| 46 | vector<int> v' \n\ |
| 47 | to \n\ |
| 48 | int v[10]\n"; |
| 49 | |
| 50 | static RegisterTransformation<VectorToArray> Trans("vector-to-array", |
| 51 | DescriptionMsg); |
| 52 | |
| 53 | class VectorToArrayCollectionVisitor |
| 54 | : public RecursiveASTVisitor<VectorToArrayCollectionVisitor> { |
| 55 | public: |
| 56 | explicit VectorToArrayCollectionVisitor(VectorToArray *Instance) |
| 57 | : Consumer(Instance) {} |
| 58 | |
| 59 | bool VisitNamedDecl(NamedDecl *D) { return true; } |
| 60 | |
| 61 | bool VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 62 | if (Consumer->isInIncludedFile(D)) |
| 63 | return true; |
| 64 | auto *NS = dyn_cast<NamespaceDecl>(D->getDeclContext()); |
| 65 | if (!NS) |
| 66 | return true; |
| 67 | if (NS->getName() != "std") |
| 68 | return true; |
| 69 | if (!dyn_cast<TranslationUnitDecl>(NS->getDeclContext())) |
| 70 | return true; |
| 71 | if (!D->isThisDeclarationADefinition()) |
| 72 | return true; |
| 73 | if (D->getName() != "vector") |
| 74 | return true; |
| 75 | |
| 76 | Consumer->TheVectorDecl = D; |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | bool VisitVarDecl(VarDecl *D) { |
| 81 | if (Consumer->isInIncludedFile(D)) |
| 82 | return true; |
| 83 | const Type *Ty = D->getType().getTypePtr(); |
| 84 | if (!Ty) |
| 85 | return true; |
| 86 | CXXRecordDecl *CXXRD = Ty->getAsCXXRecordDecl(); |
| 87 | if (!CXXRD) |
| 88 | return true; |
| 89 | CXXRecordDecl *CXXRDT = CXXRD->getTemplateInstantiationPattern(); |
| 90 | if (!CXXRDT) |
| 91 | return true; |
| 92 | ClassTemplateDecl *CTD = CXXRDT->getDescribedClassTemplate(); |
| 93 | while (CTD && !CTD->isThisDeclarationADefinition()) { |
| 94 | CTD = CTD->getPreviousDecl(); |
| 95 | } |
| 96 | if (!CTD || CTD != Consumer->TheVectorDecl) |
| 97 | return true; |
| 98 | ++Consumer->ValidInstanceNum; |
| 99 | if (Consumer->ValidInstanceNum == Consumer->TransformationCounter) |
| 100 | Consumer->TheVarDecl = D; |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | VectorToArray *Consumer; |
| 106 | }; |
| 107 | |
| 108 | class VectorToArrayRewriteVisitor |
| 109 | : public RecursiveASTVisitor<VectorToArrayRewriteVisitor> { |
| 110 | private: |
| 111 | std::string getVectorElemTypeName(VarDecl *VD) { |
| 112 | CXXRecordDecl *CXXRD = VD->getType().getTypePtr()->getAsCXXRecordDecl(); |
| 113 | auto CTSD = dyn_cast<ClassTemplateSpecializationDecl>(CXXRD); |
| 114 | const TemplateArgument &TmplArg = CTSD->getTemplateArgs()[0]; |
| 115 | return TmplArg.getAsType().getAsString(); |
| 116 | } |
| 117 | |
| 118 | public: |
| 119 | bool VisitVarDecl(VarDecl *VD) { |
| 120 | if (VD != Consumer->TheVarDecl) |
| 121 | return true; |
| 122 | |
| 123 | Rewriter &TheRewriter = Consumer->TheRewriter; |
| 124 | const SourceManager &SM = TheRewriter.getSourceMgr(); |
| 125 | |
| 126 | SourceLocation NameLoc = VD->getLocation(); |
| 127 | IdentifierInfo *IdInfo = VD->getIdentifier(); |
| 128 | assert(IdInfo && "Nameless variable"); |
| 129 | unsigned NameLength = IdInfo->getLength(); |
| 130 | assert(NameLength && "Variable name has no length"); |
| 131 | |
| 132 | SourceLocation TypeLocStart = VD->getBeginLoc(); |
| 133 | const std::string ElemTypeName = getVectorElemTypeName(VD); |
| 134 | |
| 135 | SourceLocation NameLocM1 = NameLoc.getLocWithOffset(-1); |
| 136 | bool isInvalid = false; |
| 137 | const char *charBeforeName = SM.getCharacterData(NameLocM1, &isInvalid); |
| 138 | assert(!isInvalid && "failed to get char before name"); |
| 139 | |
| 140 | TheRewriter.ReplaceText(NameLoc.getLocWithOffset(NameLength), 0, |
| 141 | ARRAY_SUFFIX); |
| 142 | if (!std::isspace(*charBeforeName)) |
| 143 | TheRewriter.InsertText(NameLoc, " "); |
| 144 | TheRewriter.ReplaceText(SourceRange(TypeLocStart, NameLocM1), ElemTypeName); |
| 145 | |
| 146 | return true; |
| 147 | } |
| 148 | explicit VectorToArrayRewriteVisitor(VectorToArray *Cons) : Consumer(Cons) {} |
| 149 | |
| 150 | private: |
| 151 | VectorToArray *Consumer; |
| 152 | }; |
| 153 | |
| 154 | VectorToArray::~VectorToArray() { |
| 155 | delete RewriteVisitor; |
| 156 | delete CollectionVisitor; |
| 157 | } |
| 158 | |
| 159 | void VectorToArray::Initialize(ASTContext &ctx) { |
| 160 | Transformation::Initialize(ctx); |
| 161 | CollectionVisitor = new VectorToArrayCollectionVisitor(this); |
| 162 | RewriteVisitor = new VectorToArrayRewriteVisitor(this); |
| 163 | } |
| 164 | |
| 165 | void VectorToArray::HandleTranslationUnit(ASTContext &ctx) { |
| 166 | |
| 167 | CollectionVisitor->TraverseDecl(ctx.getTranslationUnitDecl()); |
| 168 | if (QueryInstanceOnly) |
| 169 | return; |
| 170 | |
| 171 | if (TransformationCounter > ValidInstanceNum) { |
| 172 | TransError = TransMaxInstanceError; |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | TransAssert(RewriteVisitor && "NULL RewriteVisitor!"); |
| 177 | TransAssert(TheVectorDecl && "NULL TheRecordDecl!"); |
| 178 | TransAssert(TheVarDecl && "NULL TheVarDecl!"); |
| 179 | // RewriteVisitor->TraverseDecl(ctx.getTranslationUnitDecl()); |
| 180 | // ORI NOTE: This is much more efficient, but will not work if we want |
| 181 | // xto do more than one change |
| 182 | RewriteVisitor->VisitVarDecl(TheVarDecl); |
| 183 | |
| 184 | if (ctx.getDiagnostics().hasErrorOccurred() || |
| 185 | ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 186 | TransError = TransInternalError; |
| 187 | } |