17
17
18
18
using namespace mlir ;
19
19
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
+
20
38
LogicalResult mlir::parseSourceFile (const llvm::SourceMgr &sourceMgr,
21
39
Block *block, const ParserConfig &config,
22
40
LocationAttr *sourceFileLoc) {
23
41
const auto *sourceBuf = sourceMgr.getMemoryBuffer (sourceMgr.getMainFileID ());
24
42
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);
28
46
}
29
47
if (isBytecode (*sourceBuf))
30
48
return readBytecodeFile (*sourceBuf, block, config);
@@ -37,9 +55,9 @@ mlir::parseSourceFile(const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
37
55
const auto *sourceBuf =
38
56
sourceMgr->getMemoryBuffer (sourceMgr->getMainFileID ());
39
57
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);
43
61
}
44
62
if (isBytecode (*sourceBuf))
45
63
return readBytecodeFile (sourceMgr, block, config);
0 commit comments