Skip to content
Snippets Groups Projects
Commit 15acce32 authored by chrg's avatar chrg
Browse files

Work work!

parent 0f54a4c2
No related branches found
No related tags found
No related merge requests found
Showing
with 185 additions and 43 deletions
......@@ -165,35 +165,41 @@ reduceCExternalDeclaration r ctx = case r of
pure (Just $ C.CFDefExt <$> reduceCFunDef fun ctx, ctx)
| otherwise ->
case functionName fun of
Just fid -> do
let nctx =
ctx & foldr \case
(Just t, Just i) -> addInlineExpr i (IEKeep t)
(Nothing, Just i) -> addInlineExpr i IEDelete
(_, Nothing) -> id
let red fun' ps = reduceCFunDef fun' (nctx ps)
case Map.lookup fid . inlineExprs $ ctx of
Just (IEKeep (CTFun args)) -> do
(fun', ps) <- reduceParamsTo args fun
pure
( Just (C.CFDefExt <$> red fun' ps)
, addInlineExpr fid (IEKeep (CTFun (map fst ps))) ctx
)
_ow -> do
split
("remove function " <> C.identToString fid, C.posOf r)
(pure (Nothing, addInlineExpr fid IEDelete ctx))
do
(fun', ps) <- reduceParams ctx fun
Just fid
| shouldDeleteFunction ctx fun -> do
pure (Nothing, addInlineExpr fid IEDelete ctx)
| otherwise -> do
let nctx =
ctx & foldr \case
(Just t, Just i) -> addInlineExpr i (IEKeep t)
(Nothing, Just i) -> addInlineExpr i IEDelete
(_, Nothing) -> id
let red fun' ps = reduceCFunDef fun' (nctx ps)
case Map.lookup fid . inlineExprs $ ctx of
Just (IEKeep (CTFun args)) -> do
(fun', ps) <- reduceParamsTo args fun
pure
( Just (C.CFDefExt <$> red fun' ps)
, addInlineExpr fid (IEKeep (CTFun (map fst ps))) ctx
)
Nothing ->
split
("remove function", C.posOf r)
(pure (Nothing, ctx))
(pure (Just (C.CFDefExt <$> reduceCFunDef fun ctx), ctx))
_ow -> do
split
("remove function " <> C.identToString fid, C.posOf r)
(pure (Nothing, addInlineExpr fid IEDelete ctx))
do
(fun', ps) <- reduceParams ctx fun
pure
( Just (C.CFDefExt <$> red fun' ps)
, addInlineExpr fid (IEKeep (CTFun (map fst ps))) ctx
)
Nothing
| shouldDeleteFunction ctx fun -> do
pure (Nothing, ctx)
| otherwise -> do
split
("remove function", C.posOf r)
(pure (Nothing, ctx))
(pure (Just (C.CFDefExt <$> reduceCFunDef fun ctx), ctx))
C.CDeclExt decl -> do
(decl', ctx') <- handleDecl decl ctx
case decl' of
......@@ -600,21 +606,26 @@ reduceCStatement smt labs ctx = case smt of
C.CFor e1 e2 e3 s ni -> do
(me1', ctx') <- case e1 of
C.CForDecl d@(C.CDecl rec decl ni') -> do
(decl', ctx') <- foldr (reduceCDeclarationItem (shouldDeleteDeclaration ctx d) (ctype ctx rec)) (pure ([], ctx)) decl
(decl', ctx') <-
foldr
(reduceCDeclarationItem (shouldDeleteDeclaration ctx d) (ctype ctx rec))
(pure ([], ctx))
decl
res <-
if null decl'
then
whenSplit
(AllowEmptyDeclarations `isIn` ctx')
("remove empty declaration", C.posOf ni')
(pure Nothing)
(pure $ Just $ C.CForDecl (C.CDecl rec decl' ni'))
if AllowEmptyDeclarations `isIn` ctx'
then
split
("remove empty declaration", C.posOf ni')
(pure Nothing)
(pure $ Just $ C.CForDecl (C.CDecl rec decl' ni'))
else pure Nothing
else pure $ Just $ C.CForDecl (C.CDecl rec decl' ni')
pure (res, ctx')
C.CForInitializing e -> do
e' <- maybeSplit ("remove initializer", C.posOf ni) (e >>= \e' -> reduceCExpr e' ctx)
whenSplit
(AllowEmptyDeclarations `isIn` ctx)
split
("remove empty declaration", C.posOf ni)
(pure (Nothing, ctx))
(pure (Just $ C.CForInitializing e', ctx))
......@@ -867,10 +878,14 @@ inlineTypeDefsCDeclaration decl ctx =
C.CDecl (inlineTypeDefsSpecs items ctx) (map (`inlineTypeDefsCDI` ctx) decli) ni
a -> don'tHandle a
shouldDeleteFunction :: Context -> C.CFunctionDef C.NodeInfo -> Bool
shouldDeleteFunction ctx (C.CFunDef spec _ _ _ _) =
any (shouldDeleteDeclSpec ctx) spec
shouldDeleteDeclaration :: Context -> C.CDeclaration C.NodeInfo -> Bool
shouldDeleteDeclaration ctx decl =
case decl of
C.CDecl items decli _ -> any shouldDeleteDeclSpec items || any shouldDeleteDeclItem decli
C.CDecl items decli _ -> any (shouldDeleteDeclSpec ctx) items || any shouldDeleteDeclItem decli
a -> don'tHandle a
where
shouldDeleteDeclItem = \case
......@@ -887,15 +902,16 @@ shouldDeleteDeclaration ctx decl =
C.CPtrDeclr _ _ -> False
a -> don'tHandle a
shouldDeleteDeclSpec = \case
C.CTypeSpec (C.CSUType (C.CStruct _ (Just idx) Nothing _ _) _) ->
case Map.lookup idx . structs $ ctx of
Just ISDelete -> True
Just ISKeep -> False
Nothing -> error ("could not find struct:" <> show idx)
C.CTypeSpec (C.CSUType (C.CStruct _ _ (Just c) _ _) _) ->
any (shouldDeleteDeclaration ctx) c
_ow -> False
shouldDeleteDeclSpec :: Context -> C.CDeclarationSpecifier C.NodeInfo -> Bool
shouldDeleteDeclSpec ctx = \case
C.CTypeSpec (C.CSUType (C.CStruct _ (Just idx) Nothing _ _) _) ->
case Map.lookup idx . structs $ ctx of
Just ISDelete -> True
Just ISKeep -> False
Nothing -> error ("could not find struct:" <> show idx)
C.CTypeSpec (C.CSUType (C.CStruct _ _ (Just c) _ _) _) ->
any (shouldDeleteDeclaration ctx) c
_ow -> False
inlineTypeDefsSpecs :: [C.CDeclarationSpecifier C.NodeInfo] -> Context -> [C.CDeclarationSpecifier C.NodeInfo]
inlineTypeDefsSpecs r ctx =
......
......@@ -3,6 +3,7 @@
// 0 remove initializer at ("test/cases/small/for.c": line 4)
// 0 reduce to left at ("test/cases/small/for.c": line 4)
// 0 reduce to right at ("test/cases/small/for.c": line 4)
// 0 remove empty declaration at ("test/cases/small/for.c": line 4)
// 0 remove empty compound at ("test/cases/small/for.c": line 4)
static int a = 0;
......
......@@ -3,6 +3,7 @@
// 0 remove initializer at ("test/cases/small/for.c": line 4)
// 0 reduce to left at ("test/cases/small/for.c": line 4)
// 0 reduce to right at ("test/cases/small/for.c": line 4)
// 0 remove empty declaration at ("test/cases/small/for.c": line 4)
// 1 remove empty compound at ("test/cases/small/for.c": line 4)
static int a = 0;
......
// 0 inline variable a at ("test/cases/small/for.c": line 1)
// 0 remove static at ("test/cases/small/for.c": line 1)
// 0 remove initializer at ("test/cases/small/for.c": line 4)
// 0 reduce to left at ("test/cases/small/for.c": line 4)
// 0 reduce to right at ("test/cases/small/for.c": line 4)
// 1 remove empty declaration at ("test/cases/small/for.c": line 4)
// 0 remove empty compound at ("test/cases/small/for.c": line 4)
// 0 remove the for loop at ("test/cases/small/for.c": line 4)
static int a = 0;
int main()
{
for (;;)
{
}
}
// 0 inline variable a at ("test/cases/small/for.c": line 1)
// 0 remove static at ("test/cases/small/for.c": line 1)
// 0 remove initializer at ("test/cases/small/for.c": line 4)
// 0 reduce to left at ("test/cases/small/for.c": line 4)
// 0 reduce to right at ("test/cases/small/for.c": line 4)
// 1 remove empty declaration at ("test/cases/small/for.c": line 4)
// 0 remove empty compound at ("test/cases/small/for.c": line 4)
// 1 remove the for loop at ("test/cases/small/for.c": line 4)
// 0 expand compound statment at ("test/cases/small/for.c": line 4)
static int a = 0;
int main()
{
{
}
}
// 0 inline variable a at ("test/cases/small/for.c": line 1)
// 0 remove static at ("test/cases/small/for.c": line 1)
// 0 remove initializer at ("test/cases/small/for.c": line 4)
// 0 reduce to left at ("test/cases/small/for.c": line 4)
// 0 reduce to right at ("test/cases/small/for.c": line 4)
// 1 remove empty declaration at ("test/cases/small/for.c": line 4)
// 0 remove empty compound at ("test/cases/small/for.c": line 4)
// 1 remove the for loop at ("test/cases/small/for.c": line 4)
// 1 expand compound statment at ("test/cases/small/for.c": line 4)
// 0 remove empty compound at ("test/cases/small/for.c": line 3)
static int a = 0;
int main()
{
}
// 0 inline variable a at ("test/cases/small/for.c": line 1)
// 0 remove static at ("test/cases/small/for.c": line 1)
// 0 remove initializer at ("test/cases/small/for.c": line 4)
// 0 reduce to left at ("test/cases/small/for.c": line 4)
// 0 reduce to right at ("test/cases/small/for.c": line 4)
// 1 remove empty declaration at ("test/cases/small/for.c": line 4)
// 0 remove empty compound at ("test/cases/small/for.c": line 4)
// 1 remove the for loop at ("test/cases/small/for.c": line 4)
// 1 expand compound statment at ("test/cases/small/for.c": line 4)
// 1 remove empty compound at ("test/cases/small/for.c": line 3)
static int a = 0;
int main()
{
}
// 0 inline variable a at ("test/cases/small/for.c": line 1)
// 0 remove static at ("test/cases/small/for.c": line 1)
// 0 remove initializer at ("test/cases/small/for.c": line 4)
// 0 reduce to left at ("test/cases/small/for.c": line 4)
// 0 reduce to right at ("test/cases/small/for.c": line 4)
// 1 remove empty declaration at ("test/cases/small/for.c": line 4)
// 1 remove empty compound at ("test/cases/small/for.c": line 4)
// 0 remove the for loop at ("test/cases/small/for.c": line 4)
static int a = 0;
int main()
{
for (;;)
{
}
}
// 0 inline variable a at ("test/cases/small/for.c": line 1)
// 0 remove static at ("test/cases/small/for.c": line 1)
// 0 remove initializer at ("test/cases/small/for.c": line 4)
// 0 reduce to left at ("test/cases/small/for.c": line 4)
// 0 reduce to right at ("test/cases/small/for.c": line 4)
// 1 remove empty declaration at ("test/cases/small/for.c": line 4)
// 1 remove empty compound at ("test/cases/small/for.c": line 4)
// 1 remove the for loop at ("test/cases/small/for.c": line 4)
// 0 expand compound statment at ("test/cases/small/for.c": line 4)
static int a = 0;
int main()
{
{
}
}
// 0 inline variable a at ("test/cases/small/for.c": line 1)
// 0 remove static at ("test/cases/small/for.c": line 1)
// 0 remove initializer at ("test/cases/small/for.c": line 4)
// 0 reduce to left at ("test/cases/small/for.c": line 4)
// 0 reduce to right at ("test/cases/small/for.c": line 4)
// 1 remove empty declaration at ("test/cases/small/for.c": line 4)
// 1 remove empty compound at ("test/cases/small/for.c": line 4)
// 1 remove the for loop at ("test/cases/small/for.c": line 4)
// 1 expand compound statment at ("test/cases/small/for.c": line 4)
// 0 remove empty compound at ("test/cases/small/for.c": line 3)
static int a = 0;
int main()
{
}
// 0 inline variable a at ("test/cases/small/for.c": line 1)
// 0 remove static at ("test/cases/small/for.c": line 1)
// 0 remove initializer at ("test/cases/small/for.c": line 4)
// 0 reduce to left at ("test/cases/small/for.c": line 4)
// 0 reduce to right at ("test/cases/small/for.c": line 4)
// 1 remove empty declaration at ("test/cases/small/for.c": line 4)
// 1 remove empty compound at ("test/cases/small/for.c": line 4)
// 1 remove the for loop at ("test/cases/small/for.c": line 4)
// 1 expand compound statment at ("test/cases/small/for.c": line 4)
// 1 remove empty compound at ("test/cases/small/for.c": line 3)
static int a = 0;
int main()
{
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment