[Synopsis-changes] Synopsis/Synopsis/Synopsis/Parsers/Cpp Parser.py,1.2,1.3 cpp.cc,1.3,1.4

Stefan Seefeld stefan at synopsis.fresco.org
Tue Jan 13 07:44:28 UTC 2004


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

Modified Files:
	Parser.py cpp.cc 
Log Message:
usr new C++ API

Index: Parser.py
===================================================================
RCS file: /cvs/synopsis/Synopsis/Synopsis/Parsers/Cpp/Parser.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -p -d -r1.2 -r1.3
--- Parser.py	4 Jan 2004 02:08:26 -0000	1.2
+++ Parser.py	13 Jan 2004 07:44:25 -0000	1.3
@@ -18,7 +18,6 @@ class Parser(Processor):
    emulate_compiler = Parameter('c++', 'a compiler to emulate')
    flags = Parameter([], 'list of preprocessor flags such as -I or -D')
    cpp_output = Parameter(None, 'filename for preprocessed file')
-   cpp_mmap = Parameter(None, 'output macro calls for the given file')
    prefix = Parameter(None, 'prefix to strip off from the filename')
    language = Parameter('C', 'source code programming language of the given input file')
 
@@ -35,7 +34,7 @@ class Parser(Processor):
          flags += map(lambda x:'-D%s=%s'%(x[0], x[1]), info.macros)
          for file in self.input:
             self.ast = ucpp.parse(self.ast, file, self.prefix,
-                                  self.cpp_output, self.cpp_mmap,
+                                  self.cpp_output,
                                   self.language, flags, self.verbose, self.debug)
       else:
          print 'not implemented yet: spawn external preprocessor'

Index: cpp.cc
===================================================================
RCS file: /cvs/synopsis/Synopsis/Synopsis/Parsers/Cpp/cpp.cc,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -p -d -r1.3 -r1.4
--- cpp.cc	4 Jan 2004 02:08:26 -0000	1.3
+++ cpp.cc	13 Jan 2004 07:44:25 -0000	1.4
@@ -6,7 +6,7 @@
 // see the file COPYING for details.
 //
 
-#include <Python.h>
+#include <Synopsis/AST/ASTModule.hh>
 #include <vector>
 #include <map>
 #include <set>
@@ -21,65 +21,13 @@ namespace
 
 int verbose = 0;
 int debug = 0;
-PyObject *ast;
+const char *language;
 const char *prefix = 0;
-PyObject *language = 0;
-PyObject *source_file = 0;
+Synopsis::ASTModule ast_module;
+Synopsis::AST ast;
+Synopsis::SourceFile source_file;
 const char *input = 0;
 
