| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2016, 2017, 2019 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 "RemoveUnusedOuterClass.h" |
| 16 | |
| 17 | #include "clang/Basic/SourceManager.h" |
| 18 | #include "clang/AST/RecursiveASTVisitor.h" |
| 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/Lex/Lexer.h" |
| 21 | |
| 22 | #include "TransformationManager.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | static const char *DescriptionMsg = |
| 27 | "This pass an unused outer class if \n\ |
| 28 | * the outer class doesn't have any base class, and \n\ |
| 29 | * the outer class does not have any described template, and \n\ |
| 30 | * the outer class is not been referenced \n\ |
| 31 | "; |
| 32 | |
| 33 | static RegisterTransformation<RemoveUnusedOuterClass> |
| 34 | Trans("remove-unused-outer-class", DescriptionMsg); |
| 35 | |
| 36 | class RemoveUnusedOuterClassVisitor : public |
| 37 | RecursiveASTVisitor<RemoveUnusedOuterClassVisitor> { |
| 38 | |
| 39 | public: |
| 40 | explicit RemoveUnusedOuterClassVisitor( |
| 41 | RemoveUnusedOuterClass *Instance) |
| 42 | : ConsumerInstance(Instance) |
| 43 | { } |
| 44 | |
| 45 | bool VisitCXXRecordDecl(CXXRecordDecl *CXXRD); |
| 46 | |
| 47 | bool VisitRecordTypeLoc(RecordTypeLoc TLoc); |
| 48 | |
| 49 | private: |
| 50 | RemoveUnusedOuterClass *ConsumerInstance; |
| 51 | }; |
| 52 | |
| 53 | bool RemoveUnusedOuterClassVisitor::VisitRecordTypeLoc(RecordTypeLoc TLoc) |
| 54 | { |
| 55 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TLoc.getDecl()); |
| 56 | ConsumerInstance->UsedCXXRDSet.insert(RD->getCanonicalDecl()); |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | bool RemoveUnusedOuterClassVisitor::VisitCXXRecordDecl( |
| 61 | CXXRecordDecl *CXXRD) |
| 62 | { |
| 63 | if (ConsumerInstance->isInIncludedFile(CXXRD) || |
| 64 | ConsumerInstance->isSpecialRecordDecl(CXXRD) || |
| 65 | !CXXRD->hasDefinition() || |
| 66 | dyn_cast<ClassTemplateSpecializationDecl>(CXXRD) || |
| 67 | CXXRD->hasUserDeclaredConstructor() || |
| 68 | CXXRD->hasUserDeclaredDestructor() || |
| 69 | CXXRD->getDescribedClassTemplate() || |
| 70 | CXXRD->getNumBases()) |
| 71 | return true; |
| 72 | ConsumerInstance->CXXRDDefSet.insert(CXXRD->getDefinition()); |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | void RemoveUnusedOuterClass::Initialize(ASTContext &context) |
| 77 | { |
| 78 | Transformation::Initialize(context); |
| 79 | CollectionVisitor = new RemoveUnusedOuterClassVisitor(this); |
| 80 | } |
| 81 | |
| 82 | void RemoveUnusedOuterClass::HandleTranslationUnit(ASTContext &Ctx) |
| 83 | { |
| 84 | if (TransformationManager::isCLangOpt() || |
| 85 | TransformationManager::isOpenCLLangOpt()) { |
| 86 | ValidInstanceNum = 0; |
| 87 | } |
| 88 | else { |
| 89 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 90 | analyzeCXXRDSet(); |
| 91 | } |
| 92 | |
| 93 | if (QueryInstanceOnly) |
| 94 | return; |
| 95 | |
| 96 | if (TransformationCounter > ValidInstanceNum) { |
| 97 | TransError = TransMaxInstanceError; |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 102 | removeOuterClass(); |
| 103 | |
| 104 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 105 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 106 | TransError = TransInternalError; |
| 107 | } |
| 108 | |
| 109 | void RemoveUnusedOuterClass::analyzeCXXRDSet() |
| 110 | { |
| 111 | for (CXXRecordDeclSetVector::iterator I = CXXRDDefSet.begin(), |
| 112 | E = CXXRDDefSet.end(); I != E; ++I) { |
| 113 | const CXXRecordDecl *Def = (*I); |
| 114 | if (UsedCXXRDSet.count(Def->getCanonicalDecl())) |
| 115 | continue; |
| 116 | ValidInstanceNum++; |
| 117 | if (ValidInstanceNum == TransformationCounter) |
| 118 | TheCXXRDDef = Def; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void RemoveUnusedOuterClass::removeOuterClass() |
| 123 | { |
| 124 | TransAssert(TheCXXRDDef && "NULL Base CXXRD!"); |
| 125 | SourceLocation LocStart = TheCXXRDDef->getBeginLoc(); |
| 126 | SourceLocation LocEnd = |
| 127 | RewriteHelper->getEndLocationUntil(LocStart, '{'); |
| 128 | TransAssert(LocEnd.isValid() && "Invalid Location!"); |
| 129 | TheRewriter.RemoveText(SourceRange(LocStart, LocEnd)); |
| 130 | |
| 131 | const DeclContext *Ctx = dyn_cast<DeclContext>(TheCXXRDDef); |
| 132 | for (DeclContext::decl_iterator I = Ctx->decls_begin(), |
| 133 | E = Ctx->decls_end(); I != E; ++I) { |
| 134 | if ((*I)->isImplicit()) |
| 135 | continue; |
| 136 | const AccessSpecDecl *AS = dyn_cast<AccessSpecDecl>(*I); |
| 137 | if (!AS) |
| 138 | continue; |
| 139 | TheRewriter.RemoveText(AS->getSourceRange()); |
| 140 | } |
| 141 | |
| 142 | LocStart = TheCXXRDDef->getBraceRange().getEnd(); |
| 143 | LocEnd = RewriteHelper->getLocationUntil(LocStart, ';'); |
| 144 | if (LocStart.isInvalid() || LocEnd.isInvalid()) |
| 145 | return; |
| 146 | |
| 147 | TheRewriter.RemoveText(SourceRange(LocStart, LocEnd)); |
| 148 | } |
| 149 | |
| 150 | RemoveUnusedOuterClass::~RemoveUnusedOuterClass(void) |
| 151 | { |
| 152 | delete CollectionVisitor; |
| 153 | } |
| 154 |