Skip to content

Commit dbaf56b

Browse files
committed
Add refs.is_generated to distinguis references from source/generated by ghc
1 parent 8d58bc2 commit dbaf56b

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

src/HieDb/Create.hs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
{-# LANGUAGE ViewPatterns #-}
55
{-# LANGUAGE ScopedTypeVariables #-}
66
{-# LANGUAGE LambdaCase #-}
7+
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
8+
{-# HLINT ignore "Use camelCase" #-}
79
module HieDb.Create where
810

911
import Prelude hiding (mod)
@@ -37,7 +39,7 @@ import HieDb.Types
3739
import HieDb.Utils
3840

3941
sCHEMA_VERSION :: Integer
40-
sCHEMA_VERSION = 8
42+
sCHEMA_VERSION = 9
4143

4244
dB_VERSION :: Integer
4345
dB_VERSION = read (show sCHEMA_VERSION ++ "999" ++ show hieVersion)
@@ -117,6 +119,7 @@ initConn (getConn -> conn) = do
117119
\, sc INTEGER NOT NULL \
118120
\, el INTEGER NOT NULL \
119121
\, ec INTEGER NOT NULL \
122+
\, is_generated BOOLEAN NOT NULL \
120123
\, FOREIGN KEY(hieFile) REFERENCES mods(hieFile) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED \
121124
\)"
122125
execute_ conn "CREATE INDEX IF NOT EXISTS refs_mod ON refs(hieFile)"
@@ -331,27 +334,39 @@ addRefsFromLoaded_unsafe
331334
mod = moduleName smod
332335
uid = moduleUnit smod
333336
smod = hie_module hf
334-
refmap = generateReferencesMap $ getAsts $ hie_asts hf
337+
asts = getAsts $ hie_asts hf
338+
refmapAll = generateReferencesMap asts
339+
refmapSourceOnly = generateReferencesMap $ fmap (dropNodeInfos GeneratedInfo) asts
340+
refmapGeneratedOnly = generateReferencesMap $ fmap (dropNodeInfos SourceInfo) asts
335341
(srcFile, isReal) = case sourceFile of
336342
RealFile f -> (Just f, True)
337343
FakeFile mf -> (mf, False)
338344
modrow = HieModuleRow path (ModuleInfo mod uid isBoot srcFile isReal hash)
339345

346+
-- We want to distinguish between references from source (NodeOrigin is SourceInfo)
347+
-- vs. generated by compiler (NodeOrigin is GeneratedInfo).
348+
-- Unfortunately generateReferencesMap throws away the info about NodeOrigin,
349+
-- so we need to use this to preprocess the ASTs from which the references map is generated.
350+
dropNodeInfos :: NodeOrigin -> HieAST a -> HieAST a
351+
dropNodeInfos originToDrop (Node (SourcedNodeInfo sniMap) sp children) =
352+
let sourceOnlyNodeInfo = SourcedNodeInfo $ M.delete originToDrop sniMap
353+
in Node sourceOnlyNodeInfo sp (map (dropNodeInfos originToDrop) children)
354+
340355
execute conn "INSERT INTO mods VALUES (?,?,?,?,?,?,?)" modrow
341356

342-
let AstInfo rows decls imports = genAstInfo path smod refmap
357+
let AstInfo refsSrc declsSrc importsSrc = genAstInfo path smod SourceInfo refmapSourceOnly
358+
AstInfo refsGen declsGen importsGen = genAstInfo path smod GeneratedInfo refmapGeneratedOnly
343359

344360
unless (skipRefs skipOptions) $
345-
executeMany conn "INSERT INTO refs VALUES (?,?,?,?,?,?,?,?)" rows
361+
executeMany conn "INSERT INTO refs VALUES (?,?,?,?,?,?,?,?,?)" (refsSrc <> refsGen)
346362
unless (skipDecls skipOptions) $
347-
executeMany conn "INSERT INTO decls VALUES (?,?,?,?,?,?,?)" decls
363+
executeMany conn "INSERT INTO decls VALUES (?,?,?,?,?,?,?)" (declsSrc <> declsGen)
348364
unless (skipImports skipOptions) $
349-
executeMany conn "INSERT INTO imports VALUES (?,?,?,?,?,?)" imports
365+
executeMany conn "INSERT INTO imports VALUES (?,?,?,?,?,?)" (importsSrc <> importsGen)
350366

351-
let defs = genDefRow path smod refmap
367+
let defs = genDefRow path smod refmapAll
352368
unless (skipDefs skipOptions) $
353-
forM_ defs $ \def ->
354-
execute conn "INSERT INTO defs VALUES (?,?,?,?,?,?)" def
369+
executeMany conn "INSERT INTO defs VALUES (?,?,?,?,?,?)" defs
355370

356371
let exports = generateExports path $ hie_exports hf
357372
unless (skipExports skipOptions) $

src/HieDb/Types.hs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,17 @@ data RefRow
150150
, refSCol :: Int
151151
, refELine :: Int
152152
, refECol :: Int
153+
, refIsGenerated :: Bool -- ^ True if the reference to this name is generated by GHC (NodeOrigin is GeneratedInfo)
154+
-- False if it comes from the source code (NodeOrigin is SourceInfo)
153155
}
154156

155157
instance ToRow RefRow where
156-
toRow (RefRow a b c d e f g h) = toRow ((a,b,c):.(d,e,f):.(g,h))
158+
toRow (RefRow a b c d e f g h i) = toRow ((a,b,c):.(d,e,f):.(g,h,i))
157159

158160
instance FromRow RefRow where
159161
fromRow = RefRow <$> field <*> field <*> field
160162
<*> field <*> field <*> field
161-
<*> field <*> field
163+
<*> field <*> field <*> field
162164

163165
data DeclRow
164166
= DeclRow
@@ -178,23 +180,23 @@ instance FromRow DeclRow where
178180
fromRow = DeclRow <$> field <*> field <*> field <*> field
179181
<*> field <*> field <*> field
180182

181-
data ImportRow
182-
= ImportRow
183+
data ImportRow
184+
= ImportRow
183185
{ importSrc :: FilePath
184186
, importModuleName :: ModuleName
185-
, importSLine :: Int
186-
, importSCol :: Int
187-
, importELine :: Int
188-
, importECol :: Int
187+
, importSLine :: Int
188+
, importSCol :: Int
189+
, importELine :: Int
190+
, importECol :: Int
189191
}
190192

191-
instance FromRow ImportRow where
192-
fromRow =
193-
ImportRow
194-
<$> field <*> field <*> field <*> field
193+
instance FromRow ImportRow where
194+
fromRow =
195+
ImportRow
196+
<$> field <*> field <*> field <*> field
195197
<*> field <*> field
196198

197-
instance ToRow ImportRow where
199+
instance ToRow ImportRow where
198200
toRow (ImportRow a b c d e f) = toRow ((a,b,c,d):.(e,f))
199201

200202
data TypeName = TypeName

src/HieDb/Utils.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,18 @@ instance Semigroup AstInfo where
179179
instance Monoid AstInfo where
180180
mempty = AstInfo [] [] []
181181

182-
genAstInfo :: FilePath -> Module -> M.Map Identifier [(Span, IdentifierDetails a)] -> AstInfo
183-
genAstInfo path smdl refmap = genRows $ flat $ M.toList refmap
182+
genAstInfo :: FilePath -> Module -> NodeOrigin -> M.Map Identifier [(Span, IdentifierDetails a)] -> AstInfo
183+
genAstInfo path smdl nodeOrigin refmap = genRows $ flat $ M.toList refmap
184184
where
185+
isGenerated = nodeOrigin == GeneratedInfo
185186
flat = concatMap (\(a,xs) -> map (a,) xs)
186-
genRows = foldMap go
187-
go = mkAstInfo
187+
genRows = foldMap mkAstInfo
188188

189189
mkAstInfo x = AstInfo (maybeToList $ goRef x) (maybeToList $ goDec x) (maybeToList $ goImport x)
190190

191191
goRef (Right name, (sp,_))
192192
| Just mod <- nameModule_maybe name = Just $
193-
RefRow path occ (moduleName mod) (moduleUnit mod) sl sc el ec
193+
RefRow path occ (moduleName mod) (moduleUnit mod) sl sc el ec isGenerated
194194
where
195195
occ = nameOccName name
196196
sl = srcSpanStartLine sp

0 commit comments

Comments
 (0)