Skip to content

Commit c60caa9

Browse files
authored
Merge pull request #292 from camfort/improveReporting
Improve reporting, mostly around type information but also source spans
2 parents 074296b + a767a07 commit c60caa9

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

app/Main.hs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ main = do
118118
Right a -> a
119119
outfmt = outputFormat opts
120120
mmap = combinedModuleMap mods
121-
tenv = combinedTypeEnv mods
121+
tenv = stripExtended $ combinedTypeEnv mods
122122
pvm = combinedParamVarMap mods
123123

124124
let runTypes = analyseAndCheckTypesWithEnv tenv . analyseRenamesWithModuleMap mmap . initAnalysis
@@ -143,7 +143,7 @@ main = do
143143
Lex -> ioError $ userError $ usageInfo programName options
144144
Parse -> pp parsedPF
145145
Typecheck -> let (pf, _, errs) = runTypes parsedPF in
146-
printTypeErrors errs >> printTypes (extractTypeEnv pf)
146+
printTypeErrors errs >> printTypes (extractTypeEnvExtended pf)
147147
Rename -> pp $ runRenamer parsedPF
148148
BBlocks -> putStrLn $ runBBlocks parsedPF
149149
SuperGraph -> putStrLn $ runSuperGraph parsedPF
@@ -220,7 +220,7 @@ compileFileToMod mvers mods path moutfile = do
220220
contents <- flexReadFile path
221221
let version = fromMaybe (deduceFortranVersion path) mvers
222222
mmap = combinedModuleMap mods
223-
tenv = combinedTypeEnv mods
223+
tenv = stripExtended $ combinedTypeEnv mods
224224
runCompile = genModFile . fst . analyseTypesWithEnv tenv . analyseRenamesWithModuleMap mmap . initAnalysis
225225
parsedPF <-
226226
case (Parser.byVerWithMods mods version) path contents of
@@ -295,12 +295,14 @@ showStringMap :: StringMap -> String
295295
showStringMap = showGenericMap
296296
showModuleMap :: ModuleMap -> String
297297
showModuleMap = concatMap (\ (n, m) -> show n ++ ":\n" ++ (unlines . map (" "++) . lines . showGenericMap $ m)) . M.toList
298-
showTypes :: TypeEnv -> String
298+
showTypes :: TypeEnvExtended -> String
299299
showTypes tenv =
300-
flip concatMap (M.toList tenv) $
301-
\ (name, IDType { idVType = vt, idCType = ct }) ->
302-
printf "%s\t\t%s %s\n" name (drop 1 $ maybe " -" show vt) (drop 2 $ maybe " " show ct)
303-
printTypes :: TypeEnv -> IO ()
300+
let sortedInfo = sortBy (\(_, (_, sp1, _)) (_, (_, sp2, _)) -> compare sp1 sp2) $ M.toList tenv
301+
in
302+
flip concatMap sortedInfo $
303+
\ (_, (name, sp, IDType { idVType = vt, idCType = ct })) ->
304+
printf "%s\t %s\t\t%s %s\n" (show $ ssFrom sp) name (drop 1 $ maybe " -" show vt) (drop 2 $ maybe " " show ct)
305+
printTypes :: TypeEnvExtended -> IO ()
304306
printTypes = putStrLn . showTypes
305307
showTypeErrors :: [TypeError] -> String
306308
showTypeErrors errs = unlines [ show ss ++ ": " ++ msg | (msg, ss) <- sortBy (comparing snd) errs ]

src/Language/Fortran/Analysis/Types.hs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ module Language.Fortran.Analysis.Types
22
( analyseTypes
33
, analyseTypesWithEnv
44
, analyseAndCheckTypesWithEnv
5+
, stripExtended
56
, extractTypeEnv
7+
, extractTypeEnvExtended
68
, TypeEnv
9+
, TypeEnvExtended
710
, TypeError
811
, deriveSemTypeFromDeclaration
912
, deriveSemTypeFromTypeSpec
@@ -36,6 +39,11 @@ import Language.Fortran.Version (FortranVersion(..))
3639

3740
-- | Mapping of names to type information.
3841
type TypeEnv = M.Map Name IDType
42+
-- | Mapping of names to type information with more information about the source
43+
type TypeEnvExtended = M.Map Name (Name, SrcSpan, IDType)
44+
45+
stripExtended :: TypeEnvExtended -> TypeEnv
46+
stripExtended = M.map (\(_, _, t) -> t)
3947

4048
-- | Information about a detected type error.
4149
type TypeError = (String, SrcSpan)
@@ -121,6 +129,24 @@ extractTypeEnv pf = M.union puEnv expEnv
121129
, let n = varName e
122130
, ty <- maybeToList (idType (getAnnotation e)) ]
123131

