@@ -20,6 +20,7 @@ import qualified Data.Set as Set
20
20
import Data.Text (Text )
21
21
import qualified Data.Text as Text
22
22
import qualified Data.Text.Encoding as Text
23
+ import qualified Data.Vector.Unboxed as Vector
23
24
import Compat.HieTypes (HieFile (.. ))
24
25
import HieDb.Compat (nameModule_maybe , nameOccName )
25
26
@@ -32,7 +33,7 @@ import qualified GHC.Types.Name.Occurrence as GHC
32
33
import qualified GHC.Types.Name as GHC (isSystemName )
33
34
import GHC.Unit.Types (unitFS )
34
35
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 )
36
37
37
38
import Util.Log
38
39
@@ -155,25 +156,29 @@ indexHieFile writer hie = do
155
156
modfact <- mkModule smod
156
157
157
158
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))
160
161
let fileLines = mkFileLines filefact offs
161
162
Glean. makeFact_ @ Src. FileLines fileLines
162
163
163
164
Glean. makeFact_ @ Hs. ModuleSource $
164
165
Hs. ModuleSource_key modfact filefact
165
166
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))
169
173
170
174
let allIds = [ (n, p) | (Right n, ps) <- Map. toList refmap, p <- ps ]
171
175
172
176
-- produce names & declarations
173
177
names <- fmap catMaybes $ forM allIds $ \ (name, (span , dets)) -> if
174
178
| 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
177
182
sort <- case nameModule_maybe name of
178
183
Nothing -> return $ Hs. NameSort_internal byteSpan
179
184
Just {} -> return $ Hs. NameSort_external def
@@ -220,7 +225,7 @@ indexHieFile writer hie = do
220
225
Just <$> mkName name namemod (Hs. NameSort_external def)
221
226
forM maybe_namefact $ \ namefact -> do
222
227
refspans <- forM (Map. toList kindspans) $ \ (kind, spans) -> do
223
- let gleanspans = map ( toByteSpan . srcSpanToSrcRange filefact) spans
228
+ let gleanspans = map toByteSpan spans
224
229
return $ map (Hs. RefSpan kind) gleanspans
225
230
Glean. makeFact @ Hs. Reference $
226
231
Hs. Reference_key namefact (concat refspans)
@@ -263,3 +268,11 @@ indexHieFile writer hie = do
263
268
goDecl TyVarBind {} = Just defaultSpan
264
269
goDecl (ClassTyDecl sp) = sp
265
270
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