Skip to content

Commit 7327dd5

Browse files
authored
Merge pull request #1303 from well-typed/dom/1294/sort-select-msgs
Sort select messages before emitting them
2 parents f2be944 + f082e73 commit 7327dd5

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

hs-bindgen/src-internal/HsBindgen/Frontend/Analysis/IncludeGraph.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module HsBindgen.Frontend.Analysis.IncludeGraph (
1515
-- * Query
1616
, reaches
1717
, toSortedList
18+
, toOrderMap
1819
, getIncludes
1920
-- * Debugging
2021
, Predicate
@@ -24,12 +25,13 @@ module HsBindgen.Frontend.Analysis.IncludeGraph (
2425
import Data.DynGraph.Labelled (DynGraph)
2526
import Data.DynGraph.Labelled qualified as DynGraph
2627
import Data.List qualified as List
27-
import Data.Set (Set)
28+
import Data.Map.Strict qualified as Map
2829

2930
import Clang.Paths
3031

3132
import HsBindgen.Frontend.RootHeader (HashIncludeArg (getHashIncludeArg))
3233
import HsBindgen.Frontend.RootHeader qualified as RootHeader
34+
import HsBindgen.Imports
3335

3436
{-------------------------------------------------------------------------------
3537
Definition
@@ -91,6 +93,9 @@ toSortedList :: IncludeGraph -> [SourcePath]
9193
toSortedList (IncludeGraph graph) =
9294
List.delete RootHeader.name $ DynGraph.topSort graph
9395

96+
toOrderMap :: IncludeGraph -> Map SourcePath Int
97+
toOrderMap graph = Map.fromList (zip (toSortedList graph) [0..])
98+
9499
getIncludes ::
95100
IncludeGraph
96101
-> SourcePath

hs-bindgen/src-internal/HsBindgen/Frontend/Analysis/UseDeclGraph.hs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,10 @@ toDynGraph = unwrap
6363

6464
fromDecls :: IncludeGraph -> [C.Decl Parse] -> UseDeclGraph
6565
fromDecls includeGraph decls =
66-
fromSortedDecls $ List.sortOn (annSortKey sourceMap) decls
66+
fromSortedDecls $ List.sortOn (annSortKey orderMap) decls
6767
where
68-
sourcePaths :: [SourcePath]
69-
sourcePaths = IncludeGraph.toSortedList includeGraph
70-
71-
sourceMap :: Map SourcePath Int
72-
sourceMap = Map.fromList $ zip sourcePaths [0..]
68+
orderMap :: Map SourcePath Int
69+
orderMap = IncludeGraph.toOrderMap includeGraph
7370

7471
fromSortedDecls :: [C.Decl Parse] -> UseDeclGraph
7572
fromSortedDecls decls = Wrap $

hs-bindgen/src-internal/HsBindgen/Frontend/Pass/Select.hs

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,30 @@ module HsBindgen.Frontend.Pass.Select (
22
selectDecls
33
) where
44

5+
import Data.List (sortBy)
56
import Data.Map.Strict qualified as Map
7+
import Data.Ord (comparing)
68
import Data.Set ((\\))
79
import Data.Set qualified as Set
810

911
import Clang.HighLevel.Types
12+
import Clang.Paths
1013

1114
import HsBindgen.Errors (panicPure)
1215
import HsBindgen.Frontend.Analysis.DeclIndex (DeclIndex (..), Match,
1316
selectDeclIndex)
1417
import HsBindgen.Frontend.Analysis.DeclIndex qualified as DeclIndex
1518
import HsBindgen.Frontend.Analysis.DeclUseGraph qualified as DeclUseGraph
19+
import HsBindgen.Frontend.Analysis.IncludeGraph (IncludeGraph)
20+
import HsBindgen.Frontend.Analysis.IncludeGraph qualified as IncludeGraph
1621
import HsBindgen.Frontend.Analysis.UseDeclGraph (UseDeclGraph)
1722
import HsBindgen.Frontend.Analysis.UseDeclGraph qualified as UseDeclGraph
1823
import HsBindgen.Frontend.AST.Coerce (CoercePass (coercePass))
1924
import HsBindgen.Frontend.AST.Internal qualified as C
2025
import HsBindgen.Frontend.Naming qualified as C
2126
import HsBindgen.Frontend.Pass
2227
import HsBindgen.Frontend.Pass.ConstructTranslationUnit.IsPass
28+
import HsBindgen.Frontend.Pass.HandleMacros.Error
2329
import HsBindgen.Frontend.Pass.Parse.IsPass
2430
import HsBindgen.Frontend.Pass.ResolveBindingSpecs.IsPass
2531
import HsBindgen.Frontend.Pass.Select.IsPass
@@ -164,11 +170,15 @@ selectDecls
164170
, C.unitAnn = unitAnn
165171
}
166172

167-
in ( unitSelect
168-
, selectMsgs
173+
msgs :: [Msg Select]
174+
msgs =
175+
selectMsgs
176+
++ getDelayedMsgs selectedIndex
169177
-- If there were no predicate matches we issue a warning to the user.
170178
++ [ SelectNoDeclarationsMatched | Set.null rootIds ]
171-
++ getDelayedMsgs selectedIndex
179+
180+
in ( unitSelect
181+
, sortSelectMsgs unitIncludeGraph msgs
172182
)
173183
where
174184
notAttempted, failed, failedMacros :: Set DeclId
@@ -320,3 +330,56 @@ getDelayedMsgs DeclIndex{..} = concat [
320330

321331
getMsgss :: (a -> [b]) -> Map k a -> [b]
322332
getMsgss f = concatMap f . Map.elems
333+
334+
{-------------------------------------------------------------------------------
335+
Sort messages
336+
-------------------------------------------------------------------------------}
337+
338+
compareByOrder :: Map SourcePath Int -> SourcePath -> SourcePath -> Ordering
339+
compareByOrder xs x y =
340+
let ix = lookupUnsafe x
341+
iy = lookupUnsafe y
342+
in compare ix iy
343+
where
344+
lookupUnsafe z = case Map.lookup z xs of
345+
Nothing -> panicPure $ "unknown source path: " <> show z
346+
Just v -> v
347+
348+
compareSingleLocs :: Map SourcePath Int -> SingleLoc -> SingleLoc -> Ordering
349+
compareSingleLocs xs x y =
350+
case compareByOrder xs (singleLocPath x) (singleLocPath y) of
351+
LT -> LT
352+
EQ -> comparing getLineCol x y
353+
GT -> GT
354+
where
355+
getLineCol :: SingleLoc -> (Int, Int)
356+
getLineCol z = (singleLocLine z, singleLocColumn z)
357+
358+
getSingleLoc :: Msg Select -> Maybe SingleLoc
359+
getSingleLoc = \case
360+
SelectStatusInfo _ d -> fromD d
361+
TransitiveDependencyOfDeclarationUnavailable _ _ d -> fromD d
362+
SelectDeprecated d -> fromD d
363+
SelectParseSuccess m -> fromM m
364+
SelectParseNotAttempted (ParseNotAttempted m) -> fromM m
365+
SelectParseFailure (ParseFailure m) -> fromM m
366+
SelectMacroFailure (HandleMacrosParseMsg m) -> fromM m
367+
SelectNoDeclarationsMatched -> Nothing
368+
where
369+
fromD = Just . C.declLoc . C.declInfo
370+
fromM = Just . loc
371+
372+
compareMsgs :: Map SourcePath Int -> Msg Select -> Msg Select -> Ordering
373+
compareMsgs orderMap x y =
374+
case (getSingleLoc x, getSingleLoc y) of
375+
(Just lx, Just ly) -> compareSingleLocs orderMap lx ly
376+
-- Sort messages not attached to a declaration to the back.
377+
(Nothing, _ ) -> GT
378+
(_ , Nothing) -> LT
379+
380+
sortSelectMsgs :: IncludeGraph -> [Msg Select] -> [Msg Select]
381+
sortSelectMsgs includeGraph = sortBy (compareMsgs orderMap)
382+
where
383+
-- Compute the order map once.
384+
orderMap :: Map SourcePath Int
385+
orderMap = IncludeGraph.toOrderMap includeGraph

0 commit comments

Comments
 (0)