summaryrefslogtreecommitdiff
path: root/cpp/src/slice2freeze/Main.cpp
diff options
context:
space:
mode:
authorMatthew Newhook <matthew@zeroc.com>2009-01-13 17:58:32 -0330
committerMatthew Newhook <matthew@zeroc.com>2009-01-13 17:58:32 -0330
commitefea74327637afce0a7f83b24d8c2f8b1d224f73 (patch)
treefda54ee1f4361dd3448c96753593fda264ce26cc /cpp/src/slice2freeze/Main.cpp
parentAdded patch for mcpp 2.7.2 (diff)
downloadice-efea74327637afce0a7f83b24d8c2f8b1d224f73.tar.bz2
ice-efea74327637afce0a7f83b24d8c2f8b1d224f73.tar.xz
ice-efea74327637afce0a7f83b24d8c2f8b1d224f73.zip
http://bugzilla.zeroc.com/bugzilla/show_bug.cgi?id=3640 - If slice2java errors out generated files are left behind
- Added file tracker - Added calls to file tracker when files or directories are created. - All translators cleanup created files if interrupted, or if they fail. - Normalized various error messages. - Fixed bug with slice2cs which would not correctly check if the impl files were created. Squashed commit of the following: commit 0fec143af219d59dcec5b91cb96a79179f73bd0d Author: Matthew Newhook <matthew@zeroc.com> Date: Tue Jan 13 17:57:25 2009 -0330 Fixed FileException for VC6. commit 38b22497c751ad9b2f86af2d44e87a23c0049d9c Merge: df29064... 4421a3d... Author: Matthew Newhook <matthew@zeroc.com> Date: Tue Jan 13 17:55:49 2009 -0330 Merged R3_3_branch. commit df290649637685bfff4f777ccf53f4f04fda71a0 Author: Matthew Newhook <matthew@zeroc.com> Date: Tue Jan 13 17:45:18 2009 -0330 move checksum writing outside loop commit a9bb2356167b1f9259ce2d1f1b43d40b7fd0ce82 Author: Matthew Newhook <matthew@zeroc.com> Date: Tue Jan 13 17:35:00 2009 -0330 added quotes commit a2f3d7a2414d08ebdc290222b8e97d8b203cc9ab Author: Matthew Newhook <matthew@zeroc.com> Date: Tue Jan 13 17:21:27 2009 -0330 can't -> cannot commit c3113e33a3687cae369bf7803e4f1d18c9141762 Author: Matthew Newhook <matthew@zeroc.com> Date: Tue Jan 13 17:10:12 2009 -0330 minor fixes to output. commit 2a17d6e1b6c0e5eed8be79b8353ca3c06572e19c Author: U-WIN-5WBK5GD0FYQ\matthew <matthew@WIN-5WBK5GD0FYQ.(none)> Date: Tue Jan 13 12:05:46 2009 -0800 windows fixes. commit d8d4f6dc35043fb71b599dac7171fee0c2bb87bf Author: Matthew Newhook <matthew@zeroc.com> Date: Tue Jan 13 15:58:21 2009 -0330 remove debugging. commit f1e4d7a55e13fea4c0b84244fb437bd391fbe538 Author: Matthew Newhook <matthew@zeroc.com> Date: Tue Jan 13 15:53:37 2009 -0330 Added FileTracker. Added file tracking, and cleanup to lots of translators. commit 33dbfb0124509779bd2d95bbac06a02ca9b907ac Author: Matthew Newhook <matthew@zeroc.com> Date: Tue Jan 13 09:42:16 2009 -0330 http://bugzilla/bugzilla/show_bug.cgi?id=3640 If slice2java errors out generated files are left behind
Diffstat (limited to 'cpp/src/slice2freeze/Main.cpp')
-rw-r--r--cpp/src/slice2freeze/Main.cpp428
1 files changed, 221 insertions, 207 deletions
diff --git a/cpp/src/slice2freeze/Main.cpp b/cpp/src/slice2freeze/Main.cpp
index 16e95c109eb..84f98ac7c27 100644
--- a/cpp/src/slice2freeze/Main.cpp
+++ b/cpp/src/slice2freeze/Main.cpp
@@ -14,6 +14,7 @@
#include <Slice/Preprocessor.h>
#include <Slice/Util.h>
#include <Slice/CPlusPlusUtil.h>
+#include <Slice/FileTracker.h>
#include <IceUtil/OutputUtil.h>
#include <IceUtil/StringUtil.h>
#include <cstring>
@@ -219,25 +220,25 @@ usage(const char* n)
// Note: --case-sensitive is intentionally not shown here!
}
-bool
-checkIdentifier(string n, string t, string s)
+void
+checkIdentifier(string t, string s)
{
if(s.empty() || (!isalpha(static_cast<unsigned char>(s[0])) && s[0] != '_'))
{
- cerr << n << ": `" << t << "' is not a valid type name" << endl;
- return false;
+ ostringstream os;
+ os << t << "' is not a valid type name";
+ throw os.str();
}
for(unsigned int i = 1; i < s.size(); ++i)
{
if(!isalnum(static_cast<unsigned char>(s[i])) && s[i] != '_')
{
- cerr << n << ": `" << t << "' is not a valid type name" << endl;
- return false;
+ ostringstream os;
+ os << t << "' is not a valid type name";
+ throw os.str();
}
}
-
- return true;
}
void
@@ -924,9 +925,8 @@ writeDictWithIndicesC(const string& name, const string& absolute, const Dict& di
}
}
-
-bool
-writeDict(const string& n, UnitPtr& u, const Dict& dict, Output& H, Output& C, const string& dllExport)
+void
+writeDict(const string& n, const UnitPtr& u, const Dict& dict, Output& H, Output& C, const string& dllExport)
{
string absolute = dict.name;
if(absolute.find("::") == 0)
@@ -941,32 +941,28 @@ writeDict(const string& n, UnitPtr& u, const Dict& dict, Output& H, Output& C, c
string s = name.substr(0, pos);
name.erase(0, pos + 2);
- if(!checkIdentifier(n, absolute, s))
- {
- return false;
- }
+ checkIdentifier(absolute, s);
scope.push_back(s);
}
- if(!checkIdentifier(n, absolute, name))
- {
- return false;
- }
+ checkIdentifier(absolute, name);
TypeList keyTypes = u->lookupType(dict.key, false);
if(keyTypes.empty())
{
- cerr << n << ": `" << dict.key << "' is not a valid type" << endl;
- return false;
+ ostringstream os;
+ os << "`" << dict.key << "' is not a valid type";
+ throw os.str();
}
TypePtr keyType = keyTypes.front();
TypeList valueTypes = u->lookupType(dict.value, false);
if(valueTypes.empty())
{
- cerr << n << ": `" << dict.value << "' is not a valid type" << endl;
- return false;
+ ostringstream os;
+ os << "`" << dict.value << "' is not a valid type";
+ throw os.str();
}
TypePtr valueType = valueTypes.front();
@@ -1002,19 +998,21 @@ writeDict(const string& n, UnitPtr& u, const Dict& dict, Output& H, Output& C, c
{
if(dict.indices.size() > 1)
{
- cerr << n << ": bad index for dictionary `" << dict.name << "'" << endl;
- return false;
+ ostringstream os;
+ os << "bad index for dictionary `" << dict.name << "'";
+ throw os.str();
}
bool containsSequence = false;
if(!Dictionary::legalKeyType(valueType, containsSequence))
{
- cerr << n << ": `" << dict.value << "' is not a valid index type" << endl;
- return false;
+ ostringstream os;
+ os << "`" << dict.value << "' is not a valid index type";
+ throw os.str();
}
if(containsSequence)
{
- cerr << n << ": warning: use of sequences in dictionary keys has been deprecated" << endl;
+ cerr << n << ": warning: use of sequences in dictionary keys has been deprecated";
}
@@ -1028,8 +1026,9 @@ writeDict(const string& n, UnitPtr& u, const Dict& dict, Output& H, Output& C, c
if(builtInType == 0 || builtInType->kind() != Builtin::KindString)
{
- cerr << n << ": VALUE is a `" << dict.value << "', not a string" << endl;
- return false;
+ ostringstream os;
+ os << "VALUE is a `" << dict.value << "', not a string";
+ throw os.str();
}
}
IndexType iType;
@@ -1052,8 +1051,9 @@ writeDict(const string& n, UnitPtr& u, const Dict& dict, Output& H, Output& C, c
StructPtr structDecl = StructPtr::dynamicCast(valueType);
if(structDecl == 0)
{
- cerr << n << ": `" << dict.value << "' is neither a class nor a struct." << endl;
- return false;
+ ostringstream os;
+ os << "`" << dict.value << "' is neither a class nor a struct.";
+ throw os.str();
}
dataMembers = structDecl->dataMembers();
}
@@ -1072,9 +1072,10 @@ writeDict(const string& n, UnitPtr& u, const Dict& dict, Output& H, Output& C, c
if(dataMember == 0)
{
- cerr << n << ": The value of `" << dict.name
- << "' has no data member named `" << index.member << "'" << endl;
- return false;
+ ostringstream os;
+ os << "The value of `" << dict.name
+ << "' has no data member named `" << index.member << "'";
+ throw os.str();
}
TypePtr dataMemberType = dataMember->type();
@@ -1082,12 +1083,13 @@ writeDict(const string& n, UnitPtr& u, const Dict& dict, Output& H, Output& C, c
bool containsSequence = false;
if(!Dictionary::legalKeyType(dataMemberType, containsSequence))
{
- cerr << n << ": `" << index.member << "' cannot be used as an index" << endl;
- return false;
+ ostringstream os;
+ os << "`" << index.member << "' cannot be used as an index";
+ throw os.str();
}
if(containsSequence)
{
- cerr << n << ": warning: use of sequences in dictionary keys has been deprecated" << endl;
+ cerr << n << ": warning: use of sequences in dictionary keys has been deprecated";
}
@@ -1099,8 +1101,9 @@ writeDict(const string& n, UnitPtr& u, const Dict& dict, Output& H, Output& C, c
BuiltinPtr memberType = BuiltinPtr::dynamicCast(dataMemberType);
if(memberType == 0 || memberType->kind() != Builtin::KindString)
{
- cerr << n << ": `" << index.member << "' is not a string " << endl;
- return false;
+ ostringstream os;
+ os << "`" << index.member << "' is not a string ";
+ throw os.str();
}
}
IndexType iType;
@@ -1128,11 +1131,8 @@ writeDict(const string& n, UnitPtr& u, const Dict& dict, Output& H, Output& C, c
writeDictWithIndicesC(name, absolute, dict, indexTypes, keyType, dict.keyMetaData, valueType,
dict.valueMetaData, C);
}
-
- return true;
}
-
void
writeIndexH(const string& memberTypeString, const string& name, Output& H, const string& dllExport)
{
@@ -1251,8 +1251,8 @@ writeIndexC(const TypePtr& type, const TypePtr& memberType, const string& member
C << eb;
}
-bool
-writeIndex(const string& n, UnitPtr& u, const Index& index, Output& H, Output& C, const string& dllExport)
+void
+writeIndex(const string& n, const UnitPtr& u, const Index& index, Output& H, Output& C, const string& dllExport)
{
string absolute = index.name;
if(absolute.find("::") == 0)
@@ -1267,32 +1267,28 @@ writeIndex(const string& n, UnitPtr& u, const Index& index, Output& H, Output& C
string s = name.substr(0, pos);
name.erase(0, pos + 2);
- if(!checkIdentifier(n, absolute, s))
- {
- return false;
- }
+ checkIdentifier(absolute, s);
scope.push_back(s);
}
- if(!checkIdentifier(n, absolute, name))
- {
- return false;
- }
+ checkIdentifier(absolute, name);
TypeList types = u->lookupType(index.type, false);
if(types.empty())
{
- cerr << n << ": `" << index.type << "' is not a valid type" << endl;
- return false;
+ ostringstream os;
+ os << "`" << index.type << "' is not a valid type";
+ throw os.str();
}
TypePtr type = types.front();
ClassDeclPtr classDecl = ClassDeclPtr::dynamicCast(type);
if(classDecl == 0)
{
- cerr << n << ": `" << index.type << "' is not a class" << endl;
- return false;
+ ostringstream os;
+ os << "`" << index.type << "' is not a class";
+ throw os.str();
}
DataMemberList dataMembers = classDecl->definition()->allDataMembers();
@@ -1312,8 +1308,9 @@ writeIndex(const string& n, UnitPtr& u, const Index& index, Output& H, Output& C
if(dataMember == 0)
{
- cerr << n << ": `" << index.type << "' has no data member named `" << index.member << "'" << endl;
- return false;
+ ostringstream os;
+ os << "`" << index.type << "' has no data member named `" << index.member << "'";
+ throw os.str();
}
if(index.caseSensitive == false)
@@ -1324,8 +1321,9 @@ writeIndex(const string& n, UnitPtr& u, const Index& index, Output& H, Output& C
BuiltinPtr memberType = BuiltinPtr::dynamicCast(dataMember->type());
if(memberType == 0 || memberType->kind() != Builtin::KindString)
{
- cerr << n << ": `" << index.member << "'is not a string " << endl;
- return false;
+ ostringstream os;
+ os << "`" << index.member << "'is not a string";
+ throw os.str();
}
}
@@ -1346,7 +1344,140 @@ writeIndex(const string& n, UnitPtr& u, const Index& index, Output& H, Output& C
}
writeIndexC(type, dataMember->type(), index.member, index.caseSensitive, absolute, name, C);
- return true;
+}
+
+void
+gen(const string& name, const UnitPtr& u, const vector<string>& includePaths, const vector<string>& extraHeaders,
+ const vector<Dict>& dicts, const vector<Index>& indices, const string& include, const string& headerExtension,
+ const string& sourceExtension, string dllExport, const StringList& includes, const vector<string>& args,
+ const string& output)
+{
+ string fileH = args[0];
+ fileH += "." + headerExtension;
+ string includeH = fileH;
+ string fileC = args[0];
+ fileC += "." + sourceExtension;
+
+ if(!output.empty())
+ {
+ fileH = output + '/' + fileH;
+ fileC = output + '/' + fileC;
+ }
+
+ u->mergeModules();
+ u->sort();
+
+ IceUtilInternal::Output H;
+ H.open(fileH.c_str());
+ if(!H)
+ {
+ ostringstream os;
+ os << "cannot open `" << fileH << "': " << strerror(errno);
+ throw FileException(__FILE__, __LINE__, os.str());
+ }
+
+ FileTracker::instance()->addFile(fileH);
+
+ printHeader(H);
+ printFreezeTypes(H, dicts, indices);
+
+ IceUtilInternal::Output CPP;
+ CPP.open(fileC.c_str());
+ if(!CPP)
+ {
+ ostringstream os;
+ os << "cannot open `" << fileC << "': " << strerror(errno);
+ throw FileException(__FILE__, __LINE__, os.str());
+ }
+ FileTracker::instance()->addFile(fileC);
+
+ printHeader(CPP);
+ printFreezeTypes(CPP, dicts, indices);
+
+ for(vector<string>::const_iterator i = extraHeaders.begin(); i != extraHeaders.end(); ++i)
+ {
+ string hdr = *i;
+ string guard;
+ string::size_type pos = hdr.rfind(',');
+ if(pos != string::npos)
+ {
+ hdr = i->substr(0, pos);
+ guard = i->substr(pos + 1);
+ }
+ if(!guard.empty())
+ {
+ CPP << "\n#ifndef " << guard;
+ CPP << "\n#define " << guard;
+ }
+ CPP << "\n#include <";
+ if(!include.empty())
+ {
+ CPP << include << '/';
+ }
+ CPP << hdr << '>';
+ if(!guard.empty())
+ {
+ CPP << "\n#endif";
+ }
+ }
+
+ string s = fileH;
+ transform(s.begin(), s.end(), s.begin(), ToIfdef());
+ H << "\n#ifndef __" << s << "__";
+ H << "\n#define __" << s << "__";
+ H << '\n';
+
+ if(dicts.size() > 0)
+ {
+ H << "\n#include <Freeze/Map.h>";
+ }
+
+ if(indices.size() > 0)
+ {
+ H << "\n#include <Freeze/Index.h>";
+ }
+
+ {
+ for(StringList::const_iterator p = includes.begin(); p != includes.end(); ++p)
+ {
+ H << "\n#include <" << changeInclude(*p, includePaths) << "." + headerExtension + ">";
+ }
+ }
+
+ CPP << "\n#include <Ice/BasicStream.h>";
+ CPP << "\n#include <";
+ if(include.size())
+ {
+ CPP << include << '/';
+ }
+ CPP << includeH << '>';
+
+ printVersionCheck(H);
+ printVersionCheck(CPP);
+
+ printDllExportStuff(H, dllExport);
+ if(dllExport.size())
+ {
+ dllExport += " ";
+ }
+
+ {
+ for(vector<Dict>::const_iterator p = dicts.begin(); p != dicts.end(); ++p)
+ {
+ writeDict(name, u, *p, H, CPP, dllExport);
+ }
+
+ for(vector<Index>::const_iterator q = indices.begin(); q != indices.end(); ++q)
+ {
+ writeIndex(name, u, *q, H, CPP, dllExport);
+ }
+ }
+
+ H << "\n\n#endif\n";
+ CPP << '\n';
+
+ H.close();
+ CPP.close();
}
int
@@ -1420,6 +1551,14 @@ main(int argc, char* argv[])
cppArgs.push_back("-I" + Preprocessor::normalizeIncludePath(*i));
}
+ // Convert include paths to full paths.
+ {
+ for(vector<string>::iterator p = includePaths.begin(); p != includePaths.end(); ++p)
+ {
+ *p = fullPath(*p);
+ }
+ }
+
bool preprocess= opts.isSet("E");
string include = opts.optArg("include-dir");
@@ -1775,17 +1914,6 @@ main(int argc, char* argv[])
return EXIT_FAILURE;
}
- string fileH = args[0];
- fileH += "." + headerExtension;
- string includeH = fileH;
- string fileC = args[0];
- fileC += "." + sourceExtension;
- if(!output.empty())
- {
- fileH = output + '/' + fileH;
- fileC = output + '/' + fileC;
- }
-
UnitPtr u = Unit::createUnit(true, false, ice, caseSensitive);
StringList includes;
@@ -1849,153 +1977,39 @@ main(int argc, char* argv[])
}
}
+
if(status == EXIT_SUCCESS && !preprocess)
{
- u->mergeModules();
- u->sort();
-
+ try
{
- for(vector<string>::iterator p = includePaths.begin(); p != includePaths.end(); ++p)
- {
- *p = fullPath(*p);
- }
+ gen(argv[0], u, includePaths, extraHeaders, dicts, indices, include, headerExtension,
+ sourceExtension, dllExport, includes, args, output);
}
-
- IceUtilInternal::Output H;
- H.open(fileH.c_str());
- if(!H)
+ catch(const string& ex)
{
- cerr << argv[0] << ": can't open `" << fileH << "' for writing: " << strerror(errno) << endl;
+ // If a file could not be created, then cleanup any
+ // created files.
+ FileTracker::instance()->cleanup();
u->destroy();
+ cerr << argv[0] << ": " << ex << endl;
return EXIT_FAILURE;
}
- printHeader(H);
- printFreezeTypes(H, dicts, indices);
-
- IceUtilInternal::Output CPP;
- CPP.open(fileC.c_str());
- if(!CPP)
+ catch(const Slice::FileException& ex)
{
- cerr << argv[0] << ": can't open `" << fileC << "' for writing: " << strerror(errno) << endl;
+ // If a file could not be created, then cleanup any
+ // created files.
+ FileTracker::instance()->cleanup();
u->destroy();
+ cerr << argv[0] << ": " << ex.reason() << endl;
return EXIT_FAILURE;
}
- printHeader(CPP);
- printFreezeTypes(CPP, dicts, indices);
-
- for(vector<string>::const_iterator i = extraHeaders.begin(); i != extraHeaders.end(); ++i)
- {
- string hdr = *i;
- string guard;
- string::size_type pos = hdr.rfind(',');
- if(pos != string::npos)
- {
- hdr = i->substr(0, pos);
- guard = i->substr(pos + 1);
- }
- if(!guard.empty())
- {
- CPP << "\n#ifndef " << guard;
- CPP << "\n#define " << guard;
- }
- CPP << "\n#include <";
- if(!include.empty())
- {
- CPP << include << '/';
- }
- CPP << hdr << '>';
- if(!guard.empty())
- {
- CPP << "\n#endif";
- }
- }
-
- string s = fileH;
- transform(s.begin(), s.end(), s.begin(), ToIfdef());
- H << "\n#ifndef __" << s << "__";
- H << "\n#define __" << s << "__";
- H << '\n';
-
- if(dicts.size() > 0)
- {
- H << "\n#include <Freeze/Map.h>";
- }
-
- if(indices.size() > 0)
- {
- H << "\n#include <Freeze/Index.h>";
- }
-
-
+ catch(...)
{
- for(StringList::const_iterator p = includes.begin(); p != includes.end(); ++p)
- {
- H << "\n#include <" << changeInclude(*p, includePaths) << "." + headerExtension + ">";
- }
- }
-
- CPP << "\n#include <Ice/BasicStream.h>";
- CPP << "\n#include <";
- if(include.size())
- {
- CPP << include << '/';
- }
- CPP << includeH << '>';
-
- printVersionCheck(H);
- printVersionCheck(CPP);
-
- printDllExportStuff(H, dllExport);
- if(dllExport.size())
- {
- dllExport += " ";
- }
-
- {
- for(vector<Dict>::const_iterator p = dicts.begin(); p != dicts.end(); ++p)
- {
- try
- {
- if(!writeDict(argv[0], u, *p, H, CPP, dllExport))
- {
- u->destroy();
- return EXIT_FAILURE;
- }
- }
- catch(...)
- {
- cerr << argv[0] << ": unknown exception" << endl;
- u->destroy();
- return EXIT_FAILURE;
- }
- }
-
-
- for(vector<Index>::const_iterator q = indices.begin(); q != indices.end(); ++q)
- {
- try
- {
- if(!writeIndex(argv[0], u, *q, H, CPP, dllExport))
- {
- u->destroy();
- return EXIT_FAILURE;
- }
- }
- catch(...)
- {
- cerr << argv[0] << ": unknown exception" << endl;
- u->destroy();
- return EXIT_FAILURE;
- }
- }
-
+ cerr << argv[0] << ": unknown exception" << endl;
+ FileTracker::instance()->cleanup();
+ u->destroy();
+ return EXIT_FAILURE;
}
-
- H << "\n\n#endif\n";
- CPP << '\n';
-
- H.close();
- CPP.close();
}
u->destroy();
@@ -2005,10 +2019,10 @@ main(int argc, char* argv[])
if(_interrupted)
{
+ FileTracker::instance()->cleanup();
return EXIT_FAILURE;
}
}
-
return status;
}