Skip to content

Commit 2c8b0ae

Browse files
committed
[new-hs-indexer] Workarounds to handle #included source files
1. Ignore Names that come from another source file (TODO) 2. Ignore Names that come from the current source file but have a line number outside the valid range. No idea why this happens, I didn't investigate.
1 parent 78262d3 commit 2c8b0ae

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

glean/lang/haskell/HieIndexer/Index.hs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import qualified Data.Set as Set
2020
import Data.Text (Text)
2121
import qualified Data.Text as Text
2222
import qualified Data.Text.Encoding as Text
23+
import qualified Data.Vector.Unboxed as Vector
2324
import Compat.HieTypes (HieFile(..))
2425
import HieDb.Compat (nameModule_maybe, nameOccName)
2526

@@ -32,7 +33,7 @@ import qualified GHC.Types.Name.Occurrence as GHC
3233
import qualified GHC.Types.Name as GHC (isSystemName)
3334
import GHC.Unit.Types (unitFS)
3435
import qualified GHC.Unit.Module.Name as GHC (moduleNameFS)
35-
import qualified GHC.Data.FastString as GHC (FastString, bytesFS)
36+
import qualified GHC.Data.FastString as GHC (FastString, bytesFS, mkFastString)
3637

3738
import Util.Log
3839

@@ -155,25 +156,29 @@ indexHieFile writer hie = do
155156
modfact <- mkModule smod
156157

157158
let offs = getLineOffsets (hie_hs_src hie)
158-
let fp = Text.pack $ hie_hs_file hie
159-
filefact <- Glean.makeFact @Src.File fp
159+
let hsFileFS = GHC.mkFastString $ hie_hs_file hie
160+
filefact <- Glean.makeFact @Src.File (Text.pack (hie_hs_file hie))
160161
let fileLines = mkFileLines filefact offs
161162
Glean.makeFact_ @Src.FileLines fileLines
162163

163164
Glean.makeFact_ @Hs.ModuleSource $
164165
Hs.ModuleSource_key modfact filefact
165166

166-
let toByteSpan =
167-
rangeToByteSpan .
168-
srcRangeToByteRange fileLines (hie_hs_src hie)
167+
let toByteRange = srcRangeToByteRange fileLines (hie_hs_src hie)
168+
toByteSpan sp
169+
| GHC.srcSpanEndLine sp >= Vector.length (lineOffsets offs) =
170+
Src.ByteSpan (Glean.toNat 0) (Glean.toNat 0)
171+
| otherwise =
172+
rangeToByteSpan (toByteRange (srcSpanToSrcRange filefact sp))
169173

170174
let allIds = [ (n, p) | (Right n, ps) <- Map.toList refmap, p <- ps ]
171175

172176
-- produce names & declarations
173177
names <- fmap catMaybes $ forM allIds $ \(name, (span, dets)) -> if
174178
| Just sp <- getBindSpan span (identInfo dets)
175-
, localOrGlobal name smod -> do
176-
let byteSpan = toByteSpan (srcSpanToSrcRange filefact sp)
179+
, localOrGlobal name smod
180+
, GHC.srcSpanFile sp == hsFileFS -> do -- Note [#included source files]
181+
let byteSpan = toByteSpan sp
177182
sort <- case nameModule_maybe name of
178183
Nothing -> return $ Hs.NameSort_internal byteSpan
179184
Just{} -> return $ Hs.NameSort_external def
@@ -220,7 +225,7 @@ indexHieFile writer hie = do
220225
Just <$> mkName name namemod (Hs.NameSort_external def)
221226
forM maybe_namefact $ \namefact -> do
222227
refspans <- forM (Map.toList kindspans) $ \(kind, spans) -> do
223-
let gleanspans = map (toByteSpan . srcSpanToSrcRange filefact) spans
228+
let gleanspans = map toByteSpan spans
224229
return $ map (Hs.RefSpan kind) gleanspans
225230
Glean.makeFact @Hs.Reference $
226231
Hs.Reference_key namefact (concat refspans)
@@ -263,3 +268,11 @@ indexHieFile writer hie = do
263268
goDecl TyVarBind{} = Just defaultSpan
264269
goDecl (ClassTyDecl sp) = sp
265270
goDecl _ = Nothing
271+
272+
{-
273+
Note [#included source files]
274+
275+
There might be other source files involved when compiling a module,
276+
e.g. if CPP is being used and the .hs file uses `#include`. We
277+
currently ignore Names that come from another source file (TODO).
278+
-}

0 commit comments

Comments
 (0)