aboutsummaryrefslogtreecommitdiffstats
path: root/lint
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2012-02-28 12:02:46 -0800
committerTor Norbye <tnorbye@google.com>2012-02-28 12:02:46 -0800
commitfd9fe37e806b8f14b2944d29f307cd050ecb419c (patch)
treec359698378b29eb16cda128043e6bc013bc4a710 /lint
parent8c89972f765412b93834b18f43e93a77269bd2e7 (diff)
downloadsdk-fd9fe37e806b8f14b2944d29f307cd050ecb419c.tar.gz
sdk-fd9fe37e806b8f14b2944d29f307cd050ecb419c.tar.bz2
sdk-fd9fe37e806b8f14b2944d29f307cd050ecb419c.zip
Fix configuration information for library projects
This changeset fixes one more bug related to lint configurations and library projects (see issue 26029). Some lint checks, such as the MergeRootFrameLayoutDetector, computes the warnings at the end of processing all files. At that point, the context points to the master project, so any errors which were actually found in a library project will instead be using the master project's configuration. That means that any suppress rules applies to the lint.xml in the library project will be ignored. This changeset fixes this by moving the logic which looks up the severity for a warning out of the lint clients and into the context. Now it checks the current projects being scanned and looks up the corresponding project for each file (based on the file prefix), and retrieves the configuration that way. This changeset also makes one more fix: It now consults *both* the library project *and* the master project to see if a rule should be ignored. This means that if you turn off a given check in your master project, you will no longer see those warnings from library projects either, which seems desirable. Change-Id: Icb5cdf7696b4908b0553f86896793515cb06f29c
Diffstat (limited to 'lint')
-rw-r--r--lint/cli/src/com/android/tools/lint/Main.java6
-rw-r--r--lint/libs/lint_api/src/com/android/tools/lint/client/api/DefaultConfiguration.java4
-rw-r--r--lint/libs/lint_api/src/com/android/tools/lint/client/api/LintClient.java2
-rw-r--r--lint/libs/lint_api/src/com/android/tools/lint/client/api/LintDriver.java44
-rw-r--r--lint/libs/lint_api/src/com/android/tools/lint/detector/api/Context.java28
-rw-r--r--lint/libs/lint_api/src/com/android/tools/lint/detector/api/XmlContext.java2
-rw-r--r--lint/libs/lint_checks/tests/src/com/android/tools/lint/LintCliXmlParserTest.java5
-rw-r--r--lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/AbstractCheckTest.java5
8 files changed, 81 insertions, 15 deletions
diff --git a/lint/cli/src/com/android/tools/lint/Main.java b/lint/cli/src/com/android/tools/lint/Main.java
index 2621d0a08..6cf28638e 100644
--- a/lint/cli/src/com/android/tools/lint/Main.java
+++ b/lint/cli/src/com/android/tools/lint/Main.java
@@ -842,14 +842,14 @@ public class Main extends LintClient {
}
@Override
- public void report(Context context, Issue issue, Location location, String message,
- Object data) {
+ public void report(Context context, Issue issue, Severity severity, Location location,
+ String message, Object data) {
assert context.isEnabled(issue);
- Severity severity = context.getConfiguration().getSeverity(issue);
if (severity == Severity.IGNORE) {
return;
}
+
if (severity == Severity.FATAL) {
mFatal = true;
// From here on, treat the fatal error as an error such that we don't display
diff --git a/lint/libs/lint_api/src/com/android/tools/lint/client/api/DefaultConfiguration.java b/lint/libs/lint_api/src/com/android/tools/lint/client/api/DefaultConfiguration.java
index 56cee8e6f..6dc4804c6 100644
--- a/lint/libs/lint_api/src/com/android/tools/lint/client/api/DefaultConfiguration.java
+++ b/lint/libs/lint_api/src/com/android/tools/lint/client/api/DefaultConfiguration.java
@@ -203,7 +203,9 @@ public class DefaultConfiguration extends Configuration {
}
message = "Failed to parse lint.xml configuration file: " + message;
mClient.report(new Context(null, mProject, mProject, mConfigFile),
- IssueRegistry.LINT_ERROR, Location.create(mConfigFile), message, null);
+ IssueRegistry.LINT_ERROR,
+ mProject.getConfiguration().getSeverity(IssueRegistry.LINT_ERROR),
+ Location.create(mConfigFile), message, null);
}
private void readConfig() {
diff --git a/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintClient.java b/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintClient.java
index 48e86b591..0e47ddebe 100644
--- a/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintClient.java
+++ b/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintClient.java
@@ -76,6 +76,7 @@ public abstract class LintClient {
*
* @param context the context used by the detector when the issue was found
* @param issue the issue that was found
+ * @param severity the severity of the issue
* @param location the location of the issue
* @param message the associated user message
* @param data optional extra data for a discovered issue, or null. The
@@ -88,6 +89,7 @@ public abstract class LintClient {
public abstract void report(
@NonNull Context context,
@NonNull Issue issue,
+ @NonNull Severity severity,
@Nullable Location location,
@NonNull String message,
@Nullable Object data);
diff --git a/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintDriver.java b/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintDriver.java
index d328ab847..ad736c646 100644
--- a/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintDriver.java
+++ b/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintDriver.java
@@ -123,6 +123,7 @@ public class LintDriver {
private int mPhase;
private List<Detector> mRepeatingDetectors;
private EnumSet<Scope> mRepeatScope;
+ private Project[] mCurrentProjects;
private boolean mAbbreviating = true;
/**
@@ -172,6 +173,31 @@ public class LintDriver {
}
/**
+ * Returns the project containing a given file, or null if not found. This searches
+ * only among the currently checked project and its library projects, not among all
+ * possible projects being scanned sequentially.
+ *
+ * @param file the file to be checked
+ * @return the corresponding project, or null if not found
+ */
+ @Nullable
+ public Project findProjectFor(@NonNull File file) {
+ if (mCurrentProjects != null) {
+ if (mCurrentProjects.length == 1) {
+ return mCurrentProjects[0];
+ }
+ String path = file.getPath();
+ for (Project project : mCurrentProjects) {
+ if (path.startsWith(project.getDir().getPath())) {
+ return project;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ /**
* Sets whether lint should abbreviate output when appropriate.
*
* @param abbreviating true to abbreviate output, false to include everything
@@ -620,6 +646,12 @@ public class LintDriver {
Context projectContext = new Context(this, project, null, projectDir);
fireEvent(EventType.SCANNING_PROJECT, projectContext);
+ List<Project> allLibraries = project.getAllLibraries();
+ Set<Project> allProjects = new HashSet<Project>(allLibraries.size() + 1);
+ allProjects.add(project);
+ allProjects.addAll(allLibraries);
+ mCurrentProjects = allProjects.toArray(new Project[allProjects.size()]);
+
for (Detector check : mApplicableDetectors) {
check.beforeCheckProject(projectContext);
if (mCanceled) {
@@ -627,7 +659,6 @@ public class LintDriver {
}
}
-
runFileDetectors(project, project);
if (!Scope.checkSingleFile(mScope)) {
@@ -671,9 +702,12 @@ public class LintDriver {
// is valid
Issue.create("Lint", "", "", Category.PERFORMANCE, 0, Severity.INFORMATIONAL, //$NON-NLS-1$
null, EnumSet.noneOf(Scope.class)),
+ Severity.INFORMATIONAL,
null /*range*/,
"Lint canceled by user", null);
}
+
+ mCurrentProjects = null;
}
private void runFileDetectors(@NonNull Project project, @Nullable Project main) {
@@ -876,7 +910,9 @@ public class LintDriver {
+ "Does the project need to be built first?", project.getName());
Location location = Location.create(project.getDir());
mClient.report(new Context(this, project, main, project.getDir()),
- IssueRegistry.LINT_ERROR, location, message, null);
+ IssueRegistry.LINT_ERROR,
+ project.getConfiguration().getSeverity(IssueRegistry.LINT_ERROR),
+ location, message, null);
classEntries = Collections.emptyList();
} else {
classEntries = new ArrayList<ClassEntry>(64);
@@ -1331,6 +1367,7 @@ public class LintDriver {
public void report(
@NonNull Context context,
@NonNull Issue issue,
+ @NonNull Severity severity,
@Nullable Location location,
@NonNull String message,
@Nullable Object data) {
@@ -1347,12 +1384,11 @@ public class LintDriver {
return;
}
- Severity severity = configuration.getSeverity(issue);
if (severity == Severity.IGNORE) {
return;
}
- mDelegate.report(context, issue, location, message, data);
+ mDelegate.report(context, issue, severity, location, message, data);
}
// Everything else just delegates to the embedding lint client
diff --git a/lint/libs/lint_api/src/com/android/tools/lint/detector/api/Context.java b/lint/libs/lint_api/src/com/android/tools/lint/detector/api/Context.java
index 33e39e7a4..9ddd10acd 100644
--- a/lint/libs/lint_api/src/com/android/tools/lint/detector/api/Context.java
+++ b/lint/libs/lint_api/src/com/android/tools/lint/detector/api/Context.java
@@ -257,7 +257,33 @@ public class Context {
@Nullable Location location,
@NonNull String message,
@Nullable Object data) {
- mDriver.getClient().report(this, issue, location, message, data);
+ Configuration configuration = mConfiguration;
+
+ // If this error was computed for a context where the context corresponds to
+ // a project instead of a file, the actual error may be in a different project (e.g.
+ // a library project), so adjust the configuration as necessary.
+ if (location != null && location.getFile() != null) {
+ Project project = mDriver.findProjectFor(location.getFile());
+ if (project != null) {
+ configuration = project.getConfiguration();
+ }
+ }
+
+ // If an error occurs in a library project, but you've disabled that check in the
+ // main project, disable it in the library project too. (In some cases you don't
+ // control the lint.xml of a library project, and besides, if you're not interested in
+ // a check for your main project you probably don't care about it in the library either.)
+ if (configuration != mConfiguration
+ && mConfiguration.getSeverity(issue) == Severity.IGNORE) {
+ return;
+ }
+
+ Severity severity = configuration.getSeverity(issue);
+ if (severity == Severity.IGNORE) {
+ return;
+ }
+
+ mDriver.getClient().report(this, issue, severity, location, message, data);
}
/**
diff --git a/lint/libs/lint_api/src/com/android/tools/lint/detector/api/XmlContext.java b/lint/libs/lint_api/src/com/android/tools/lint/detector/api/XmlContext.java
index 7733dc76a..aabbf793a 100644
--- a/lint/libs/lint_api/src/com/android/tools/lint/detector/api/XmlContext.java
+++ b/lint/libs/lint_api/src/com/android/tools/lint/detector/api/XmlContext.java
@@ -102,7 +102,7 @@ public class XmlContext extends Context {
if (scope != null && mDriver.isSuppressed(issue, scope)) {
return;
}
- mDriver.getClient().report(this, issue, location, message, data);
+ super.report(issue, location, message, data);
}
@Override
diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/LintCliXmlParserTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/LintCliXmlParserTest.java
index 7186fb11d..a6f4aafee 100644
--- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/LintCliXmlParserTest.java
+++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/LintCliXmlParserTest.java
@@ -25,6 +25,7 @@ import com.android.tools.lint.detector.api.Location;
import com.android.tools.lint.detector.api.Location.Handle;
import com.android.tools.lint.detector.api.Position;
import com.android.tools.lint.detector.api.Project;
+import com.android.tools.lint.detector.api.Severity;
import com.android.tools.lint.detector.api.XmlContext;
import org.w3c.dom.Attr;
@@ -153,8 +154,8 @@ public class LintCliXmlParserTest extends TestCase {
private static class TestClient extends Main {
@Override
- public void report(Context context, Issue issue, Location location, String message,
- Object data) {
+ public void report(Context context, Issue issue, Severity severity, Location location,
+ String message, Object data) {
System.out.println(location + ":" + message);
}
}
diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/AbstractCheckTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/AbstractCheckTest.java
index 455542087..ba22ed3ac 100644
--- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/AbstractCheckTest.java
+++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/AbstractCheckTest.java
@@ -270,8 +270,8 @@ public abstract class AbstractCheckTest extends TestCase {
}
@Override
- public void report(Context context, Issue issue, Location location, String message,
- Object data) {
+ public void report(Context context, Issue issue, Severity severity, Location location,
+ String message, Object data) {
StringBuilder sb = new StringBuilder();
if (issue == IssueRegistry.LINT_ERROR) {
@@ -305,7 +305,6 @@ public abstract class AbstractCheckTest extends TestCase {
sb.append(' ');
}
- Severity severity = context.getConfiguration().getSeverity(issue);
if (severity == Severity.FATAL) {
// Treat fatal errors like errors in the golden files.
severity = Severity.ERROR;