diff options
author | Tor Norbye <tnorbye@google.com> | 2012-01-26 15:04:57 -0800 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-01-26 15:14:37 -0800 |
commit | a2a7d644db6b742fda413600f62c64c1133a6c21 (patch) | |
tree | 5ffb4a48c46c6507670a151875927201ec93b01c /lint | |
parent | 73926b1e6312781da56f29954b95e1791f4e1ee9 (diff) | |
download | sdk-a2a7d644db6b742fda413600f62c64c1133a6c21.zip sdk-a2a7d644db6b742fda413600f62c64c1133a6c21.tar.gz sdk-a2a7d644db6b742fda413600f62c64c1133a6c21.tar.bz2 |
Fix a few misc Lint issues
First, stop reporting Java-related parser errors as lint
errors. Lombok fails to parse a few files (see
http://code.google.com/p/projectlombok/issues/detail?id=311) and even
when it fails, the error messages aren't useful (see
http://code.google.com/p/projectlombok/issues/detail?id=313).
Second, the XML parser could throw an exception in some circumstances,
because it passes invalid column numbers at the end of the file. This
CL guards against that (and against any other potential errors by
wrapping all exceptions during parsing as a SAXEception).
Finally, fix the positions reported in text nodes for the ExtraText
check such that it points to the line containing the extra text, not
the beginning of the text node (which is frequently the previous
line).
Change-Id: I9630ea49d30d8afdc8bd9cf2c87ca0a0306b8ec5
Diffstat (limited to 'lint')
3 files changed, 42 insertions, 2 deletions
diff --git a/lint/cli/src/com/android/tools/lint/LombokParser.java b/lint/cli/src/com/android/tools/lint/LombokParser.java index 9002e15..0aa1477 100644 --- a/lint/cli/src/com/android/tools/lint/LombokParser.java +++ b/lint/cli/src/com/android/tools/lint/LombokParser.java @@ -72,11 +72,17 @@ public class LombokParser implements IJavaParser { } return null; } catch (Throwable e) { + /* Silently ignore the errors. There are still some bugs in Lombok/Parboiled + * (triggered if you run lint on the AOSP framework directory for example), + * and having these show up as fatal errors when it's really a tool bug + * is bad. To make matters worse, the error messages aren't clear: + * http://code.google.com/p/projectlombok/issues/detail?id=313 context.report( IssueRegistry.PARSER_ERROR, Location.create(context.file), e.getCause() != null ? e.getCause().getLocalizedMessage() : e.getLocalizedMessage(), null); + */ return null; } diff --git a/lint/libs/lint_checks/src/com/android/tools/lint/checks/ExtraTextDetector.java b/lint/libs/lint_checks/src/com/android/tools/lint/checks/ExtraTextDetector.java index cf5d2e6..a14c7d0 100644 --- a/lint/libs/lint_checks/src/com/android/tools/lint/checks/ExtraTextDetector.java +++ b/lint/libs/lint_checks/src/com/android/tools/lint/checks/ExtraTextDetector.java @@ -18,7 +18,10 @@ package com.android.tools.lint.checks; import com.android.resources.ResourceFolderType; import com.android.tools.lint.detector.api.Category; +import com.android.tools.lint.detector.api.DefaultPosition; import com.android.tools.lint.detector.api.Issue; +import com.android.tools.lint.detector.api.Location; +import com.android.tools.lint.detector.api.Position; import com.android.tools.lint.detector.api.ResourceXmlDetector; import com.android.tools.lint.detector.api.Scope; import com.android.tools.lint.detector.api.Severity; @@ -88,7 +91,38 @@ public class ExtraTextDetector extends ResourceXmlDetector { if (snippet.length() > maxLength) { snippet = snippet.substring(0, maxLength) + "..."; } - context.report(ISSUE, context.getLocation(node), + Location location = context.getLocation(node); + if (i > 0) { + // Adjust the error position to point to the beginning of + // the text rather than the beginning of the text node + // (which is often the newline at the end of the previous + // line and the indentation) + Position start = location.getStart(); + if (start != null) { + int line = start.getLine(); + int column = start.getColumn(); + int offset = start.getOffset(); + + for (int j = 0; j < i; j++) { + offset++; + + if (text.charAt(j) == '\n') { + if (line != -1) { + line++; + } + if (column != -1) { + column = 0; + } + } else if (column != -1) { + column++; + } + } + + start = new DefaultPosition(line, column, offset); + location = Location.create(context.file, start, location.getEnd()); + } + } + context.report(ISSUE, location, String.format("Unexpected text found in layout file: \"%1$s\"", snippet), null); mFoundText = true; diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/ExtraTextDetectorTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/ExtraTextDetectorTest.java index 35c38ac..5016828 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/ExtraTextDetectorTest.java +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/ExtraTextDetectorTest.java @@ -27,7 +27,7 @@ public class ExtraTextDetectorTest extends AbstractCheckTest { public void testBroken() throws Exception { assertEquals( - "broken.xml:5: Warning: Unexpected text found in layout file: \"ImageButton " + + "broken.xml:6: Warning: Unexpected text found in layout file: \"ImageButton " + "android:id=\"@+id/android_logo2\" android:layout_width=\"wrap_content\"" + " android:layout_heigh...\"", lintProject("res/layout/broken.xml")); |