| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2017, 2018 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 "ReplaceDerivedClass.h" |
| 16 | |
| 17 | #include "clang/AST/ASTContext.h" |
| 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "CommonRenameClassRewriteVisitor.h" |
| 20 | #include "TransformationManager.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | using namespace clang_delta_common_visitor; |
| 24 | |
| 25 | static const char *DescriptionMsg = |
| 26 | "Replace a derived class with one of its base classes if \n\ |
| 27 | * the derived class is empty; \n\ |
| 28 | * and if the base class and the derived class are both class templates, \ |
| 29 | they require the same number of arguments for instantiation. \n"; |
| 30 | |
| 31 | static RegisterTransformation<ReplaceDerivedClass> |
| 32 | Trans("replace-derived-class", DescriptionMsg); |
| 33 | |
| 34 | class ReplaceDerivedClassASTVisitor : public |
| 35 | RecursiveASTVisitor<ReplaceDerivedClassASTVisitor> { |
| 36 | |
| 37 | public: |
| 38 | explicit ReplaceDerivedClassASTVisitor(ReplaceDerivedClass *Instance) |
| 39 | : ConsumerInstance(Instance) |
| 40 | { } |
| 41 | |
| 42 | bool VisitCXXRecordDecl(CXXRecordDecl *CXXRD); |
| 43 | |
| 44 | private: |
| 45 | ReplaceDerivedClass *ConsumerInstance; |
| 46 | }; |
| 47 | |
| 48 | class ReplaceDerivedClassRewriteVisitor : public |
| 49 | CommonRenameClassRewriteVisitor<ReplaceDerivedClassRewriteVisitor> |
| 50 | { |
| 51 | public: |
| 52 | ReplaceDerivedClassRewriteVisitor(Transformation *Instance, |
| 53 | Rewriter *RT, |
| 54 | RewriteUtils *Helper, |
| 55 | const CXXRecordDecl *CXXRD, |
| 56 | const std::string &Name) |
| 57 | : CommonRenameClassRewriteVisitor<ReplaceDerivedClassRewriteVisitor> |
| 58 | (Instance, RT, Helper, CXXRD, Name) |
| 59 | { } |
| 60 | }; |
| 61 | |
| 62 | bool ReplaceDerivedClassASTVisitor::VisitCXXRecordDecl(CXXRecordDecl *CXXRD) |
| 63 | { |
| 64 | ConsumerInstance->handleOneCXXRecordDecl(CXXRD); |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | void ReplaceDerivedClass::Initialize(ASTContext &context) |
| 69 | { |
| 70 | Transformation::Initialize(context); |
| 71 | CollectionVisitor = new ReplaceDerivedClassASTVisitor(this); |
| 72 | } |
| 73 | |
| 74 | void ReplaceDerivedClass::HandleTranslationUnit(ASTContext &Ctx) |
| 75 | { |
| 76 | if (TransformationManager::isCLangOpt() || |
| 77 | TransformationManager::isOpenCLLangOpt()) { |
| 78 | ValidInstanceNum = 0; |
| 79 | } |
| 80 | else { |
| 81 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 82 | } |
| 83 | |
| 84 | if (QueryInstanceOnly) |
| 85 | return; |
| 86 | |
| 87 | if (TransformationCounter > ValidInstanceNum) { |
| 88 | TransError = TransMaxInstanceError; |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | TransAssert(TheDerivedClass && "TheDerivedClass is NULL!"); |
| 93 | TransAssert(TheBaseClass && "TheBaseClass is NULL!"); |
| 94 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 95 | |
| 96 | RewriteVisitor = |
| 97 | new ReplaceDerivedClassRewriteVisitor(this, &TheRewriter, RewriteHelper, |
| 98 | TheDerivedClass->getCanonicalDecl(), |
| 99 | TheBaseClass->getNameAsString()); |
| 100 | TransAssert(RewriteVisitor && "NULL RewriteVisitor!"); |
| 101 | RewriteVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 102 | doRewrite(); |
| 103 | |
| 104 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 105 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 106 | TransError = TransInternalError; |
| 107 | } |
| 108 | |
| 109 | bool ReplaceDerivedClass::isValidBaseDerivedPair(const CXXRecordDecl *Base, |
| 110 | const CXXRecordDecl *Derived) |
| 111 | { |
| 112 | const ClassTemplateDecl *BaseTmplD = Base->getDescribedClassTemplate(); |
| 113 | const ClassTemplateDecl *DerivedTmplD = Derived->getDescribedClassTemplate(); |
| 114 | |
| 115 | if (!BaseTmplD && !DerivedTmplD) { |
| 116 | return true; |
| 117 | } |
| 118 | else if (BaseTmplD && DerivedTmplD) { |
| 119 | return (BaseTmplD->getTemplateParameters()->getMinRequiredArguments() == |
| 120 | DerivedTmplD->getTemplateParameters()->getMinRequiredArguments()); |
| 121 | } |
| 122 | |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | bool ReplaceDerivedClass::isEmptyClass(const CXXRecordDecl *CXXDef) |
| 127 | { |
| 128 | TransAssert(CXXDef->isThisDeclarationADefinition() && |
| 129 | "CXXDef must be a definition!"); |
| 130 | const DeclContext *Ctx = dyn_cast<DeclContext>(CXXDef); |
| 131 | TransAssert(Ctx && "Invalid DeclContext!"); |
| 132 | for (DeclContext::decl_iterator I = Ctx->decls_begin(), |
| 133 | E = Ctx->decls_end(); I != E; ++I) { |
| 134 | if (!(*I)->isImplicit()) |
| 135 | return false; |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | void ReplaceDerivedClass::handleOneCXXRecordDecl(const CXXRecordDecl *CXXRD) |
| 141 | { |
| 142 | if (isInIncludedFile(CXXRD)) |
| 143 | return; |
| 144 | const CXXRecordDecl *CXXDef = CXXRD->getDefinition(); |
| 145 | if (!CXXDef) |
| 146 | return; |
| 147 | |
| 148 | if (dyn_cast<ClassTemplateSpecializationDecl>(CXXRD)) |
| 149 | return; |
| 150 | if ((CXXDef->getNumBases() == 0) || !isEmptyClass(CXXDef)) |
| 151 | return; |
| 152 | |
| 153 | const CXXRecordDecl *CanonicalRD = CXXRD->getCanonicalDecl(); |
| 154 | if (VisitedCXXRecordDecls.count(CanonicalRD)) |
| 155 | return; |
| 156 | |
| 157 | VisitedCXXRecordDecls.insert(CanonicalRD); |
| 158 | for (CXXRecordDecl::base_class_const_iterator I = |
| 159 | CanonicalRD->bases_begin(), E = CanonicalRD->bases_end(); I != E; ++I) { |
| 160 | const CXXBaseSpecifier *BS = I; |
| 161 | const Type *Ty = BS->getType().getTypePtr(); |
| 162 | const CXXRecordDecl *Base = getBaseDeclFromType(Ty); |
| 163 | if (!Base || !isValidBaseDerivedPair(Base, CanonicalRD)) |
| 164 | continue; |
| 165 | |
| 166 | ValidInstanceNum++; |
| 167 | if (ValidInstanceNum == TransformationCounter) { |
| 168 | TheBaseClass = Base; |
| 169 | TheDerivedClass = CanonicalRD; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void ReplaceDerivedClass::doRewrite(void) |
| 175 | { |
| 176 | if (const ClassTemplateDecl *TmplD = |
| 177 | TheDerivedClass->getDescribedClassTemplate()) { |
| 178 | RewriteHelper->removeClassTemplateDecls(TmplD); |
| 179 | } |
| 180 | else { |
| 181 | if (!isDeclaringRecordDecl(TheDerivedClass)) { |
| 182 | RewriteHelper->removeClassDecls(TheDerivedClass); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | ReplaceDerivedClass::~ReplaceDerivedClass(void) |
| 188 | { |
| 189 | delete CollectionVisitor; |
| 190 | delete RewriteVisitor; |
| 191 | } |
| 192 |