| 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 "RemoveCtorInitializer.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 tries to remove an initializer from a Ctor. \n"; |
| 27 | |
| 28 | static RegisterTransformation<RemoveCtorInitializer> |
| 29 | Trans("remove-ctor-initializer", DescriptionMsg); |
| 30 | |
| 31 | class RemoveCtorInitializerASTVisitor : public |
| 32 | RecursiveASTVisitor<RemoveCtorInitializerASTVisitor> { |
| 33 | |
| 34 | public: |
| 35 | explicit RemoveCtorInitializerASTVisitor( |
| 36 | RemoveCtorInitializer *Instance) |
| 37 | : ConsumerInstance(Instance) |
| 38 | { } |
| 39 | |
| 40 | bool VisitCXXConstructorDecl(CXXConstructorDecl *Ctor); |
| 41 | |
| 42 | private: |
| 43 | RemoveCtorInitializer *ConsumerInstance; |
| 44 | }; |
| 45 | |
| 46 | bool RemoveCtorInitializerASTVisitor::VisitCXXConstructorDecl( |
| 47 | CXXConstructorDecl *Ctor) |
| 48 | { |
| 49 | if (ConsumerInstance->isInIncludedFile(Ctor)) |
| 50 | return true; |
| 51 | |
| 52 | unsigned Idx = 0; |
| 53 | for (CXXConstructorDecl::init_const_iterator I = Ctor->init_begin(), |
| 54 | E = Ctor->init_end(); I != E; ++I) { |
| 55 | const CXXCtorInitializer *Init = (*I); |
| 56 | if (!Init->isWritten()) { |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | if (Init->isInClassMemberInitializer() || |
| 61 | Init->isPackExpansion() || |
| 62 | Init->isDelegatingInitializer()) { |
| 63 | Idx++; |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | if (const FieldDecl *Field = Init->getAnyMember()) { |
| 68 | const Type *Ty = Field->getType().getTypePtr(); |
| 69 | if (!ConsumerInstance->isValidType(Ty)) { |
| 70 | Idx++; |
| 71 | continue; |
| 72 | } |
| 73 | } |
| 74 | else if (const Type *Ty = Init->getBaseClass()) { |
| 75 | const CXXRecordDecl *Base = ConsumerInstance->getBaseDeclFromType(Ty); |
| 76 | if (Base && Base->hasDefinition() && |
| 77 | Base->needsImplicitDefaultConstructor()) { |
| 78 | Idx++; |
| 79 | continue; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | ConsumerInstance->ValidInstanceNum++; |
| 84 | if (ConsumerInstance->ValidInstanceNum == |
| 85 | ConsumerInstance->TransformationCounter) { |
| 86 | ConsumerInstance->TheInitializer = Init; |
| 87 | ConsumerInstance->TheCtorDecl = Ctor; |
| 88 | ConsumerInstance->TheIndex = Idx; |
| 89 | } |
| 90 | Idx++; |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | void RemoveCtorInitializer::Initialize(ASTContext &context) |
| 97 | { |
| 98 | Transformation::Initialize(context); |
| 99 | CollectionVisitor = new RemoveCtorInitializerASTVisitor(this); |
| 100 | } |
| 101 | |
| 102 | void RemoveCtorInitializer::HandleTranslationUnit(ASTContext &Ctx) |
| 103 | { |
| 104 | if (TransformationManager::isCLangOpt() || |
| 105 | TransformationManager::isOpenCLLangOpt()) { |
| 106 | ValidInstanceNum = 0; |
| 107 | } |
| 108 | else { |
| 109 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 110 | } |
| 111 | |
| 112 | if (QueryInstanceOnly) |
| 113 | return; |
| 114 | |
| 115 | if (TransformationCounter > ValidInstanceNum) { |
| 116 | TransError = TransMaxInstanceError; |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | TransAssert(TheCtorDecl && "TheCtorDecl is NULL!"); |
| 121 | TransAssert(TheInitializer && "TheInitializer is NULL!"); |
| 122 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 123 | |
| 124 | RewriteHelper->removeCXXCtorInitializer(TheInitializer, TheIndex, |
| 125 | getNumCtorWrittenInitializers(*TheCtorDecl)); |
| 126 | |
| 127 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 128 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 129 | TransError = TransInternalError; |
| 130 | } |
| 131 | |
| 132 | bool RemoveCtorInitializer::isValidType(const Type *Ty) |
| 133 | { |
| 134 | if (Ty->isReferenceType()) |
| 135 | return false; |
| 136 | |
| 137 | if (const RecordType *RTy = Ty->getAs<RecordType>()) { |
| 138 | const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RTy->getDecl()); |
| 139 | if (!CXXRD) |
| 140 | return true; |
| 141 | return !CXXRD->needsImplicitDefaultConstructor(); |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | RemoveCtorInitializer::~RemoveCtorInitializer(void) |
| 147 | { |
| 148 | delete CollectionVisitor; |
| 149 | } |
| 150 |