@@ -103,32 +103,6 @@ llvm::DILocation *getDILocation(const llvm::Value *V) {
103103 return nullptr ;
104104}
105105
106- llvm::DIFile *getDIFile (const llvm::Value *V) {
107- if (const auto *GO = llvm::dyn_cast<llvm::GlobalObject>(V)) {
108- if (auto *MN = GO->getMetadata (llvm::LLVMContext::MD_dbg)) {
109- if (auto *Subpr = llvm::dyn_cast<llvm::DISubprogram>(MN)) {
110- return Subpr->getFile ();
111- }
112- if (auto *GVExpr = llvm::dyn_cast<llvm::DIGlobalVariableExpression>(MN)) {
113- return GVExpr->getVariable ()->getFile ();
114- }
115- }
116- } else if (const auto *Arg = llvm::dyn_cast<llvm::Argument>(V)) {
117- if (auto *LocVar = getDILocalVariable (Arg)) {
118- return LocVar->getFile ();
119- }
120- } else if (const auto *I = llvm::dyn_cast<llvm::Instruction>(V)) {
121- if (I->isUsedByMetadata ()) {
122- if (auto *LocVar = getDILocalVariable (I)) {
123- return LocVar->getFile ();
124- }
125- } else if (I->getMetadata (llvm::LLVMContext::MD_dbg)) {
126- return I->getDebugLoc ()->getFile ();
127- }
128- }
129- return nullptr ;
130- }
131-
132106std::string getVarNameFromIR (const llvm::Value *V) {
133107 if (auto *LocVar = getDILocalVariable (V)) {
134108 return LocVar->getName ().str ();
@@ -154,7 +128,7 @@ std::string getFunctionNameFromIR(const llvm::Value *V) {
154128}
155129
156130std::string getFilePathFromIR (const llvm::Value *V) {
157- if (auto *DIF = getDIFile (V)) {
131+ if (auto *DIF = getDIFileFromIR (V)) {
158132 std::filesystem::path File (DIF->getFilename ().str ());
159133 std::filesystem::path Dir (DIF->getDirectory ().str ());
160134 if (!File.empty ()) {
@@ -181,32 +155,58 @@ std::string getFilePathFromIR(const llvm::Value *V) {
181155 return " " ;
182156}
183157
184- unsigned int getLineFromIR (const llvm::Value *V) {
158+ const llvm::DIFile *getDIFileFromIR (const llvm::Value *V) {
159+ if (const auto *GO = llvm::dyn_cast<llvm::GlobalObject>(V)) {
160+ if (auto *MN = GO->getMetadata (llvm::LLVMContext::MD_dbg)) {
161+ if (auto *Subpr = llvm::dyn_cast<llvm::DISubprogram>(MN)) {
162+ return Subpr->getFile ();
163+ }
164+ if (auto *GVExpr = llvm::dyn_cast<llvm::DIGlobalVariableExpression>(MN)) {
165+ return GVExpr->getVariable ()->getFile ();
166+ }
167+ }
168+ } else if (const auto *Arg = llvm::dyn_cast<llvm::Argument>(V)) {
169+ if (auto *LocVar = getDILocalVariable (Arg)) {
170+ return LocVar->getFile ();
171+ }
172+ } else if (const auto *I = llvm::dyn_cast<llvm::Instruction>(V)) {
173+ if (I->isUsedByMetadata ()) {
174+ if (auto *LocVar = getDILocalVariable (I)) {
175+ return LocVar->getFile ();
176+ }
177+ } else if (I->getMetadata (llvm::LLVMContext::MD_dbg)) {
178+ return I->getDebugLoc ()->getFile ();
179+ }
180+ }
181+ return nullptr ;
182+ }
183+
184+ std::string getDirectoryFromIR (const llvm::Value *V) {
185185 // Argument and Instruction
186186 if (auto *DILoc = getDILocation (V)) {
187- return DILoc->getLine ();
187+ return DILoc->getDirectory (). str ();
188188 }
189189 if (auto *DISubpr = getDISubprogram (V)) { // Function
190- return DISubpr->getLine ();
190+ return DISubpr->getDirectory (). str ();
191191 }
192192 if (auto *DIGV = getDIGlobalVariable (V)) { // Globals
193- return DIGV->getLine ();
193+ return DIGV->getDirectory (). str ();
194194 }
195- return 0 ;
195+ return " " ;
196196}
197197
198- std::string getDirectoryFromIR (const llvm::Value *V) {
198+ unsigned int getLineFromIR (const llvm::Value *V) {
199199 // Argument and Instruction
200200 if (auto *DILoc = getDILocation (V)) {
201- return DILoc->getDirectory (). str ();
201+ return DILoc->getLine ();
202202 }
203203 if (auto *DISubpr = getDISubprogram (V)) { // Function
204- return DISubpr->getDirectory (). str ();
204+ return DISubpr->getLine ();
205205 }
206206 if (auto *DIGV = getDIGlobalVariable (V)) { // Globals
207- return DIGV->getDirectory (). str ();
207+ return DIGV->getLine ();
208208 }
209- return " " ;
209+ return 0 ;
210210}
211211
212212unsigned int getColumnFromIR (const llvm::Value *V) {
@@ -217,7 +217,21 @@ unsigned int getColumnFromIR(const llvm::Value *V) {
217217 return 0 ;
218218}
219219
220- std::string getSrcCodeFromIR (const llvm::Value *V) {
220+ std::pair<unsigned , unsigned > getLineAndColFromIR (const llvm::Value *V) {
221+ // Argument and Instruction
222+ if (auto *DILoc = getDILocation (V)) {
223+ return {DILoc->getLine (), DILoc->getColumn ()};
224+ }
225+ if (auto *DISubpr = getDISubprogram (V)) { // Function
226+ return {DISubpr->getLine (), 0 };
227+ }
228+ if (auto *DIGV = getDIGlobalVariable (V)) { // Globals
229+ return {DIGV->getLine (), 0 };
230+ }
231+ return {0 , 0 };
232+ }
233+
234+ std::string getSrcCodeFromIR (const llvm::Value *V, bool Trim) {
221235 unsigned int LineNr = getLineFromIR (V);
222236 if (LineNr > 0 ) {
223237 std::filesystem::path Path (getFilePathFromIR (V));
@@ -230,7 +244,7 @@ std::string getSrcCodeFromIR(const llvm::Value *V) {
230244 Ifs.ignore (std::numeric_limits<std::streamsize>::max (), ' \n ' );
231245 }
232246 std::getline (Ifs, SrcLine);
233- return llvm::StringRef (SrcLine).trim ().str ();
247+ return Trim ? llvm::StringRef (SrcLine).trim ().str () : SrcLine ;
234248 }
235249 }
236250 }
0 commit comments