4141#include < llvm/ADT/SmallString.h>
4242#include < clang/Basic/SourceLocation.h>
4343#include < llvm/ADT/StringSet.h>
44+ #include " llvm/IR/DebugInfoMetadata.h"
4445#include < llvm/IR/Function.h>
4546#include < llvm/IR/Module.h>
4647#include < llvm/Support/Debug.h>
@@ -83,8 +84,6 @@ namespace {
8384// / The visitor searches a pragma `split` and performs splitting for a scope
8485// / after it. It also checks absence a macros in this scope and print some
8586// / other warnings.
86- bool isNotSingleFlag = false ;
87- bool isAfterNotSingleFlag = false ;
8887class ClangSplitter : public RecursiveASTVisitor <ClangSplitter> {
8988public:
9089 ClangSplitter (TransformationContext &TfmCtx, const ASTImportInfo &ImportInfo,
@@ -172,26 +171,6 @@ class ClangSplitter : public RecursiveASTVisitor<ClangSplitter> {
172171 return Res;
173172 }
174173
175- void buildTxtStr (std::string varType, std::string varName) {
176- std::vector<std::string> tokens;
177- std::string delimiter (" " );
178- size_t prev = 0 ;
179- size_t next;
180- size_t delta = delimiter.length ();
181- while ((next = varType.find (delimiter, prev)) != std::string::npos){
182- tokens.push_back (varType.substr (prev, next-prev));
183- prev = next + delta;
184- }
185- tokens.push_back (varType.substr (prev));
186- txtStr = " " ;
187- for (std::string token : tokens) {
188- if (token == tokens.back ())
189- txtStr += varName + token + " ;\n " ;
190- else
191- txtStr += token + " " ;
192- }
193- }
194-
195174 bool TraverseTypeLoc (TypeLoc Loc) {
196175 if (isNotSingleFlag && varDeclsNum == 1 ) {
197176 SourceRange varDeclRange (start, Loc.getEndLoc ());
@@ -227,6 +206,11 @@ class ClangSplitter : public RecursiveASTVisitor<ClangSplitter> {
227206 txtStr = Canvas.getRewrittenText (varDeclRange).str ();
228207 auto it = std::remove (txtStr.begin (), txtStr.end (), ' ,' );
229208 txtStr.erase (it, txtStr.end ());
209+ size_t foundIndex = txtStr.find (" \n " );
210+ if (foundIndex != std::string::npos)
211+ {
212+ txtStr.erase (foundIndex, 2 );
213+ }
230214 // txtStr.erase(txtStr.find(","),1);
231215 std::cout << " varDeclsNum = " << varDeclsNum << " " << txtStr << std::endl;
232216 }
@@ -236,7 +220,6 @@ class ClangSplitter : public RecursiveASTVisitor<ClangSplitter> {
236220 }
237221
238222 bool VisitDeclStmt (DeclStmt *S) {
239- bool tmp;
240223 if (!(S->isSingleDecl ())) {
241224 start = S->getBeginLoc ();
242225 if (!isNotSingleFlag)
@@ -287,6 +270,8 @@ class ClangSplitter : public RecursiveASTVisitor<ClangSplitter> {
287270 SourceRange TypeRange;
288271 bool isArrayTypeFlag = false ;
289272 bool SplitDeclarationFlag = false ;
273+ bool isNotSingleFlag = false ;
274+ bool isAfterNotSingleFlag = false ;
290275 bool isFirstVar = true ;
291276 std::string txtStr;
292277 std::string varDeclType;
@@ -295,19 +280,34 @@ class ClangSplitter : public RecursiveASTVisitor<ClangSplitter> {
295280}
296281
297282bool ClangSplitDeclsPass::runOnModule (llvm::Module &M) {
298- auto &TfmInfo = getAnalysis<TransformationEnginePass>();
299- auto *TfmCtx{TfmInfo ? TfmInfo->getContext (M) : nullptr };
300- if (!TfmCtx || !TfmCtx->hasInstance ()) {
301- M.getContext ().emitError (" can not transform sources"
302- " : transformation context is not available" );
283+ auto &TfmInfo{getAnalysis<TransformationEnginePass>()};
284+ if (!TfmInfo) {
285+ M.getContext ().emitError (" cannot transform sources"
286+ " : transformation context is not available" );
303287 return false ;
304288 }
305289 ASTImportInfo ImportStub;
306290 const auto *ImportInfo = &ImportStub;
307291 if (auto *ImportPass = getAnalysisIfAvailable<ImmutableASTImportInfoPass>())
308292 ImportInfo = &ImportPass->getImportInfo ();
309- auto &GIP = getAnalysis<ClangGlobalInfoPass>();
310- ClangSplitter Vis (*TfmCtx, *ImportInfo, GIP.getRawInfo ());
311- Vis.TraverseDecl (TfmCtx->getContext ().getTranslationUnitDecl ());
312- return false ;
293+ auto &GIP{getAnalysis<ClangGlobalInfoPass>()};
294+ auto *CUs{M.getNamedMetadata (" llvm.dbg.cu" )};
295+ for (auto *MD : CUs->operands ()) {
296+ auto *CU{cast<DICompileUnit>(MD)};
297+ auto *TfmCtx{
298+ dyn_cast_or_null<ClangTransformationContext>(TfmInfo->getContext (*CU))};
299+ if (!TfmCtx || !TfmCtx->hasInstance ()) {
300+ M.getContext ().emitError (" cannot transform sources"
301+ " : transformation context is not available" );
302+ return false ;
303+ }
304+ ASTImportInfo ImportStub;
305+ const auto *ImportInfo = &ImportStub;
306+ if (auto *ImportPass = getAnalysisIfAvailable<ImmutableASTImportInfoPass>())
307+ ImportInfo = &ImportPass->getImportInfo ();
308+ auto &GIP = getAnalysis<ClangGlobalInfoPass>();
309+ ClangSplitter Vis (*TfmCtx, *ImportInfo, GIP.getRawInfo ());
310+ Vis.TraverseDecl (TfmCtx->getContext ().getTranslationUnitDecl ());
311+ return false ;
312+ }
313313}
0 commit comments