Alan Zimmerman pushed to branch wip/az/ghc-cpp at Glasgow Haskell Compiler / GHC

Commits:

2 changed files:

Changes:

  • utils/check-cpp/Macro.hs
    ... ... @@ -64,15 +64,19 @@ expand s str = expanded
    64 64
         toks = case cppLex False str of
    
    65 65
             Left err -> error $ "expand:" ++ show (err, str)
    
    66 66
             Right tks -> tks
    
    67
    -    expanded = combineToks $ map t_str $ expandToks s toks
    
    67
    +    expanded = combineToks $ map t_str $ expandToks maxExpansions s toks
    
    68 68
     
    
    69
    -expandToks :: MacroDefines -> [Token] -> [Token]
    
    70
    -expandToks s ts =
    
    69
    +maxExpansions :: Int
    
    70
    +maxExpansions = 15
    
    71
    +
    
    72
    +expandToks :: Int -> MacroDefines -> [Token] -> [Token]
    
    73
    +expandToks 0 _ ts = error $ "macro_expansion limit (" ++ show maxExpansions ++ ") hit, aborting. ts=" ++ show ts
    
    74
    +expandToks cnt s ts =
    
    71 75
         let
    
    72
    -        (expansionDone, r) = doExpandToks False s ts
    
    76
    +        (!expansionDone, !r) = doExpandToks False s ts
    
    73 77
         in
    
    74 78
             if expansionDone
    
    75
    -            then expandToks s r
    
    79
    +            then expandToks (cnt -1) s r
    
    76 80
                 else r
    
    77 81
     
    
    78 82
     doExpandToks :: Bool -> MacroDefines -> [Token] -> (Bool, [Token])
    
    ... ... @@ -99,13 +103,14 @@ doExpandToks ed s (TIdentifier n : ts) = (ed'', expanded ++ rest)
    99 103
       where
    
    100 104
         (ed', expanded, ts') = case Map.lookup n s of
    
    101 105
             Nothing -> (ed, [TIdentifier n], ts)
    
    102
    -        Just defs -> (ed0, r, rest0)
    
    106
    +        Just defs -> (ed0, r, rest1)
    
    103 107
               where
    
    104 108
                 (args, rest0) = getExpandArgs ts
    
    105
    -            (m_args, rhs) = fromMaybe (Nothing, [TIdentifier n]) (Map.lookup (arg_arity args) defs)
    
    106
    -            (ed0, r) = case m_args of
    
    107
    -                Nothing -> (True, rhs)
    
    108
    -                Just _ -> (True, replace_args args m_args rhs)
    
    109
    +            fallbackArgs = fromMaybe (Nothing, [TIdentifier n]) (Map.lookup Nothing defs)
    
    110
    +            (m_args, rhs) = fromMaybe fallbackArgs (Map.lookup (arg_arity args) defs)
    
    111
    +            (ed0, r, rest1) = case m_args of
    
    112
    +                Nothing -> (True, rhs, ts)
    
    113
    +                Just _ -> (True, replace_args args m_args rhs, rest0)
    
    109 114
         (ed'', rest) = doExpandToks ed' s ts'
    
    110 115
     doExpandToks ed s (t : ts) = (ed', t : r)
    
    111 116
       where
    

  • utils/check-cpp/Main.hs
    ... ... @@ -820,3 +820,21 @@ t35 = do
    820 820
             , "#endif"
    
    821 821
             , ""
    
    822 822
             ]
    
    823
    +
    
    824
    +t36 :: IO ()
    
    825
    +t36 = do
    
    826
    +    dump
    
    827
    +        [ "{-# LANGUAGE GHC_CPP #-}"
    
    828
    +        , "module Example15 where"
    
    829
    +        , "#define MIN_VERSION_Cabal(a,b,c) 1"
    
    830
    +        , ""
    
    831
    +        , "#ifdef MIN_VERSION_Cabal"
    
    832
    +        , "#undef CH_MIN_VERSION_Cabal"
    
    833
    +        , "#define CH_MIN_VERSION_Cabal MIN_VERSION_Cabal"
    
    834
    +        , "#endif"
    
    835
    +        , ""
    
    836
    +        , "#if CH_MIN_VERSION_Cabal(1,22,0)"
    
    837
    +        , "x = 1"
    
    838
    +        , "#endif"
    
    839
    +        , ""
    
    840
    +        ]