| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.debugging.sourcemap; |
| 18 |
|
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.collect.Lists; |
| 21 |
|
| 22 |
import java.io.IOException; |
| 23 |
import java.util.ArrayDeque; |
| 24 |
import java.util.Deque; |
| 25 |
import java.util.List; |
| 26 |
|
| 27 |
import javax.annotation.Nullable; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class SourceMapGeneratorV1 implements SourceMapGenerator { |
| 35 |
|
| 36 |
private final static int UNMAPPED = -1; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static class Mapping { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
int id = UNMAPPED; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
String sourceFile; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
FilePosition originalPosition; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
FilePosition startPosition; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
FilePosition endPosition; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
String originalName; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
boolean used = false; |
| 83 |
} |
| 84 |
|
| 85 |
private class MappingWriter { |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
private String lastSourceFile = null; |
| 90 |
private String lastSourceFileEscaped = null; |
| 91 |
private int lastLine = 0; |
| 92 |
private String lastLineString = String.valueOf(0); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
private void appendMappingTo( |
| 98 |
Mapping m, Appendable out) throws IOException { |
| 99 |
out.append("["); |
| 100 |
|
| 101 |
String sourceFile = m.sourceFile; |
| 102 |
|
| 103 |
String escapedSourceFile; |
| 104 |
if (lastSourceFile != sourceFile) { |
| 105 |
lastSourceFile = sourceFile; |
| 106 |
lastSourceFileEscaped = escapeString(sourceFile); |
| 107 |
} |
| 108 |
escapedSourceFile = lastSourceFileEscaped; |
| 109 |
|
| 110 |
out.append(escapedSourceFile); |
| 111 |
out.append(","); |
| 112 |
|
| 113 |
int line = m.originalPosition.getLine(); |
| 114 |
if (line != lastLine) { |
| 115 |
lastLineString = String.valueOf(line); |
| 116 |
} |
| 117 |
String lineValue = lastLineString; |
| 118 |
|
| 119 |
out.append(lineValue); |
| 120 |
|
| 121 |
out.append(","); |
| 122 |
out.append(String.valueOf( |
| 123 |
m.originalPosition.getColumn())); |
| 124 |
|
| 125 |
if (m.originalName != null) { |
| 126 |
out.append(","); |
| 127 |
out.append(escapeString(m.originalName)); |
| 128 |
} |
| 129 |
|
| 130 |
out.append("]\n"); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
void appendMappings(Appendable out) throws IOException { |
| 137 |
for (Mapping m : mappings) { |
| 138 |
if (m.used) { |
| 139 |
appendMappingTo(m, out); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
private List<Mapping> mappings = Lists.newArrayList(); |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
private Mapping lastMapping; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
private FilePosition offsetPosition = new FilePosition(0, 0); |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private FilePosition prefixPosition = new FilePosition(0, 0); |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
private static String escapeString(String value) { |
| 172 |
return Util.escapeString(value); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
@Override |
| 181 |
public void addMapping( |
| 182 |
String sourceName, @Nullable String symbolName, |
| 183 |
FilePosition sourceStartPosition, |
| 184 |
FilePosition startPosition, FilePosition endPosition) { |
| 185 |
|
| 186 |
|
| 187 |
if (sourceName == null || sourceStartPosition.getLine() < 0) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
Mapping mapping = new Mapping(); |
| 193 |
mapping.sourceFile = sourceName; |
| 194 |
mapping.originalPosition = sourceStartPosition; |
| 195 |
mapping.originalName = symbolName; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
if (offsetPosition.getLine() == 0 |
| 200 |
&& offsetPosition.getColumn() == 0) { |
| 201 |
mapping.startPosition = startPosition; |
| 202 |
mapping.endPosition = endPosition; |
| 203 |
} else { |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
int offsetLine = offsetPosition.getLine(); |
| 209 |
int startOffsetPosition = offsetPosition.getColumn(); |
| 210 |
int endOffsetPosition = offsetPosition.getColumn(); |
| 211 |
|
| 212 |
if (startPosition.getLine() > 0) { |
| 213 |
startOffsetPosition = 0; |
| 214 |
} |
| 215 |
|
| 216 |
if (endPosition.getLine() > 0) { |
| 217 |
endOffsetPosition = 0; |
| 218 |
} |
| 219 |
|
| 220 |
mapping.startPosition = |
| 221 |
new FilePosition(startPosition.getLine() + offsetLine, |
| 222 |
startPosition.getColumn() + startOffsetPosition); |
| 223 |
|
| 224 |
mapping.endPosition = |
| 225 |
new FilePosition(endPosition.getLine() + offsetLine, |
| 226 |
endPosition.getColumn() + endOffsetPosition); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
if (lastMapping != null) { |
| 231 |
int lastLine = lastMapping.startPosition.getLine(); |
| 232 |
int lastColumn = lastMapping.startPosition.getColumn(); |
| 233 |
int nextLine = mapping.startPosition.getLine(); |
| 234 |
int nextColumn = mapping.startPosition.getColumn(); |
| 235 |
Preconditions.checkState(nextLine > lastLine |
| 236 |
|| (nextLine == lastLine && nextColumn >= lastColumn), |
| 237 |
"Incorrect source mappings order, previous : (%s,%s)\n" |
| 238 |
+ "new : (%s,%s)\nnode : %s", |
| 239 |
lastLine, lastColumn, nextLine, nextColumn); |
| 240 |
} |
| 241 |
|
| 242 |
lastMapping = mapping; |
| 243 |
mappings.add(mapping); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
@Override |
| 254 |
public void setWrapperPrefix(String prefix) { |
| 255 |
|
| 256 |
int prefixLine = 0; |
| 257 |
int prefixIndex = 0; |
| 258 |
|
| 259 |
for (int i = 0; i < prefix.length(); ++i) { |
| 260 |
if (prefix.charAt(i) == '\n') { |
| 261 |
prefixLine++; |
| 262 |
prefixIndex = 0; |
| 263 |
} else { |
| 264 |
prefixIndex++; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
prefixPosition = new FilePosition(prefixLine, prefixIndex); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
@Override |
| 282 |
public void setStartingPosition(int offsetLine, int offsetIndex) { |
| 283 |
Preconditions.checkState(offsetLine >= 0); |
| 284 |
Preconditions.checkState(offsetIndex >= 0); |
| 285 |
offsetPosition = new FilePosition(offsetLine, offsetIndex); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
@Override |
| 292 |
public void reset() { |
| 293 |
mappings = Lists.newArrayList(); |
| 294 |
lastMapping = null; |
| 295 |
offsetPosition = new FilePosition(0, 0); |
| 296 |
prefixPosition = new FilePosition(0, 0); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
@Override |
| 307 |
public void appendTo(Appendable out, String name) throws IOException { |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
int maxLine = prepMappings(); |
| 340 |
|
| 341 |
|
| 342 |
out.append("/** Begin line maps. **/{ \"file\" : "); |
| 343 |
out.append(escapeString(name)); |
| 344 |
out.append(", \"count\": "); |
| 345 |
out.append(String.valueOf(maxLine + 1)); |
| 346 |
out.append(" }\n"); |
| 347 |
(new LineMapper(out)).appendLineMappings(); |
| 348 |
|
| 349 |
|
| 350 |
out.append("/** Begin file information. **/\n"); |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
for (int i = 0; i <= maxLine; ++i) { |
| 355 |
out.append("[]\n"); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
out.append("/** Begin mapping definitions. **/\n"); |
| 360 |
|
| 361 |
(new MappingWriter()).appendMappings(out); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
private int prepMappings() throws IOException { |
| 368 |
|
| 369 |
(new MappingTraversal()).traverse(new UsedMappingCheck()); |
| 370 |
|
| 371 |
|
| 372 |
int id = 0; |
| 373 |
int maxLine = 0; |
| 374 |
for (Mapping m : mappings) { |
| 375 |
if (m.used) { |
| 376 |
m.id = id++; |
| 377 |
int endPositionLine = m.endPosition.getLine(); |
| 378 |
maxLine = Math.max(maxLine, endPositionLine); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
return maxLine + prefixPosition.getLine(); |
| 384 |
} |
| 385 |
|
| 386 |
private class LineMapper implements MappingVisitor { |
| 387 |
|
| 388 |
private final Appendable out; |
| 389 |
|
| 390 |
|
| 391 |
private boolean firstChar = true; |
| 392 |
|
| 393 |
private final static String UNMAPPED_STRING = "-1"; |
| 394 |
|
| 395 |
private int lastId = UNMAPPED; |
| 396 |
private String lastIdString = UNMAPPED_STRING; |
| 397 |
|
| 398 |
LineMapper(Appendable out) { |
| 399 |
this.out = out; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
@Override |
| 406 |
public void visit(Mapping m, int line, int col, int nextLine, int nextCol) |
| 407 |
throws IOException { |
| 408 |
|
| 409 |
int id = (m != null) ? m.id : UNMAPPED; |
| 410 |
if (lastId != id) { |
| 411 |
|
| 412 |
|
| 413 |
lastIdString = (id == UNMAPPED) ? UNMAPPED_STRING : String.valueOf(id); |
| 414 |
lastId = id; |
| 415 |
} |
| 416 |
String idString = lastIdString; |
| 417 |
|
| 418 |
for (int i = line; i <= nextLine; i++) { |
| 419 |
if (i == nextLine) { |
| 420 |
for (int j = col; j < nextCol; j++) { |
| 421 |
addCharEntry(idString); |
| 422 |
} |
| 423 |
break; |
| 424 |
} |
| 425 |
|
| 426 |
closeLine(); |
| 427 |
openLine(); |
| 428 |
|
| 429 |
|
| 430 |
col = 0; |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
void appendLineMappings() throws IOException { |
| 436 |
|
| 437 |
openLine(); |
| 438 |
|
| 439 |
(new MappingTraversal()).traverse(this); |
| 440 |
|
| 441 |
|
| 442 |
closeLine(); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
private void openLine() throws IOException { |
| 449 |
if (out != null) { |
| 450 |
out.append("["); |
| 451 |
this.firstChar = true; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
private void closeLine() throws IOException { |
| 459 |
if (out != null) { |
| 460 |
out.append("]\n"); |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
private void addCharEntry(String id) throws IOException { |
| 469 |
if (out != null) { |
| 470 |
if (firstChar) { |
| 471 |
firstChar = false; |
| 472 |
} else { |
| 473 |
out.append(","); |
| 474 |
} |
| 475 |
out.append(id); |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
private class UsedMappingCheck implements MappingVisitor { |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
@Override |
| 488 |
public void visit(Mapping m, int line, int col, int nextLine, int nextCol) |
| 489 |
throws IOException { |
| 490 |
if (m != null) { |
| 491 |
m.used = true; |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
private interface MappingVisitor { |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
void visit(Mapping m, int line, int col, int endLine, int endCol) |
| 507 |
throws IOException; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
private class MappingTraversal { |
| 515 |
|
| 516 |
private int line; |
| 517 |
private int col; |
| 518 |
|
| 519 |
MappingTraversal() { |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
void traverse(MappingVisitor v) throws IOException { |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
Deque<Mapping> stack = new ArrayDeque<Mapping>(); |
| 528 |
for (Mapping m : mappings) { |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
while (!stack.isEmpty() && !isOverlapped(stack.peek(), m)) { |
| 534 |
Mapping previous = stack.pop(); |
| 535 |
maybeVisit(v, previous); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
Mapping parent = stack.peek(); |
| 541 |
maybeVisitParent(v, parent, m); |
| 542 |
|
| 543 |
stack.push(m); |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
while (!stack.isEmpty()) { |
| 549 |
Mapping m = stack.pop(); |
| 550 |
maybeVisit(v, m); |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
private int getAdjustedLine(FilePosition p) { |
| 558 |
return p.getLine() + prefixPosition.getLine(); |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
private int getAdjustedCol(FilePosition p) { |
| 565 |
int rawLine = p.getLine(); |
| 566 |
int rawCol = p.getColumn(); |
| 567 |
|
| 568 |
return (rawLine != 0) |
| 569 |
? rawCol : rawCol + prefixPosition.getColumn(); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
private boolean isOverlapped(Mapping m1, Mapping m2) { |
| 576 |
|
| 577 |
int l1 = m1.endPosition.getLine(); |
| 578 |
int l2 = m2.startPosition.getLine(); |
| 579 |
int c1 = m1.endPosition.getColumn(); |
| 580 |
int c2 = m2.startPosition.getColumn(); |
| 581 |
|
| 582 |
return (l1 == l2 && c1 >= c2) || l1 > l2; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
private void maybeVisit(MappingVisitor v, Mapping m) throws IOException { |
| 590 |
int nextLine = getAdjustedLine(m.endPosition); |
| 591 |
int nextCol = getAdjustedCol(m.endPosition); |
| 592 |
|
| 593 |
|
| 594 |
if (line < nextLine || (line == nextLine && col < nextCol)) { |
| 595 |
visit(v, m, nextLine, nextCol); |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
private void maybeVisitParent(MappingVisitor v, Mapping parent, Mapping m) |
| 603 |
throws IOException { |
| 604 |
int nextLine = getAdjustedLine(m.startPosition); |
| 605 |
int nextCol = getAdjustedCol(m.startPosition); |
| 606 |
|
| 607 |
Preconditions.checkState(line < nextLine || col <= nextCol); |
| 608 |
if (line < nextLine || (line == nextLine && col < nextCol)) { |
| 609 |
visit(v, parent, nextLine, nextCol); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
private void visit(MappingVisitor v, Mapping m, |
| 618 |
int nextLine, int nextCol) |
| 619 |
throws IOException { |
| 620 |
Preconditions.checkState(line <= nextLine); |
| 621 |
Preconditions.checkState(line < nextLine || col < nextCol); |
| 622 |
|
| 623 |
if (line == nextLine && col == nextCol) { |
| 624 |
|
| 625 |
Preconditions.checkState(false); |
| 626 |
return; |
| 627 |
} |
| 628 |
|
| 629 |
v.visit(m, line, col, nextLine, nextCol); |
| 630 |
|
| 631 |
line = nextLine; |
| 632 |
col = nextCol; |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
@Override |
| 637 |
public void validate(boolean validate) { |
| 638 |
|
| 639 |
} |
| 640 |
|
| 641 |
@Override |
| 642 |
public void appendIndexMapTo( |
| 643 |
Appendable out, String name, List<SourceMapSection> appSections) { |
| 644 |
throw new UnsupportedOperationException(); |
| 645 |
} |
| 646 |
} |