| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2017 The University of Utah |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See the file COPYING for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #if HAVE_CONFIG_H |
| 12 | # include <config.h> |
| 13 | #endif |
| 14 | |
| 15 | #include "SimplifyRecursiveTemplateInstantiation.h" |
| 16 | |
| 17 | #include "clang/Lex/Lexer.h" |
| 18 | #include "clang/AST/RecursiveASTVisitor.h" |
| 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
| 21 | |
| 22 | #include "TransformationManager.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | static const char *DescriptionMsg = |
| 27 | "This pass handles a special case where we have recursive template \ |
| 28 | instantiations. For example, given the code blow: \n\ |
| 29 | C<B<C<B<C<B<D>, G<int> > > \n\ |
| 30 | it will replace an outer instantiation of B with an inner one, e.g. B<D>.\n\ |
| 31 | The the number of transformation instances counts from inside to \n\ |
| 32 | outside. Currently we only handle class template instantiations. \n\ |
| 33 | "; |
| 34 | |
| 35 | static RegisterTransformation<SimplifyRecursiveTemplateInstantiation> |
| 36 | Trans("simplify-recursive-template-instantiation", DescriptionMsg); |
| 37 | |
| 38 | namespace { |
| 39 | class LocalTemplateArgVisitor : public |
| 40 | RecursiveASTVisitor<LocalTemplateArgVisitor> { |
| 41 | |
| 42 | public: |
| 43 | LocalTemplateArgVisitor(const TemplateSpecializationTypeLoc &TLoc, |
| 44 | SimplifyRecursiveTemplateInstantiation *Instance) |
| 45 | : ParentTLoc(TLoc), |
| 46 | ConsumerInstance(Instance) |
| 47 | { } |
| 48 | |
| 49 | bool VisitTemplateSpecializationTypeLoc( |
| 50 | TemplateSpecializationTypeLoc TLoc); |
| 51 | |
| 52 | private: |
| 53 | const TemplateSpecializationTypeLoc &ParentTLoc; |
| 54 | |
| 55 | SimplifyRecursiveTemplateInstantiation *ConsumerInstance; |
| 56 | }; |
| 57 | |
| 58 | bool LocalTemplateArgVisitor::VisitTemplateSpecializationTypeLoc( |
| 59 | TemplateSpecializationTypeLoc TLoc) |
| 60 | { |
| 61 | ConsumerInstance->handleInnerTemplateSpecializationTypeLoc(ParentTLoc, TLoc); |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | } // end anon namespace |
| 66 | |
| 67 | class SimplifyRecursiveTemplateInstantiationASTVisitor : public |
| 68 | RecursiveASTVisitor<SimplifyRecursiveTemplateInstantiationASTVisitor> { |
| 69 | |
| 70 | public: |
| 71 | explicit SimplifyRecursiveTemplateInstantiationASTVisitor( |
| 72 | SimplifyRecursiveTemplateInstantiation *Instance) |
| 73 | : ConsumerInstance(Instance) |
| 74 | { } |
| 75 | |
| 76 | bool VisitTemplateSpecializationTypeLoc( |
| 77 | TemplateSpecializationTypeLoc TLoc); |
| 78 | |
| 79 | private: |
| 80 | SimplifyRecursiveTemplateInstantiation *ConsumerInstance; |
| 81 | |
| 82 | }; |
| 83 | |
| 84 | bool SimplifyRecursiveTemplateInstantiationASTVisitor:: |
| 85 | VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TLoc) |
| 86 | { |
| 87 | ConsumerInstance->handleTemplateSpecializationTypeLoc(TLoc); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | void SimplifyRecursiveTemplateInstantiation::Initialize(ASTContext &context) |
| 92 | { |
| 93 | Transformation::Initialize(context); |
| 94 | CollectionVisitor = |
| 95 | new SimplifyRecursiveTemplateInstantiationASTVisitor(this); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | SimplifyRecursiveTemplateInstantiation::HandleTranslationUnit(ASTContext &Ctx) |
| 100 | { |
| 101 | if (TransformationManager::isCLangOpt() || |
| 102 | TransformationManager::isOpenCLLangOpt()) { |
| 103 | ValidInstanceNum = 0; |
| 104 | } |
| 105 | else { |
| 106 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 107 | analyzeLocPairs(); |
| 108 | } |
| 109 | |
| 110 | if (QueryInstanceOnly) |
| 111 | return; |
| 112 | |
| 113 | if (TransformationCounter > ValidInstanceNum) { |
| 114 | TransError = TransMaxInstanceError; |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 119 | rewriteTemplateArgument(); |
| 120 | |
| 121 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 122 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 123 | TransError = TransInternalError; |
| 124 | } |
| 125 | |
| 126 | void SimplifyRecursiveTemplateInstantiation::analyzeLocPairs() |
| 127 | { |
| 128 | for (SpecTypeLocPairQueue::reverse_iterator I = LocPairQueue.rbegin(), |
| 129 | E = LocPairQueue.rend(); I != E; ++I) { |
| 130 | ValidInstanceNum++; |
| 131 | if (ValidInstanceNum == TransformationCounter) |
| 132 | TheLocPair = (*I); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | SimplifyRecursiveTemplateInstantiation::handleTemplateSpecializationTypeLoc( |
| 138 | const TemplateSpecializationTypeLoc &TLoc) |
| 139 | { |
| 140 | if (isInIncludedFile(TLoc.getBeginLoc())) |
| 141 | return; |
| 142 | for (unsigned I = 0; I < TLoc.getNumArgs(); ++I) { |
| 143 | TemplateArgumentLoc ArgLoc = TLoc.getArgLoc(I); |
| 144 | if (ArgLoc.getLocation().isInvalid()) |
| 145 | continue; |
| 146 | LocalTemplateArgVisitor ArgVisitor(TLoc, this); |
| 147 | ArgVisitor.TraverseTemplateArgumentLoc(ArgLoc); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | const TemplateDecl * |
| 152 | SimplifyRecursiveTemplateInstantiation::getTemplateDeclFromSpecLoc( |
| 153 | const TemplateSpecializationTypeLoc &TLoc) |
| 154 | { |
| 155 | const Type *Ty = TLoc.getTypePtr(); |
| 156 | const TemplateSpecializationType *TST = |
| 157 | Ty->getAs<TemplateSpecializationType>(); |
| 158 | TemplateName TplName = TST->getTemplateName(); |
| 159 | return TplName.getAsTemplateDecl(); |
| 160 | } |
| 161 | |
| 162 | void SimplifyRecursiveTemplateInstantiation:: |
| 163 | handleInnerTemplateSpecializationTypeLoc( |
| 164 | const TemplateSpecializationTypeLoc &ParentTLoc, |
| 165 | const TemplateSpecializationTypeLoc &TLoc) |
| 166 | { |
| 167 | const TemplateDecl *ParentD = getTemplateDeclFromSpecLoc(ParentTLoc); |
| 168 | TransAssert(ParentD && "NULL ParentD!"); |
| 169 | const TemplateDecl *D = getTemplateDeclFromSpecLoc(TLoc); |
| 170 | TransAssert(D && "NULL Decl!"); |
| 171 | if (ParentD->getCanonicalDecl() != D->getCanonicalDecl()) |
| 172 | return; |
| 173 | SpecTypeLocPair *P = new SpecTypeLocPair(); |
| 174 | TransAssert(P && "Failed to alloc SpecTypeLocPair!"); |
| 175 | P->push_back(ParentTLoc); |
| 176 | P->push_back(TLoc); |
| 177 | LocPairQueue.push_back(P); |
| 178 | } |
| 179 | |
| 180 | void SimplifyRecursiveTemplateInstantiation::rewriteTemplateArgument() |
| 181 | { |
| 182 | TransAssert(TheLocPair && "NULL TheLocPair!"); |
| 183 | TransAssert((TheLocPair->size() == 2) && "Invalid size of TheLocPair!"); |
| 184 | TemplateSpecializationTypeLoc TLoc = TheLocPair->pop_back_val(); |
| 185 | TemplateSpecializationTypeLoc ParentTLoc = TheLocPair->pop_back_val(); |
| 186 | |
| 187 | SourceLocation LAngleLoc = getRealLocation(TLoc.getLAngleLoc()); |
| 188 | SourceLocation RAngleLoc = getRealLocation(TLoc.getRAngleLoc()); |
| 189 | SourceLocation ParentLAngleLoc = getRealLocation(ParentTLoc.getLAngleLoc()); |
| 190 | SourceLocation ParentRAngleLoc = getRealLocation(ParentTLoc.getRAngleLoc()); |
| 191 | std::string InnerStr = ""; |
| 192 | RewriteHelper->getStringBetweenLocs(InnerStr, LAngleLoc, RAngleLoc); |
| 193 | TheRewriter.ReplaceText(SourceRange(ParentLAngleLoc, ParentRAngleLoc), |
| 194 | InnerStr + ">"); |
| 195 | } |
| 196 | |
| 197 | SimplifyRecursiveTemplateInstantiation:: |
| 198 | ~SimplifyRecursiveTemplateInstantiation() |
| 199 | { |
| 200 | for (SpecTypeLocPairQueue::iterator I = LocPairQueue.begin(), |
| 201 | E = LocPairQueue.end(); I != E; ++I) { |
| 202 | delete (*I); |
| 203 | } |
| 204 | delete CollectionVisitor; |
| 205 | } |
| 206 |