132+
extractTypeEnvExtended :: forall a. Data a => ProgramFile (Analysis a) -> TypeEnvExtended
133+
extractTypeEnvExtended pf = M.union puEnv expEnv
134+
where
135+
puEnv = M.fromList [ (n, (srcName, getSpan pu, ty)) | pu <- universeBi pf :: [ProgramUnit (Analysis a)]
136+
, Named n <- [puName pu]
137+
, Named srcName <- [puSrcName pu]
138+
, ty <- maybeToList (idType (getAnnotation pu)) ]
139+
expEnv = M.fromList [ (n, (srcName e, sp, ty)) | e@(ExpValue _ _ ValVariable{}) <- universeBi pf :: [Expression (Analysis a)]
140+
, let n = varName e
141+
, sp <- getDeclarator n
142+
, ty <- maybeToList (idType (getAnnotation e)) ]
143+
getDeclarator v' =
144+
[ sp | d@(Declarator _ sp ev _ _ _) <- universeBi pf :: [Declarator (Analysis a)]
145+
, varName ev == v' ]
146+
147+
148+
149+
124150
type TransType f g a = (f (Analysis a) -> Infer (f (Analysis a))) -> g (Analysis a) -> Infer (g (Analysis a))
125151
annotateTypes :: Data a => ProgramFile (Analysis a) -> Infer (ProgramFile (Analysis a))
126152
annotateTypes pf = (transformBiM :: Data a => TransType Expression ProgramFile a) annotateExpression pf >>=

src/Language/Fortran/Transformation/Monad.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Language.Fortran.Transformation.Monad
99
import Prelude hiding (lookup)
1010
import Control.Monad.State.Lazy hiding (state)
1111
import Data.Data
12+
import qualified Data.Map as M
1213

1314
import Language.Fortran.Analysis
1415
import Language.Fortran.Analysis.Types
@@ -22,13 +23,14 @@ type Transform a = State (TransformationState a)
2223

2324
runTransform
2425
:: Data a
25-
=> TypeEnv -> ModuleMap -> Transform a () -> ProgramFile a -> ProgramFile a
26+
=> TypeEnvExtended -> ModuleMap -> Transform a () -> ProgramFile a -> ProgramFile a
2627
runTransform env mmap trans pf =
2728
stripAnalysis . transProgramFile . execState trans $ initState
2829
where
29-
(pf', _) = analyseTypesWithEnv env . analyseRenamesWithModuleMap mmap . initAnalysis $ pf
30+
(pf', _) = analyseTypesWithEnv (removeExtendedInfo env) . analyseRenamesWithModuleMap mmap . initAnalysis $ pf
3031
initState = TransformationState
3132
{ transProgramFile = pf' }
33+
removeExtendedInfo = M.map (\(_, _, t) -> t)
3234

3335
getProgramFile :: Transform a (ProgramFile (Analysis a))
3436
getProgramFile = gets transProgramFile

src/Language/Fortran/Util/ModFile.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ data ModFile = ModFile { mfFilename :: String
120120
, mfStringMap :: StringMap
121121
, mfModuleMap :: FAR.ModuleMap
122122
, mfDeclMap :: DeclMap
123-
, mfTypeEnv :: FAT.TypeEnv
123+
, mfTypeEnv :: FAT.TypeEnvExtended
124124
, mfParamVarMap :: ParamVarMap
125125
, mfOtherData :: M.Map String LB.ByteString
126126
}
@@ -145,7 +145,7 @@ emptyModFile = ModFile "" M.empty M.empty M.empty M.empty M.empty M.empty
145145
regenModFile :: forall a. (Data a) => F.ProgramFile (FA.Analysis a) -> ModFile -> ModFile
146146
regenModFile pf mf = mf { mfModuleMap = extractModuleMap pf
147147
, mfDeclMap = extractDeclMap pf
148-
, mfTypeEnv = FAT.extractTypeEnv pf
148+
, mfTypeEnv = FAT.extractTypeEnvExtended pf
149149
, mfParamVarMap = extractParamVarMap pf
150150
, mfFilename = F.pfGetFilename pf }
151151

@@ -227,7 +227,7 @@ localisedModuleMap = M.map (M.filter (not . FA.isImported . snd))
227227

228228
-- | Extract the combined module map from a set of ModFiles. Useful
229229
-- for parsing a Fortran file in a large context of other modules.
230-
combinedTypeEnv :: ModFiles -> FAT.TypeEnv
230+
combinedTypeEnv :: ModFiles -> FAT.TypeEnvExtended
231231
combinedTypeEnv = M.unions . map mfTypeEnv
232232

233233
-- | Extract the combined declaration map from a set of

src/Language/Fortran/Util/Position.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ instance Binary Position
2626
instance NFData Position
2727

2828
instance Show Position where
29-
show (Position _ c l _ _) = show l ++ ':' : show c
29+
-- Column number decrement by 1 as the lexer generates column numbers
30+
-- starting at position 1
31+
-- See PR https://github.com/camfort/fortran-src/pull/292
32+
show (Position _ c l _ _) = show l ++ ':' : show (c - 1)
3033

3134
initPosition :: Position
3235
initPosition = Position

0 commit comments

Comments
 (0)