aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2018-08-15 15:09:16 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2018-08-15 15:09:16 +0200
commite78ef8665dbf82d77e958f37204bce6e36bfcb59 (patch)
tree82b68a064ead4a340d75f72e8343218b7a4d27a3
parentb1aafd32c2e8eaa636eb13398219f225f480e788 (diff)
Use sanitized name stem for language identifiers in bdep-new
-rw-r--r--bdep/new.cxx32
-rw-r--r--tests/new.test18
2 files changed, 37 insertions, 13 deletions
diff --git a/bdep/new.cxx b/bdep/new.cxx
index ca8367b..5044405 100644
--- a/bdep/new.cxx
+++ b/bdep/new.cxx
@@ -114,7 +114,7 @@ namespace bdep
//
// We use the full name in the manifest and the top-level directory, the
// base name for inner filesystem directories and preprocessor macros,
- // while the stem for modules, namespaces, etc.
+ // while the (sanitized) stem for modules, namespaces, etc.
//
const string& n (pkgn.string ());
const string& b (pkgn.base ());
@@ -134,7 +134,12 @@ namespace bdep
case type::lib:
{
if (s.compare (0, 3, "lib") == 0)
+ {
s.erase (0, 3);
+
+ if (s.empty ())
+ fail << "empty library name stem in '" << b << "'";
+ }
else
warn << "library name does not start with 'lib'" <<
info << "this package may not be acceptable to some repositories";
@@ -146,6 +151,25 @@ namespace bdep
break;
}
+ // Sanitize the stem to be a valid language identifier.
+ //
+ string id;
+ switch (l)
+ {
+ case lang::c:
+ case lang::cxx:
+ {
+ auto sanitize = [] (char c)
+ {
+ return (c == '-' || c == '+' || c == '.') ? '_' : c;
+ };
+
+ transform (s.begin (), s.end (), back_inserter (id), sanitize);
+ break;
+ }
+ }
+
+
dir_path out; // Project/package output directory.
dir_path prj; // Project.
optional<dir_path> pkg; // Package relative to its project root.
@@ -654,7 +678,7 @@ namespace bdep
<< endl
<< "#include <" << b << "/" << exp << ">" << endl
<< endl
- << "namespace " << s << endl
+ << "namespace " << id << endl
<< "{" << endl
<< " // Print a greeting for the specified name into the specified" << endl
<< " // stream. Throw std::invalid_argument if the name is empty." << endl
@@ -675,7 +699,7 @@ namespace bdep
<< endl
<< "using namespace std;" << endl
<< endl
- << "namespace " << s << endl
+ << "namespace " << id << endl
<< "{" << endl
<< " void say_hello (ostream& o, const string& n)" << endl
<< " {" << endl
@@ -1024,7 +1048,7 @@ namespace bdep
<< "int main ()" << endl
<< "{" << endl
<< " using namespace std;" << endl
- << " using namespace " << s << ";" << endl
+ << " using namespace " << id << ";" << endl
<< endl
<< " // Basics." << endl
<< " //" << endl
diff --git a/tests/new.test b/tests/new.test
index 3a62df3..491c155 100644
--- a/tests/new.test
+++ b/tests/new.test
@@ -21,25 +21,25 @@ status += -d prj
: exe
{
- $* --no-amalgamation -t exe -l c++ prj 2>>/"EOE" &prj/***;
- created new executable project prj in $~/prj/
+ $* --no-amalgamation -t exe -l c++ prj-foo 2>>/"EOE" &prj-foo/***;
+ created new executable project prj-foo in $~/prj-foo/
EOE
- $build prj/ $cxx 2>>/EOE
- c++ prj/prj/cxx{prj}
- ld prj/prj/exe{prj}
+ $build prj-foo/ $cxx 2>>/EOE
+ c++ prj-foo/prj-foo/cxx{prj-foo}
+ ld prj-foo/prj-foo/exe{prj-foo}
EOE
}
: lib
{
- $* --no-amalgamation -t lib -l c++ libprj 2>>/"EOE" &libprj/***;
- created new library project libprj in $~/libprj/
+ $* --no-amalgamation -t lib -l c++ libprj-foo 2>>/"EOE" &libprj-foo/***;
+ created new library project libprj-foo in $~/libprj-foo/
EOE
- $build libprj/ $cxx 2>>/~%EOE%
+ $build libprj-foo/ $cxx 2>>/~%EOE%
%.{4}
- %ld libprj/.+%{3}
+ %ld libprj-foo/.+%{3}
EOE
}