src/share/vm/adlc/filebuff.cpp

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 435
a61af66fc99e
child 850
4d9884b01ba6
permissions
-rw-r--r--

Initial load

     1 /*
     2  * Copyright 1997-2002 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 // FILEBUFF.CPP - Routines for handling a parser file buffer
    26 #include "adlc.hpp"
    28 //------------------------------FileBuff---------------------------------------
    29 // Create a new parsing buffer
    30 FileBuff::FileBuff( BufferedFile *fptr, ArchDesc& archDesc) : _fp(fptr), _AD(archDesc) {
    31   _err = fseek(_fp->_fp, 0, SEEK_END);  // Seek to end of file
    32   if (_err) {
    33     file_error(SEMERR, 0, "File seek error reading input file");
    34     exit(1);                    // Exit on seek error
    35   }
    36   _filepos = ftell(_fp->_fp);   // Find offset of end of file
    37   _bufferSize = _filepos + 5;   // Filepos points to last char, so add padding
    38   _err = fseek(_fp->_fp, 0, SEEK_SET);  // Reset to beginning of file
    39   if (_err) {
    40     file_error(SEMERR, 0, "File seek error reading input file\n");
    41     exit(1);                    // Exit on seek error
    42   }
    43   _filepos = ftell(_fp->_fp);      // Reset current file position
    45   _bigbuf = new char[_bufferSize]; // Create buffer to hold text for parser
    46   if( !_bigbuf ) {
    47     file_error(SEMERR, 0, "Buffer allocation failed\n");
    48     exit(1);                    // Exit on allocation failure
    49   }
    50   *_bigbuf = '\n';               // Lead with a sentinal newline
    51   _buf = _bigbuf+1;                     // Skip sentinal
    52   _bufmax = _buf;               // Buffer is empty
    53   _bufeol = _bigbuf;              // _bufeol points at sentinal
    54   _filepos = -1;                 // filepos is in sync with _bufeol
    55   _bufoff = _offset = 0L;       // Offset at file start
    57   _bufmax += fread(_buf, 1, _bufferSize-2, _fp->_fp); // Fill buffer & set end value
    58   if (_bufmax == _buf) {
    59     file_error(SEMERR, 0, "File read error, no input read\n");
    60     exit(1);                     // Exit on read error
    61   }
    62   *_bufmax = '\n';               // End with a sentinal new-line
    63   *(_bufmax+1) = '\0';           // Then end with a sentinal NULL
    64 }
    66 //------------------------------~FileBuff--------------------------------------
    67 // Nuke the FileBuff
    68 FileBuff::~FileBuff() {
    69   delete _bigbuf;
    70 }
    72 //------------------------------get_line----------------------------------------
    73 char *FileBuff::get_line(void) {
    74   char *retval;
    76   // Check for end of file & return NULL
    77   if (_bufeol >= _bufmax) return NULL;
    79   retval = ++_bufeol;      // return character following end of previous line
    80   if (*retval == '\0') return NULL; // Check for EOF sentinal
    81   // Search for newline character which must end each line
    82   for(_filepos++; *_bufeol != '\n'; _bufeol++)
    83     _filepos++;                    // keep filepos in sync with _bufeol
    84   // _bufeol & filepos point at end of current line, so return pointer to start
    85   return retval;
    86 }
    88 //------------------------------FileBuffRegion---------------------------------
    89 // Create a new region in a FileBuff.
    90 FileBuffRegion::FileBuffRegion( FileBuff* bufr, int soln, int ln,
    91                                 int off, int len)
    92 : _bfr(bufr), _sol(soln), _line(ln), _offset(off), _length(len) {
    93   _next = NULL;                 // No chained regions
    94 }
    96 //------------------------------~FileBuffRegion--------------------------------
    97 // Delete the entire linked list of buffer regions.
    98 FileBuffRegion::~FileBuffRegion() {
    99   if( _next ) delete _next;
   100 }
   102 //------------------------------copy-------------------------------------------
   103 // Deep copy a FileBuffRegion
   104 FileBuffRegion *FileBuffRegion::copy() {
   105   if( !this ) return NULL;      // The empty buffer region
   106   FileBuffRegion *br = new FileBuffRegion(_bfr,_sol,_line,_offset,_length);
   107   if( _next ) br->_next = _next->copy();
   108   return br;
   109 }
   111 //------------------------------merge------------------------------------------
   112 // Merge another buffer region into this buffer region.  Make overlapping areas
   113 // become a single region.  Remove (delete) the input FileBuffRegion.
   114 // Since the buffer regions are sorted by file offset, this is a varient of a
   115 // "sorted-merge" running in linear time.
   116 FileBuffRegion *FileBuffRegion::merge( FileBuffRegion *br ) {
   117   if( !br ) return this;        // Merging nothing
   118   if( !this ) return br;        // Merging into nothing
   120   assert( _bfr == br->_bfr, "" );     // Check for pointer-equivalent buffers
   122   if( _offset < br->_offset ) { // "this" starts before "br"
   123     if( _offset+_length < br->_offset ) { // "this" ends before "br"
   124       if( _next ) _next->merge( br );    // Merge with remainder of list
   125       else _next = br;                 // No more in this list; just append.
   126     } else {                           // Regions overlap.
   127       int l = br->_offset + br->_length - _offset;
   128       if( l > _length ) _length = l;     // Pick larger region
   129       FileBuffRegion *nr = br->_next;     // Get rest of region
   130       br->_next = NULL;         // Remove indication of rest of region
   131       delete br;                // Delete this region (it's been subsumed).
   132       if( nr ) merge( nr );     // Merge with rest of region
   133     }                           // End of if regions overlap or not.
   134   } else {                      // "this" starts after "br"
   135     if( br->_offset+br->_length < _offset ) {    // "br" ends before "this"
   136       FileBuffRegion *nr = new FileBuffRegion(_bfr,_sol,_line,_offset,_length);
   137       nr->_next = _next;                // Structure copy "this" guy to "nr"
   138       *this = *br;              // Structure copy "br" over "this".
   139       br->_next = NULL;         // Remove indication of rest of region
   140       delete br;                // Delete this region (it's been copied)
   141       merge( nr );              // Finish merging
   142     } else {                    // Regions overlap.
   143       int l = _offset + _length - br->_offset;
   144       if( l > _length ) _length = l;    // Pick larger region
   145       _offset = br->_offset;            // Start with earlier region
   146       _sol = br->_sol;                  // Also use earlier line start
   147       _line = br->_line;                        // Also use earlier line
   148       FileBuffRegion *nr = br->_next;   // Get rest of region
   149       br->_next = NULL;         // Remove indication of rest of region
   150       delete br;                // Delete this region (it's been subsumed).
   151       if( nr ) merge( nr );     // Merge with rest of region
   152     }                           // End of if regions overlap or not.
   153   }
   154   return this;
   155 }
   157 //------------------------------expandtab--------------------------------------
   158 static int expandtab( ostream &os, int off, char c, char fill1, char fill2 ) {
   159   if( c == '\t' ) {             // Tab?
   160     do os << fill1;             // Expand the tab; Output space
   161     while( (++off) & 7 );       // Expand to tab stop
   162   } else {                      // Normal character
   163     os << fill2;                // Display normal character
   164     off++;                      // Increment "cursor" offset
   165   }
   166   return off;
   167 }
   169 //------------------------------printline--------------------------------------
   170 // Print and highlite a region of a line.  Return the amount of highliting left
   171 // to do (i.e. highlite length minus length of line).
   172 static int printline( ostream& os, const char *fname, int line,
   173                         const char *_sol, int skip, int len ) {
   175   // Display the entire tab-expanded line
   176   os << fname << ":" << line << ": ";
   177   const char *t = strchr(_sol,'\n')+1; // End of line
   178   int off = 0;                  // Cursor offset for tab expansion
   179   const char *s = _sol;         // Nice string pointer
   180   while( t-s ) {                // Display whole line
   181     char c = *s++;              // Get next character to display
   182     off = expandtab(os,off,c,' ',c);
   183   }
   185   // Display the tab-expanded skippings before underlining.
   186   os << fname << ":" << line << ": ";
   187   off = 0;                      // Cursor offset for tab expansion
   188   s = _sol;                     // Restart string pointer
   190   // Start underlining.
   191   if( skip != -1 ) {            // The no-start-indicating flag
   192     const char *u = _sol+skip;  // Amount to skip
   193     while( u-s )                // Display skipped part
   194       off = expandtab(os,off,*s++,' ',' ');
   195     os << '^';                  // Start region
   196     off++;                      // Moved cursor
   197     len--;                      // 1 less char to do
   198     if( *s++ == '\t' )          // Starting character is a tab?
   199       off = expandtab(os,off,'\t','-','^');
   200   }
   202   // Long region doesn't end on this line
   203   int llen = (int)(t-s);        // Length of line, minus what's already done
   204   if( len > llen ) {            // Doing entire rest of line?
   205     while( t-s )                // Display rest of line
   206       off = expandtab(os,off,*s++,'-','-');
   207     os << '\n';                 // EOL
   208     return len-llen;            // Return what's not yet done.
   209   }
   211   // Region does end on this line.  This code fails subtly if the region ends
   212   // in a tab character.
   213   int i;
   214   for( i=1; i<len; i++ )        // Underline just what's needed
   215     off = expandtab(os,off,*s++,'-','-');
   216   if( i == len ) os << '^';     // Mark end of region
   217   os << '\n';                   // End of marked line
   218   return 0L;                    // All done
   219 }
   221 //------------------------------print------------------------------------------
   222 //std::ostream& operator<< ( std::ostream& os, FileBuffRegion &br ) {
   223 ostream& operator<< ( ostream& os, FileBuffRegion &br ) {
   224   if( &br == NULL ) return os;  // The empty buffer region
   225   FileBuffRegion *brp = &br;    // Pointer to region
   226   while( brp ) {                // While have chained regions
   227     brp->print(os);             // Print region
   228     brp = brp->_next;           // Chain to next
   229   }
   230   return os;                    // Return final stream
   231 }
   233 //------------------------------print------------------------------------------
   234 // Print the FileBuffRegion to a stream. FileBuffRegions are printed with the
   235 // filename and line number to the left, and complete text lines to the right.
   236 // Selected portions (portions of a line actually in the FileBuffRegion are
   237 // underlined.  Ellipses are used for long multi-line regions.
   238 //void FileBuffRegion::print( std::ostream& os ) {
   239 void FileBuffRegion::print( ostream& os ) {
   240   if( !this ) return;           // Nothing to print
   241   char *s = _bfr->get_line();
   242   int skip = (int)(_offset - _sol);     // Amount to skip to start of data
   243   int len = printline( os, _bfr->_fp->_name, _line, s, skip, _length );
   245   if( !len ) return;                    // All done; exit
   247   // Here we require at least 2 lines
   248   int off1 = _length - len + skip;      // Length of line 1
   249   int off2 = off1 + _sol;               // Offset to start of line 2
   250   char *s2 = _bfr->get_line();           // Start of line 2
   251   char *s3 = strchr( s2, '\n' )+1;      // Start of line 3 (unread)
   252   if( len <= (s3-s2) ) {                // It all fits on the next line
   253     printline( os, _bfr->_fp->_name, _line+1, s2, -1, len ); // Print&underline
   254     return;
   255   }
   257   // Here we require at least 3 lines
   258   int off3 = off2 + (int)(s3-s2);       // Offset to start of line 3
   259   s3 = _bfr->get_line();                // Start of line 3 (read)
   260   const char *s4 = strchr( s3, '\n' )+1;// Start of line 4 (unread)
   261   if( len < (s4-s3) ) {                 // It all fits on the next 2 lines
   262     s2 = _bfr->get_line();
   263     len = printline( os, _bfr->_fp->_name, _line+1, s2, -1, len ); // Line 2
   264     s3 = _bfr->get_line();
   265     printline( os, _bfr->_fp->_name, _line+2, s3, -1, len );     // Line 3
   266     return;
   267   }
   269   // Here we require at least 4 lines.
   270   // Print only the 1st and last line, with ellipses in middle.
   271   os << "...\n";                // The ellipses
   272   int cline = _line+1;          // Skipped 2 lines
   273   do {                          // Do until find last line
   274     len -= (int)(s3-s2);        // Remove length of line
   275     cline++;                    // Next line
   276     s2 = _bfr->get_line();      // Get next line from end of this line
   277     s3 = strchr( s2, '\n' ) + 1;// Get end of next line
   278   } while( len > (s3-s2) );     // Repeat until last line
   279   printline( os, _bfr->_fp->_name, cline, s2, -1, len ); // Print & underline
   280 }
   282 //------------------------------file_error-------------------------------------
   283 void FileBuff::file_error(int flag, int linenum, const char *fmt, ...)
   284 {
   285   va_list args;
   287   va_start(args, fmt);
   288   switch (flag) {
   289   case 0: _AD._warnings += _AD.emit_msg(0, flag, linenum, fmt, args);
   290   case 1: _AD._syntax_errs += _AD.emit_msg(0, flag, linenum, fmt, args);
   291   case 2: _AD._semantic_errs += _AD.emit_msg(0, flag, linenum, fmt, args);
   292   default: assert(0, ""); break;
   293   }
   294   va_end(args);
   295   _AD._no_output = 1;
   296 }

mercurial