-//. MacroMap is a map from preprocessed file positions to input file positions.
-//. This may eventually be part of the AST (if more than just declarations are
-//. supported), but for now it is persisted into a file in parallel so it can be
-//. read in by C/C++ parsers for correct symbol cross referencing.
-struct MacroMap
-{
-  struct Node
-  {
-    std::string name;
-    int start;
-    int end;
-    enum Type { START, END } type;
-    int diff;
-    bool operator <(const Node &other) const
-    {
-        return start < other.start;
-    }
-  };
-  typedef std::set<Node> Line;
-  typedef std::map<int, Line> LineMap;
-
-  //. Adds a map at the given line. out_{start,end} define the start and
-  //. past-the-end markers for the macro in the output file. diff defines
-  //. the signed difference to add to the pos.
-  void add(const char *name, int linenum, int start, int end, int diff)
-  {
-    Line &line = lines[linenum];
-    Node node;
-    node.name = name; // name isn't really needed. For now keep it for debugging...
-    node.start = start;
-    node.end = end;
-    node.type = Node::START;
-    node.diff = diff;
-    line.insert(node);
-  }
-  void write(const char *filename)
-  {
-    std::ofstream ofs(filename);
-    for (LineMap::iterator i = lines.begin();
-	 i != lines.end(); ++i)
-      for (MacroMap::Line::iterator j = (*i).second.begin();
-	   j != (*i).second.end(); ++j)
-      ofs << (*i).first << ' '
-	  << (*j).name << ' '
-	  << (*j).start << ' '
-	  << (*j).end << ' '
-	  << (*j).type << ' '
-	  << (*j).diff << '\n';
-  }
-
-  LineMap lines;
-} mmap;
-
 const char *strip_prefix(const char *filename, const char *prefix)
 {
   if (!prefix) return filename;
@@ -91,47 +39,23 @@ const char *strip_prefix(const char *fil
 }
 
 //. creates new SourceFile object
-PyObject *create_source_file(const char *filename, bool is_main)
+Synopsis::SourceFile create_source_file(const char *filename, bool is_main)
 {
-  PyObject *short_filename = PyString_FromString(strip_prefix(filename, prefix));
-  PyObject *long_filename = PyString_FromString(filename);
-  PyObject *ast_module = PyImport_ImportModule("Synopsis.AST");
-  PyObject *source_file = PyObject_CallMethod(ast_module, "SourceFile", "OOO",
-				    short_filename, long_filename, language);
-  Py_DECREF(ast_module);
-  if (is_main) PyObject_CallMethod(source_file, "set_is_main", "i", 1);
-  PyObject *pyfiles = PyObject_CallMethod(ast, "files", 0);
-  PyDict_SetItem(pyfiles, short_filename, source_file);
-  Py_DECREF(short_filename);
-  Py_DECREF(long_filename);
-  Py_DECREF(pyfiles);
-  return source_file;
+  Synopsis::SourceFile sf = ast_module.create_source_file(strip_prefix(filename,
+								       prefix),
+							  filename,
+							  language);
+  sf.is_main(true);
+  return sf;
 }
 
 //. creates new or returns existing SourceFile object
 //. with the given filename
-PyObject *lookup_source_file(const char *filename)
-{
-  PyObject *short_filename = PyString_FromString(strip_prefix(filename, prefix));
-  PyObject *pyfiles = PyObject_CallMethod(ast, "files", 0);
-  PyObject *source_file = PyDict_GetItem(pyfiles, short_filename);
-  if (source_file) Py_INCREF(source_file);
-  Py_DECREF(pyfiles);
-  Py_DECREF(short_filename);
-  return source_file ? source_file : create_source_file(filename, false);
-}
-
-void create_include(PyObject *source, PyObject *target,
-		    bool is_macro, bool is_next)
+Synopsis::SourceFile lookup_source_file(const char *filename)
 {
-  PyObject *ast_module = PyImport_ImportModule("Synopsis.AST");
-  PyObject *include = PyObject_CallMethod(ast_module, "Include", "Oii",
-					  target, is_macro, is_next);
-  Py_DECREF(ast_module);
-  PyObject *includes = PyObject_CallMethod(source, "includes", 0);
-  PyObject_CallMethod(includes, "append", "O", include);
-  Py_DECREF(includes);
-  Py_DECREF(include);
+  Synopsis::Dict files = ast.files();
+  Synopsis::SourceFile sf = files.get(strip_prefix(filename, prefix));
+  return sf ? sf : create_source_file(filename, false);
 }
 
 //. creates new Macro object
@@ -139,47 +63,32 @@ void create_macro(const char *filename, 
 		  const char *macro_name, int num_args, const char **args,
 		  int vaarg, const char *text)
 {
-  PyObject *file = lookup_source_file(filename);
-  PyObject *type = PyString_FromString("macro");
-  PyObject *name = PyTuple_New(1);
-  PyTuple_SET_ITEM(name, 0, PyString_FromString(macro_name));
-  PyObject *params = 0;
+  Synopsis::Tuple name(macro_name);
+  Synopsis::SourceFile sf = lookup_source_file(filename);
+  std::vector<std::string> params;
   if (args)
   {
-    params = PyList_New(vaarg ? num_args + 1 : num_args);
-    for (int i = 0; i < num_args; i++)
-      PyList_SET_ITEM(params, i, PyString_FromString(args[i]));
+    params.resize(vaarg ? num_args + 1 : num_args);
+    for (int i = 0; i < num_args; ++i)
+      params[i] = args[i];
     if (vaarg)
-      PyList_SET_ITEM(params, num_args, PyString_FromString("..."));
-  }
-  else
-  {
-    params = Py_None;
-    Py_INCREF(Py_None);
+      params[num_args] = "...";
   }
-  PyObject *pytext = PyString_FromString(text);
-  PyObject *ast_module = PyImport_ImportModule("Synopsis.AST");
-  PyObject *macro = PyObject_CallMethod(ast_module, "Macro", "OiOOOOO",
-					file, line, language, type,
-					name, params, pytext);
-  Py_DECREF(ast_module);
-  Py_DECREF(pytext);
-  Py_DECREF(params);
-  Py_DECREF(name);
-  Py_DECREF(type);
-  Py_DECREF(file);
-  PyObject *declarations = PyObject_CallMethod(ast, "declarations", 0);
-  PyObject_CallMethod(declarations, "append", "O", macro);
-  Py_DECREF(macro);
-  Py_DECREF(declarations);
+  Synopsis::Macro macro = ast_module.create_macro(sf, line, language,
+						  "macro", name,
+						  params, text);
+
+  Synopsis::List declarations = ast.declarations();
+  declarations.append(macro);
 }
 
-bool extract(PyObject *list, std::vector<const char *> &out)
+bool extract(PyObject *py_flags, std::vector<const char *> &out)
 {
-  size_t argsize = PyList_Size(list);
-  for (size_t i = 0; i != argsize; ++i)
+  Py_INCREF(py_flags);
+  Synopsis::List list = Synopsis::Object(py_flags);
+  for (size_t i = 0; i != list.size(); ++i)
   {
-    const char *value = PyString_AsString(PyList_GetItem(list, i));
+    const char *value = Synopsis::Object::narrow<const char *>(list.get(i));
     if (!value) return false;
     out.push_back(value);
   }
@@ -195,83 +104,105 @@ extern "C"
 
 PyObject *ucpp_parse(PyObject *self, PyObject *args)
 {
-  char *output;
-  char *mmap_file;
-  PyObject *py_cppflags;
-  std::vector<const char *> cppflags;
-  if (!PyArg_ParseTuple(args, "OszszOO!ii",
-                        &ast,
-                        &input,
-			&prefix,
-                        &output,
-			&mmap_file,
-			&language,
-                        &PyList_Type, &py_cppflags,
-                        &verbose,
-                        &debug)
-      || !extract(py_cppflags, cppflags))
-    return 0;
-
-  Py_INCREF(ast);
-  source_file = create_source_file(input, true);
-
-  cppflags.insert(cppflags.begin(), "ucpp");
-  cppflags.push_back("-C"); // keep comments
-  cppflags.push_back("-lg"); // gcc-like line numbers
-  if (output)
+  try
   {
-    cppflags.push_back("-o"); // output to...
-    cppflags.push_back(output);
+    char *output;
+    PyObject *py_flags;
+    std::vector<const char *> flags;
+    PyObject *py_ast;
+    if (!PyArg_ParseTuple(args, "OszssO!ii",
+			  &py_ast,
+			  &input,
+			  &prefix,
+			  &output,
+			  &language,
+			  &PyList_Type, &py_flags,
+			  &verbose,
+			  &debug)
+	|| !extract(py_flags, flags))
+      return 0;
+    
+    Py_INCREF(py_ast);
+    ast = Synopsis::AST(py_ast);
+    ast_module = Synopsis::ASTModule();
+    source_file = create_source_file(input, true);
+    
+    flags.insert(flags.begin(), "ucpp");
+    flags.push_back("-C"); // keep comments
+    flags.push_back("-lg"); // gcc-like line numbers
+    if (output)
+    {
+      flags.push_back("-o"); // output to...
+      flags.push_back(output);
+    }
+    flags.push_back(input);
+    if (verbose)
+    {
+      std::cout << "calling ucpp\n";
+      for (std::vector<const char *>::iterator i = flags.begin();
+	   i != flags.end(); ++i)
+	std::cout << ' ' << *i;
+      std::cout << std::endl;
+    }
+    
+    int status = ucpp_main(flags.size(), (char **)&*flags.begin());
+    if (status != 0)
+      std::cerr << "ucpp returned error flag. ignoring error." << std::endl;
+
+    Synopsis::Dict files = ast.files();
+    files.set(input, source_file);
+    return ast.ref();
   }
-  cppflags.push_back(input);
-  if (verbose)
+  catch (const std::exception &e)
   {
-    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;
+    std::cerr << "Internal error : " << e.what() << std::endl;
+    return 0;
   }
-    
-  int status = ucpp_main(cppflags.size(), (char **)&*cppflags.begin());
-  if (status != 0)
-    std::cerr << "ucpp returned error flag. ignoring error." << std::endl;
-
-  if (mmap_file) mmap.write(mmap_file);
-
-  return ast;
 }
 
 extern "C"
 {
   //. 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)
+  void synopsis_macro_hook(const char *name, int line_num,
+			   int start, int end, int diff)
   {
     if (debug) 
-      std::cout << "macro : " << name << ' ' << line << ' ' << start << ' ' << end << ' ' << diff << std::endl;
+      std::cout << "macro : " << name << ' ' << line_num << ' '
+		<< start << ' ' << end << ' ' << diff << std::endl;
     
-    mmap.add(name, line, start, end, diff);
+    Synopsis::Dict mmap = source_file.macro_calls();
+    Synopsis::List line = mmap.get(line_num, Synopsis::List());
+    line.append(ast_module.create_macro_call(name, start, end, diff));
+    mmap.set(line_num, line);
   }
 
   //. This function is a callback from the ucpp code to store includes
-  void synopsis_include_hook(const char *source, const char *target, int is_macro, int is_next)
+  void synopsis_include_hook(const char *source, const char *target,
+			     int is_macro, int is_next)
   {
     if (debug) 
-      std::cout << "include : " << source << ' ' << target << ' ' << is_macro << ' ' << is_next << std::endl;
+      std::cout << "include : " << source << ' ' << target << ' ' 
+		<< is_macro << ' ' << is_next << std::endl;
 
     if (strcmp(input, source) != 0) return;
-    PyObject *target_file = lookup_source_file(target);
-    create_include(source_file, target_file, is_macro, is_next);
-    Py_DECREF(target_file);
+    Synopsis::SourceFile target_file = lookup_source_file(target);
+
+    Synopsis::Include include = ast_module.create_include(target_file, is_macro, is_next);
+    Synopsis::List includes = source_file.includes();
+    includes.append(include);
   }
 
   //. 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)
+  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;
+      std::cout << "define : " << filename << ' ' << line << ' ' 
+		<< name << ' ' << num_args << ' ' << text << std::endl;
 
     if (strcmp(input, filename) != 0) return;
     create_macro(filename, line, name, num_args, args, vaarg, text);





More information about the Synopsis-changes mailing list