| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2016, 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 "RemoveTrivialBaseTemplate.h" |
| 16 | |
| 17 | #include "clang/Basic/SourceManager.h" |
| 18 | #include "clang/AST/RecursiveASTVisitor.h" |
| 19 | #include "clang/AST/ASTContext.h" |
| 20 | |
| 21 | #include "TransformationManager.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | static const char *DescriptionMsg = |
| 26 | "This pass removes a base class if it is an instantiation from a class \ |
| 27 | template which doesn't have definition. \n"; |
| 28 | |
| 29 | static RegisterTransformation<RemoveTrivialBaseTemplate> |
| 30 | Trans("remove-trivial-base-template", DescriptionMsg); |
| 31 | |
| 32 | class RemoveTrivialBaseTemplateBaseVisitor : public |
| 33 | RecursiveASTVisitor<RemoveTrivialBaseTemplateBaseVisitor> { |
| 34 | |
| 35 | public: |
| 36 | explicit RemoveTrivialBaseTemplateBaseVisitor( |
| 37 | RemoveTrivialBaseTemplate *Instance) |
| 38 | : ConsumerInstance(Instance) |
| 39 | { } |
| 40 | |
| 41 | bool VisitCXXRecordDecl(CXXRecordDecl *CXXRD); |
| 42 | |
| 43 | private: |
| 44 | RemoveTrivialBaseTemplate *ConsumerInstance; |
| 45 | }; |
| 46 | |
| 47 | bool RemoveTrivialBaseTemplateBaseVisitor::VisitCXXRecordDecl( |
| 48 | CXXRecordDecl *CXXRD) |
| 49 | { |
| 50 | ConsumerInstance->handleOneCXXRecordDecl(CXXRD); |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | void RemoveTrivialBaseTemplate::Initialize(ASTContext &context) |
| 55 | { |
| 56 | Transformation::Initialize(context); |
| 57 | CollectionVisitor = new RemoveTrivialBaseTemplateBaseVisitor(this); |
| 58 | } |
| 59 | |
| 60 | void RemoveTrivialBaseTemplate::HandleTranslationUnit(ASTContext &Ctx) |
| 61 | { |
| 62 | if (TransformationManager::isCLangOpt() || |
| 63 | TransformationManager::isOpenCLLangOpt()) { |
| 64 | ValidInstanceNum = 0; |
| 65 | } |
| 66 | else { |
| 67 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 68 | } |
| 69 | |
| 70 | if (QueryInstanceOnly) |
| 71 | return; |
| 72 | |
| 73 | if (TransformationCounter > ValidInstanceNum) { |
| 74 | TransError = TransMaxInstanceError; |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | TransAssert(TheDerivedClass && "TheDerivedClass is NULL!"); |
| 79 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 80 | |
| 81 | removeBaseSpecifier(); |
| 82 | |
| 83 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 84 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 85 | TransError = TransInternalError; |
| 86 | } |
| 87 | |
| 88 | void RemoveTrivialBaseTemplate::handleOneCXXRecordDecl( |
| 89 | const CXXRecordDecl *CXXRD) |
| 90 | { |
| 91 | if (isInIncludedFile(CXXRD) || isSpecialRecordDecl(CXXRD) || |
| 92 | !CXXRD->hasDefinition()) |
| 93 | return; |
| 94 | |
| 95 | const CXXRecordDecl *CanonicalRD = CXXRD->getCanonicalDecl(); |
| 96 | if (VisitedCXXRecordDecls.count(CanonicalRD)) |
| 97 | return; |
| 98 | VisitedCXXRecordDecls.insert(CanonicalRD); |
| 99 | unsigned Idx = 0; |
| 100 | for (CXXRecordDecl::base_class_const_iterator I = CanonicalRD->bases_begin(), |
| 101 | E = CanonicalRD->bases_end(); I != E; ++I, ++Idx) { |
| 102 | |
| 103 | const CXXBaseSpecifier *BS = I; |
| 104 | const Type *Ty = BS->getType().getTypePtr(); |
| 105 | const CXXRecordDecl *Base = getBaseDeclFromType(Ty); |
| 106 | if (!Base || getNumExplicitDecls(Base)) { |
| 107 | continue; |
| 108 | } |
| 109 | const ClassTemplateDecl *TmplD = Base->getDescribedClassTemplate(); |
| 110 | if (!TmplD) |
| 111 | continue; |
| 112 | ValidInstanceNum++; |
| 113 | if (ValidInstanceNum == TransformationCounter) { |
| 114 | TheDerivedClass = CanonicalRD; |
| 115 | ThePos = Idx; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void RemoveTrivialBaseTemplate::removeBaseSpecifier(void) |
| 121 | { |
| 122 | unsigned NumBases = TheDerivedClass->getNumBases(); |
| 123 | TransAssert((NumBases >= 1) && "TheDerivedClass doesn't have any base!"); |
| 124 | if (NumBases == 1) { |
| 125 | SourceLocation StartLoc = TheDerivedClass->getLocation(); |
| 126 | StartLoc = RewriteHelper->getLocationUntil(StartLoc, ':'); |
| 127 | SourceLocation EndLoc = RewriteHelper->getLocationUntil(StartLoc, '{'); |
| 128 | EndLoc = EndLoc.getLocWithOffset(-1); |
| 129 | |
| 130 | TheRewriter.RemoveText(SourceRange(StartLoc, EndLoc)); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | TransAssert((ThePos < NumBases) && "Invalid ThePos for the base specifier!"); |
| 135 | CXXRecordDecl::base_class_const_iterator I = TheDerivedClass->bases_begin(); |
| 136 | if (ThePos == 0) { |
| 137 | RewriteHelper->removeTextUntil((*I).getSourceRange(), ','); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | ++I; |
| 142 | CXXRecordDecl::base_class_const_iterator E = TheDerivedClass->bases_end(); |
| 143 | unsigned Idx = 1; |
| 144 | for (; I != E; ++I, ++Idx) { |
| 145 | if (Idx == ThePos) { |
| 146 | SourceRange Range = (*I).getSourceRange(); |
| 147 | SourceLocation EndLoc = RewriteHelper->getEndLocationFromBegin(Range); |
| 148 | RewriteHelper->removeTextFromLeftAt(Range, ',', EndLoc); |
| 149 | return; |
| 150 | } |
| 151 | } |
| 152 | TransAssert(0 && "Unreachable code!"); |
| 153 | } |
| 154 | |
| 155 | RemoveTrivialBaseTemplate::~RemoveTrivialBaseTemplate(void) |
| 156 | { |
| 157 | delete CollectionVisitor; |
| 158 | } |
| 159 |