summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhil de Joux <philderbeast@gmail.com>2022-03-20 11:25:47 -0400
committerMatthew Pickering <matthewtpickering@gmail.com>2022-04-01 11:04:56 +0100
commitdd7c7c998aea402bd5d8ae7ef7854a3bb60bcf2b (patch)
treeeead6719eb0c2a958b17fbb2e0cd36f78a7b1379
parent418e6fab732b9aaa0a00c5e235f445fb547ae196 (diff)
downloadhaskell-dd7c7c998aea402bd5d8ae7ef7854a3bb60bcf2b.tar.gz
Add tests and docs on plugin args and order.
-rw-r--r--docs/users_guide/extending_ghc.rst31
-rw-r--r--testsuite/tests/plugins/Makefile16
-rw-r--r--testsuite/tests/plugins/all.T20
-rw-r--r--testsuite/tests/plugins/echo-plugin/Echo.hs35
-rw-r--r--testsuite/tests/plugins/echo-plugin/Echo1.hs1
-rw-r--r--testsuite/tests/plugins/echo-plugin/Echo2.hs1
-rw-r--r--testsuite/tests/plugins/echo-plugin/Makefile18
-rw-r--r--testsuite/tests/plugins/echo-plugin/Setup.hs3
-rw-r--r--testsuite/tests/plugins/echo-plugin/plugin-echo.cabal10
-rw-r--r--testsuite/tests/plugins/test-echo-in-line-many-args.hs13
-rw-r--r--testsuite/tests/plugins/test-echo-in-line-many-args.stdout4
-rw-r--r--testsuite/tests/plugins/test-echo-in-line.hs13
-rw-r--r--testsuite/tests/plugins/test-echo-in-line.stdout4
-rw-r--r--testsuite/tests/plugins/test-echo-in-turn-many-args.hs15
-rw-r--r--testsuite/tests/plugins/test-echo-in-turn-many-args.stdout4
-rw-r--r--testsuite/tests/plugins/test-echo-in-turn.hs17
-rw-r--r--testsuite/tests/plugins/test-echo-in-turn.stdout4
17 files changed, 208 insertions, 1 deletions
diff --git a/docs/users_guide/extending_ghc.rst b/docs/users_guide/extending_ghc.rst
index b2b555c005..a265a8323e 100644
--- a/docs/users_guide/extending_ghc.rst
+++ b/docs/users_guide/extending_ghc.rst
@@ -216,7 +216,36 @@ option. The list of enabled plugins can be reset with the
:category: plugins
Give arguments to a plugin module; module must be specified with
- :ghc-flag:`-fplugin=⟨module⟩`.
+ :ghc-flag:`-fplugin=⟨module⟩`. The order of plugin pragmas matter but the
+ order of arg pragmas does not. The same set of arguments go to all plugins
+ from the same module.
+
+ ::
+
+ -- Two Echo plugins will both get args A and B.
+ {-# OPTIONS -fplugin Echo -fplugin-opt Echo:A #-}
+ {-# OPTIONS -fplugin Echo -fplugin-opt Echo:B #-}
+
+ -- While order of the plugins matters, arg order does not.
+ {-# OPTIONS -fplugin-opt Echo2:B #-}
+
+ {-# OPTIONS -fplugin Echo1 #-}
+ {-# OPTIONS -fplugin-opt Echo1:A #-}
+
+ {-# OPTIONS -fplugin Echo2 #-}
+
+ If you want to use the same plugin with different arguments then rexport the
+ same plugin from different lightweight modules.
+
+ ::
+
+ -- Echo1 and Echo2 as lightweight modules re-exporting Echo.plugin.
+ module Echo1 (plugin) where import Echo (plugin)
+ module Echo2 (plugin) where import Echo (plugin)
+
+ -- Echo1 gets arg A while Echo2 gets arg B.
+ {-# OPTIONS -fplugin Echo1 -fplugin-opt Echo1:A #-}
+ {-# OPTIONS -fplugin Echo2 -fplugin-opt Echo2:B #-}
.. ghc-flag:: -fplugin-trustworthy
:shortdesc: Trust the used plugins and no longer mark the compiled module
diff --git a/testsuite/tests/plugins/Makefile b/testsuite/tests/plugins/Makefile
index 442e69cd92..482d543c6a 100644
--- a/testsuite/tests/plugins/Makefile
+++ b/testsuite/tests/plugins/Makefile
@@ -189,3 +189,19 @@ T20803a:
.PHONY: T20803b
T20803b:
"$(TEST_HC)" $(TEST_HC_OPTS) $(ghcPluginWayFlags) --make -v0 T20803b.hs -package-db T20803-plugin/pkg.T20803b/local.package.conf
+
+.PHONY: test-echo-in-turn
+test-echo-in-turn:
+ "$(TEST_HC)" $(TEST_HC_OPTS) $(ghcPluginWayFlags) -v0 test-echo-in-turn.hs -package-db echo-plugin/pkg.test-echo-in-turn/local.package.conf
+
+.PHONY: test-echo-in-line
+test-echo-in-line:
+ "$(TEST_HC)" $(TEST_HC_OPTS) $(ghcPluginWayFlags) -v0 test-echo-in-line.hs -package-db echo-plugin/pkg.test-echo-in-line/local.package.conf
+
+.PHONY: test-echo-in-turn-many-args
+test-echo-in-turn-many-args:
+ "$(TEST_HC)" $(TEST_HC_OPTS) $(ghcPluginWayFlags) -v0 test-echo-in-turn-many-args.hs -package-db echo-plugin/pkg.test-echo-in-turn-many-args/local.package.conf
+
+.PHONY: test-echo-in-line-many-args
+test-echo-in-line-many-args:
+ "$(TEST_HC)" $(TEST_HC_OPTS) $(ghcPluginWayFlags) -v0 test-echo-in-line-many-args.hs -package-db echo-plugin/pkg.test-echo-in-line-many-args/local.package.conf
diff --git a/testsuite/tests/plugins/all.T b/testsuite/tests/plugins/all.T
index d2681ac658..4e8663c73b 100644
--- a/testsuite/tests/plugins/all.T
+++ b/testsuite/tests/plugins/all.T
@@ -289,3 +289,23 @@ test('T20803b',
pre_cmd('$MAKE -s --no-print-directory -C T20803-plugin package.T20803b TOP={top}')],
compile_fail,
['-package-db T20803-plugin/pkg.T20803b/local.package.conf -fplugin AddErrorPlugin -package T20803-plugin ' + config.plugin_way_flags])
+
+test('test-echo-in-turn',
+ [extra_files(['echo-plugin/']),
+ pre_cmd('$MAKE -s --no-print-directory -C echo-plugin package.test-echo-in-turn TOP={top}')],
+ makefile_test, [])
+
+test('test-echo-in-line',
+ [extra_files(['echo-plugin/']),
+ pre_cmd('$MAKE -s --no-print-directory -C echo-plugin package.test-echo-in-line TOP={top}')],
+ makefile_test, [])
+
+test('test-echo-in-turn-many-args',
+ [extra_files(['echo-plugin/']),
+ pre_cmd('$MAKE -s --no-print-directory -C echo-plugin package.test-echo-in-turn-many-args TOP={top}')],
+ makefile_test, [])
+
+test('test-echo-in-line-many-args',
+ [extra_files(['echo-plugin/']),
+ pre_cmd('$MAKE -s --no-print-directory -C echo-plugin package.test-echo-in-line-many-args TOP={top}')],
+ makefile_test, [])
diff --git a/testsuite/tests/plugins/echo-plugin/Echo.hs b/testsuite/tests/plugins/echo-plugin/Echo.hs
new file mode 100644
index 0000000000..9c2a71a088
--- /dev/null
+++ b/testsuite/tests/plugins/echo-plugin/Echo.hs
@@ -0,0 +1,35 @@
+module Echo (plugin) where
+
+import GHC.Plugins
+import GHC.Tc.Plugin
+import GHC.Tc.Utils.Monad
+import qualified GHC.Tc.Utils.Monad as Utils
+import GHC.Types.Unique.FM ( emptyUFM )
+
+plugin :: Plugin
+plugin = mkPureOptTcPlugin optCallCount
+
+mkPureOptTcPlugin :: ([CommandLineOption] -> Maybe Utils.TcPlugin) -> Plugin
+mkPureOptTcPlugin p =
+ defaultPlugin
+ { tcPlugin = p
+ , pluginRecompile = impurePlugin
+ }
+
+newtype State = State{callref :: IORef Int}
+
+optCallCount :: [CommandLineOption] -> Maybe Utils.TcPlugin
+optCallCount opts = Just $
+ Utils.TcPlugin
+ { tcPluginInit = return . State =<< (unsafeTcPluginTcM $ newMutVar 1)
+
+ , tcPluginSolve = \State{callref = c} _ _ _ -> do
+ n <- unsafeTcPluginTcM $ readMutVar c
+ let msg = if null opts then "" else mconcat opts
+ tcPluginIO . putStrLn $ "Echo TcPlugin " ++ msg ++ "#" ++ show n
+ unsafeTcPluginTcM $ writeMutVar c (n + 1)
+ return $ TcPluginOk [] []
+
+ , tcPluginRewrite = \ _ -> emptyUFM
+ , tcPluginStop = const $ return ()
+ }
diff --git a/testsuite/tests/plugins/echo-plugin/Echo1.hs b/testsuite/tests/plugins/echo-plugin/Echo1.hs
new file mode 100644
index 0000000000..c0a2acb517
--- /dev/null
+++ b/testsuite/tests/plugins/echo-plugin/Echo1.hs
@@ -0,0 +1 @@
+module Echo1 (plugin) where { import Echo (plugin) }
diff --git a/testsuite/tests/plugins/echo-plugin/Echo2.hs b/testsuite/tests/plugins/echo-plugin/Echo2.hs
new file mode 100644
index 0000000000..c0d459a2d9
--- /dev/null
+++ b/testsuite/tests/plugins/echo-plugin/Echo2.hs
@@ -0,0 +1 @@
+module Echo2 (plugin) where import Echo (plugin)
diff --git a/testsuite/tests/plugins/echo-plugin/Makefile b/testsuite/tests/plugins/echo-plugin/Makefile
new file mode 100644
index 0000000000..7ce5b78e75
--- /dev/null
+++ b/testsuite/tests/plugins/echo-plugin/Makefile
@@ -0,0 +1,18 @@
+TOP=../../..
+include $(TOP)/mk/boilerplate.mk
+include $(TOP)/mk/test.mk
+
+clean.%:
+ rm -rf pkg.$*
+
+HERE := $(abspath .)
+$(eval $(call canonicalise,HERE))
+
+package.%:
+ $(MAKE) -s --no-print-directory clean.$*
+ mkdir pkg.$*
+ "$(TEST_HC)" -outputdir pkg.$* --make -v0 -o pkg.$*/setup Setup.hs
+ "$(GHC_PKG)" init pkg.$*/local.package.conf
+ pkg.$*/setup configure --distdir pkg.$*/dist -v0 $(CABAL_PLUGIN_BUILD) --prefix="$(HERE)/pkg.$*/install" --with-compiler="$(TEST_HC)" $(if $(findstring YES,$(HAVE_PROFILING)), --enable-library-profiling) --with-hc-pkg="$(GHC_PKG)" --package-db=pkg.$*/local.package.conf
+ pkg.$*/setup build --distdir pkg.$*/dist -v0
+ pkg.$*/setup install --distdir pkg.$*/dist -v0
diff --git a/testsuite/tests/plugins/echo-plugin/Setup.hs b/testsuite/tests/plugins/echo-plugin/Setup.hs
new file mode 100644
index 0000000000..e8ef27dbba
--- /dev/null
+++ b/testsuite/tests/plugins/echo-plugin/Setup.hs
@@ -0,0 +1,3 @@
+import Distribution.Simple
+
+main = defaultMain
diff --git a/testsuite/tests/plugins/echo-plugin/plugin-echo.cabal b/testsuite/tests/plugins/echo-plugin/plugin-echo.cabal
new file mode 100644
index 0000000000..1f8ccfd30d
--- /dev/null
+++ b/testsuite/tests/plugins/echo-plugin/plugin-echo.cabal
@@ -0,0 +1,10 @@
+name: echo-plugin
+cabal-version: >=1.24
+build-type: Simple
+version: 0.1.0.0
+
+library
+ default-language: Haskell2010
+ build-depends: base, ghc
+ exposed-modules: Echo, Echo1, Echo2
+ ghc-options: -Wall
diff --git a/testsuite/tests/plugins/test-echo-in-line-many-args.hs b/testsuite/tests/plugins/test-echo-in-line-many-args.hs
new file mode 100644
index 0000000000..80e99fe9f2
--- /dev/null
+++ b/testsuite/tests/plugins/test-echo-in-line-many-args.hs
@@ -0,0 +1,13 @@
+{-# OPTIONS -fplugin Echo -fplugin-opt Echo:A #-}
+{-# OPTIONS -fplugin Echo -fplugin-opt Echo:B #-}
+
+module Main where
+
+foo :: IO a
+foo = undefined
+
+bar :: IO a
+bar = undefined
+
+main :: IO ()
+main = return ()
diff --git a/testsuite/tests/plugins/test-echo-in-line-many-args.stdout b/testsuite/tests/plugins/test-echo-in-line-many-args.stdout
new file mode 100644
index 0000000000..115cbfebfd
--- /dev/null
+++ b/testsuite/tests/plugins/test-echo-in-line-many-args.stdout
@@ -0,0 +1,4 @@
+Echo TcPlugin AB#1
+Echo TcPlugin AB#1
+Echo TcPlugin AB#2
+Echo TcPlugin AB#2 \ No newline at end of file
diff --git a/testsuite/tests/plugins/test-echo-in-line.hs b/testsuite/tests/plugins/test-echo-in-line.hs
new file mode 100644
index 0000000000..858e507b11
--- /dev/null
+++ b/testsuite/tests/plugins/test-echo-in-line.hs
@@ -0,0 +1,13 @@
+{-# OPTIONS -fplugin Echo1 -fplugin-opt Echo1:A #-}
+{-# OPTIONS -fplugin-opt Echo2:B -fplugin Echo2 #-}
+
+module Main where
+
+foo :: IO a
+foo = undefined
+
+bar :: IO a
+bar = undefined
+
+main :: IO ()
+main = return ()
diff --git a/testsuite/tests/plugins/test-echo-in-line.stdout b/testsuite/tests/plugins/test-echo-in-line.stdout
new file mode 100644
index 0000000000..4c7cbd1978
--- /dev/null
+++ b/testsuite/tests/plugins/test-echo-in-line.stdout
@@ -0,0 +1,4 @@
+Echo TcPlugin A#1
+Echo TcPlugin B#1
+Echo TcPlugin A#2
+Echo TcPlugin B#2 \ No newline at end of file
diff --git a/testsuite/tests/plugins/test-echo-in-turn-many-args.hs b/testsuite/tests/plugins/test-echo-in-turn-many-args.hs
new file mode 100644
index 0000000000..534a48a8ec
--- /dev/null
+++ b/testsuite/tests/plugins/test-echo-in-turn-many-args.hs
@@ -0,0 +1,15 @@
+{-# OPTIONS -fplugin Echo #-}
+{-# OPTIONS -fplugin-opt Echo:A #-}
+{-# OPTIONS -fplugin Echo #-}
+{-# OPTIONS -fplugin-opt Echo:B #-}
+
+module Main where
+
+foo :: IO a
+foo = undefined
+
+bar :: IO a
+bar = undefined
+
+main :: IO ()
+main = return ()
diff --git a/testsuite/tests/plugins/test-echo-in-turn-many-args.stdout b/testsuite/tests/plugins/test-echo-in-turn-many-args.stdout
new file mode 100644
index 0000000000..115cbfebfd
--- /dev/null
+++ b/testsuite/tests/plugins/test-echo-in-turn-many-args.stdout
@@ -0,0 +1,4 @@
+Echo TcPlugin AB#1
+Echo TcPlugin AB#1
+Echo TcPlugin AB#2
+Echo TcPlugin AB#2 \ No newline at end of file
diff --git a/testsuite/tests/plugins/test-echo-in-turn.hs b/testsuite/tests/plugins/test-echo-in-turn.hs
new file mode 100644
index 0000000000..272ca11afe
--- /dev/null
+++ b/testsuite/tests/plugins/test-echo-in-turn.hs
@@ -0,0 +1,17 @@
+{-# OPTIONS -fplugin-opt Echo2:B #-}
+
+{-# OPTIONS -fplugin Echo1 #-}
+{-# OPTIONS -fplugin-opt Echo1:A #-}
+
+{-# OPTIONS -fplugin Echo2 #-}
+
+module Main where
+
+foo :: IO a
+foo = undefined
+
+bar :: IO a
+bar = undefined
+
+main :: IO ()
+main = return ()
diff --git a/testsuite/tests/plugins/test-echo-in-turn.stdout b/testsuite/tests/plugins/test-echo-in-turn.stdout
new file mode 100644
index 0000000000..4c7cbd1978
--- /dev/null
+++ b/testsuite/tests/plugins/test-echo-in-turn.stdout
@@ -0,0 +1,4 @@
+Echo TcPlugin A#1
+Echo TcPlugin B#1
+Echo TcPlugin A#2
+Echo TcPlugin B#2 \ No newline at end of file