diff options
author | 2015-07-01 16:24:56 +0000 | |
---|---|---|
committer | 2015-07-02 09:22:51 +0000 | |
commit | 6190ffb63c0568d59d2e020e676eb72bc6709c0a (patch) | |
tree | 211d6c689b5781e49ef14fe183fd0252de776d82 /src | |
parent | 28da3658dd620879589badac61153fde39dcca8a (diff) |
Add a function to fetch all subincludes calls in a BUILD file.
--
MOS_MIGRATED_REVID=97334001
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/BuildFileAST.java | 60 | ||||
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/syntax/BuildFileASTTest.java | 14 |
2 files changed, 62 insertions, 12 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/BuildFileAST.java b/src/main/java/com/google/devtools/build/lib/syntax/BuildFileAST.java index 800aa65535..dc0995ac1b 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/BuildFileAST.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/BuildFileAST.java @@ -13,7 +13,6 @@ // limitations under the License. package com.google.devtools.build.lib.syntax; -import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.hash.HashCode; @@ -25,9 +24,7 @@ import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; import java.io.IOException; -import java.util.HashSet; import java.util.List; -import java.util.Set; import javax.annotation.Nullable; @@ -40,7 +37,9 @@ public class BuildFileAST extends ASTNode { private final ImmutableList<Comment> comments; - private final ImmutableSet<PathFragment> imports; + private ImmutableSet<PathFragment> loads; + + private ImmutableSet<String> subincludes; /** * Whether any errors were encountered during scanning or parsing. @@ -62,7 +61,6 @@ public class BuildFileAST extends ASTNode { this.comments = ImmutableList.copyOf(result.comments); this.containsErrors = result.containsErrors; this.contentHashCode = contentHashCode; - this.imports = fetchImports(this.stmts); if (!result.statements.isEmpty()) { setLocation(lexer.createLocation( result.statements.get(0).getLocation().getStartOffset(), @@ -72,15 +70,40 @@ public class BuildFileAST extends ASTNode { } } - private ImmutableSet<PathFragment> fetchImports(List<Statement> stmts) { - Set<PathFragment> imports = new HashSet<>(); + /** Collects labels from all subinclude statements */ + private ImmutableSet<String> fetchSubincludes(List<Statement> stmts) { + ImmutableSet.Builder<String> subincludes = new ImmutableSet.Builder<>(); + for (Statement stmt : stmts) { + // The code below matches: subinclude("literal string") + if (!(stmt instanceof ExpressionStatement)) { + continue; + } + Expression expr = ((ExpressionStatement) stmt).getExpression(); + if (!(expr instanceof FuncallExpression)) { + continue; + } + FuncallExpression call = (FuncallExpression) expr; + if (!call.getFunction().getName().equals("subinclude") || call.getArguments().size() != 1) { + continue; + } + Expression arg = call.getArguments().get(0).getValue(); + if (arg instanceof StringLiteral) { + subincludes.add(((StringLiteral) arg).getValue()); + } + } + return subincludes.build(); + } + + /** Collects paths from all load statements */ + private ImmutableSet<PathFragment> fetchLoads(List<Statement> stmts) { + ImmutableSet.Builder<PathFragment> loads = new ImmutableSet.Builder<>(); for (Statement stmt : stmts) { if (stmt instanceof LoadStatement) { LoadStatement imp = (LoadStatement) stmt; - imports.add(imp.getImportPath()); + loads.add(imp.getImportPath()); } } - return ImmutableSet.copyOf(imports); + return loads.build(); } /** @@ -107,10 +130,23 @@ public class BuildFileAST extends ASTNode { } /** - * Returns an (immutable) set of imports in this BUILD file. + * Returns a set of loads in this BUILD file. */ - public ImmutableCollection<PathFragment> getImports() { - return imports; + public synchronized ImmutableSet<PathFragment> getImports() { + if (loads == null) { + loads = fetchLoads(stmts); + } + return loads; + } + + /** + * Returns a set of subincludes in this BUILD file. + */ + public synchronized ImmutableSet<String> getSubincludes() { + if (subincludes == null) { + subincludes = fetchSubincludes(stmts); + } + return subincludes; } /** diff --git a/src/test/java/com/google/devtools/build/lib/syntax/BuildFileASTTest.java b/src/test/java/com/google/devtools/build/lib/syntax/BuildFileASTTest.java index 97d3a5f1fd..84562358e4 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/BuildFileASTTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/BuildFileASTTest.java @@ -321,4 +321,18 @@ public class BuildFileASTTest { BuildFileAST buildFileAST = parseBuildFile("include('//foo:bar')"); assertThat(buildFileAST.getStatements()).hasSize(1); } + + @Test + public void testFetchSubincludes() throws Exception { + BuildFileAST buildFileAST = + parseBuildFile( + "foo('a')\n", + "subinclude()\n", + "subinclude('hello')\n", + "1 + subinclude('ignored')", + "var = 'also ignored'", + "subinclude(var)\n", + "subinclude('world')"); + assertThat(buildFileAST.getSubincludes()).containsExactly("hello", "world"); + } } |