| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 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 "ReplaceSimpleTypedef.h" |
| 16 | |
| 17 | #include "clang/AST/RecursiveASTVisitor.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
| 20 | |
| 21 | #include "TransformationManager.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | static const char *DescriptionMsg = |
| 26 | "This pass replaces typedef names with the underlying type if the \ |
| 27 | underlying type falls into one of the following categories: \ |
| 28 | * scalar type; \n\ |
| 29 | * union; \n\ |
| 30 | * non-templated class; \n\ |
| 31 | * pointer to scalar type\n"; |
| 32 | |
| 33 | static RegisterTransformation<ReplaceSimpleTypedef> |
| 34 | Trans("replace-simple-typedef", DescriptionMsg); |
| 35 | |
| 36 | class ReplaceSimpleTypedefCollectionVisitor : public |
| 37 | RecursiveASTVisitor<ReplaceSimpleTypedefCollectionVisitor> { |
| 38 | |
| 39 | public: |
| 40 | explicit ReplaceSimpleTypedefCollectionVisitor(ReplaceSimpleTypedef *Instance) |
| 41 | : ConsumerInstance(Instance) |
| 42 | { } |
| 43 | |
| 44 | bool VisitTypedefDecl(TypedefDecl *D); |
| 45 | |
| 46 | private: |
| 47 | ReplaceSimpleTypedef *ConsumerInstance; |
| 48 | |
| 49 | }; |
| 50 | |
| 51 | class ReplaceSimpleTypedefRewriteVisitor : public |
| 52 | RecursiveASTVisitor<ReplaceSimpleTypedefRewriteVisitor> { |
| 53 | |
| 54 | public: |
| 55 | explicit ReplaceSimpleTypedefRewriteVisitor(ReplaceSimpleTypedef *Instance) |
| 56 | : ConsumerInstance(Instance) |
| 57 | { } |
| 58 | |
| 59 | bool VisitTypedefTypeLoc(TypedefTypeLoc Loc); |
| 60 | |
| 61 | bool VisitElaboratedTypeLoc(ElaboratedTypeLoc Loc); |
| 62 | |
| 63 | private: |
| 64 | ReplaceSimpleTypedef *ConsumerInstance; |
| 65 | |
| 66 | }; |
| 67 | |
| 68 | bool ReplaceSimpleTypedefCollectionVisitor::VisitTypedefDecl(TypedefDecl *TdefD) |
| 69 | { |
| 70 | if (ConsumerInstance->isInIncludedFile(TdefD)) |
| 71 | return true; |
| 72 | TypedefDecl *CanonicalD = dyn_cast<TypedefDecl>(TdefD->getCanonicalDecl()); |
| 73 | if (!ConsumerInstance->VisitedTypedefDecls.count(CanonicalD)) { |
| 74 | ConsumerInstance->handleOneTypedefDecl(CanonicalD); |
| 75 | ConsumerInstance->VisitedTypedefDecls.insert(CanonicalD); |
| 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | bool ReplaceSimpleTypedefRewriteVisitor::VisitTypedefTypeLoc(TypedefTypeLoc Loc) |
| 82 | { |
| 83 | if (ConsumerInstance->isInIncludedFile(Loc.getBeginLoc())) |
| 84 | return true; |
| 85 | |
| 86 | const TypedefType *TdefTy = Loc.getTypePtr(); |
| 87 | const TypedefDecl *TdefD = dyn_cast<TypedefDecl>(TdefTy->getDecl()); |
| 88 | if (!TdefD || TdefD->getBeginLoc().isInvalid()) |
| 89 | return true; |
| 90 | |
| 91 | if (dyn_cast<TypedefDecl>(TdefD->getCanonicalDecl()) == |
| 92 | ConsumerInstance->TheTypedefDecl) { |
| 93 | SourceRange Range = Loc.getSourceRange(); |
| 94 | ConsumerInstance->TheRewriter.ReplaceText(Range, ConsumerInstance->TyName); |
| 95 | ConsumerInstance->Rewritten = true; |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | // Handle cases like: |
| 101 | // struct S { |
| 102 | // typedef int Int; |
| 103 | // }; |
| 104 | // S::Int g; |
| 105 | // where S::Int is referred as an ElaboratedType |
| 106 | bool ReplaceSimpleTypedefRewriteVisitor::VisitElaboratedTypeLoc( |
| 107 | ElaboratedTypeLoc Loc) |
| 108 | { |
| 109 | const ElaboratedType *ETy = dyn_cast<ElaboratedType>(Loc.getTypePtr()); |
| 110 | const Type *NamedTy = ETy->getNamedType().getTypePtr(); |
| 111 | const TypedefType *TdefTy = NamedTy->getAs<TypedefType>(); |
| 112 | if (!TdefTy) |
| 113 | return true; |
| 114 | |
| 115 | const TypedefDecl *TdefD = dyn_cast<TypedefDecl>(TdefTy->getDecl()); |
| 116 | if (!TdefD || (dyn_cast<TypedefDecl>(TdefD->getCanonicalDecl()) != |
| 117 | ConsumerInstance->TheTypedefDecl)) { |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | NestedNameSpecifierLoc QualifierLoc = Loc.getQualifierLoc(); |
| 122 | if (QualifierLoc && ConsumerInstance->IsScalarType) { |
| 123 | ConsumerInstance->TheRewriter.RemoveText(QualifierLoc.getSourceRange()); |
| 124 | ConsumerInstance->Rewritten = true; |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | void ReplaceSimpleTypedef::Initialize(ASTContext &context) |
| 130 | { |
| 131 | Transformation::Initialize(context); |
| 132 | CollectionVisitor = new ReplaceSimpleTypedefCollectionVisitor(this); |
| 133 | RewriteVisitor = new ReplaceSimpleTypedefRewriteVisitor(this); |
| 134 | } |
| 135 | |
| 136 | void ReplaceSimpleTypedef::HandleTranslationUnit(ASTContext &Ctx) |
| 137 | { |
| 138 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 139 | |
| 140 | if (QueryInstanceOnly) |
| 141 | return; |
| 142 | |
| 143 | if (TransformationCounter > ValidInstanceNum) { |
| 144 | TransError = TransMaxInstanceError; |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 149 | TransAssert(TheTypedefDecl && "NULL TheTypedefDecl!"); |
| 150 | RewriteVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 151 | removeTypedefs(); |
| 152 | |
| 153 | if (!Rewritten) { |
| 154 | TransError = TransNoTextModificationError; |
| 155 | return; |
| 156 | } |
| 157 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 158 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 159 | TransError = TransInternalError; |
| 160 | } |
| 161 | |
| 162 | void ReplaceSimpleTypedef::removeTypedefs() |
| 163 | { |
| 164 | for (TypedefDecl::redecl_iterator I = TheTypedefDecl->redecls_begin(), |
| 165 | E = TheTypedefDecl->redecls_end(); I != E; ++I) { |
| 166 | SourceRange Range = (*I)->getSourceRange(); |
| 167 | if (Range.isValid()) { |
| 168 | RewriteHelper->removeTextUntil(Range, ';'); |
| 169 | Rewritten = true; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | bool ReplaceSimpleTypedef::isValidType(const Type *Ty, const TypedefDecl *D) |
| 175 | { |
| 176 | if (Ty->isEnumeralType() || Ty->isUnionType()) |
| 177 | return true; |
| 178 | |
| 179 | if (const RecordType *RDTy = Ty->getAs<RecordType>()) { |
| 180 | const RecordDecl *RD = RDTy->getDecl(); |
| 181 | // omit some trivial cases, e.g., |
| 182 | // typedef struct S { int x; } S; |
| 183 | if (RD->getNameAsString() == D->getNameAsString()) |
| 184 | return false; |
| 185 | |
| 186 | if (TransformationManager::isCLangOpt()) |
| 187 | return true; |
| 188 | const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD); |
| 189 | TransAssert(CXXRD && "NULL CXXRecordDecl!"); |
| 190 | return (CXXRD->getDescribedClassTemplate() == NULL); |
| 191 | } |
| 192 | |
| 193 | if (Ty->isPointerType()) |
| 194 | Ty = getBasePointerElemType(Ty); |
| 195 | |
| 196 | if (Ty->isScalarType()) { |
| 197 | IsScalarType = true; |
| 198 | return true; |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | void ReplaceSimpleTypedef::handleOneTypedefDecl(const TypedefDecl *CanonicalD) |
| 204 | { |
| 205 | // omit some typedefs injected by Clang |
| 206 | if (CanonicalD->getBeginLoc().isInvalid()) |
| 207 | return; |
| 208 | |
| 209 | FullSourceLoc FullLoc = Context->getFullLoc(CanonicalD->getBeginLoc()); |
| 210 | if (FullLoc.isInSystemHeader()) |
| 211 | return; |
| 212 | |
| 213 | const Type *Ty = CanonicalD->getUnderlyingType().getTypePtr(); |
| 214 | if (!isValidType(Ty, CanonicalD)) |
| 215 | return; |
| 216 | |
| 217 | ValidInstanceNum++; |
| 218 | if (ValidInstanceNum == TransformationCounter) { |
| 219 | TheTypedefDecl = CanonicalD; |
| 220 | CanonicalD->getUnderlyingType().getAsStringInternal(TyName, |
| 221 | Context->getPrintingPolicy()); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | ReplaceSimpleTypedef::~ReplaceSimpleTypedef(void) |
| 226 | { |
| 227 | delete CollectionVisitor; |
| 228 | delete RewriteVisitor; |
| 229 | } |
| 230 |