[Synopsis-changes] Synopsis/Synopsis/Synopsis/Parsers/Cpp Makefile.in,1.11,1.12 cpp-posix.cc,1.2,1.3 cpp-win32.cc,1.1,1.2 cpp.cc,1.1,1.2

Stefan Seefeld stefan at synopsis.fresco.org
Fri Jan 2 03:58:09 UTC 2004


Update of /cvs/synopsis/Synopsis/Synopsis/Parsers/Cpp
In directory frida:/tmp/cvs-serv28361/Synopsis/Parsers/Cpp

Modified Files:
	Makefile.in cpp-posix.cc cpp-win32.cc cpp.cc 
Log Message:
first working version of standalone ucpp parser

Index: Makefile.in
===================================================================
RCS file: /cvs/synopsis/Synopsis/Synopsis/Parsers/Cpp/Makefile.in,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -p -d -r1.11 -r1.12
--- Makefile.in	31 Dec 2003 16:35:38 -0000	1.11
+++ Makefile.in	2 Jan 2004 03:58:05 -0000	1.12
@@ -62,7 +62,7 @@ ucpp:		$(AOBJ)
 	$(CC) -o $@ $^
 
 $(UCPP_SO): $(OBJ)
-	$(CC) -shared $(LDFLAGS) -o $@ $^ $(LIBS)
+	$(CXX) -shared $(LDFLAGS) -o $@ $^ $(LIBS)
 
 clean :
 	rm -f *~ ucpp/*.o ucpp/*.go ucpp/*.a ucpp/*.ga \

Index: cpp-posix.cc
===================================================================
RCS file: /cvs/synopsis/Synopsis/Synopsis/Parsers/Cpp/cpp-posix.cc,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -p -d -r1.2 -r1.3
--- cpp-posix.cc	18 Dec 2003 03:36:52 -0000	1.2
+++ cpp-posix.cc	2 Jan 2004 03:58:05 -0000	1.3
@@ -10,119 +10,3 @@
 #  include <signal.h>
 #  include <sys/wait.h>
 
-#include <vector>
-#include <iostream>
-#include <cstring>
-#include <cstdio>
-
-// ucpp_main is the renamed main() func of ucpp, since it is included in this
-// module
-extern "C" int ucpp_main(int argc, char** argv);
-
-const char *
-RunPreprocessor(const char *preprocessor,
-                const char *src, const std::vector<const char *> &flags,
-                bool verbose)
-{
-  static char cppfile[1024];
-  strcpy(cppfile, "/tmp/synopsis-XXXXXX");
-  int temp_fd = mkstemp(cppfile);
-  if (temp_fd == -1)
-  {
-    std::perror("RunPreprocessor");
-    exit(1);
-  }
-  // Not interested in the open file, just the unique filename
-  close(temp_fd);
-   
-  if (preprocessor)
-  {
-    switch(fork())
-    {
-      case 0:
-      {
-        std::vector<const char *> 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(cppfile);
-        args.push_back("-x"); // language c++
-        args.push_back("c++");
-        args.push_back(src);
-        if (verbose)
-        {
-          std::cout << "calling external preprocessor\n" << args[0];
-          for (std::vector<const char *>::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());
-        std::perror("cannot invoke compiler");
-        exit(-1);
-        break;
-      }
-      case -1:
-        std::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
-
-  }
-  else
-  { // else use ucpp
-    // Create argv vector
-    std::vector<const char *> args = flags;
-    char *cc = getenv("CC");
-    args.insert(args.begin(), cc ? cc : "ucpp");
-    args.push_back("-C"); // keep comments
-    args.push_back("-lg"); // gcc-like line numbers
-    args.push_back("-o"); // output to...
-    args.push_back(cppfile);
-    args.push_back(src);
-    if (verbose)
-    {
-      std::cout << "calling ucpp\n";
-      for (std::vector<const char *>::iterator i = args.begin(); i != args.end(); ++i)
-        std::cout << ' ' << *i;
-      std::cout << std::endl;
-    }
-    
-    // Call ucpp
-    int status = ucpp_main(args.size(), (char **)&*args.begin());
-    if (status != 0)
-      std::cerr << "ucpp returned error flag. ignoring error." << std::endl;
-  }
-  return cppfile;
-}
-

Index: cpp-win32.cc
===================================================================
RCS file: /cvs/synopsis/Synopsis/Synopsis/Parsers/Cpp/cpp-win32.cc,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -p -d -r1.1 -r1.2
--- cpp-win32.cc	17 Dec 2003 15:07:24 -0000	1.1
+++ cpp-win32.cc	2 Jan 2004 03:58:05 -0000	1.2
@@ -34,42 +34,6 @@ RunPreprocessor(const char *preprocessor
   _splitpath(src, drive, dir, fname, ext);
   _makepath(cppfile, drive, dir, fname, ".i");
   
-  if (preprocessor)
-  {
-
-    std::vector<const char *> args;
-    args.push_back("-nologo");
-    args.insert(args.end(), flags.begin(), flags.end());
-    args.push_back("-E");  // Use -EP if you don't want the directives #line
-    args.push_back("-C");  // Don't strip comments
-    args.push_back("-P");  // Preprocessor output to <file>.i
-    args.push_back("-Tp"); // Consider source file as C++ file,
-                           // whatever extension.
-//     args.push_back("-Fp"); // Preprocessor output 
-//     args.push_back(dest);
-    args.push_back(src);
-    args.push_back((char*)0);
-
-    if (verbose)
-    {
-       std::cout << "calling external preprocessor 'cl'\n";
-       for (std::vector<const char *>::iterator i = args.begin(); i != args.end(); ++i)
-          std::cout << ' ' << *i;
-       std::cout << std::endl;
-    }
-
-
-    int status = _spawnvp(_P_WAIT, "cl", (char **)&*args.begin());
-    if(status != 0)
-    {
-       if(status == -1) perror("cannot invoke the preprocessor");
-       // VIC Feb 20 1998
-       // MSVC 5.0 returns non-zero exit code on normal input
-       if (status != 2) exit(status);
-    }
-
-  }
-  else
   { // else use ucpp
     // Create argv vector
     std::vector<const char *> args = flags;

Index: cpp.cc
===================================================================
RCS file: /cvs/synopsis/Synopsis/Synopsis/Parsers/Cpp/cpp.cc,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -p -d -r1.1 -r1.2
--- cpp.cc	17 Dec 2003 15:07:24 -0000	1.1
+++ cpp.cc	2 Jan 2004 03:58:05 -0000	1.2
@@ -6,12 +6,147 @@
 // see the file COPYING for details.
 //
 
-#ifdef __WIN32__
+#include <Python.h>
+#include <vector>
+#include <iostream>
+#include <cstring>
+#include <cstdio>
 
-# include "cpp-win32.cc"
+int verbose = 0;
+int debug = 0;
 
-#else
+extern "C"
+{
+  // ucpp_main is the renamed main() func of ucpp, since it is included in this
+  // module
+  int ucpp_main(int argc, char** argv);
 
-# include "cpp-posix.cc"
+  //. This function is a callback from the ucpp code to store macro
+  //. expansions
+  void synopsis_macro_hook(const char *name, int line, int start, int end, int diff)
+  {
+    if (debug) 
+      std::cout << "macro : " << name << ' ' << line << ' ' << start << ' ' << end << ' ' << diff << std::endl;
+#if 0
+    LinkMap::instance()->add(name, line, start, end, diff);
+#endif
+  }
+
+  //. This function is a callback from the ucpp code to store includes
+  void synopsis_include_hook(const char *source_file, const char *target_file, int is_macro, int is_next)
+  {
+    if (debug) 
+      std::cout << "include : " << source_file << ' ' << target_file << ' ' << is_macro << ' ' << is_next << std::endl;
+#if 0
+    // There is not enough code here to make another class..
+    FileFilter *filter = FileFilter::instance();
+    if (!filter) return;
 
+    // Add another Include to the source's SourceFile
+    // We don't deal with duplicates here. The Linker's Unduplicator will
+    // take care of that.
+    //std::cout << "Include: " << source_file << " -> " << target_file << std::endl;
+    AST::SourceFile *file = filter->get_sourcefile(source_file);
+    AST::SourceFile *target = filter->get_sourcefile(target_file);
+    AST::Include *include = new AST::Include(target, is_macro, is_next);
+    file->includes().push_back(include);
 #endif
+  }
+
+  //. This function is a callback from the ucpp code to store macro
+  //. definitions
+  void synopsis_define_hook(const char* filename, int line, const char *name, int num_args, const char **args, int vaarg, const char *text)
+  {
+    if (debug) 
+      std::cout << "define : " << filename << ' ' << line << ' ' << name << ' ' << num_args << ' ' << text << std::endl;
+#if 0
+    FileFilter *filter = FileFilter::instance();
+    if (!filter) return;
+    AST::SourceFile *file = filter->get_sourcefile(filename);
+    if (!file->is_main()) return;
+    if (!syn_macro_defines) syn_macro_defines = new std::vector<AST::Macro*>;
+    AST::Macro::Parameters *params = 0;
+    if (args)
+    {
+      params = new AST::Macro::Parameters;
+      for (int i = 0; i < num_args; i++)
+	params->push_back(args[i]);
+      if (vaarg) params->push_back("...");
+    }
+    ScopedName macro_name;
+    macro_name.push_back(name);
+    AST::Macro* macro = new AST::Macro(file, line, macro_name, params, text);
+    //. Don't know global NS yet.. Will have to extract these later
+    file->declarations().push_back(macro);
+    syn_macro_defines->push_back(macro);
+#endif
+  }
+};
+
+namespace
+{
+
+bool extract(PyObject *list, std::vector<const char *> &out)
+{
+  size_t argsize = PyList_Size(list);
+  for (size_t i = 0; i != argsize; ++i)
+  {
+    const char *value = PyString_AsString(PyList_GetItem(list, i));
+    if (!value) return false;
+    out.push_back(value);
+  }
+  return true;
+}
+
+PyObject *ucpp_parse(PyObject *self, PyObject *args)
+{
+  PyObject *ast;
+  char *input, *output;
+  PyObject *py_cppflags;
+  std::vector<const char *> cppflags;
+  if (!PyArg_ParseTuple(args, "OszO!ii",
+                        &ast,
+                        &input,
+                        &output,
+                        &PyList_Type, &py_cppflags,
+                        &verbose,
+                        &debug)
+      || !extract(py_cppflags, cppflags))
+    return 0;
+
+  Py_INCREF(ast);
+
+
+  cppflags.insert(cppflags.begin(), "ucpp");
+  cppflags.push_back("-C"); // keep comments
+  cppflags.push_back("-lg"); // gcc-like line numbers
+  if (output)
+  {
+    cppflags.push_back("-o"); // output to...
+    cppflags.push_back(output);
+  }
+  cppflags.push_back(input);
+  if (verbose)
+  {
+    std::cout << "calling ucpp\n";
+    for (std::vector<const char *>::iterator i = cppflags.begin();
+	 i != cppflags.end(); ++i)
+      std::cout << ' ' << *i;
+    std::cout << std::endl;
+  }
+    
+  int status = ucpp_main(cppflags.size(), (char **)&*cppflags.begin());
+  if (status != 0)
+    std::cerr << "ucpp returned error flag. ignoring error." << std::endl;
+  return ast;
+}
+
+PyMethodDef ucpp_methods[] = {{(char*)"parse", ucpp_parse, METH_VARARGS},
+                             {0, 0}};
+};
+
+extern "C" void initucpp()
+{
+  PyObject* m = Py_InitModule((char*)"ucpp", ucpp_methods);
+  PyObject_SetAttrString(m, (char*)"version", PyString_FromString("0.1"));
+}





More information about the Synopsis-changes mailing list