1717
1818using namespace mlir ;
1919
20+ static std::pair<int64_t , int64_t >
21+ getLineAndColStart (const llvm::SourceMgr &sourceMgr) {
22+ unsigned lastFileID = sourceMgr.getNumBuffers ();
23+ if (lastFileID == 1 )
24+ return {0 , 0 };
25+
26+ auto bufferID = sourceMgr.getMainFileID ();
27+ const llvm::MemoryBuffer *main = sourceMgr.getMemoryBuffer (bufferID);
28+ const llvm::MemoryBuffer *last = sourceMgr.getMemoryBuffer (lastFileID);
29+ // Exclude same start.
30+ if (main->getBufferStart () < last->getBufferStart () &&
31+ main->getBufferEnd () >= last->getBufferEnd ()) {
32+ return sourceMgr.getLineAndColumn (
33+ llvm::SMLoc::getFromPointer (last->getBufferStart ()), bufferID);
34+ }
35+ return {0 , 0 };
36+ }
37+
2038LogicalResult mlir::parseSourceFile (const llvm::SourceMgr &sourceMgr,
2139 Block *block, const ParserConfig &config,
2240 LocationAttr *sourceFileLoc) {
2341 const auto *sourceBuf = sourceMgr.getMemoryBuffer (sourceMgr.getMainFileID ());
2442 if (sourceFileLoc) {
25- *sourceFileLoc = FileLineColLoc::get (config. getContext (),
26- sourceBuf-> getBufferIdentifier (),
27- /* line= */ 0 , /* column= */ 0 );
43+ auto [line, column] = getLineAndColStart (sourceMgr);
44+ *sourceFileLoc = FileLineColLoc::get (
45+ config. getContext (), sourceBuf-> getBufferIdentifier (), line, column);
2846 }
2947 if (isBytecode (*sourceBuf))
3048 return readBytecodeFile (*sourceBuf, block, config);
@@ -37,9 +55,9 @@ mlir::parseSourceFile(const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
3755 const auto *sourceBuf =
3856 sourceMgr->getMemoryBuffer (sourceMgr->getMainFileID ());
3957 if (sourceFileLoc) {
40- *sourceFileLoc = FileLineColLoc::get (config. getContext (),
41- sourceBuf-> getBufferIdentifier (),
42- /* line= */ 0 , /* column= */ 0 );
58+ auto [line, column] = getLineAndColStart (*sourceMgr);
59+ *sourceFileLoc = FileLineColLoc::get (
60+ config. getContext (), sourceBuf-> getBufferIdentifier (), line, column);
4361 }
4462 if (isBytecode (*sourceBuf))
4563 return readBytecodeFile (sourceMgr, block, config);
0 commit comments