Debian Codegraph @

clang_delta/ReplaceDependentName.cpp

raw symbols SCIP
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 "ReplaceDependentName.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 a dependent name (referred by typename) with \n\
27 a resolved type by looking up this dependent name in its decl context. \n\
28 ";
29
30 static RegisterTransformation<ReplaceDependentName>
31 Trans("replace-dependent-name", DescriptionMsg);
32
33 class ReplaceDependentNameCollectionVisitor : public
34 RecursiveASTVisitor<ReplaceDependentNameCollectionVisitor> {
35
36 public:
37 explicit
38 ReplaceDependentNameCollectionVisitor(ReplaceDependentName *Instance)
39 : ConsumerInstance(Instance)
40 { }
41
42 bool VisitDependentNameTypeLoc(DependentNameTypeLoc TLoc);
43
44 bool VisitElaboratedTypeLoc(ElaboratedTypeLoc TLoc);
45
46 private:
47 ReplaceDependentName *ConsumerInstance;
48
49 };
50
51 bool ReplaceDependentNameCollectionVisitor::VisitDependentNameTypeLoc(
52 DependentNameTypeLoc TLoc)
53 {
54 ConsumerInstance->handleOneDependentNameTypeLoc(TLoc);
55 return true;
56 }
57
58 bool ReplaceDependentNameCollectionVisitor::VisitElaboratedTypeLoc(
59 ElaboratedTypeLoc TLoc)
60 {
61 ConsumerInstance->handleOneElaboratedTypeLoc(TLoc);
62 return true;
63 }
64
65 void ReplaceDependentName::Initialize(ASTContext &context)
66 {
67 Transformation::Initialize(context);
68 CollectionVisitor = new ReplaceDependentNameCollectionVisitor(this);
69 }
70
71 void ReplaceDependentName::HandleTranslationUnit(ASTContext &Ctx)
72 {
73 if (TransformationManager::isCLangOpt() ||
74 TransformationManager::isOpenCLLangOpt()) {
75 ValidInstanceNum = 0;
76 }
77 else {
78 CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
79 }
80
81 if (QueryInstanceOnly)
82 return;
83
84 if (TransformationCounter > ValidInstanceNum) {
85 TransError = TransMaxInstanceError;
86 return;
87 }
88
89 Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
90 rewriteDependentName();
91
92 if (Ctx.getDiagnostics().hasErrorOccurred() ||
93 Ctx.getDiagnostics().hasFatalErrorOccurred())
94 TransError = TransInternalError;
95 }
96
97 SourceLocation ReplaceDependentName::getElaboratedTypeLocBegin(
98 const ElaboratedTypeLoc &TLoc)
99 {
100 SourceLocation Loc = TLoc.getElaboratedKeywordLoc();
101 if (Loc.isValid())
102 return Loc;
103 NestedNameSpecifierLoc SpecLoc = TLoc.getQualifierLoc();
104 NestedNameSpecifierLoc Prefix = SpecLoc.getPrefix();
105
106 while (Prefix.getBeginLoc().isValid()) {
107 SpecLoc = Prefix;
108 Prefix = Prefix.getPrefix();
109 }
110 Loc = SpecLoc.getBeginLoc();
111 TransAssert(Loc.isValid() && "Failed to get ElaboratedTypeLoc!");
112 return Loc;
113 }
114
115 void ReplaceDependentName::handleOneElaboratedTypeLoc(
116 const ElaboratedTypeLoc &TLoc)
117 {
118 SourceLocation Loc = TLoc.getBeginLoc();
119 if (Loc.isInvalid() || isInIncludedFile(Loc))
120 return;
121
122 const ElaboratedType *ET = TLoc.getTypePtr();
123
124 #if LLVM_VERSION_MAJOR >= 18
125 if ((ET->getKeyword() != ElaboratedTypeKeyword::Typename) && (ET->getKeyword() != ElaboratedTypeKeyword::None))
126 return;
127 #else
128 if ((ET->getKeyword() != ETK_Typename) && (ET->getKeyword() != ETK_None))
129 return;
130 #endif
131
132 if (TLoc.getQualifierLoc().getBeginLoc().isInvalid())
133 return;
134 std::string Str = "";
135 if (ValidInstanceNum == 8)
136 TransAssert(ET);
137 bool Typename = false;
138 if (!getTypeString(ET->getNamedType(), Str, Typename))
139 return;
140 std::string TyStr = "";
141 ET->getNamedType().getAsStringInternal(TyStr, Context->getPrintingPolicy());
142 if (TyStr == Str)
143 return;
144
145 ValidInstanceNum++;
146 if (ValidInstanceNum == TransformationCounter) {
147 TheTyName = Str;
148 NeedTypenameKeyword = Typename;
149 TheLocBegin = getElaboratedTypeLocBegin(TLoc);
150 TheNameLocEnd = TLoc.getEndLoc();
151 }
152 }
153
154 void ReplaceDependentName::handleOneDependentNameTypeLoc(
155 const DependentNameTypeLoc &TLoc)
156 {
157 SourceLocation Loc = TLoc.getBeginLoc();
158 if (Loc.isInvalid() || isInIncludedFile(Loc))
159 return;
160
161 const DependentNameType *DNT =
162 dyn_cast<DependentNameType>(TLoc.getTypePtr());
163 TransAssert(DNT && "NULL DependentNameType!");
164
165 #if LLVM_VERSION_MAJOR >= 18
166 if (DNT->getKeyword() != ElaboratedTypeKeyword::Typename)
167 return;
168 #else
169 if (DNT->getKeyword() != ETK_Typename)
170 return;
171 #endif
172
173 std::string Str = "";
174 bool Typename = false;
175 if (!getDependentNameTypeString(DNT, Str, Typename))
176 return;
177
178 ValidInstanceNum++;
179 if (ValidInstanceNum == TransformationCounter) {
180 TheTyName = Str;
181 NeedTypenameKeyword = Typename;
182 TheLocBegin = TLoc.getElaboratedKeywordLoc();
183 TheNameLocEnd = TLoc.getEndLoc();
184 }
185 }
186
187 void ReplaceDependentName::rewriteDependentName()
188 {
189 std::string NewStr = "";
190 if (NeedTypenameKeyword)
191 NewStr += "typename ";
192 NewStr += TheTyName;
193 TheRewriter.ReplaceText(SourceRange(TheLocBegin, TheNameLocEnd), NewStr);
194 }
195
196 ReplaceDependentName::~ReplaceDependentName()
197 {
198 delete CollectionVisitor;
199 }
200