summaryrefslogtreecommitdiff
path: root/compiler/utils
diff options
context:
space:
mode:
authorVladislav Zavialov <vlad.z.4096@gmail.com>2020-01-23 23:03:04 +0300
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-02-29 05:06:31 -0500
commit327b29e1a05d9f1ea04465c9b23aed92473dd453 (patch)
tree0b6db26b4677c2677a32754de523eb842f9cb849 /compiler/utils
parent37f126033f1e5bf0331143f005ef90ba6e2e02cd (diff)
downloadhaskell-327b29e1a05d9f1ea04465c9b23aed92473dd453.tar.gz
Monotonic locations (#17632)
When GHC is parsing a file generated by a tool, e.g. by the C preprocessor, the tool may insert #line pragmas to adjust the locations reported to the user. As the result, the locations recorded in RealSrcLoc are not monotonic. Elements that appear later in the StringBuffer are not guaranteed to have a higher line/column number. In fact, there are no guarantees whatsoever, as #line pragmas can arbitrarily modify locations. This lack of guarantees makes ideas such as #17544 infeasible. This patch adds an additional bit of information to every SrcLoc: newtype BufPos = BufPos { bufPos :: Int } A BufPos represents the location in the StringBuffer, unaffected by any pragmas. Updates haddock submodule. Metric Increase: haddock.Cabal haddock.base haddock.compiler MultiLayerModules Naperian parsing001 T12150
Diffstat (limited to 'compiler/utils')
-rw-r--r--compiler/utils/Binary.hs19
1 files changed, 17 insertions, 2 deletions
diff --git a/compiler/utils/Binary.hs b/compiler/utils/Binary.hs
index 498c4924de..1c52cb56fb 100644
--- a/compiler/utils/Binary.hs
+++ b/compiler/utils/Binary.hs
@@ -1380,10 +1380,24 @@ instance Binary RealSrcSpan where
return (mkRealSrcSpan (mkRealSrcLoc f sl sc)
(mkRealSrcLoc f el ec))
+instance Binary BufPos where
+ put_ bh (BufPos i) = put_ bh i
+ get bh = BufPos <$> get bh
+
+instance Binary BufSpan where
+ put_ bh (BufSpan start end) = do
+ put_ bh start
+ put_ bh end
+ get bh = do
+ start <- get bh
+ end <- get bh
+ return (BufSpan start end)
+
instance Binary SrcSpan where
- put_ bh (RealSrcSpan ss) = do
+ put_ bh (RealSrcSpan ss sb) = do
putByte bh 0
put_ bh ss
+ put_ bh sb
put_ bh (UnhelpfulSpan s) = do
putByte bh 1
@@ -1393,7 +1407,8 @@ instance Binary SrcSpan where
h <- getByte bh
case h of
0 -> do ss <- get bh
- return (RealSrcSpan ss)
+ sb <- get bh
+ return (RealSrcSpan ss sb)
_ -> do s <- get bh
return (UnhelpfulSpan s)