From stefan at users.sourceforge.net Tue Aug 19 20:23:30 2003 From: stefan at users.sourceforge.net (Stefan Seefeld) Date: Wed Mar 2 21:06:30 2005 Subject: [Synopsis-cvs] Synopsis/Synopsis/Synopsis/Parser/C - New directory Message-ID: Update of /cvsroot/synopsis/Synopsis/Synopsis/Parser/C In directory sc8-pr-cvs1:/tmp/cvs-serv2041/Synopsis/Parser/C Log Message: Directory /cvsroot/synopsis/Synopsis/Synopsis/Parser/C added to the repository From stefan at users.sourceforge.net Tue Aug 19 20:37:10 2003 From: stefan at users.sourceforge.net (Stefan Seefeld) Date: Wed Mar 2 21:06:30 2005 Subject: [Synopsis-cvs] Synopsis/Synopsis configure.in,1.17,1.18 Message-ID: Update of /cvsroot/synopsis/Synopsis In directory sc8-pr-cvs1:/tmp/cvs-serv8439 Modified Files: configure.in Log Message: little omission Index: configure.in =================================================================== RCS file: /cvsroot/synopsis/Synopsis/configure.in,v retrieving revision 1.17 retrieving revision 1.18 diff -u -p -d -r1.17 -r1.18 --- configure.in 20 Aug 2003 02:16:37 -0000 1.17 +++ configure.in 20 Aug 2003 03:06:24 -0000 1.18 @@ -139,6 +139,7 @@ if test ".$config_ctool" = ".yes"; then CPPFLAGS="$CPPFLAGS $CTOOL_CPPFLAGS" AC_CHECK_HEADER(ctool/ctool.h, CTOOL=1) CPPFLAGS="$save_CPPFLAGS" + CTOOL_LIBS="$CTOOL_LIBS -lctool" fi AC_SUBST(CTOOL) AC_SUBST(CTOOL_CPPFLAGS) From stefan at users.sourceforge.net Tue Aug 19 21:58:02 2003 From: stefan at users.sourceforge.net (Stefan Seefeld) Date: Wed Mar 2 21:06:30 2005 Subject: [Synopsis-cvs] Synopsis/Synopsis/Synopsis/Parser/C Makefile,1.1,1.2 ctool.cc,1.1,1.2 Message-ID: Update of /cvsroot/synopsis/Synopsis/Synopsis/Parser/C In directory sc8-pr-cvs1:/tmp/cvs-serv11348/Synopsis/Parser/C Modified Files: Makefile ctool.cc Log Message: little fixes Index: Makefile =================================================================== RCS file: /cvsroot/synopsis/Synopsis/Synopsis/Parser/C/Makefile,v retrieving revision 1.1 retrieving revision 1.2 diff -u -p -d -r1.1 -r1.2 --- Makefile 20 Aug 2003 02:16:37 -0000 1.1 +++ Makefile 20 Aug 2003 03:25:16 -0000 1.2 @@ -32,6 +32,9 @@ else CPPFLAGS+= -I $(python_prefix)/include -D PYTHON_INCLUDE="$(python_include)" endif +CPPFLAGS+= $(CTOOL_CPPFLAGS) +LIBS := $(CTOOL_LIBS) $(LIBS) + SRC := Translator.cc ctool.cc OBJ := $(patsubst %.cc, %.o, $(SRC)) DEP := $(patsubst %.cc, %.d, $(SRC)) Index: ctool.cc =================================================================== RCS file: /cvsroot/synopsis/Synopsis/Synopsis/Parser/C/ctool.cc,v retrieving revision 1.1 retrieving revision 1.2 diff -u -p -d -r1.1 -r1.2 --- ctool.cc 20 Aug 2003 02:16:37 -0000 1.1 +++ ctool.cc 20 Aug 2003 03:25:16 -0000 1.2 @@ -19,6 +19,9 @@ // 02111-1307, USA. // // $Log$ +// Revision 1.2 2003/08/20 03:25:16 stefan +// little fixes +// // Revision 1.1 2003/08/20 02:16:37 stefan // first steps towards a C parser backend (based on the ctool) // @@ -36,6 +39,10 @@ #endif #include +#if 1 +int Trace::level = 0; +#endif + bool verbose; // If true then everything but what's in the main file will be stripped @@ -65,7 +72,8 @@ void unexpected() void getopts(PyObject *args, std::vector &cppflags, std::vector &ctflags, - PyObject* config) + PyObject *config, + PyObject *extra_files) { verbose = false; syn_main_only = false; @@ -187,6 +195,27 @@ Py_XDECREF(value); else if (strcmp(argument, "-t") == 0) syn_extract_tails = true; } + + // If multi_files is set, we check the extra_files argument to see if it + // has a list of filenames like it should do + if (extra_files && PyList_Check(extra_files)) + { +#if 0 + size_t extra_size = PyList_Size(extra_files); + if (extra_size > 0) + { + PyObject* item; + const char* string; + syn_extra_filenames = new std::vector; + for (size_t i = 0; i < extra_size; i++) + { + item = PyList_GetItem(extra_files, i); + string = PyString_AsString(item); + syn_extra_filenames->push_back(string); + } + } +#endif + } } char *RunPreprocessor(const char *file, const std::vector &flags) @@ -325,6 +354,7 @@ void do_parse(const char *src, PyObject *ast, PyObject *types, PyObject *declarations, PyObject* files) { + Trace trace("do_parse"); // Run the preprocessor char *cppfile = RunPreprocessor(src, cppargs); @@ -338,12 +368,12 @@ PyObject *ctoolParse(PyObject *self, PyO Trace trace("ctoolParse"); char *src; - PyObject *parserargs, *types, *declarations, *config, *ast; - if (!PyArg_ParseTuple(args, "sO!O", &src, &PyList_Type, &parserargs, &config)) + PyObject *extra_files, *parserargs, *types, *declarations, *config, *ast; + if (!PyArg_ParseTuple(args, "sOO!O", &src, &extra_files, &PyList_Type, &parserargs, &config)) return 0; std::vector cppargs; std::vector ctargs; - getopts(parserargs, cppargs, ctargs, config); + getopts(parserargs, cppargs, ctargs, config, extra_files); if (!src || *src == '\0') { std::cerr << "No source file" << std::endl; From stefan at users.sourceforge.net Tue Aug 19 22:15:04 2003 From: stefan at users.sourceforge.net (Stefan Seefeld) Date: Wed Mar 2 21:06:31 2005 Subject: [Synopsis-cvs] Synopsis/Synopsis/demo/C - New directory Message-ID: Update of /cvsroot/synopsis/Synopsis/demo/C In directory sc8-pr-cvs1:/tmp/cvs-serv8465/demo/C Log Message: Directory /cvsroot/synopsis/Synopsis/demo/C added to the repository From stefan at users.sourceforge.net Wed Aug 20 00:01:03 2003 From: stefan at users.sourceforge.net (Stefan Seefeld) Date: Wed Mar 2 21:06:31 2005 Subject: [Synopsis-cvs] Synopsis/Synopsis/demo/C API.h,NONE,1.1 Makefile,NONE,1.1 Message-ID: Update of /cvsroot/synopsis/Synopsis/demo/C In directory sc8-pr-cvs1:/tmp/cvs-serv8576/demo/C Added Files: API.h Makefile Log Message: little sandbox to work with to develop the C backend --- NEW FILE: API.h --- #ifndef _API_h #define _API_h struct Foo { int bar; char *baz; }; typedef int Baz; #endif --- NEW FILE: Makefile --- synopsis := ../../bin/synopsis -v all: html html: API.syn rm -rf $@ $(synopsis) -f HTML -o $@ $< API.syn: API.h $(synopsis) -p C -o $@ $< From stefan at users.sourceforge.net Wed Aug 20 00:01:18 2003 From: stefan at users.sourceforge.net (Stefan Seefeld) Date: Wed Mar 2 21:06:31 2005 Subject: [Synopsis-cvs] Synopsis/Synopsis/demo Makefile,1.4,1.5 Message-ID: Update of /cvsroot/synopsis/Synopsis/demo In directory sc8-pr-cvs1:/tmp/cvs-serv8576/demo Modified Files: Makefile Log Message: little sandbox to work with to develop the C backend Index: Makefile =================================================================== RCS file: /cvsroot/synopsis/Synopsis/demo/Makefile,v retrieving revision 1.4 retrieving revision 1.5 diff -u -p -d -r1.4 -r1.5 --- Makefile 16 Mar 2003 03:22:39 -0000 1.4 +++ Makefile 20 Aug 2003 03:07:25 -0000 1.5 @@ -27,6 +27,9 @@ endif endif subdirs := IDL C++ Python Mixed Graph Config Perceps +ifeq ($(CTOOL), 1) +subdirs += C +endif action := all From stefan at users.sourceforge.net Wed Aug 20 00:44:02 2003 From: stefan at users.sourceforge.net (Stefan Seefeld) Date: Wed Mar 2 21:06:31 2005 Subject: [Synopsis-cvs] Synopsis/Synopsis/Synopsis/Parser Makefile,1.15,1.16 Message-ID: Update of /cvsroot/synopsis/Synopsis/Synopsis/Parser In directory sc8-pr-cvs1:/tmp/cvs-serv2431/Synopsis/Parser Modified Files: Makefile Log Message: first steps towards a C parser backend (based on the ctool) Index: Makefile =================================================================== RCS file: /cvsroot/synopsis/Synopsis/Synopsis/Parser/Makefile,v retrieving revision 1.15 retrieving revision 1.16 diff -u -p -d -r1.15 -r1.16 --- Makefile 1 Feb 2003 05:43:04 -0000 1.15 +++ Makefile 20 Aug 2003 02:16:37 -0000 1.16 @@ -30,6 +30,9 @@ SRC := __init__ PY := $(patsubst %, %.py, $(SRC)) $(patsubst %, %.pyc, $(SRC)) subdirs := IDL C++ Python +ifeq ($(CTOOL), 1) +subdirs += C +endif action := all From stefan at users.sourceforge.net Wed Aug 20 01:14:04 2003 From: stefan at users.sourceforge.net (Stefan Seefeld) Date: Wed Mar 2 21:06:32 2005 Subject: [Synopsis-cvs] Synopsis/Synopsis/Synopsis/Parser/C Makefile,NONE,1.1 Trace.hh,NONE,1.1 Translator.cc,NONE,1.1 Translator.hh,NONE,1.1 __init__.py,NONE,1.1 ctool.cc,NONE,1.1 Message-ID: Update of /cvsroot/synopsis/Synopsis/Synopsis/Parser/C In directory sc8-pr-cvs1:/tmp/cvs-serv2431/Synopsis/Parser/C Added Files: Makefile Trace.hh Translator.cc Translator.hh __init__.py ctool.cc Log Message: first steps towards a C parser backend (based on the ctool) --- NEW FILE: Makefile --- # $Id: Makefile,v 1.1 2003/08/20 02:16:37 stefan Exp $ # # This source file is a part of the Synopsis Project # Copyright (C) 2003 Stefan Seefeld # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Library General Public # License as published by the Free Software Foundation; either # version 2 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Library General Public License for more details. # # You should have received a copy of the GNU Library General Public # License along with this library; if not, write to the # Free Software Foundation, Inc., 675 Mass Ave, Cambridge, # MA 02139, USA. SHELL = /bin/sh ifeq (,$(findstring $(MAKECMDGOALS), clean distclean)) ifeq (,$(findstring $(action), clean distclean)) include ../../../local.mk endif endif ifeq '$(python_prefix)' '/usr' CPPFLAGS+= -D PYTHON_INCLUDE="$(python_include)" else CPPFLAGS+= -I $(python_prefix)/include -D PYTHON_INCLUDE="$(python_include)" endif SRC := Translator.cc ctool.cc OBJ := $(patsubst %.cc, %.o, $(SRC)) DEP := $(patsubst %.cc, %.d, $(SRC)) MODULE := ctool.$(LIBRARY_EXT) PY := __init__.py __init__.pyc $(MODULE) all: $(MODULE) $(MODULE): $(OBJ) $(CXX) -shared $(CPPFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) clean: distclean: clean install: mkdir -p $(packagedir)/Synopsis/Parser/C install -m 644 $(PY) $(packagedir)/Synopsis/Parser/C %.o : %.cc $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $< %.d : %.cc $(SHELL) -ec '$(MAKEDEP) $(CPPFLAGS) $< \ | sed "s/$*\\.o[ :]*/$*\\.d $*\\.o : /g" > $@' ifeq (,$(filter $(MAKECMDGOALS), depend doc clean distclean maintainer-clean)) -include $(DEP) endif --- NEW FILE: Trace.hh --- // $Id: Trace.hh,v 1.1 2003/08/20 02:16:37 stefan Exp $ // // This file is a part of Synopsis. // Copyright (C) 2003 Stefan Seefeld // // Synopsis is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. // // $Log: Trace.hh,v $ // Revision 1.1 2003/08/20 02:16:37 stefan // first steps towards a C parser backend (based on the ctool) // // #ifndef _Trace_hh #define _Trace_hh #include #include #if 1 class Trace { public: Trace(const std::string &s) : scope(s) { std::cout << indent() << "entering " << scope << std::endl; ++level; } ~Trace() { --level; std::cout << indent() << "leaving " << scope << std::endl; } private: std::string indent() { return std::string(level, ' ');} static int level; std::string scope; }; #else class Trace { public: Trace(const std::string &) {} ~Trace() {} }; #endif #endif --- NEW FILE: Translator.cc --- // $Id: Translator.cc,v 1.1 2003/08/20 02:16:37 stefan Exp $ // // This file is a part of Synopsis. // Copyright (C) 2003 Stefan Seefeld // // Synopsis is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. // // $Log: Translator.cc,v $ // Revision 1.1 2003/08/20 02:16:37 stefan // first steps towards a C parser backend (based on the ctool) // // #include #include "Translator.hh" Translator::Translator() { } Translator::~Translator() { } void Translator::traverse_base(BaseType *) { } void Translator::traverse_ptr(PtrType *) { } void Translator::traverse_array(ArrayType *) { } void Translator::traverse_bit_field(BitFieldType *) { } void Translator::traverse_function(FunctionType *) { } void Translator::traverse_symbol(Symbol *) { } void Translator::traverse_int(IntConstant *) { } void Translator::traverse_uint(UIntConstant *) { } void Translator::traverse_float(FloatConstant *) { } void Translator::traverse_char(CharConstant *) { } void Translator::traverse_string(StringConstant *) { } void Translator::traverse_array(ArrayConstant *) { } void Translator::traverse_enum(EnumConstant *) { } void Translator::traverse_variable(Variable *) { } void Translator::traverse_call(FunctionCall *) { } void Translator::traverse_unary(UnaryExpr *) { } void Translator::traverse_binary(BinaryExpr *) { } void Translator::traverse_trinary(TrinaryExpr *) { } void Translator::traverse_assign(AssignExpr *) { } void Translator::traverse_rel(RelExpr *) { } void Translator::traverse_cast(CastExpr *) { } void Translator::traverse_sizeof(SizeofExpr *) { } void Translator::traverse_index(IndexExpr *) { } void Translator::traverse_label(Label *) { } void Translator::traverse_decl(Decl *) { } void Translator::traverse_statement(Statement *) { } void Translator::traverse_file_line(FileLineStemnt *) { } void Translator::traverse_include(InclStemnt *) { } void Translator::traverse_end_include(EndInclStemnt *) { } void Translator::traverse_expression(ExpressionStemnt *) { } void Translator::traverse_if(IfStemnt *) { } void Translator::traverse_switch(SwitchStemnt *) { } void Translator::traverse_for(ForStemnt *) { } void Translator::traverse_while(WhileStemnt *) { } void Translator::traverse_do_while(DoWhileStemnt *) { } void Translator::traverse_goto(GotoStemnt *) { } void Translator::traverse_return(ReturnStemnt *) { } void Translator::traverse_declaration(DeclStemnt *) { } void Translator::traverse_typedef(TypedefStemnt *) { } void Translator::traverse_block(Block *) { } void Translator::traverse_function_definition(FunctionDef *) { } void Translator::traverse_unit(TransUnit *) { } --- NEW FILE: Translator.hh --- // $Id: Translator.hh,v 1.1 2003/08/20 02:16:37 stefan Exp $ // // This file is a part of Synopsis. // Copyright (C) 2003 Stefan Seefeld // // Synopsis is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. // // $Log: Translator.hh,v $ // Revision 1.1 2003/08/20 02:16:37 stefan // first steps towards a C parser backend (based on the ctool) // // #ifndef _Translator_hh #define _Translator_hh #include #include #include #include class Translator : public Traversal { public: Translator(); virtual ~Translator(); virtual void traverse_base(BaseType *); virtual void traverse_ptr(PtrType *); virtual void traverse_array(ArrayType *); virtual void traverse_bit_field(BitFieldType *); virtual void traverse_function(FunctionType *); virtual void traverse_symbol(Symbol *); virtual void traverse_int(IntConstant *); virtual void traverse_uint(UIntConstant *); virtual void traverse_float(FloatConstant *); virtual void traverse_char(CharConstant *); virtual void traverse_string(StringConstant *); virtual void traverse_array(ArrayConstant *); virtual void traverse_enum(EnumConstant *); virtual void traverse_variable(Variable *); virtual void traverse_call(FunctionCall *); virtual void traverse_unary(UnaryExpr *); virtual void traverse_binary(BinaryExpr *); virtual void traverse_trinary(TrinaryExpr *); virtual void traverse_assign(AssignExpr *); virtual void traverse_rel(RelExpr *); virtual void traverse_cast(CastExpr *); virtual void traverse_sizeof(SizeofExpr *); virtual void traverse_index(IndexExpr *); virtual void traverse_label(Label *); virtual void traverse_decl(Decl *); virtual void traverse_statement(Statement *); virtual void traverse_file_line(FileLineStemnt *); virtual void traverse_include(InclStemnt *); virtual void traverse_end_include(EndInclStemnt *); virtual void traverse_expression(ExpressionStemnt *); virtual void traverse_if(IfStemnt *); virtual void traverse_switch(SwitchStemnt *); virtual void traverse_for(ForStemnt *); virtual void traverse_while(WhileStemnt *); virtual void traverse_do_while(DoWhileStemnt *); virtual void traverse_goto(GotoStemnt *); virtual void traverse_return(ReturnStemnt *); virtual void traverse_declaration(DeclStemnt *); virtual void traverse_typedef(TypedefStemnt *); virtual void traverse_block(Block *); virtual void traverse_function_definition(FunctionDef *); virtual void traverse_unit(TransUnit *); private: }; #endif --- NEW FILE: __init__.py --- """Parser for C using CTool for low-level parsing. This parser is written entirely in C++, and compiled into shared libraries for use by python. """ # # configure for the parser you want here... # try: from ctool import parse, usage except: import sys print sys.exc_type, sys.exc_value # THIS-IS-A-PARSER --- NEW FILE: ctool.cc --- // $Id: ctool.cc,v 1.1 2003/08/20 02:16:37 stefan Exp $ // // This file is a part of Synopsis. // Copyright (C) 2003 Stefan Seefeld // // Synopsis is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. // // $Log: ctool.cc,v $ // Revision 1.1 2003/08/20 02:16:37 stefan // first steps towards a C parser backend (based on the ctool) // // #include #include "Translator.hh" #include "Trace.hh" #include #include #ifdef PYTHON_INCLUDE # include PYTHON_INCLUDE #else # include #endif #include bool verbose; // If true then everything but what's in the main file will be stripped bool syn_main_only; bool syn_extract_tails; std::string syn_cpp; bool syn_multi_files; // If set then this is stripped from the start of all filenames const char* syn_basename = ""; // If set then this is the prefix for the filename to store links to const char* syn_syntax_prefix = 0; // A place to temporarily store Python's thread state PyThreadState *pythread_save; namespace { //. Override unexpected() to print a message before we abort void unexpected() { std::cout << "Warning: Aborting due to unexpected exception." << std::endl; throw std::bad_exception(); } void getopts(PyObject *args, std::vector &cppflags, std::vector &ctflags, PyObject* config) { verbose = false; syn_main_only = false; syn_extract_tails = false; syn_multi_files = false; #define IsType(obj, type) (Py##type##_Check(obj)) #define OPT_FLAG(syn_flag, config_name) \ if ((value = PyObject_GetAttrString(config, config_name)) != 0)\ syn_flag = PyObject_IsTrue(value);\ Py_XDECREF(value); #define OPT_STRING(syn_name, config_name) \ if ((value = PyObject_GetAttrString(config, config_name)) != 0)\ { if (!IsType(value, String)) throw "Error: " config_name " must be a string.";\ syn_name = PyString_AsString(value);\ }\ Py_XDECREF(value); // Check config object first if (config) { PyObject* value; OPT_FLAG(verbose, "verbose"); OPT_FLAG(syn_main_only, "main_file"); // Grab the include paths if ((value = PyObject_GetAttrString(config, "include_path")) != 0) { if (!IsType(value, List)) { std::cerr << "Error: include_path must be a list of strings." << std::endl; exit(1); } // Loop through the include paths for (int i=0, end=PyList_Size(value); i < end; i++) { PyObject* item = PyList_GetItem(value, i); if (!item || !IsType(item, String)) { std::cerr << "Error: include_path must be a list of strings." << std::endl; exit(1); } // mem leak.. how to fix? char* buf = new char[PyString_Size(item)+3]; strcpy(buf, "-I"); strcat(buf, PyString_AsString(item)); cppflags.push_back(buf); } // for } Py_XDECREF(value); // Grab the list of defines if ((value = PyObject_GetAttrString(config, "defines")) != 0) { if (!IsType(value, List)) { std::cerr << "Error: defines must be a list of strings." << std::endl; exit(1); } // Loop through the include paths for (int i=0, end=PyList_Size(value); i < end; i++) { PyObject* item = PyList_GetItem(value, i); if (!item || !IsType(item, String)) { std::cerr << "Error: defines must be a list of strings." << std::endl; exit(1); } // mem leak.. how to fix? char* buf = new char[PyString_Size(item)+3]; strcpy(buf, "-D"); strcat(buf, PyString_AsString(item)); cppflags.push_back(buf); } // for } Py_XDECREF(value); // Basename is the prefix to strip from filenames OPT_STRING(syn_basename, "basename"); // Extract tails tells the parser to find tail comments OPT_FLAG(syn_extract_tails, "extract_tails"); // 'syntax_prefix' defines the prefix for the filename to write syntax hilite info to OPT_STRING(syn_syntax_prefix, "syntax_prefix"); // 'preprocessor' defines the preprocessor string char *temp_string = 0; OPT_STRING(temp_string, "preprocessor"); syn_cpp = temp_string ? temp_string : "cpp"; // If multiple_files is set then the parser handles multiple files // included from the main one at the same time (they get into the AST OPT_FLAG(syn_multi_files, "multiple_files"); } // if config #undef OPT_STRING #undef OPT_FLAG #undef IsType // Now check command line args size_t argsize = PyList_Size(args); for (size_t i = 0; i != argsize; ++i) { const char *argument = PyString_AsString(PyList_GetItem(args, i)); if (strncmp(argument, "-I", 2) == 0) { cppflags.push_back(argument); if (strlen(argument) == 2) cppflags.push_back(PyString_AsString(PyList_GetItem(args, ++i))); } else if (strncmp(argument, "-D", 2) == 0) { cppflags.push_back(argument); if (strlen(argument) == 2) cppflags.push_back(PyString_AsString(PyList_GetItem(args, ++i))); } else if (strcmp(argument, "-v") == 0) verbose = true; else if (strcmp(argument, "-m") == 0) syn_main_only = true; else if (strcmp(argument, "-b") == 0) syn_basename = PyString_AsString(PyList_GetItem(args, ++i)); else if (strcmp(argument, "-t") == 0) syn_extract_tails = true; } } char *RunPreprocessor(const char *file, const std::vector &flags) { static char dest[1024]; strcpy(dest, "/tmp/synopsis-XXXXXX"); int temp_fd = mkstemp(dest); if (temp_fd == -1) { perror("RunPreprocessor"); exit(1); } // Not interested in the open file, just the unique filename close(temp_fd); // Release Python's global interpreter lock pythread_save = PyEval_SaveThread(); switch(fork()) { case 0: { std::vector args; char *cc = getenv("CC"); if (cc) { // separate command and arguments do { args.push_back(cc); cc = strchr(cc, ' '); // find next whitespace... while (cc && *cc == ' ') *cc++ = '\0'; // ...and skip to next non-ws } while (cc && *cc != '\0'); } else { args.push_back("cpp"); } args.insert(args.end(), flags.begin(), flags.end()); args.push_back("-C"); // keep comments args.push_back("-E"); // stop after preprocessing args.push_back("-o"); // output to... args.push_back(dest); //args.push_back("-x"); // language c++ //args.push_back("c++"); args.push_back(file); if (verbose) { std::cout << "calling external preprocessor\n" << args[0]; for (std::vector::iterator i = args.begin(); i != args.end(); ++i) std::cout << ' ' << *i; std::cout << std::endl; } args.push_back(0); execvp(args[0], (char **)&*args.begin()); perror("cannot invoke compiler"); exit(-1); break; } case -1: perror("RunPreprocessor"); exit(-1); break; default: { int status; wait(&status); if (status != 0) { if (WIFEXITED(status)) std::cout << "exited with status " << WEXITSTATUS(status) << std::endl; else if (WIFSIGNALED(status)) std::cout << "stopped with status " << WTERMSIG(status) << std::endl; exit(1); } } } // switch return dest; } void sighandler(int signo) { std::string signame; switch (signo) { case SIGABRT: signame = "Abort"; break; case SIGBUS: signame = "Bus error"; break; case SIGSEGV: signame = "Segmentation Violation"; break; default: signame = "unknown"; break; }; // SWalker *instance = SWalker::instance(); #if 0 std::cerr << signame << " caught while processing " << instance->current_file()->filename() << " at line " << instance->current_lineno() << std::endl; #endif std::cerr << signame << " caught" << std::endl; exit(-1); } void RunCTool(const char *src, const char *file, const std::vector &args, PyObject *ast, PyObject *types, PyObject *declarations, PyObject* files) { Trace trace("RunCTool"); std::set_unexpected(unexpected); struct sigaction olda; struct sigaction newa; newa.sa_handler = &sighandler; sigaction(SIGSEGV, &newa, &olda); sigaction(SIGBUS, &newa, &olda); sigaction(SIGABRT, &newa, &olda); Project *prj = new Project(); // TransUnit *unit = prj->parse(file_list[i], use_cpp, cpp_dir, // keep_cpp_file, cpp_file, cpp_cmmd, cd_cmmd); sigaction(SIGABRT, &olda, 0); sigaction(SIGBUS, &olda, 0); sigaction(SIGSEGV, &olda, 0); } void do_parse(const char *src, const std::vector& cppargs, const std::vector& ctargs, PyObject *ast, PyObject *types, PyObject *declarations, PyObject* files) { // Run the preprocessor char *cppfile = RunPreprocessor(src, cppargs); // Run CTool to generate the AST RunCTool(src, cppfile, ctargs, ast, types, declarations, files); unlink(cppfile); } PyObject *ctoolParse(PyObject *self, PyObject *args) { Trace trace("ctoolParse"); char *src; PyObject *parserargs, *types, *declarations, *config, *ast; if (!PyArg_ParseTuple(args, "sO!O", &src, &PyList_Type, &parserargs, &config)) return 0; std::vector cppargs; std::vector ctargs; getopts(parserargs, cppargs, ctargs, config); if (!src || *src == '\0') { std::cerr << "No source file" << std::endl; exit(-1); } // Make AST object #define assertObject(pyo) if (!pyo) PyErr_Print(); assert(pyo) PyObject* ast_module = PyImport_ImportModule("Synopsis.Core.AST"); assertObject(ast_module); ast = PyObject_CallMethod(ast_module, "AST", ""); assertObject(ast); PyObject* files = PyObject_CallMethod(ast, "files", ""); assertObject(files); declarations = PyObject_CallMethod(ast, "declarations", ""); assertObject(declarations); types = PyObject_CallMethod(ast, "types", ""); assertObject(types); #undef assertObject do_parse(src, cppargs, ctargs, ast, types, declarations, files); Py_DECREF(ast_module); Py_DECREF(declarations); Py_DECREF(files); Py_DECREF(types); return ast; } PyObject *ctoolUsage(PyObject *self, PyObject *) { Trace trace("ctoolUsage"); std::cout << " -I Specify include path to be used by the preprocessor\n" << " -D Specify macro to be used by the preprocessor\n" << " -m Unly keep declarations from the main file\n" << " -b basepath Strip basepath from start of filenames" << std::endl; Py_INCREF(Py_None); return Py_None; } PyMethodDef ctool_methods[] = { {(char*)"parse", ctoolParse, METH_VARARGS}, {(char*)"usage", ctoolUsage, METH_VARARGS}, {0, 0} }; }; extern "C" void initctool() { PyObject *m = Py_InitModule((char*)"ctool", ctool_methods); PyObject_SetAttrString(m, (char*)"version", PyString_FromString("0.1")); } From stefan at users.sourceforge.net Wed Aug 20 01:21:03 2003 From: stefan at users.sourceforge.net (Stefan Seefeld) Date: Wed Mar 2 21:06:32 2005 Subject: [Synopsis-cvs] Synopsis/Synopsis/Synopsis/Parser/C++/syn Makefile,1.54,1.55 Message-ID: Update of /cvsroot/synopsis/Synopsis/Synopsis/Parser/C++/syn In directory sc8-pr-cvs1:/tmp/cvs-serv2431/Synopsis/Parser/C++/syn Modified Files: Makefile Log Message: first steps towards a C parser backend (based on the ctool) Index: Makefile =================================================================== RCS file: /cvsroot/synopsis/Synopsis/Synopsis/Parser/C++/syn/Makefile,v retrieving revision 1.54 retrieving revision 1.55 diff -u -p -d -r1.54 -r1.55 --- Makefile 16 Mar 2003 03:12:31 -0000 1.54 +++ Makefile 20 Aug 2003 02:16:37 -0000 1.55 @@ -46,7 +46,7 @@ SYN_FLAGS := -I../occ -DPYTHON_INCLUDE=" else SYN_FLAGS := -I../occ -I$(python_prefix)/include -DPYTHON_INCLUDE="$(python_include)" endif -CPPFLAGS += -Wall +CXXFLAGS += -Wall ifeq '$(use_gc)' 'no' CPPFLAGS += -DDONT_GC GCLIB := @@ -95,34 +95,34 @@ debug: occ.gdb link-synopsis ucpp # This compiles the OCC files in the parent dir $(OCC_OBJ): %.o : %.cc - $(CXX) $(OPTIMISE) -I.. -I../gc/include -I../occ $(CPPFLAGS) -c -o $@ $< + $(CXX) $(CPPFLAGS) -I.. -I../gc/include -I../occ $(CXXFLAGS) $(OPTIMISE) -c -o $@ $< # This compiles the parser files with optimisation $(SYN_OBJ): %.o : %.cc - $(CXX) $(OPTIMISE) -I.. -I../gc/include $(SYN_FLAGS) $(CPPFLAGS) -c -o $@ $< + $(CXX) $(CPPFLAGS) -I.. -I../gc/include $(SYN_FLAGS) $(CXXFLAGS) $(OPTIMISE) -c -o $@ $< # This compiles the UCPP files from ../ucpp $(UCPP_OBJ): %.o : %.c - $(CC) $(OPTIMISE) $(CPPFLAGS) $(UCPP_FLAGS) -c -o $@ $< + $(CC) $(CPPFLAGS) $(CFLAGS) $(UCPP_FLAGS) $(OPTIMISE) -c -o $@ $< # This compiles DEBUG versions of the OCC files in the parent dir $(OCC_GOBJ): %.go : %.cc - $(CXX) $(DEBUG) -I.. -I../gc/include -I../occ $(CPPFLAGS) -c -o $@ $< + $(CXX) $(CPPFLAGS) -I.. -I../gc/include -I../occ $(CXXFLAGS) $(DEBUG) -c -o $@ $< # This compiles DEBUG versions of the object files with .go suffix $(SYN_GOBJ): %.go : %.cc - $(CXX) $(DEBUG) -I.. -I../gc/include $(SYN_FLAGS) $(CPPFLAGS) -c -o $@ $< + $(CXX) $(CPPFLAGS) -I.. -I../gc/include $(SYN_FLAGS) $(CXXFLAGS) $(DEBUG) -c -o $@ $< # This compiles DEBUG versions of the UCPP files $(UCPP_GOBJ): %.go : %.c - $(CC) $(DEBUG) $(CPPFLAGS) $(UCPP_FLAGS) -c -o $@ $< + $(CC) $(CPPFLAGS) $(UCPP_FLAGS) $(CFLAGS) $(DEBUG) -c -o $@ $< # This compiles STANDALONE versions of the UCPP files $(UCPP_AOBJ): %.ao : %.c - $(CC) $(DEBUG) $(CFLAGS) $(UCPP_AFLAGS) -c -o $@ $< + $(CC) $(CPPFLAGS) $(UCPP_AFLAGS) $(CFLAGS) $(DEBUG) -c -o $@ $< link-synopsis: link.cc - $(CXX) -DSTANDALONE $(DEBUG) $(SYN_FLAGS) $(CPPFLAGS) -o link-synopsis link.cc + $(CXX) $(CPPFLAGS) -DSTANDALONE $(CXXFLAGS) $(DEBUG) $(SYN_FLAGS) -o link-synopsis link.cc ucpp: $(UCPP_AOBJ) $(CC) -o $@ $^ @@ -138,15 +138,15 @@ $(GCLIB): # This creates the final occ python module $(OCC_SO): $(SYN_OBJ) opencxx.a $(UCPP_LIB) - $(CXX) -shared $(CPPFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) + $(CXX) -shared $(LDFLAGS) -o $@ $^ $(LIBS) # This creates the final link python module $(LINK_SO): link.cc - $(CXX) -shared $(SYN_FLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) + $(CXX) -shared $(SYN_FLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) # This creates a binary with debugging and profiling that you can run occ.gdb: $(SYN_GOBJ) opencxx.ga $(UCPP_GLIB) - $(CXX) -ggdb -pg $(CPPFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) $(PYLIB) + $(CXX) -ggdb -pg $(LDFLAGS) -o $@ $^ $(LIBS) $(PYLIB) # Compile an archive of the OCC files opencxx.a : $(OCC_OBJ) $(GCLIB) From stefan at users.sourceforge.net Wed Aug 20 01:21:03 2003 From: stefan at users.sourceforge.net (Stefan Seefeld) Date: Wed Mar 2 21:06:33 2005 Subject: [Synopsis-cvs] Synopsis/Synopsis local.mk.in,1.10,1.11 configure.in,1.16,1.17 Message-ID: Update of /cvsroot/synopsis/Synopsis In directory sc8-pr-cvs1:/tmp/cvs-serv2431 Modified Files: local.mk.in configure.in Log Message: first steps towards a C parser backend (based on the ctool) Index: local.mk.in =================================================================== RCS file: /cvsroot/synopsis/Synopsis/local.mk.in,v retrieving revision 1.10 retrieving revision 1.11 diff -u -p -d -r1.10 -r1.11 --- local.mk.in 23 Mar 2003 17:36:27 -0000 1.10 +++ local.mk.in 20 Aug 2003 02:16:37 -0000 1.11 @@ -44,8 +44,14 @@ packagedir := $(prefix)/lib/python$(pyth CC := @CC@ CXX := @CXX@ +MAKEDEP := $(CXX) -M use_gc := @use_gc@ LIBS := @python_lib@ @LIBS@ -lpthread CPPFLAGS := @CPPFLAGS@ +CFLAGS := @CFLAGS@ +CXXFLAGS := @CXXFLAGS@ LIBRARY_EXT := @library_ext@ CPP_PROFILE := @cpp_profile@ +CTOOL := @CTOOL@ +CTOOL_CPPFLAGS := @CTOOL_CPPFLAGS@ +CTOOL_LIBS := @CTOOL_LIBS@ Index: configure.in =================================================================== RCS file: /cvsroot/synopsis/Synopsis/configure.in,v retrieving revision 1.16 retrieving revision 1.17 diff -u -p -d -r1.16 -r1.17 --- configure.in 24 Mar 2003 09:05:38 -0000 1.16 +++ configure.in 20 Aug 2003 02:16:37 -0000 1.17 @@ -96,7 +96,7 @@ CYGWIN*) library_ext=dll ;; *) - CPPFLAGS="-fPIC $CPPFLAGS" + CXXFLAGS="$CXXFLAGS -fPIC" cpp_profile="-pg" library_ext=so ;; @@ -119,8 +119,28 @@ if test "$use_gc" = yes; then echo "- Please rerun manually if required." fi -AC_OUTPUT([ - local.mk:local.mk.in - synopsis.spec:synopsis.spec.in - Synopsis/Config.py:Synopsis/Config.py.in -]) +dnl +dnl optionally support ctool C parser +dnl +AC_ARG_ENABLE(ctool, + AC_HELP_STRING([--enable-ctool],[Configure with ctool support]), + [config_ctool="$enableval"],[config_ctool="yes"]) +CTOOL= +if test ".$config_ctool" = ".yes"; then + AC_LANG(C++) + AC_ARG_WITH(ctool-prefix, + [ --with-ctool-prefix ctool prefix], + [ctool_prefix="$withval"]) + if test ".$ctool_prefix" != "." ; then + CTOOL_LIBS="-L$ctool_prefix/lib" + CTOOL_CPPFLAGS="-I$ctool_prefix/include" + fi + save_CPPFLAGS="$CPPFLAGS" + CPPFLAGS="$CPPFLAGS $CTOOL_CPPFLAGS" + AC_CHECK_HEADER(ctool/ctool.h, CTOOL=1) + CPPFLAGS="$save_CPPFLAGS" +fi +AC_SUBST(CTOOL) +AC_SUBST(CTOOL_CPPFLAGS) +AC_SUBST(CTOOL_LIBS) +AC_OUTPUT([local.mk synopsis.spec Synopsis/Config.py])