Skip to content

Commit 9d3bd35

Browse files
committed
Add pragma handling, remove unused variables, update work with TransformationContext
1 parent dc6681b commit 9d3bd35

File tree

1 file changed

+34
-60
lines changed

1 file changed

+34
-60
lines changed

lib/Transform/Clang/SplitDecls.cpp

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,6 @@ using namespace tsar;
6363

6464
char ClangSplitDeclsPass::ID = 0;
6565

66-
struct notSingleDecl {
67-
bool isNotSingleFlag = false;
68-
int varDeclsNum = 0;
69-
bool isFirstVar = true;
70-
SourceLocation notSingleDeclStart;
71-
SourceLocation notSingleDeclEnd;
72-
std::deque<SourceLocation> starts;
73-
std::deque<SourceLocation> ends;
74-
std::deque<std::string> names;
75-
std::string varDeclType;
76-
};
77-
78-
7966
INITIALIZE_PASS_IN_GROUP_BEGIN(ClangSplitDeclsPass, "clang-split",
8067
"Separation of variable declaration statements (Clang)", false, false,
8168
tsar::TransformationQueryManager::getPassRegistry())
@@ -99,22 +86,38 @@ namespace {
9986
/// The visitor searches a pragma `split` and performs splitting for a scope
10087
/// after it. It also checks absence a macros in this scope and print some
10188
/// other warnings.
89+
90+
struct notSingleDecl {
91+
bool isNotSingleFlag = false;
92+
int varDeclsNum = 0;
93+
bool isFirstVar = true;
94+
SourceLocation notSingleDeclStart;
95+
SourceLocation notSingleDeclEnd;
96+
std::deque<SourceLocation> starts;
97+
std::deque<SourceLocation> ends;
98+
std::deque<std::string> names;
99+
std::string varDeclType;
100+
};
101+
102+
102103
class ClangSplitter : public RecursiveASTVisitor<ClangSplitter> {
103104
public:
104-
ClangSplitter(TransformationContext &TfmCtx, const ASTImportInfo &ImportInfo, const GlobalInfoExtractor &GlobalInfo,
105-
ClangGlobalInfoPass::RawInfo &RawInfo) :
106-
mTfmCtx(&TfmCtx), mImportInfo(ImportInfo), mGlobalInfo(GlobalInfo),
107-
mRawInfo(&RawInfo), mRewriter(TfmCtx.getRewriter()),
108-
mContext(TfmCtx.getContext()), mSrcMgr(mRewriter.getSourceMgr()),
109-
mLangOpts(mRewriter.getLangOpts()) {}
105+
ClangSplitter(ClangTransformationContext &TfmCtx,
106+
const ASTImportInfo &ImportInfo,
107+
const GlobalInfoExtractor &GlobalInfo,
108+
ClangGlobalInfo::RawInfo &RawInfo)
109+
: mTfmCtx(&TfmCtx), mImportInfo(ImportInfo), mGlobalInfo(GlobalInfo),
110+
mRawInfo(&RawInfo), mRewriter(TfmCtx.getRewriter()),
111+
mContext(TfmCtx.getContext()), mSrcMgr(mRewriter.getSourceMgr()),
112+
mLangOpts(mRewriter.getLangOpts()) {}
110113

111114
bool TraverseStmt(Stmt *S) { // to traverse the parse tree and visit each statement
112115
if (!S) {
113116
return RecursiveASTVisitor::TraverseStmt(S);
114117
}
115118
Pragma P(*S); // the Pragma class is used to check if a statement is a pragma or not
116-
splitPragmaFlag = true;
117119
if (findClause(P, ClauseId::SplitDeclaration, mClauses)) { // mClauses contains all SplitDeclaration pragmas
120+
splitPragmaFlag = true;
118121
llvm::SmallVector<clang::CharSourceRange, 8> ToRemove; // a vector of statements that will match the root in the tree
119122
auto IsPossible = pragmaRangeToRemove(P, mClauses, mSrcMgr, mLangOpts,
120123
mImportInfo, ToRemove); // ToRemove - the range of positions we want to remove
@@ -188,34 +191,7 @@ class ClangSplitter : public RecursiveASTVisitor<ClangSplitter> {
188191
}
189192
}
190193
}
191-
if (mClauses.empty() || !isa<CompoundStmt>(S) &&
192-
!isa<ForStmt>(S) && !isa<DoStmt>(S) && !isa<WhileStmt>(S))
193-
return RecursiveASTVisitor::TraverseStmt(S);
194-
// There was a pragma split, so check absence of macros and perform
195-
// splitting.
196-
mClauses.clear();
197-
bool StashSplitState = mActiveSplit;
198-
// We do not perform search of macros in case of nested 'split'
199-
// directives and active splitting. The search has been already performed.
200-
if (!mActiveSplit) {
201-
bool HasMacro = false;
202-
for_each_macro(S, mSrcMgr, mContext.getLangOpts(), mRawInfo->Macros,
203-
[&HasMacro, this](clang::SourceLocation Loc) {
204-
if (!HasMacro) {
205-
toDiag(mContext.getDiagnostics(), Loc,
206-
tsar::diag::warn_splitdeclaration_macro_prevent);
207-
HasMacro = true;
208-
}
209-
});
210-
// We should not stop traverse because some nested split directives
211-
// may exist.
212-
if (HasMacro)
213-
return RecursiveASTVisitor::TraverseStmt(S);
214-
mActiveSplit = true;
215-
}
216-
auto Res = RecursiveASTVisitor::TraverseStmt(S);
217-
mActiveSplit = StashSplitState;
218-
return Res;
194+
return RecursiveASTVisitor::TraverseStmt(S);
219195
}
220196

221197
std::string getType(SourceLocation start, SourceLocation endLoc) {
@@ -303,10 +279,10 @@ class ClangSplitter : public RecursiveASTVisitor<ClangSplitter> {
303279
std::map<clang::SourceLocation, notSingleDecl>::iterator it;
304280
for (it = globalVarDeclsMap.begin(); it != globalVarDeclsMap.end(); it++) {
305281
if (it->first != S->getBeginLoc()) {
306-
it->second.starts.clear();
307-
it->second.ends.clear();
308-
it->second.names.clear();
309-
globalVarDeclsMap.erase(it->first);
282+
//it->second.starts.clear();
283+
//it->second.ends.clear();
284+
//it->second.names.clear();
285+
//globalVarDeclsMap.erase(it->first);
310286
}
311287
}
312288
}
@@ -370,22 +346,22 @@ class ClangSplitter : public RecursiveASTVisitor<ClangSplitter> {
370346

371347
private:
372348

373-
TransformationContext *mTfmCtx;
349+
ClangTransformationContext *mTfmCtx;
374350
const ASTImportInfo &mImportInfo;
375351
const GlobalInfoExtractor &mGlobalInfo;
376352

377-
ClangGlobalInfoPass::RawInfo *mRawInfo;
353+
ClangGlobalInfo::RawInfo *mRawInfo;
378354
Rewriter &mRewriter;
379355
ASTContext &mContext;
380356
SourceManager &mSrcMgr;
381357
const LangOptions &mLangOpts;
382358
SmallVector<Stmt *, 1> mClauses;
383-
bool mActiveSplit = false;
384359
bool splitPragmaFlag = false;
385360

386361
std::map<SourceLocation, notSingleDecl> globalVarDeclsMap;
387362
std::map<SourceLocation, SourceLocation> varPositions;
388363
notSingleDecl localVarDecls;
364+
notSingleDecl tmpVarDecl;
389365
std::string txtStr;
390366
SourceLocation start;
391367
};
@@ -402,7 +378,7 @@ bool ClangSplitDeclsPass::runOnModule(llvm::Module &M) {
402378
const auto *ImportInfo = &ImportStub;
403379
if (auto *ImportPass = getAnalysisIfAvailable<ImmutableASTImportInfoPass>())
404380
ImportInfo = &ImportPass->getImportInfo();
405-
auto &GIP{getAnalysis<ClangGlobalInfoPass>()};
381+
//auto &GIP{getAnalysis<ClangGlobalInfoPass>()};
406382
auto *CUs{M.getNamedMetadata("llvm.dbg.cu")};
407383
for (auto *MD : CUs->operands()) {
408384
auto *CU{cast<DICompileUnit>(MD)};
@@ -418,10 +394,8 @@ bool ClangSplitDeclsPass::runOnModule(llvm::Module &M) {
418394
if (auto *ImportPass = getAnalysisIfAvailable<ImmutableASTImportInfoPass>())
419395
ImportInfo = &ImportPass->getImportInfo();
420396
auto &GIP = getAnalysis<ClangGlobalInfoPass>();
421-
//mGlobalInfo = &GIP.getGlobalInfo();
422-
const auto &GlobalInfo = GIP.getGlobalInfo();
423-
ClangSplitter Vis(*TfmCtx, *ImportInfo, GlobalInfo, GIP.getRawInfo());
397+
ClangSplitter Vis(*TfmCtx, *ImportInfo, GIP.getGlobalInfo(TfmCtx)->GIE, GIP.getGlobalInfo(TfmCtx)->RI);
424398
Vis.TraverseDecl(TfmCtx->getContext().getTranslationUnitDecl());
425-
return false;
426399
}
400+
return false;
427401
}

0 commit comments

Comments
 (0)