Skip to content

Commit

Permalink
Merge branch 'pmderodat/c' into 'master'
Browse files Browse the repository at this point in the history
C-related testsuite refactorings

See merge request eng/libadalang/langkit!848
  • Loading branch information
pmderodat committed Jun 12, 2023
2 parents ae4ffd3 + 4f0edda commit 17219fe
Show file tree
Hide file tree
Showing 77 changed files with 364 additions and 278 deletions.
5 changes: 0 additions & 5 deletions langkit/libmanage.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,6 @@ def add_common_args(subparser: argparse.ArgumentParser) -> None:
'--trace', '-t', action='append', default=[],
help='Activate given debug trace.'
)
subparser.add_argument(
'--no-ada-api', action='store_true',
help='Do not generate units to provide an Ada API, and disable the'
' generation of mains.'
)

# Don't enable this by default so that errors will not make automated
# tasks hang.
Expand Down
75 changes: 32 additions & 43 deletions testsuite/python_support/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ def build(grammar=None, lexer=None, lkt_file=None,
warning_set=warning_set)


def build_and_run(grammar=None, py_script=None, ada_main=None, with_c=False,
def build_and_run(grammar=None, py_script=None, gpr_mains=None,
lexer=None, lkt_file=None, types_from_lkt=False,
lkt_semantic_checks=False, ocaml_main=None, java_main=None,
ni_main=None, mains=False, warning_set=default_warning_set,
ni_main=None, warning_set=default_warning_set,
generate_unparser=False, symbol_canonicalizer=None,
show_property_logging=False, unparse_script=unparse_script,
case_insensitive: bool = False,
Expand Down Expand Up @@ -237,13 +237,9 @@ def build_and_run(grammar=None, py_script=None, ada_main=None, with_c=False,
:param None|str py_script: If not None, name of the Python script to run
with the built library available.
:param None|str|list[str] ada_main: If not None, list of name of main
source files for Ada programs to build and run with the generated
library. If the input is a single string, consider it's a single mail
source file.
:param bool with_c: Whether to add "C" to the languages of the generated
project.
:param None|list[str] gpr_mains: If not None, list of name of main source
files (Ada and/or C) for the generated GPR file, to build and run with
the generated library.
:param None|str ocaml_main: If not None, name of the OCaml source file to
build and run with the built library available.
Expand All @@ -258,8 +254,6 @@ def build_and_run(grammar=None, py_script=None, ada_main=None, with_c=False,
:param langkit.compile_context.LibraryEntity|None symbol_canonicalizer:
Symbol canonicalizer to use for this context, if any.
:param bool mains: Whether to build mains.
:param bool show_property_logging: If true, any property that has been
marked with tracing activated will be traced on stdout by default,
without need for any config file.
Expand Down Expand Up @@ -325,12 +319,6 @@ def manage_run(generate_only, types_from_lkt, additional_args):
if full_error_traces:
argv.append("--full-error-traces")

# Generate the public Ada API only when necessary (i.e. if we have
# mains that do use this API). This reduces the time it takes to run
# tests.
if not mains and not ada_main:
argv.append('--no-ada-api')

# If there is a Java main, enable the Java bindings building
if java_main is not None or ni_main is not None:
argv.append('--enable-java')
Expand All @@ -353,10 +341,8 @@ def manage_run(generate_only, types_from_lkt, additional_args):
if generate_unparser:
argv.append('--generate-unparser')

# For testsuite performance, do not generate mains unless told
# otherwise.
if not mains:
argv.append('--disable-all-mains')
# No testcase uses the generated mains, so save time: never build them
argv.append('--disable-all-mains')

argv.extend(additional_args)
argv.extend(additional_make_args)
Expand Down Expand Up @@ -426,42 +412,45 @@ def run(*argv, **kwargs):
args.append(py_script)
run(*args)

if ada_main is not None:
if isinstance(ada_main, str):
ada_main = [ada_main]
if gpr_mains:
source_dirs = [".", c_support_dir]

langs = ["Ada"]
source_dirs = ["."]
if with_c:
langs.append("C")
source_dirs.append(c_support_dir)
# Detect languages based on the source files present in the test
# directory.
langs = set()
for f in os.listdir("."):
if any(f.endswith(ext) for ext in [".c", ".h"]):
langs.add("C")
if any(f.endswith(ext) for ext in [".adb", ".ads"]):
langs.add("Ada")

# Generate a project file to build the given Ada main and then run
# the program. Do a static build to improve the debugging experience.
with open('gen.gpr', 'w') as f:
# Generate a project file to build the given mains. Do a static build
# (the default) to improve the debugging experience.
with open("gen.gpr", "w") as f:

def fmt_str_list(strings: List[str]) -> str:
return ", ".join(f'"{s}"' for s in strings)

f.write(project_template.format(
languages=fmt_str_list(langs),
source_dirs=fmt_str_list(source_dirs),
main_sources=fmt_str_list(ada_main),
main_sources=fmt_str_list(gpr_mains),
))
run('gprbuild', '-Pgen', '-q', '-p',
'-XLIBRARY_TYPE=static',
'-XXMLADA_BUILD=static')
run("gprbuild", "-Pgen", "-q", "-p")

for i, m in enumerate(ada_main):
assert m.endswith('.adb')
# Now run all mains. If there are more than one main to run, print a
# heading before each one.
for i, main in enumerate(gpr_mains):
if i > 0:
print('')
if len(ada_main) > 1:
print('== {} =='.format(m))
print("")
if len(gpr_mains) > 1:
print(f"== {main} ==")
sys.stdout.flush()
run(P.join('obj', m[:-4]),
run(
P.join("obj", os.path.splitext(main)[0]),
valgrind=True,
valgrind_suppressions=['gnat'])
valgrind_suppressions=["gnat"],
)

if ocaml_main is not None:
# Set up a Dune project
Expand Down
9 changes: 6 additions & 3 deletions testsuite/tests/ada_api/bigint_array_comp/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def prop(a=T.BigInt.array):
return a


build_and_run(lkt_file='expected_concrete_syntax.lkt', ada_main=['main.adb'],
types_from_lkt=True)
print('Done')
build_and_run(
lkt_file="expected_concrete_syntax.lkt",
gpr_mains=["main.adb"],
types_from_lkt=True,
)
print("Done")
2 changes: 1 addition & 1 deletion testsuite/tests/ada_api/calls_on_null/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ class Decl(FooNode):
value = Field(type=Identifier)


build_and_run(lkt_file='expected_concrete_syntax.lkt', ada_main=['main.adb'])
build_and_run(lkt_file="expected_concrete_syntax.lkt", gpr_mains=["main.adb"])
print('Done')
9 changes: 6 additions & 3 deletions testsuite/tests/ada_api/children_and_trivia/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class Decl(FooNode):
error = Field(type=DeclError)


build_and_run(lkt_file='expected_concrete_syntax.lkt', ada_main='main.adb',
types_from_lkt=True)
print('Done')
build_and_run(
lkt_file="expected_concrete_syntax.lkt",
gpr_mains=["main.adb"],
types_from_lkt=True,
)
print("Done")
2 changes: 1 addition & 1 deletion testsuite/tests/ada_api/event_handler/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Example(FooNode):

build_and_run(
lkt_file="expected_concrete_syntax.lkt",
ada_main="main.adb",
gpr_mains=["main.adb"],
types_from_lkt=True,
)

Expand Down
9 changes: 6 additions & 3 deletions testsuite/tests/ada_api/general/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class Decl(FooNode):
error = Field(type=DeclError)


build_and_run(lkt_file='expected_concrete_syntax.lkt', ada_main='main.adb',
types_from_lkt=True)
print('Done')
build_and_run(
lkt_file="expected_concrete_syntax.lkt",
gpr_mains=["main.adb"],
types_from_lkt=True,
)
print("Done")
8 changes: 6 additions & 2 deletions testsuite/tests/ada_api/generic_api/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ class Ref(Expr):

build_and_run(
lkt_file="expected_concrete_syntax.lkt",
ada_main=["analysis.adb", "introspection_types.adb",
"introspection_values.adb", "hash.adb"]
gpr_mains=[
"analysis.adb",
"introspection_types.adb",
"introspection_values.adb",
"hash.adb",
],
)
print("Done")
9 changes: 6 additions & 3 deletions testsuite/tests/ada_api/hashes/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class Example(FooNode):
token_node = True


build_and_run(lkt_file='expected_concrete_syntax.lkt', ada_main=['main.adb'],
types_from_lkt=True)
print('Done')
build_and_run(
lkt_file="expected_concrete_syntax.lkt",
gpr_mains=["main.adb"],
types_from_lkt=True,
)
print("Done")
9 changes: 6 additions & 3 deletions testsuite/tests/ada_api/lifetimes/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Example(FooNode):
token_node = True


build_and_run(lkt_file='expected_concrete_syntax.lkt', ada_main=['main.adb'],
types_from_lkt=True)
print('Done')
build_and_run(
lkt_file="expected_concrete_syntax.lkt",
gpr_mains=["main.adb"],
types_from_lkt=True,
)
print("Done")
2 changes: 1 addition & 1 deletion testsuite/tests/ada_api/member_is_null_for/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class A2B2(A2):

build_and_run(
lkt_file="expected_concrete_syntax.lkt",
ada_main=["main.adb"],
gpr_mains=["main.adb"],
types_from_lkt=True,
)
print("Done")
7 changes: 5 additions & 2 deletions testsuite/tests/ada_api/node_conversion/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class Ref(Expr):
name = Field()


build_and_run(lkt_file='expected_concrete_syntax.lkt', ada_main=['main.adb'],
types_from_lkt=True)
build_and_run(
lkt_file='expected_concrete_syntax.lkt',
gpr_mains=['main.adb'],
types_from_lkt=True,
)
print('Done')
4 changes: 2 additions & 2 deletions testsuite/tests/ada_api/null_field_rebindings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ def rebind(from_node=T.FooNode.entity, to_node=T.FooNode.entity):
return T.Decl.entity.new(node=Self, info=e_info)


build_and_run(lkt_file='expected_concrete_syntax.lkt', ada_main=['main.adb'])
print('Done')
build_and_run(lkt_file="expected_concrete_syntax.lkt", gpr_mains=["main.adb"])
print("Done")
9 changes: 6 additions & 3 deletions testsuite/tests/ada_api/pred_kind_in/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class Ref(Expr):
name = Field()


build_and_run(lkt_file='expected_concrete_syntax.lkt', ada_main=['main.adb'],
types_from_lkt=True)
print('Done')
build_and_run(
lkt_file="expected_concrete_syntax.lkt",
gpr_mains=["main.adb"],
types_from_lkt=True,
)
print("Done")
2 changes: 1 addition & 1 deletion testsuite/tests/ada_api/syntax_field_indexes/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class A2B2(A2):

build_and_run(
lkt_file="expected_concrete_syntax.lkt",
ada_main=["main.adb"],
gpr_mains=["main.adb"],
types_from_lkt=True,
)
print("Done")
7 changes: 5 additions & 2 deletions testsuite/tests/ada_api/token_ref_origin/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Example(FooNode):
token_node = True


build_and_run(lkt_file='expected_concrete_syntax.lkt', ada_main='main.adb',
types_from_lkt=True)
build_and_run(
lkt_file="expected_concrete_syntax.lkt",
gpr_mains=["main.adb"],
types_from_lkt=True,
)
print('Done')
12 changes: 7 additions & 5 deletions testsuite/tests/ada_api/token_ref_stale/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ class Example(FooNode):
token_node = True


build_and_run(lkt_file='expected_concrete_syntax.lkt',
py_script='main.py',
ada_main='main.adb',
types_from_lkt=True)
print('Done')
build_and_run(
lkt_file="expected_concrete_syntax.lkt",
py_script="main.py",
gpr_mains=["main.adb"],
types_from_lkt=True,
)
print("Done")
9 changes: 6 additions & 3 deletions testsuite/tests/ada_api/unbounded_string_buffer/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Example(FooNode):
token_node = True


build_and_run(lkt_file='expected_concrete_syntax.lkt', ada_main=['main.adb'],
types_from_lkt=True)
print('Done')
build_and_run(
lkt_file="expected_concrete_syntax.lkt",
gpr_mains=["main.adb"],
types_from_lkt=True,
)
print("Done")
5 changes: 0 additions & 5 deletions testsuite/tests/c_api/event_handler/main.adb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ eh_unit_parsed (void *data, foo_analysis_context ctx, foo_analysis_unit unit,
puts ("");
}

void
c_main (void)
int
main (void)
{
const char *buffer = "example\n";
const size_t buffer_size = strlen (buffer);
Expand Down Expand Up @@ -176,4 +176,5 @@ c_main (void)
abort_on_exception ();

puts ("main.c: Done.");
return 0;
}
3 changes: 1 addition & 2 deletions testsuite/tests/c_api/event_handler/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class Example(FooNode):

build_and_run(
lkt_file="expected_concrete_syntax.lkt",
ada_main="main.adb",
with_c=True,
gpr_mains=["main.c"],
types_from_lkt=True,
)

Expand Down
5 changes: 0 additions & 5 deletions testsuite/tests/c_api/general/main.adb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include "utils_exc.h"
#include "utils_text.h"

void
c_main (void)
int
main (void)
{
// Create the source code buffer.
const char *buffer = "example\nexample\n";
Expand Down Expand Up @@ -125,4 +125,7 @@ c_main (void)
// Free the children and tokens.
free (children);
free (tokens);

puts ("main.c: Done.");
return 0;
}
1 change: 1 addition & 0 deletions testsuite/tests/c_api/general/test.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ Child 1 of root = "Example"
Child 2 of root = "Example"
Start token of the 1 child = "Example"
Start token of the 2 child = "Example"
main.c: Done.
Done
Loading

0 comments on commit 17219fe

Please sign in to comment.