Debian Codegraph @

src-ghc/Clash/GHC/Util.hs

raw
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE QuasiQuotes #-}
3
4 module Clash.GHC.Util
5 ( module Clash.GHC.Util
6 #if MIN_VERSION_ghc(9,2,0)
7 , module X
8 #endif
9 )
10 where
11
12 #if MIN_VERSION_ghc(9,8,0)
13 import GHC.Driver.Errors.Types (GhcMessage(GhcUnknownMessage))
14 import GHC.Utils.Outputable as X (showPprUnsafe, showSDocUnsafe)
15 import GHC.Utils.Outputable (SDoc, neverQualify)
16 import GHC.Utils.Error (mkErrorMsgEnvelope, mkPlainError)
17 import GHC.Plugins
18 (DynFlags, SourceError, ($$), blankLine, empty, isGoodSrcSpan, liftIO,
19 noSrcSpan, text, throwOneError)
20 import GHC.Types.Error (mkSimpleUnknownDiagnostic)
21 #elif MIN_VERSION_ghc(9,6,0)
22 import GHC.Driver.Errors.Types (GhcMessage(GhcUnknownMessage))
23 import GHC.Utils.Outputable as X (showPprUnsafe, showSDocUnsafe)
24 import GHC.Utils.Outputable (SDoc, neverQualify)
25 import GHC.Utils.Error (mkErrorMsgEnvelope, mkPlainError)
26 import GHC.Plugins
27 (DynFlags, SourceError, ($$), blankLine, empty, isGoodSrcSpan, liftIO,
28 noSrcSpan, text, throwOneError)
29 import GHC.Types.Error (UnknownDiagnostic(..))
30 #elif MIN_VERSION_ghc(9,4,0)
31 import GHC.Driver.Errors.Types (GhcMessage(GhcUnknownMessage))
32 import GHC.Utils.Outputable as X (showPprUnsafe, showSDocUnsafe)
33 import GHC.Utils.Outputable (SDoc, neverQualify)
34 import GHC.Utils.Error (mkErrorMsgEnvelope, mkPlainError)
35 import GHC.Plugins
36 (DynFlags, SourceError, ($$), blankLine, empty, isGoodSrcSpan, liftIO,
37 noSrcSpan, text, throwOneError)
38 #elif MIN_VERSION_ghc(9,2,0)
39 import GHC.Utils.Outputable as X (showPprUnsafe, showSDocUnsafe)
40 import GHC.Utils.Outputable (SDoc, neverQualify)
41 import GHC.Utils.Error (mkMsgEnvelope)
42 import GHC.Plugins
43 (DynFlags, SourceError, ($$), blankLine, empty, isGoodSrcSpan, liftIO,
44 noSrcSpan, text, throwOneError)
45 #elif MIN_VERSION_ghc(9,0,0)
46 import GHC.Driver.Session (unsafeGlobalDynFlags)
47 import GHC.Utils.Outputable (Outputable, SDoc, showPpr, showSDoc)
48 import GHC.Utils.Error (mkPlainErrMsg)
49 import GHC.Plugins
50 (DynFlags, SourceError, ($$), blankLine, empty, isGoodSrcSpan, liftIO,
51 noSrcSpan, text, throwOneError)
52 #else
53 import DynFlags (unsafeGlobalDynFlags)
54 import Outputable (Outputable, SDoc, showPpr, showSDoc)
55 import ErrUtils (mkPlainErrMsg)
56 import GhcPlugins (DynFlags, SourceError, ($$), blankLine, empty, isGoodSrcSpan, liftIO, noSrcSpan, text, throwOneError)
57 #endif
58 import GHC (GhcMonad(..), printException)
59
60 import Control.Exception (Exception(..), ErrorCall(..))
61 import GHC.Exception (SomeException)
62 import System.Exit (ExitCode(ExitFailure), exitWith)
63
64 import Clash.Util (ClashException(..))
65 import Clash.Util.Interpolate (i)
66 import Clash.Driver.Types (ClashOpts(..))
67
68 -- | Like 'lines', but returning a horizontally spaced SDoc instead of a list:
69 --
70 -- >>> textLines "a\nb"
71 -- a $$ b
72 textLines :: String -> SDoc
73 textLines s = foldl1 ($$) (map text (lines s))
74
75 handleClashException
76 :: GhcMonad m
77 => DynFlags
78 -> ClashOpts
79 -> SomeException
80 -> m a
81 #if MIN_VERSION_ghc(9,2,0)
82 handleClashException _df opts e = case fromException e of
83 #else
84 handleClashException df opts e = case fromException e of
85 #endif
86 Just (ClashException sp s eM) -> do
87 let srcInfo' | isGoodSrcSpan sp = srcInfo
88 | otherwise = empty
89 throwOneError
90 #if MIN_VERSION_ghc(9,8,0)
91 (mkErrorMsgEnvelope sp neverQualify $ GhcUnknownMessage $ mkSimpleUnknownDiagnostic $ mkPlainError []
92 #elif MIN_VERSION_ghc(9,6,0)
93 (mkErrorMsgEnvelope sp neverQualify $ GhcUnknownMessage $ UnknownDiagnostic $ mkPlainError []
94 #elif MIN_VERSION_ghc(9,4,0)
95 (mkErrorMsgEnvelope sp neverQualify $ GhcUnknownMessage $ mkPlainError []
96 #elif MIN_VERSION_ghc(9,2,0)
97 (mkMsgEnvelope sp neverQualify
98 #else
99 (mkPlainErrMsg df sp
100 #endif
101 (blankLine $$ textLines s $$ blankLine $$ srcInfo' $$ showExtra (opt_errorExtra opts) eM))
102 _ -> case fromException e of
103 Just (ErrorCallWithLocation _ _) ->
104 throwOneError
105 #if MIN_VERSION_ghc(9,8,0)
106 (mkErrorMsgEnvelope noSrcSpan neverQualify $ GhcUnknownMessage $ mkSimpleUnknownDiagnostic $ mkPlainError []
107 #elif MIN_VERSION_ghc(9,6,0)
108 (mkErrorMsgEnvelope noSrcSpan neverQualify $ GhcUnknownMessage $ UnknownDiagnostic $ mkPlainError []
109 #elif MIN_VERSION_ghc(9,4,0)
110 (mkErrorMsgEnvelope noSrcSpan neverQualify $ GhcUnknownMessage $ mkPlainError []
111 #elif MIN_VERSION_ghc(9,2,0)
112 (mkMsgEnvelope noSrcSpan neverQualify
113 #else
114 (mkPlainErrMsg df noSrcSpan
115 #endif
116 (text "Clash error call:" $$ textLines (show e)))
117 _ -> case fromException e of
118 Just (e' :: SourceError) -> do
119 GHC.printException e'
120 liftIO $ exitWith (ExitFailure 1)
121 _ -> throwOneError
122 #if MIN_VERSION_ghc(9,8,0)
123 (mkErrorMsgEnvelope noSrcSpan neverQualify $ GhcUnknownMessage $ mkSimpleUnknownDiagnostic $ mkPlainError []
124 #elif MIN_VERSION_ghc(9,6,0)
125 (mkErrorMsgEnvelope noSrcSpan neverQualify $ GhcUnknownMessage $ UnknownDiagnostic $ mkPlainError []
126 #elif MIN_VERSION_ghc(9,4,0)
127 (mkErrorMsgEnvelope noSrcSpan neverQualify $ GhcUnknownMessage $ mkPlainError []
128 #elif MIN_VERSION_ghc(9,2,0)
129 (mkMsgEnvelope noSrcSpan neverQualify
130 #else
131 (mkPlainErrMsg df noSrcSpan
132 #endif
133 (text "Other error:" $$ textLines (displayException e)))
134 where
135 srcInfo = textLines [i|
136 The source location of the error is not exact, only indicative, as it
137 is acquired after optimizations. The actual location of the error can be
138 in a function that is inlined. To prevent inlining of those functions,
139 annotate them with a NOINLINE pragma.
140 |]
141
142 showExtra False (Just _) =
143 blankLine $$
144 text "This error contains additional information, rerun with '-fclash-error-extra' to show this information."
145 showExtra True (Just msg) =
146 blankLine $$
147 text "Additional information:" $$ blankLine $$
148 textLines msg
149 showExtra _ _ = empty
150
151 #if !MIN_VERSION_ghc(9,2,0)
152 showPprUnsafe :: Outputable a => a -> String
153 showPprUnsafe = showPpr unsafeGlobalDynFlags
154
155 showSDocUnsafe :: SDoc -> String
156 showSDocUnsafe = showSDoc unsafeGlobalDynFlags
157 #endif