summaryrefslogtreecommitdiff
path: root/testsuite/tests/th
diff options
context:
space:
mode:
authorFrancesco Mazzoli <f@mazzo.li>2017-03-07 23:39:51 -0500
committerBen Gamari <ben@smart-cactus.org>2017-03-08 19:15:54 -0500
commit0fac488cca04a07224926e35be9c45ee2d0e1631 (patch)
tree48c5317fa66d9e09ff9bd829daf26539a971abc8 /testsuite/tests/th
parentde62f587463f6377df1e69e11504578833dfe653 (diff)
downloadhaskell-0fac488cca04a07224926e35be9c45ee2d0e1631.tar.gz
Allow compilation of C/C++/ObjC/ObjC++ files with module from TH
The main goal is to easily allow the inline-c project (and similar projects such as inline-java) to emit C/C++ files to be compiled and linked with the current module. Moreover, `addCStub` is removed, since it's quite fragile. Most notably, the C stubs end up in the file generated by `CodeOutput.outputForeignStubs`, which is tuned towards generating a file for stubs coming from `capi` and Haskell-to-C exports. Reviewers: simonmar, austin, goldfire, facundominguez, dfeuer, bgamari Reviewed By: dfeuer, bgamari Subscribers: snowleopard, rwbarton, dfeuer, thomie, duncan, mboes Differential Revision: https://phabricator.haskell.org/D3280
Diffstat (limited to 'testsuite/tests/th')
-rw-r--r--testsuite/tests/th/T13366.hs39
-rw-r--r--testsuite/tests/th/T13366.stdout4
-rw-r--r--testsuite/tests/th/TH_addCStub1.hs22
-rw-r--r--testsuite/tests/th/TH_addCStub1.stdout2
-rw-r--r--testsuite/tests/th/TH_addCStub2.hs22
-rw-r--r--testsuite/tests/th/all.T6
6 files changed, 44 insertions, 51 deletions
diff --git a/testsuite/tests/th/T13366.hs b/testsuite/tests/th/T13366.hs
new file mode 100644
index 0000000000..2573235a01
--- /dev/null
+++ b/testsuite/tests/th/T13366.hs
@@ -0,0 +1,39 @@
+{-# LANGUAGE ForeignFunctionInterface #-}
+{-# LANGUAGE TemplateHaskell #-}
+{-# OPTIONS_GHC -optc-DA_MACRO=1 #-}
+
+import Language.Haskell.TH.Syntax
+import System.IO (hFlush, stdout)
+
+foreign import ccall fc :: Int -> IO Int
+
+do addForeignFile LangC $ unlines
+ [ "#include <stdio.h>"
+ , "int fc(int x) {"
+ , " printf(\"calling f(%d)\\n\",x);"
+ , " fflush(stdout);"
+ , " return A_MACRO + x;"
+ , "}"
+ ]
+ return []
+
+foreign import ccall fcxx :: Int -> IO Int
+
+do addForeignFile LangCxx $ unlines
+ [ "#include <iostream>"
+ , "extern \"C\" {"
+ , " int fcxx(int x) {"
+ , " std::cout << \"calling fcxx(\" << x << \")\" << std::endl;"
+ , " std::cout.flush();"
+ , " return A_MACRO + x;"
+ , " }"
+ , "}"
+ ]
+ return []
+
+main :: IO ()
+main = do
+ fc 2 >>= print
+ hFlush stdout
+ fcxx 5 >>= print
+ hFlush stdout
diff --git a/testsuite/tests/th/T13366.stdout b/testsuite/tests/th/T13366.stdout
new file mode 100644
index 0000000000..16cfeeb9fa
--- /dev/null
+++ b/testsuite/tests/th/T13366.stdout
@@ -0,0 +1,4 @@
+calling f(2)
+3
+calling fcxx(5)
+6
diff --git a/testsuite/tests/th/TH_addCStub1.hs b/testsuite/tests/th/TH_addCStub1.hs
deleted file mode 100644
index 3a2c5c3609..0000000000
--- a/testsuite/tests/th/TH_addCStub1.hs
+++ /dev/null
@@ -1,22 +0,0 @@
--- Tests that addCStub includes the C code in the final object file and that
--- -optc options are passed when building it.
-
-{-# LANGUAGE ForeignFunctionInterface #-}
-{-# LANGUAGE TemplateHaskell #-}
-{-# OPTIONS_GHC -optc-DA_MACRO=1 #-}
-
-import Language.Haskell.TH.Syntax
-
-foreign import ccall f :: Int -> IO Int
-
-do addCStub $ unlines
- [ "#include <stdio.h>"
- , "int f(int x) {"
- , " printf(\"calling f(%d)\\n\",x);"
- , " return A_MACRO + x;"
- , "}"
- ]
- return []
-
-main :: IO ()
-main = f 2 >>= print
diff --git a/testsuite/tests/th/TH_addCStub1.stdout b/testsuite/tests/th/TH_addCStub1.stdout
deleted file mode 100644
index e46825eb2b..0000000000
--- a/testsuite/tests/th/TH_addCStub1.stdout
+++ /dev/null
@@ -1,2 +0,0 @@
-3
-calling f(2)
diff --git a/testsuite/tests/th/TH_addCStub2.hs b/testsuite/tests/th/TH_addCStub2.hs
deleted file mode 100644
index 10119d9370..0000000000
--- a/testsuite/tests/th/TH_addCStub2.hs
+++ /dev/null
@@ -1,22 +0,0 @@
--- Tests that a reasonable error is reported when addCStub is used with
--- incorrect C code.
-
-{-# LANGUAGE ForeignFunctionInterface #-}
-{-# LANGUAGE TemplateHaskell #-}
-{-# OPTIONS_GHC -optc-DA_MACRO=1 #-}
-
-import Language.Haskell.TH.Syntax
-
-foreign import ccall f :: Int -> IO Int
-
-do addCStub $ unlines
- [ "#include <stdio.h>"
- , "int f(int x {"
- , " printf(\"calling f(%d)\\n\",x);"
- , " return A_MACRO + x;"
- , "}"
- ]
- return []
-
-main :: IO ()
-main = f 2 >>= print
diff --git a/testsuite/tests/th/all.T b/testsuite/tests/th/all.T
index d73ad8600c..e4d4731f9a 100644
--- a/testsuite/tests/th/all.T
+++ b/testsuite/tests/th/all.T
@@ -71,11 +71,6 @@ def error_has(pattern):
# the following fails only if both the command fails and the pattern is found
return('bash -o pipefail -c \'! (! "$@" {swap12}) | grep {pattern} {swap12} &> /dev/null\' --'.format(**locals()))
-test('TH_addCStub1', normal, compile_and_run, ['-v0'])
-test('TH_addCStub2'
- , [compile_cmd_prefix(error_has('TH_addCStub2.hs:13:'))]
- , compile_fail, ['-v0'])
-
test('TH_reifyMkName', normal, compile, ['-v0'])
test('TH_reifyInstances', normal, compile, ['-v0'])
@@ -385,3 +380,4 @@ test('T13018', normal, compile, ['-v0'])
test('T13123', normal, compile, ['-v0'])
test('T13098', normal, compile, ['-v0'])
test('T11046', normal, multimod_compile, ['T11046','-v0'])
+test('T13366', normal, compile_and_run, ['-lstdc++ -v0'])