@@ -2,24 +2,30 @@ module HsBindgen.Frontend.Pass.Select (
22 selectDecls
33 ) where
44
5+ import Data.List (sortBy )
56import Data.Map.Strict qualified as Map
7+ import Data.Ord (comparing )
68import Data.Set ((\\) )
79import Data.Set qualified as Set
810
911import Clang.HighLevel.Types
12+ import Clang.Paths
1013
1114import HsBindgen.Errors (panicPure )
1215import HsBindgen.Frontend.Analysis.DeclIndex (DeclIndex (.. ), Match ,
1316 selectDeclIndex )
1417import HsBindgen.Frontend.Analysis.DeclIndex qualified as DeclIndex
1518import HsBindgen.Frontend.Analysis.DeclUseGraph qualified as DeclUseGraph
19+ import HsBindgen.Frontend.Analysis.IncludeGraph (IncludeGraph )
20+ import HsBindgen.Frontend.Analysis.IncludeGraph qualified as IncludeGraph
1621import HsBindgen.Frontend.Analysis.UseDeclGraph (UseDeclGraph )
1722import HsBindgen.Frontend.Analysis.UseDeclGraph qualified as UseDeclGraph
1823import HsBindgen.Frontend.AST.Coerce (CoercePass (coercePass ))
1924import HsBindgen.Frontend.AST.Internal qualified as C
2025import HsBindgen.Frontend.Naming qualified as C
2126import HsBindgen.Frontend.Pass
2227import HsBindgen.Frontend.Pass.ConstructTranslationUnit.IsPass
28+ import HsBindgen.Frontend.Pass.HandleMacros.Error
2329import HsBindgen.Frontend.Pass.Parse.IsPass
2430import HsBindgen.Frontend.Pass.ResolveBindingSpecs.IsPass
2531import 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