| 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 "SimplifyNestedClass.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 tries to simplify nested classes by replacing the \ |
| 28 | outer class with the inner class, if \n\ |
| 29 | * the outer class doesn't have any base class, and \n\ |
| 30 | * the outer class has only one inner class definition, and \n\ |
| 31 | * the outer class does not have any described template, and \n\ |
| 32 | * the outer class does not have any other declarations except \ |
| 33 | the inner class \n\ |
| 34 | "; |
| 35 | |
| 36 | static RegisterTransformation<SimplifyNestedClass> |
| 37 | Trans("simplify-nested-class", DescriptionMsg); |
| 38 | |
| 39 | class SimplifyNestedClassVisitor : public |
| 40 | RecursiveASTVisitor<SimplifyNestedClassVisitor> { |
| 41 | |
| 42 | public: |
| 43 | explicit SimplifyNestedClassVisitor( |
| 44 | SimplifyNestedClass *Instance) |
| 45 | : ConsumerInstance(Instance) |
| 46 | { } |
| 47 | |
| 48 | bool VisitCXXRecordDecl(CXXRecordDecl *CXXRD); |
| 49 | |
| 50 | private: |
| 51 | SimplifyNestedClass *ConsumerInstance; |
| 52 | }; |
| 53 | |
| 54 | bool SimplifyNestedClassVisitor::VisitCXXRecordDecl( |
| 55 | CXXRecordDecl *CXXRD) |
| 56 | { |
| 57 | if (ConsumerInstance->isInIncludedFile(CXXRD) || |
| 58 | ConsumerInstance->isSpecialRecordDecl(CXXRD) || !CXXRD->hasDefinition()) |
| 59 | return true; |
| 60 | ConsumerInstance->handleOneCXXRecordDecl(CXXRD->getDefinition()); |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | class SimplifyNestedClassRewriteVisitor : public |
| 65 | RecursiveASTVisitor<SimplifyNestedClassRewriteVisitor> { |
| 66 | public: |
| 67 | explicit SimplifyNestedClassRewriteVisitor( |
| 68 | SimplifyNestedClass *Instance) |
| 69 | : ConsumerInstance(Instance) |
| 70 | { } |
| 71 | |
| 72 | bool VisitRecordTypeLoc(RecordTypeLoc TLoc); |
| 73 | |
| 74 | private: |
| 75 | SimplifyNestedClass *ConsumerInstance; |
| 76 | }; |
| 77 | |
| 78 | bool SimplifyNestedClassRewriteVisitor::VisitRecordTypeLoc(RecordTypeLoc TLoc) |
| 79 | { |
| 80 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TLoc.getDecl()); |
| 81 | if (!RD || (RD->getCanonicalDecl() != |
| 82 | ConsumerInstance->TheBaseCXXRD->getCanonicalDecl())) |
| 83 | return true; |
| 84 | |
| 85 | if (ConsumerInstance->isBeforeColonColon(TLoc)) { |
| 86 | SourceLocation LocEnd = |
| 87 | ConsumerInstance->RewriteHelper->getLocationAfter( |
| 88 | TLoc.getEndLoc(), ':'); |
| 89 | ConsumerInstance->TheRewriter.RemoveText( |
| 90 | SourceRange(TLoc.getBeginLoc(), LocEnd)); |
| 91 | } |
| 92 | else { |
| 93 | ConsumerInstance->RewriteHelper->replaceRecordType(TLoc, |
| 94 | ConsumerInstance->TheBaseCXXRD->getNameAsString() + " "); |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | void SimplifyNestedClass::Initialize(ASTContext &context) |
| 100 | { |
| 101 | Transformation::Initialize(context); |
| 102 | CollectionVisitor = new SimplifyNestedClassVisitor(this); |
| 103 | RewriteVisitor = new SimplifyNestedClassRewriteVisitor(this); |
| 104 | } |
| 105 | |
| 106 | void SimplifyNestedClass::HandleTranslationUnit(ASTContext &Ctx) |
| 107 | { |
| 108 | if (TransformationManager::isCLangOpt() || |
| 109 | TransformationManager::isOpenCLLangOpt()) { |
| 110 | ValidInstanceNum = 0; |
| 111 | } |
| 112 | else { |
| 113 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 114 | } |
| 115 | |
| 116 | if (QueryInstanceOnly) |
| 117 | return; |
| 118 | |
| 119 | if (TransformationCounter > ValidInstanceNum) { |
| 120 | TransError = TransMaxInstanceError; |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 125 | TransAssert(RewriteVisitor && "NULL RewriteVisitor!"); |
| 126 | RewriteVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 127 | removeOuterClass(); |
| 128 | |
| 129 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 130 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 131 | TransError = TransInternalError; |
| 132 | } |
| 133 | |
| 134 | void SimplifyNestedClass::removeOuterClass() |
| 135 | { |
| 136 | TransAssert(TheBaseCXXRD && "NULL Base CXXRD!"); |
| 137 | SourceLocation LocStart = TheBaseCXXRD->getBeginLoc(); |
| 138 | SourceLocation LocEnd = TheInnerDecl->getBeginLoc(); |
| 139 | LocEnd = LocEnd.getLocWithOffset(-1); |
| 140 | TheRewriter.RemoveText(SourceRange(LocStart, LocEnd)); |
| 141 | |
| 142 | LocStart = TheBaseCXXRD->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 | void SimplifyNestedClass::handleOneCXXRecordDecl(const CXXRecordDecl *CXXRD) |
| 151 | { |
| 152 | TransAssert(CXXRD && "NULL CXXRD!"); |
| 153 | TransAssert(CXXRD->isThisDeclarationADefinition() && "Not a definition!"); |
| 154 | if (CXXRD->getDescribedClassTemplate() || |
| 155 | CXXRD->getNumBases() || |
| 156 | dyn_cast<ClassTemplateSpecializationDecl>(CXXRD)) |
| 157 | return; |
| 158 | // anon class |
| 159 | if (CXXRD->getNameAsString() == "") |
| 160 | return; |
| 161 | |
| 162 | const Decl *InnerDecl = NULL; |
| 163 | const DeclContext *Ctx = dyn_cast<DeclContext>(CXXRD); |
| 164 | for (DeclContext::decl_iterator I = Ctx->decls_begin(), |
| 165 | E = Ctx->decls_end(); I != E; ++I) { |
| 166 | if ((*I)->isImplicit() || dyn_cast<AccessSpecDecl>(*I)) |
| 167 | continue; |
| 168 | if (dyn_cast<CXXRecordDecl>(*I) || dyn_cast<ClassTemplateDecl>(*I)) { |
| 169 | if (InnerDecl) |
| 170 | return; |
| 171 | InnerDecl = (*I); |
| 172 | } |
| 173 | else { |
| 174 | return; |
| 175 | } |
| 176 | } |
| 177 | if (!InnerDecl) |
| 178 | return; |
| 179 | ValidInstanceNum++; |
| 180 | if (ValidInstanceNum == TransformationCounter) { |
| 181 | TheBaseCXXRD = CXXRD; |
| 182 | TheInnerDecl = InnerDecl; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | SimplifyNestedClass::~SimplifyNestedClass(void) |
| 187 | { |
| 188 | delete CollectionVisitor; |
| 189 | delete RewriteVisitor; |
| 190 | } |
| 191 |