Skip to content

Commit a833250

Browse files
committed
SplitDecls with ElaboratedTypeLoc
1 parent 04090b2 commit a833250

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

lib/Transform/Clang/SplitDecls.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class ClangSplitter : public RecursiveASTVisitor<ClangSplitter> {
103103
llvm::SmallVector<clang::CharSourceRange, 8> ToRemove; // a vector of statements that will match the root in the tree
104104
auto IsPossible = pragmaRangeToRemove(P, mClauses, mSrcMgr, mLangOpts,
105105
mImportInfo, ToRemove); // ToRemove - the range of positions we want to remove
106+
SplitDeclarationFlag = true;
106107
if (!IsPossible.first)
107108
if (IsPossible.second & PragmaFlags::IsInMacro)
108109
toDiag(mSrcMgr.getDiagnostics(), mClauses.front()->getBeginLoc(),
@@ -128,16 +129,16 @@ class ClangSplitter : public RecursiveASTVisitor<ClangSplitter> {
128129
Rewriter::RewriteOptions RemoveEmptyLine;
129130
RemoveEmptyLine.RemoveLineIfEmpty = false;
130131

131-
if (isNotSingleFlag) {
132+
if (SplitDeclarationFlag && isNotSingleFlag) {
132133
SourceRange toInsert(notSingleDeclStart, notSingleDeclEnd);
133134
mRewriter.RemoveText(toInsert, RemoveEmptyLine);
134-
bool isFirstVar = true;
135135
while (varDeclsNames.size()) {
136136
if (isFirstVar) {
137137
mRewriter.InsertTextAfterToken(notSingleDeclEnd, varDeclsNames.back());
138138
isFirstVar = false;
139+
} else {
140+
mRewriter.InsertTextAfterToken(notSingleDeclEnd, varDeclType + varDeclsNames.back());
139141
}
140-
mRewriter.InsertTextAfterToken(notSingleDeclEnd, varDeclType + varDeclsNames.back());
141142
varDeclsNames.pop_back();
142143
}
143144
}
@@ -191,18 +192,19 @@ class ClangSplitter : public RecursiveASTVisitor<ClangSplitter> {
191192
}
192193
}
193194

194-
bool TraverseTypeLoc(TypeLoc Loc) {
195-
if (isNotSingleFlag && varDeclsNum == 1) {
196-
TypeRange = Loc.getSourceRange();
197-
std::string type = mRewriter.getRewrittenText(TypeRange);
198-
std::cout << "type = " << type << std::endl;
199-
varDeclType = type;
200-
return RecursiveASTVisitor::TraverseTypeLoc(Loc);
195+
bool TraverseTypeLoc(TypeLoc Loc) {
196+
if (isNotSingleFlag && varDeclsNum == 1) {
197+
SourceRange varDeclRange(start, Loc.getEndLoc());
198+
// TypeRange = Loc.getSourceRange();
199+
std::string type = mRewriter.getRewrittenText(varDeclRange);
200+
std::cout << "type = " << type << std::endl;
201+
varDeclType = type;
202+
return RecursiveASTVisitor::TraverseTypeLoc(Loc);
203+
}
204+
return true;
201205
}
202-
return true;
203-
}
204206

205-
bool VisitVarDecl(VarDecl *S) { // to traverse the parse tree and visit each statement
207+
bool VisitVarDecl(VarDecl *S) { // to traverse the parse tree and visit each statement
206208
if (isNotSingleFlag) {
207209
varDeclsNum++;
208210
SourceRange toInsert(notSingleDeclStart, notSingleDeclEnd);
@@ -213,9 +215,8 @@ bool VisitVarDecl(VarDecl *S) { // to traverse the parse tree and visit each sta
213215
varDeclsEnds.push_front(S->getEndLoc());
214216
SourceRange varDeclRange(S->getBeginLoc(), S->getEndLoc());
215217
if (varDeclsNum == 1) {
218+
isFirstVar = true;
216219
SourceRange toInsert2(Range.getBegin(), S->getEndLoc());
217-
std::cout << "toInsert2" << Canvas.getRewrittenText(toInsert2).str() << std::endl;
218-
std::cout << "varDeclRange" << Canvas.getRewrittenText(varDeclRange).str() << std::endl;
219220
txtStr = Canvas.getRewrittenText(varDeclRange).str();
220221
}
221222
if (varDeclsNum > 1) {
@@ -226,15 +227,18 @@ bool VisitVarDecl(VarDecl *S) { // to traverse the parse tree and visit each sta
226227
txtStr = Canvas.getRewrittenText(varDeclRange).str();
227228
auto it = std::remove(txtStr.begin(), txtStr.end(), ',');
228229
txtStr.erase(it, txtStr.end());
230+
//txtStr.erase(txtStr.find(","),1);
231+
std::cout << "varDeclsNum = " << varDeclsNum << " " << txtStr << std::endl;
229232
}
230233
varDeclsNames.push_front(txtStr + ";\n");
231234
}
232235
return true;
233236
}
234237

235-
bool TraverseDeclStmt(DeclStmt *S) {
238+
bool VisitDeclStmt(DeclStmt *S) {
236239
bool tmp;
237240
if(!(S->isSingleDecl())) {
241+
start = S->getBeginLoc();
238242
if (!isNotSingleFlag)
239243
varDeclsNum = 0;
240244
isNotSingleFlag = true;
@@ -243,7 +247,7 @@ bool VisitVarDecl(VarDecl *S) { // to traverse the parse tree and visit each sta
243247
} else {
244248
isNotSingleFlag = false;
245249
}
246-
return RecursiveASTVisitor::TraverseDeclStmt(S);
250+
return true;
247251
}
248252

249253
bool VisitStmt(Stmt *S) {
@@ -282,8 +286,11 @@ bool VisitVarDecl(VarDecl *S) { // to traverse the parse tree and visit each sta
282286
SourceLocation notSingleDeclEnd;
283287
SourceRange TypeRange;
284288
bool isArrayTypeFlag = false;
289+
bool SplitDeclarationFlag = false;
290+
bool isFirstVar = true;
285291
std::string txtStr;
286292
std::string varDeclType;
293+
SourceLocation start;
287294
};
288295
}
289296

0 commit comments

Comments
 (0)