-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add -dune-optional-output mode for dune's internal use
Fixes #461 This PR adds a new command line flag that tells the driver not to write to the output file if there is no rewriting to be done. It's not 100% accurate if there are non context free transformations registered as we do not compare the AST for this feature but simply keep track of generated code via a hook. If any non context free transformation is registered, we simply assume it will rewrite something and always output. Signed-off-by: Nathan Rebours <nathan.p.rebours@gmail.com>
- Loading branch information
Showing
7 changed files
with
166 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
test/driver/dune-optional-output/context_free_only_driver.ml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
open Ppxlib | ||
|
||
let rule = | ||
Context_free.Rule.extension | ||
(Extension.V3.declare "iam1" Extension.Context.expression | ||
Ast_pattern.(pstr nil) | ||
(fun ~ctxt -> | ||
let loc = Expansion_context.Extension.extension_point_loc ctxt in | ||
[%expr 1])) | ||
|
||
let () = Driver.register_transformation ~rules:[ rule ] "iam1" | ||
let () = Driver.standalone () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
open Ppxlib | ||
|
||
let rule = | ||
Context_free.Rule.extension | ||
(Extension.V3.declare "iam1" Extension.Context.expression | ||
Ast_pattern.(pstr nil) | ||
(fun ~ctxt -> | ||
let loc = Expansion_context.Extension.extension_point_loc ctxt in | ||
[%expr 1])) | ||
|
||
let () = Driver.register_transformation ~rules:[ rule ] "iam1" | ||
|
||
let () = | ||
Driver.register_transformation ~impl:(fun str -> str) "IdentityInDisguise" | ||
|
||
let () = Driver.standalone () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
(executable | ||
(name context_free_only_driver) | ||
(libraries ppxlib) | ||
(preprocess | ||
(pps ppxlib.metaquot)) | ||
(modules context_free_only_driver)) | ||
|
||
(executable | ||
(name driver_with_impl) | ||
(libraries ppxlib) | ||
(preprocess | ||
(pps ppxlib.metaquot)) | ||
(modules driver_with_impl)) | ||
|
||
(cram | ||
(deps context_free_only_driver.exe driver_with_impl.exe)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
The -dune-optional-output flag is meant for dune to be able | ||
to use ppx internally without having a build dependency on ppxlib | ||
or any ppx. | ||
|
||
When enabled, it should not write to the output if it can absolutely | ||
tell no transformation occured. | ||
|
||
We have a driver with a single context free rule to expand [%iam1] extension | ||
|
||
Let us consider the following file: | ||
|
||
$ cat > foo.ml << EOF | ||
> let x = 1 | ||
> let y = 2 | ||
> EOF | ||
|
||
If we call the driver with the -dune-optional-output flag, it should not write a file: | ||
|
||
$ ./context_free_only_driver.exe -impl -dune-optional-output -o foo.pp.ml foo.ml | ||
$ ls foo.* | ||
foo.ml | ||
|
||
We can see that it did not write test.pp.ml | ||
|
||
Now if we actually use the extension: | ||
|
||
$ cat > bar.ml << EOF | ||
> let x = [%iam1] | ||
> let y = 2 | ||
> EOF | ||
|
||
It should actually detect the transformation and therefore write the output file: | ||
|
||
$ ./context_free_only_driver.exe -impl -dune-optional-output -o bar.pp.ml bar.ml | ||
$ ls bar.* | ||
bar.ml | ||
bar.pp.ml | ||
|
||
Now we have another driver that has the same context free rule but also another | ||
transformation with an "impl", i.e. a rule to rewrite the whole AST unconditionnally. | ||
This rule does not rewrite anything and is just the identity rewriter. | ||
We cannot tell without actually comparing the ASTs if any rewriting happened so in | ||
that case we always write to the output. | ||
|
||
$ cat > baz.ml << EOF | ||
> let x = 1 | ||
> let y = 2 | ||
> EOF | ||
$ ./driver_with_impl.exe -impl -dune-optional-output -o baz.pp.ml baz.ml | ||
$ ls baz.* | ||
baz.ml | ||
baz.pp.ml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters