src/share/vm/adlc/filebuff.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 2314
f95d63e2154a
parent 0
f90c822e73f8
child 9852
70aa912cebe5
permissions
-rw-r--r--

merge

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

mercurial