Debian Codegraph @

clang_delta/ReplaceDependentTypedef.cpp

raw symbols SCIP
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 "ReplaceDependentTypedef.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 is dependent, e.g.: \n\
28 \n\
29 template <class T> struct S { typedef T type; }; \n\
30 struct A { }; \n\
31 struct B { typedef S<A>::type type; }; \n\
32 ==> \n\
33 template <class T> struct S { typedef T type; }; \n\
34 struct A { }; \n\
35 struct B { typedef A type; }; \n\
36 \n\
37 It also tries to reduce the typedef chain, e.g. \n\
38 typedef long xx_t; \n\
39 typedef xx_t xx; \n\
40 ==> \n\
41 typedef long xx_t; \n\
42 typedef long xx; \n\
43 ";
44
45 static RegisterTransformation<ReplaceDependentTypedef>
46 Trans("replace-dependent-typedef", DescriptionMsg);
47
48 class ReplaceDependentTypedefCollectionVisitor : public
49 RecursiveASTVisitor<ReplaceDependentTypedefCollectionVisitor> {
50
51 public:
52 explicit
53 ReplaceDependentTypedefCollectionVisitor(ReplaceDependentTypedef *Instance)
54 : ConsumerInstance(Instance)
55 { }
56
57 bool VisitTypedefDecl(TypedefDecl *D);
58
59 private:
60 ReplaceDependentTypedef *ConsumerInstance;
61
62 };
63
64 bool
65 ReplaceDependentTypedefCollectionVisitor::VisitTypedefDecl(TypedefDecl *D)
66 {
67 ConsumerInstance->handleOneTypedefDecl(D);
68 return true;
69 }
70
71 void ReplaceDependentTypedef::Initialize(ASTContext &context)
72 {
73 Transformation::Initialize(context);
74 CollectionVisitor = new ReplaceDependentTypedefCollectionVisitor(this);
75 }
76
77 void ReplaceDependentTypedef::HandleTranslationUnit(ASTContext &Ctx)
78 {
79 if (TransformationManager::isCLangOpt() ||
80 TransformationManager::isOpenCLLangOpt()) {
81 ValidInstanceNum = 0;
82 }
83 else {
84 CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
85 }
86
87 if (QueryInstanceOnly)
88 return;
89
90 if (TransformationCounter > ValidInstanceNum) {
91 TransError = TransMaxInstanceError;
92 return;
93 }
94
95 Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
96 rewriteTypedefDecl();
97
98 if (Ctx.getDiagnostics().hasErrorOccurred() ||
99 Ctx.getDiagnostics().hasFatalErrorOccurred())
100 TransError = TransInternalError;
101 }
102
103 bool ReplaceDependentTypedef::isValidType(const QualType &QT)
104 {
105 const Type *Ty = QT.getTypePtr();
106 Type::TypeClass TC = Ty->getTypeClass();
107
108 switch (TC) {
109 case Type::SubstTemplateTypeParm:
110 case Type::Typedef:
111 case Type::DependentName: // fall-through
112 return true;
113
114 case Type::Elaborated: {
115 const ElaboratedType *ETy = dyn_cast<ElaboratedType>(Ty);
116 ElaboratedTypeKeyword Keyword = ETy->getKeyword();
117
118 #if LLVM_VERSION_MAJOR >= 18
119 return ((Keyword == ElaboratedTypeKeyword::Typename) || (Keyword == ElaboratedTypeKeyword::None));
120 #else
121 return ((Keyword == ETK_Typename) || (Keyword == ETK_None));
122 #endif
123 }
124
125 default:
126 return false;
127 }
128
129 TransAssert(0 && "Unreachable code!");
130 return false;
131 }
132
133 void ReplaceDependentTypedef::handleOneTypedefDecl(const TypedefDecl *D)
134 {
135 if (isInIncludedFile(D) || D->getBeginLoc().isInvalid())
136 return;
137
138 if (!isValidType(D->getUnderlyingType()))
139 return;
140
141 std::string Str = "";
142 bool Typename = false;
143 if (!getTypeString(D->getUnderlyingType(), Str, Typename))
144 return;
145 std::string TdefTyStr = "";
146 D->getUnderlyingType().getAsStringInternal(
147 TdefTyStr, Context->getPrintingPolicy());
148 if (Str == TdefTyStr)
149 return;
150
151 ValidInstanceNum++;
152 if (ValidInstanceNum == TransformationCounter) {
153 TheTypedefDecl = D;
154 TheTyName = Str;
155 NeedTypenameKeyword = Typename;
156 }
157 }
158
159 void ReplaceDependentTypedef::rewriteTypedefDecl()
160 {
161 std::string NewStr = "typedef ";
162 if (NeedTypenameKeyword)
163 NewStr += "typename ";
164 NewStr = NewStr + TheTyName + " " + TheTypedefDecl->getNameAsString();
165 SourceRange Range = TheTypedefDecl->getSourceRange();
166 TheRewriter.ReplaceText(Range, NewStr);
167 }
168
169 ReplaceDependentTypedef::~ReplaceDependentTypedef()
170 {
171 delete CollectionVisitor;
172 }
173