Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(525)

Unified Diff: pkg/compiler/lib/src/compiler.dart

Issue 1520293002: Add token invariant to DiagnosticReporter (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Rebase + status update Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/compiler/lib/src/diagnostics/diagnostic_listener.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/compiler/lib/src/compiler.dart
diff --git a/pkg/compiler/lib/src/compiler.dart b/pkg/compiler/lib/src/compiler.dart
index 91a75c230aa31f8e594e79465dd0faf99d4aaa89..ceb9143e30744b1981d6ac8cb801dba3ae9fcbbe 100644
--- a/pkg/compiler/lib/src/compiler.dart
+++ b/pkg/compiler/lib/src/compiler.dart
@@ -1792,6 +1792,67 @@ class _CompilerDiagnosticReporter extends DiagnosticReporter {
}
if (uri == null && currentElement != null) {
uri = currentElement.compilationUnit.script.resourceUri;
+ assert(invariant(currentElement, () {
+
+ /// Check that [begin] and [end] can be found between [from] and [to].
+ validateToken(Token from, Token to) {
+ if (from == null || to == null) return true;
+ bool foundBegin = false;
+ bool foundEnd = false;
+ Token token = from;
+ while (true) {
+ if (token == begin) {
+ foundBegin = true;
+ }
+ if (token == end) {
+ foundEnd = true;
+ }
+ if (foundBegin && foundEnd) {
+ return true;
+ }
+ if (token == to || token == token.next || token.next == null) {
+ break;
+ }
+ token = token.next;
+ }
+
+ // Create a good message for when the tokens were not found.
+ StringBuffer sb = new StringBuffer();
+ sb.write('Invalid current element: $currentElement. ');
+ sb.write('Looking for ');
+ sb.write('[${begin} (${begin.hashCode}),');
+ sb.write('${end} (${end.hashCode})] in');
+
+ token = from;
+ while (true) {
+ sb.write('\n ${token} (${token.hashCode})');
+ if (token == to || token == token.next || token.next == null) {
+ break;
+ }
+ token = token.next;
+ }
+ return sb.toString();
+ }
+
+ if (currentElement.enclosingClass != null &&
+ currentElement.enclosingClass.isEnumClass) {
+ // Enums ASTs are synthesized (and give messed up messages).
+ return true;
+ }
+
+ if (currentElement is AstElement) {
+ AstElement astElement = currentElement;
+ if (astElement.hasNode) {
+ Token from = astElement.node.getBeginToken();
+ Token to = astElement.node.getEndToken();
+ if (astElement.metadata.isNotEmpty) {
+ from = astElement.metadata.first.beginToken;
+ }
+ return validateToken(from, to);
+ }
+ }
+ return true;
+ }, message: "Invalid current element: $currentElement [$begin,$end]."));
}
return new SourceSpan.fromTokens(uri, begin, end);
}
« no previous file with comments | « no previous file | pkg/compiler/lib/src/diagnostics/diagnostic_listener.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698