Hello,

here is an improvment to http://gcc.gnu.org/ml/gcc-patches/2011-06/msg01888.html.

The function meltgc_read_from_val (in melt-runtime.c) takes two arguments, a string value and a second one which is a location. In the comments, it is written that we can pass a NULL pointer if we have no location (it is a direct string). However, this conduct MELT to crash because it doesn't handle correctly the absence of file.

In the first patch, I modified makesexpr to create a location with a 'virtual' file. This works however, as this function is used recursively this is not very elegant.

This patch is more elegant, it adds a boolean field to the struct reading_st to declare if there is no file location. meltgc_read_* functions are modified to test if there is a given location, and if no, it create a location with a virtual file and set the boolean to false, conducting to avoid crash.

I have been able to build MELT with this pass and test it without problem.

On 24/06/2011 18:13, Pierre Vittet wrote:
Hello,

The function meltgc_read_from_val (in melt-runtime.c) takes two
arguments, a string value and a second one which is a location.
In the comments, it is written that we can pass a NULL pointer if we
have no location (it is a direct string). However, this conduct MELT to
crash because it doesn't handle correctly the absence of file.

This patch correct this, if there is no file, it create a "virtual" one
which is named "stringBuffer".

Pierre Vittet


correct_read_from_val_without_location-175348.diff



Index: gcc/melt-runtime.c
===================================================================
--- gcc/melt-runtime.c  (revision 175348)
+++ gcc/melt-runtime.c  (working copy)
@@ -6292,6 +6292,7 @@ struct reading_st
   int rcol;                    /* current column */
   source_location rsrcloc;     /* current source location */
   melt_ptr_t *rpfilnam;        /* pointer to location of file name string */
+  bool has_file_location;      /* precise if the string comes from a file */
 };
 
 #define MELT_READ_TABULATION_FACTOR 8
@@ -6326,7 +6327,7 @@ melt_linemap_compute_current_location (struct read
 {
   int colnum = 1;
   int cix = 0;
-  if (!rd || !rd->rcurlin) 
+  if (!rd || !rd->rcurlin || !rd->has_file_location)
     return;
   for (cix=0; cix<rd->rcol; cix++) {
     char c = rd->rcurlin[cix];
@@ -8314,6 +8315,7 @@ meltgc_read_file (const char *filnam, const char *
   rd = &rds;
   locnamv = meltgc_new_stringdup ((meltobject_ptr_t) MELT_PREDEF 
(DISCR_STRING), locnam);
   rds.rpfilnam = (melt_ptr_t *) & locnamv;
+  rds.has_file_location = true;
   seqv = meltgc_new_list ((meltobject_ptr_t) MELT_PREDEF (DISCR_LIST));
   while (!rdeof ())
     {
@@ -8371,7 +8373,16 @@ meltgc_read_from_rawstring (const char *rawstr, co
   rds.rsrcloc = loch;
   rd = &rds;
   if (locnam)
-    locnamv = meltgc_new_stringdup ((meltobject_ptr_t) MELT_PREDEF 
(DISCR_STRING), locnam);
+    {
+      rds.has_file_location = true;
+      locnamv = meltgc_new_stringdup ((meltobject_ptr_t) MELT_PREDEF 
(DISCR_STRING), locnam);
+    }
+  else
+    {
+      rds.has_file_location = false;
+      locnamv = meltgc_new_string ((meltobject_ptr_t) 
MELT_PREDEF(DISCR_STRING),
+                                  "stringBuffer");
+    }
   seqv = meltgc_new_list ((meltobject_ptr_t) MELT_PREDEF (DISCR_LIST));
   rds.rpfilnam = (melt_ptr_t *) & locnamv;
   while (rdcurc ())
@@ -8415,6 +8426,7 @@ meltgc_read_from_val (melt_ptr_t strv_p, melt_ptr_
   locnamv = locnam_p;
   rbuf = 0;
   strmagic = melt_magic_discr ((melt_ptr_t) strv);
+  seqv = meltgc_new_list ((meltobject_ptr_t) MELT_PREDEF (DISCR_LIST));
   switch (strmagic)
     {
     case MELTOBMAG_STRING:
@@ -8441,7 +8453,14 @@ meltgc_read_from_val (melt_ptr_t strv_p, melt_ptr_
   rds.rpath = 0;
   rds.rlineno = 0;
   rds.rcurlin = rbuf;
+  rds.has_file_location = true;
   rd = &rds;
+  if (locnamv == NULL){
+    rds.has_file_location = false;
+    locnamv = meltgc_new_string ((meltobject_ptr_t) MELT_PREDEF(DISCR_STRING),
+                                 "stringBuffer");
+    rd->rpfilnam = (melt_ptr_t *) &locnamv;
+  }
   rds.rpfilnam = (melt_ptr_t *) & locnamv;
   while (rdcurc ())
     {
2011-06-29  Pierre Vittet  <pier...@pvittet.com>

        * melt-runtime.c (struct reading_st): add a boolean has_file_location
        field.
        (melt_linemap_compute_current_location): return immediately if no file
        location.
        (meltgc_read_file, meltgc_read_from_rawstring, meltgc_read_from_val):
        set has_file_location accordingly
        (meltgc_read_from_val): create seqv list (it was used without being
        created).

Reply via email to