aboutsummaryrefslogtreecommitdiff
path: root/unit-tests/cc
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2017-05-25 10:41:20 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2017-05-25 10:41:20 +0200
commita1f459f8446370704695919b3131653300866ee9 (patch)
tree8bc4670b2f9355d694e38443d99f506dd0ea9efc /unit-tests/cc
parent0cef93b4e2e9bf39b0ca542876f9ab1af6d0f01d (diff)
Implement parsing of C++ module declarations
Diffstat (limited to 'unit-tests/cc')
-rw-r--r--unit-tests/cc/parser/buildfile17
-rw-r--r--unit-tests/cc/parser/driver.cxx69
-rw-r--r--unit-tests/cc/parser/module.test147
3 files changed, 233 insertions, 0 deletions
diff --git a/unit-tests/cc/parser/buildfile b/unit-tests/cc/parser/buildfile
new file mode 100644
index 0000000..5d20367
--- /dev/null
+++ b/unit-tests/cc/parser/buildfile
@@ -0,0 +1,17 @@
+# file : unit-tests/cc/parser/buildfile
+# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
+# license : MIT; see accompanying LICENSE file
+
+#@@ Temporary until we get utility library support.
+#
+import libs = libbutl%lib{butl}
+src = cc/{lexer parser} token lexer diagnostics utility variable name b-options types-parsers \
+context scope parser target operation rule prerequisite file module function \
+functions-builtin functions-path functions-process-path functions-string \
+functions-target-triplet algorithm search dump filesystem scheduler \
+config/{utility init operation module} spec
+
+exe{driver}: cxx{driver} ../../../build2/cxx{$src} ../../../build2/liba{b} \
+$libs test{*}
+
+include ../../../build2/
diff --git a/unit-tests/cc/parser/driver.cxx b/unit-tests/cc/parser/driver.cxx
new file mode 100644
index 0000000..cdddaca
--- /dev/null
+++ b/unit-tests/cc/parser/driver.cxx
@@ -0,0 +1,69 @@
+// file : unit-tests/cc/parser/driver.cxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#include <cassert>
+#include <iostream>
+
+#include <build2/types.hxx>
+#include <build2/utility.hxx>
+
+#include <build2/cc/parser.hxx>
+
+using namespace std;
+
+namespace build2
+{
+ namespace cc
+ {
+ // Usage: argv[0] [<file>]
+ //
+ int
+ main (int argc, char* argv[])
+ {
+ try
+ {
+ istream* is;
+ const char* in;
+
+ // Reading from file is several times faster.
+ //
+ ifdstream ifs;
+ if (argc > 1)
+ {
+ in = argv[1];
+ ifs.open (in);
+ is = &ifs;
+ }
+ else
+ {
+ in = "stdin";
+ cin.exceptions (istream::failbit | istream::badbit);
+ is = &cin;
+ }
+
+ parser p;
+ translation_unit u (p.parse (*is, path (in)));
+
+ for (const string& n: u.module_imports)
+ cout << "import " << n << ';' << endl;
+
+ if (!u.module_name.empty ())
+ cout << (u.module_interface ? "export " : "")
+ << "module " << u.module_name << ';' << endl;
+ }
+ catch (const failed&)
+ {
+ return 1;
+ }
+
+ return 0;
+ }
+ }
+}
+
+int
+main (int argc, char* argv[])
+{
+ return build2::cc::main (argc, argv);
+}
diff --git a/unit-tests/cc/parser/module.test b/unit-tests/cc/parser/module.test
new file mode 100644
index 0000000..f85c969
--- /dev/null
+++ b/unit-tests/cc/parser/module.test
@@ -0,0 +1,147 @@
+# file : unit-tests/cc/parser/module.test
+# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
+# license : MIT; see accompanying LICENSE file
+
+# Test C++ module constructs.
+#
+
+: import
+:
+$* <<EOI >>EOI
+import foo;
+import foo.bar;
+import foo.bar.baz;
+EOI
+
+: module-implementation
+:
+$* <<EOI >>EOI
+module foo;
+EOI
+
+: module-interface
+:
+$* <<EOI >>EOI
+export module foo;
+EOI
+
+: export-imported
+:
+$* <<EOI >>EOO
+export import foo;
+EOI
+import foo;
+EOO
+
+: export-imported-block
+:
+$* <<EOI >>EOO
+export {import foo;}
+
+export
+{
+ namespace foo
+ {
+ class c {};
+ }
+
+ template <typename T> int f ();
+
+ import bar;
+}
+EOI
+import foo;
+import bar;
+EOO
+
+: non-module
+:
+$* <<EOI
+#pragma import module foo;
+#pragma export module foo;
+#pragma module foo;
+extern module foo: int foo ();
+export namespace bar {int fox ();}
+EOI
+
+: attribute
+:
+$* <<EOI >>EOO
+import foo [[export({import})]];
+module bar [[module({module})]];
+EOI
+import foo;
+module bar;
+EOO
+
+: import-duplicate
+:
+$* <<EOI >>EOO
+import foo;
+import bar.baz;
+import foo;
+import bar . baz;
+EOI
+import foo;
+import bar.baz;
+EOO
+
+: brace-missing
+:
+$* <<EOI 2>>EOE
+export
+{
+ class foo
+ {
+ //};
+ module foo;
+}
+EOI
+stdin:8:1: warning: {}-imbalance detected
+EOE
+
+: brace-stray
+:
+$* <<EOI 2>>EOE
+export
+{
+ class foo
+ {
+ };}
+}
+module foo;
+EOI
+stdin:6:1: warning: {}-imbalance detected
+EOE
+
+: import-missing-name
+:
+$* <<EOI 2>>EOE != 0
+import ;
+EOI
+stdin:1:8: error: module name expected instead of ';'
+EOE
+
+: module-missing-name
+:
+$* <<EOI 2>>EOE != 0
+module ;
+EOI
+stdin:1:8: error: module name expected instead of ';'
+EOE
+
+: import-missing-semi
+:
+$* <<EOI 2>>EOE != 0
+import foo
+EOI
+stdin:2:1: error: ';' expected instead of <end of file>
+EOE
+
+: module-missing-semi
+:
+$* <<EOI 2>>EOE != 0
+export module foo
+EOI
+stdin:2:1: error: ';' expected instead of <end of file>
+EOE