Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.syntax; |
| 16 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 17 | import com.google.common.base.Preconditions; |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.events.Event; |
| 19 | import com.google.devtools.build.lib.events.EventHandler; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.lib.events.Location; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import java.util.HashSet; |
Laurent Le Brun | 6874316 | 2015-05-13 13:18:09 +0000 | [diff] [blame] | 22 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import java.util.Set; |
laurentlb | 137e6c8 | 2017-08-16 20:16:39 +0200 | [diff] [blame] | 24 | import javax.annotation.Nullable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 26 | /** A class for doing static checks on files, before evaluating them. */ |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 27 | public final class ValidationEnvironment extends SyntaxTreeVisitor { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | |
laurentlb | f7956d6 | 2018-08-22 07:25:28 -0700 | [diff] [blame^] | 29 | private enum Scope { |
| 30 | /** Symbols defined inside a function or a comprehension. */ |
| 31 | Local, |
| 32 | /** Symbols defined at a module top-level, e.g. functions, loaded symbols. */ |
| 33 | Module, |
| 34 | /** Predefined symbols (builtins) */ |
| 35 | Universe, |
| 36 | } |
| 37 | |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 38 | private static class Block { |
laurentlb | 137e6c8 | 2017-08-16 20:16:39 +0200 | [diff] [blame] | 39 | private final Set<String> variables = new HashSet<>(); |
| 40 | private final Set<String> readOnlyVariables = new HashSet<>(); |
laurentlb | f7956d6 | 2018-08-22 07:25:28 -0700 | [diff] [blame^] | 41 | private final Scope scope; |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 42 | @Nullable private final Block parent; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | |
laurentlb | f7956d6 | 2018-08-22 07:25:28 -0700 | [diff] [blame^] | 44 | Block(Scope scope, @Nullable Block parent) { |
| 45 | this.scope = scope; |
laurentlb | 137e6c8 | 2017-08-16 20:16:39 +0200 | [diff] [blame] | 46 | this.parent = parent; |
| 47 | } |
| 48 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 50 | /** |
| 51 | * We use an unchecked exception around EvalException because the SyntaxTreeVisitor doesn't let |
| 52 | * visit methods throw checked exceptions. We might change that later. |
| 53 | */ |
| 54 | private static class ValidationException extends RuntimeException { |
| 55 | EvalException exception; |
| 56 | |
| 57 | ValidationException(EvalException e) { |
| 58 | exception = e; |
| 59 | } |
| 60 | |
| 61 | ValidationException(Location location, String message, String url) { |
| 62 | exception = new EvalException(location, message, url); |
| 63 | } |
| 64 | |
| 65 | ValidationException(Location location, String message) { |
| 66 | exception = new EvalException(location, message); |
| 67 | } |
| 68 | } |
| 69 | |
brandjon | 3c16191 | 2017-10-05 05:06:05 +0200 | [diff] [blame] | 70 | private final SkylarkSemantics semantics; |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 71 | private Block block; |
laurentlb | a9b9aea | 2017-09-04 17:39:09 +0200 | [diff] [blame] | 72 | private int loopCount; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | |
laurentlb | f7956d6 | 2018-08-22 07:25:28 -0700 | [diff] [blame^] | 74 | /** Create a ValidationEnvironment for a given global Environment (containing builtins). */ |
laurentlb | bde7c41 | 2017-06-12 15:22:37 +0200 | [diff] [blame] | 75 | ValidationEnvironment(Environment env) { |
Francois-Rene Rideau | 6e7160d | 2015-08-26 17:22:35 +0000 | [diff] [blame] | 76 | Preconditions.checkArgument(env.isGlobal()); |
laurentlb | f7956d6 | 2018-08-22 07:25:28 -0700 | [diff] [blame^] | 77 | block = new Block(Scope.Universe, null); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 78 | Set<String> builtinVariables = env.getVariableNames(); |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 79 | block.variables.addAll(builtinVariables); |
| 80 | block.readOnlyVariables.addAll(builtinVariables); |
laurentlb | a0fd766 | 2017-05-10 12:26:15 -0400 | [diff] [blame] | 81 | semantics = env.getSemantics(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | } |
| 83 | |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 84 | @Override |
| 85 | public void visit(LoadStatement node) { |
| 86 | for (Identifier symbol : node.getSymbols()) { |
| 87 | declare(symbol.getName(), node.getLocation()); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void visit(Identifier node) { |
| 93 | if (!hasSymbolInEnvironment(node.getName())) { |
| 94 | throw new ValidationException(node.createInvalidIdentifierException(getAllSymbols())); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private void validateLValue(Location loc, Expression expr) { |
| 99 | if (expr instanceof Identifier) { |
| 100 | declare(((Identifier) expr).getName(), loc); |
| 101 | } else if (expr instanceof IndexExpression) { |
| 102 | visit(expr); |
| 103 | } else if (expr instanceof ListLiteral) { |
| 104 | for (Expression e : ((ListLiteral) expr).getElements()) { |
| 105 | validateLValue(loc, e); |
| 106 | } |
| 107 | } else { |
| 108 | throw new ValidationException(loc, "cannot assign to '" + expr + "'"); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void visit(LValue node) { |
| 114 | validateLValue(node.getLocation(), node.getExpression()); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void visit(ReturnStatement node) { |
laurentlb | f7956d6 | 2018-08-22 07:25:28 -0700 | [diff] [blame^] | 119 | if (block.scope != Scope.Local) { |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 120 | throw new ValidationException( |
laurentlb | a9b9aea | 2017-09-04 17:39:09 +0200 | [diff] [blame] | 121 | node.getLocation(), "return statements must be inside a function"); |
| 122 | } |
| 123 | super.visit(node); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void visit(ForStatement node) { |
| 128 | loopCount++; |
| 129 | super.visit(node); |
| 130 | Preconditions.checkState(loopCount > 0); |
| 131 | loopCount--; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void visit(FlowStatement node) { |
| 136 | if (loopCount <= 0) { |
| 137 | throw new ValidationException( |
| 138 | node.getLocation(), node.getKind().getName() + " statement must be inside a for loop"); |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 139 | } |
| 140 | super.visit(node); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void visit(DotExpression node) { |
| 145 | visit(node.getObject()); |
| 146 | // Do not visit the field. |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void visit(AbstractComprehension node) { |
laurentlb | f7956d6 | 2018-08-22 07:25:28 -0700 | [diff] [blame^] | 151 | openBlock(Scope.Local); |
laurentlb | 2195b1c | 2018-02-16 04:14:46 -0800 | [diff] [blame] | 152 | super.visit(node); |
| 153 | closeBlock(); |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void visit(FunctionDefStatement node) { |
| 158 | for (Parameter<Expression, Expression> param : node.getParameters()) { |
| 159 | if (param.isOptional()) { |
| 160 | visit(param.getDefaultValue()); |
| 161 | } |
| 162 | } |
laurentlb | f7956d6 | 2018-08-22 07:25:28 -0700 | [diff] [blame^] | 163 | openBlock(Scope.Local); |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 164 | for (Parameter<Expression, Expression> param : node.getParameters()) { |
| 165 | if (param.hasName()) { |
| 166 | declare(param.getName(), param.getLocation()); |
| 167 | } |
| 168 | } |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 169 | visitAll(node.getStatements()); |
| 170 | closeBlock(); |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 171 | } |
| 172 | |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 173 | @Override |
| 174 | public void visit(IfStatement node) { |
laurentlb | f7956d6 | 2018-08-22 07:25:28 -0700 | [diff] [blame^] | 175 | if (block.scope != Scope.Local) { |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 176 | throw new ValidationException( |
| 177 | node.getLocation(), |
| 178 | "if statements are not allowed at the top level. You may move it inside a function " |
laurentlb | 2213d92 | 2018-04-12 09:27:57 -0700 | [diff] [blame] | 179 | + "or use an if expression (x if condition else y)."); |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 180 | } |
| 181 | super.visit(node); |
| 182 | } |
| 183 | |
laurentlb | e368449 | 2017-08-21 12:02:46 +0200 | [diff] [blame] | 184 | @Override |
| 185 | public void visit(AugmentedAssignmentStatement node) { |
| 186 | if (node.getLValue().getExpression() instanceof ListLiteral) { |
| 187 | throw new ValidationException( |
| 188 | node.getLocation(), "cannot perform augmented assignment on a list or tuple expression"); |
| 189 | } |
| 190 | // Other bad cases are handled when visiting the LValue node. |
| 191 | super.visit(node); |
| 192 | } |
| 193 | |
laurentlb | bde7c41 | 2017-06-12 15:22:37 +0200 | [diff] [blame] | 194 | /** Declare a variable and add it to the environment. */ |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 195 | private void declare(String varname, Location location) { |
laurentlb | f7956d6 | 2018-08-22 07:25:28 -0700 | [diff] [blame^] | 196 | boolean readOnlyViolation = false; |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 197 | if (block.readOnlyVariables.contains(varname)) { |
laurentlb | f7956d6 | 2018-08-22 07:25:28 -0700 | [diff] [blame^] | 198 | readOnlyViolation = true; |
| 199 | } |
| 200 | if (block.scope == Scope.Module && block.parent.readOnlyVariables.contains(varname)) { |
| 201 | // TODO(laurentlb): This behavior is buggy. Symbols in the module scope should shadow symbols |
| 202 | // from the universe. https://github.com/bazelbuild/bazel/issues/5637 |
| 203 | readOnlyViolation = true; |
| 204 | } |
| 205 | if (readOnlyViolation) { |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 206 | throw new ValidationException( |
Laurent Le Brun | fa407e5 | 2016-11-04 15:53:08 +0000 | [diff] [blame] | 207 | location, |
| 208 | String.format("Variable %s is read only", varname), |
| 209 | "https://bazel.build/versions/master/docs/skylark/errors/read-only-variable.html"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 210 | } |
laurentlb | f7956d6 | 2018-08-22 07:25:28 -0700 | [diff] [blame^] | 211 | if (block.scope == Scope.Module) { |
| 212 | // Symbols defined in the module scope cannot be reassigned. |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 213 | block.readOnlyVariables.add(varname); |
| 214 | } |
| 215 | block.variables.add(varname); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 216 | } |
| 217 | |
laurentlb | 9d5c0a0 | 2017-06-13 23:08:06 +0200 | [diff] [blame] | 218 | /** Returns true if the symbol exists in the validation environment (or a parent). */ |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 219 | private boolean hasSymbolInEnvironment(String varname) { |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 220 | for (Block b = block; b != null; b = b.parent) { |
| 221 | if (b.variables.contains(varname)) { |
laurentlb | 137e6c8 | 2017-08-16 20:16:39 +0200 | [diff] [blame] | 222 | return true; |
| 223 | } |
| 224 | } |
| 225 | return false; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Laurent Le Brun | e102a2d | 2017-01-02 12:06:18 +0000 | [diff] [blame] | 228 | /** Returns the set of all accessible symbols (both local and global) */ |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 229 | private Set<String> getAllSymbols() { |
Laurent Le Brun | e102a2d | 2017-01-02 12:06:18 +0000 | [diff] [blame] | 230 | Set<String> all = new HashSet<>(); |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 231 | for (Block b = block; b != null; b = b.parent) { |
| 232 | all.addAll(b.variables); |
Laurent Le Brun | e102a2d | 2017-01-02 12:06:18 +0000 | [diff] [blame] | 233 | } |
| 234 | return all; |
| 235 | } |
| 236 | |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 237 | /** Throws ValidationException if a load() appears after another kind of statement. */ |
| 238 | private static void checkLoadAfterStatement(List<Statement> statements) { |
laurentlb | a0fd766 | 2017-05-10 12:26:15 -0400 | [diff] [blame] | 239 | Location firstStatement = null; |
| 240 | |
| 241 | for (Statement statement : statements) { |
| 242 | // Ignore string literals (e.g. docstrings). |
| 243 | if (statement instanceof ExpressionStatement |
| 244 | && ((ExpressionStatement) statement).getExpression() instanceof StringLiteral) { |
| 245 | continue; |
| 246 | } |
| 247 | |
| 248 | if (statement instanceof LoadStatement) { |
| 249 | if (firstStatement == null) { |
| 250 | continue; |
| 251 | } |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 252 | throw new ValidationException( |
laurentlb | a0fd766 | 2017-05-10 12:26:15 -0400 | [diff] [blame] | 253 | statement.getLocation(), |
| 254 | "load() statements must be called before any other statement. " |
| 255 | + "First non-load() statement appears at " |
| 256 | + firstStatement |
brandjon | f5b8d6f | 2017-06-23 18:03:28 +0200 | [diff] [blame] | 257 | + ". Use --incompatible_bzl_disallow_load_after_statement=false to temporarily " |
| 258 | + "disable this check."); |
laurentlb | a0fd766 | 2017-05-10 12:26:15 -0400 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | if (firstStatement == null) { |
| 262 | firstStatement = statement.getLocation(); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
laurentlb | bde7c41 | 2017-06-12 15:22:37 +0200 | [diff] [blame] | 267 | /** Validates the AST and runs static checks. */ |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 268 | private void validateAst(List<Statement> statements) { |
laurentlb | a0fd766 | 2017-05-10 12:26:15 -0400 | [diff] [blame] | 269 | // Check that load() statements are on top. |
brandjon | 3c16191 | 2017-10-05 05:06:05 +0200 | [diff] [blame] | 270 | if (semantics.incompatibleBzlDisallowLoadAfterStatement()) { |
laurentlb | a0fd766 | 2017-05-10 12:26:15 -0400 | [diff] [blame] | 271 | checkLoadAfterStatement(statements); |
| 272 | } |
| 273 | |
laurentlb | f7956d6 | 2018-08-22 07:25:28 -0700 | [diff] [blame^] | 274 | openBlock(Scope.Module); |
| 275 | |
Laurent Le Brun | 6874316 | 2015-05-13 13:18:09 +0000 | [diff] [blame] | 276 | // Add every function in the environment before validating. This is |
| 277 | // necessary because functions may call other functions defined |
| 278 | // later in the file. |
| 279 | for (Statement statement : statements) { |
| 280 | if (statement instanceof FunctionDefStatement) { |
| 281 | FunctionDefStatement fct = (FunctionDefStatement) statement; |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 282 | declare(fct.getIdentifier().getName(), fct.getLocation()); |
Laurent Le Brun | 6874316 | 2015-05-13 13:18:09 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 286 | this.visitAll(statements); |
laurentlb | f7956d6 | 2018-08-22 07:25:28 -0700 | [diff] [blame^] | 287 | closeBlock(); |
Laurent Le Brun | 6874316 | 2015-05-13 13:18:09 +0000 | [diff] [blame] | 288 | } |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 289 | |
laurentlb | bde7c41 | 2017-06-12 15:22:37 +0200 | [diff] [blame] | 290 | public static void validateAst(Environment env, List<Statement> statements) throws EvalException { |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 291 | try { |
| 292 | ValidationEnvironment venv = new ValidationEnvironment(env); |
| 293 | venv.validateAst(statements); |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 294 | // Check that no closeBlock was forgotten. |
| 295 | Preconditions.checkState(venv.block.parent == null); |
laurentlb | aa8cc6c | 2017-08-17 15:39:50 +0200 | [diff] [blame] | 296 | } catch (ValidationException e) { |
| 297 | throw e.exception; |
| 298 | } |
laurentlb | bde7c41 | 2017-06-12 15:22:37 +0200 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | public static boolean validateAst( |
| 302 | Environment env, List<Statement> statements, EventHandler eventHandler) { |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 303 | try { |
laurentlb | bde7c41 | 2017-06-12 15:22:37 +0200 | [diff] [blame] | 304 | validateAst(env, statements); |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 305 | return true; |
| 306 | } catch (EvalException e) { |
| 307 | if (!e.isDueToIncompleteAST()) { |
| 308 | eventHandler.handle(Event.error(e.getLocation(), e.getMessage())); |
| 309 | } |
| 310 | return false; |
| 311 | } |
| 312 | } |
laurentlb | 137e6c8 | 2017-08-16 20:16:39 +0200 | [diff] [blame] | 313 | |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 314 | /** Open a new lexical block that will contain the future declarations. */ |
laurentlb | f7956d6 | 2018-08-22 07:25:28 -0700 | [diff] [blame^] | 315 | private void openBlock(Scope scope) { |
| 316 | block = new Block(scope, block); |
laurentlb | 137e6c8 | 2017-08-16 20:16:39 +0200 | [diff] [blame] | 317 | } |
| 318 | |
laurentlb | 65345cd | 2017-08-17 19:59:59 +0200 | [diff] [blame] | 319 | /** Close a lexical block (and lose all declarations it contained). */ |
| 320 | private void closeBlock() { |
| 321 | block = Preconditions.checkNotNull(block.parent); |
laurentlb | 137e6c8 | 2017-08-16 20:16:39 +0200 | [diff] [blame] | 322 | } |
laurentlb | ab58a92 | 2017-08-22 16:45:28 +0200 | [diff] [blame] | 323 | |
| 324 | /** |
| 325 | * Checks that the AST is using the restricted syntax. |
| 326 | * |
| 327 | * <p>Restricted syntax is used by Bazel BUILD files. It forbids function definitions, *args, and |
| 328 | * **kwargs. This creates a better separation between code and data. |
| 329 | */ |
| 330 | public static boolean checkBuildSyntax( |
| 331 | List<Statement> statements, final EventHandler eventHandler) { |
| 332 | // Wrap the boolean inside an array so that the inner class can modify it. |
| 333 | final boolean[] success = new boolean[] {true}; |
| 334 | // TODO(laurentlb): Merge with the visitor above when possible (i.e. when BUILD files use it). |
| 335 | SyntaxTreeVisitor checker = |
| 336 | new SyntaxTreeVisitor() { |
brandjon | aadf660 | 2018-01-17 10:40:38 -0800 | [diff] [blame] | 337 | |
| 338 | private void error(ASTNode node, String message) { |
| 339 | eventHandler.handle(Event.error(node.getLocation(), message)); |
| 340 | success[0] = false; |
laurentlb | ab58a92 | 2017-08-22 16:45:28 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public void visit(FunctionDefStatement node) { |
brandjon | aadf660 | 2018-01-17 10:40:38 -0800 | [diff] [blame] | 345 | error( |
| 346 | node, |
| 347 | "function definitions are not allowed in BUILD files. You may move the function to " |
| 348 | + "a .bzl file and load it."); |
| 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public void visit(ForStatement node) { |
| 353 | error( |
| 354 | node, |
| 355 | "for statements are not allowed in BUILD files. You may inline the loop, move it " |
| 356 | + "to a function definition (in a .bzl file), or as a last resort use a list " |
| 357 | + "comprehension."); |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public void visit(IfStatement node) { |
| 362 | error( |
| 363 | node, |
| 364 | "if statements are not allowed in BUILD files. You may move conditional logic to a " |
| 365 | + "function definition (in a .bzl file), or for simple cases use an if " |
| 366 | + "expression."); |
| 367 | } |
| 368 | |
| 369 | @Override |
| 370 | public void visit(FuncallExpression node) { |
| 371 | for (Argument.Passed arg : node.getArguments()) { |
| 372 | if (arg.isStarStar()) { |
| 373 | error( |
| 374 | node, |
| 375 | "**kwargs arguments are not allowed in BUILD files. Pass the arguments in " |
| 376 | + "explicitly."); |
| 377 | } else if (arg.isStar()) { |
| 378 | error( |
| 379 | node, |
| 380 | "*args arguments are not allowed in BUILD files. Pass the arguments in " |
| 381 | + "explicitly."); |
| 382 | } |
| 383 | } |
laurentlb | ab58a92 | 2017-08-22 16:45:28 +0200 | [diff] [blame] | 384 | } |
| 385 | }; |
| 386 | checker.visitAll(statements); |
| 387 | return success[0]; |
| 388 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 389 | } |