You could use your solution 2, but with a "line index" of your source file.
You make a first phase to obtain an array of positions of lines starts. You can then obtain the line number of a position in O(log n) by binary search (n being the count of lines).
By the way, it gets you as well the position in the line as you know pos - lines_start[line]
on O(1), this is precious in error reporting for lines of code that are not trivial.