OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/gn/header_checker.h" | 5 #include "tools/gn/header_checker.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 // For all targets containing this file, we require that at least one be | 323 // For all targets containing this file, we require that at least one be |
324 // a direct or public dependency of the current target, and that the header | 324 // a direct or public dependency of the current target, and that the header |
325 // is public within the target. | 325 // is public within the target. |
326 // | 326 // |
327 // If there is more than one target containing this header, we may encounter | 327 // If there is more than one target containing this header, we may encounter |
328 // some error cases before finding a good one. This error stores the previous | 328 // some error cases before finding a good one. This error stores the previous |
329 // one encountered, which we may or may not throw away. | 329 // one encountered, which we may or may not throw away. |
330 Err last_error; | 330 Err last_error; |
331 | 331 |
332 bool found_dependency = false; | 332 bool found_dependency = false; |
333 for (size_t i = 0; i < targets.size(); i++) { | 333 for (const auto& target : targets) { |
334 // We always allow source files in a target to include headers also in that | 334 // We always allow source files in a target to include headers also in that |
335 // target. | 335 // target. |
336 const Target* to_target = targets[i].target; | 336 const Target* to_target = target.target; |
337 if (to_target == from_target) | 337 if (to_target == from_target) |
338 return true; | 338 return true; |
339 | 339 |
340 // Additionally, allow a target to include files from that same target | 340 // Additionally, allow a target to include files from that same target |
341 // in other toolchains. This is a bit of a hack to account for the fact that | 341 // in other toolchains. This is a bit of a hack to account for the fact that |
342 // the include finder doesn't understand the preprocessor. | 342 // the include finder doesn't understand the preprocessor. |
343 // | 343 // |
344 // If a source file conditionally depends on a platform-specific include in | 344 // If a source file conditionally depends on a platform-specific include in |
345 // the same target, and there is a cross-compile such that GN sees | 345 // the same target, and there is a cross-compile such that GN sees |
346 // definitions of the target both with and without that include, it would | 346 // definitions of the target both with and without that include, it would |
347 // give an error that the target needs to depend on itself in the other | 347 // give an error that the target needs to depend on itself in the other |
348 // toolchain (where the platform-specific header is defined as a source). | 348 // toolchain (where the platform-specific header is defined as a source). |
349 if (TargetLabelsMatchExceptToolchain(to_target, from_target)) | 349 if (TargetLabelsMatchExceptToolchain(to_target, from_target)) |
350 return true; | 350 return true; |
351 | 351 |
352 bool is_permitted_chain = false; | 352 bool is_permitted_chain = false; |
353 if (IsDependencyOf(to_target, from_target, &chain, &is_permitted_chain)) { | 353 if (IsDependencyOf(to_target, from_target, &chain, &is_permitted_chain)) { |
354 DCHECK(chain.size() >= 2); | 354 DCHECK(chain.size() >= 2); |
355 DCHECK(chain[0].target == to_target); | 355 DCHECK(chain[0].target == to_target); |
356 DCHECK(chain[chain.size() - 1].target == from_target); | 356 DCHECK(chain[chain.size() - 1].target == from_target); |
357 | 357 |
358 found_dependency = true; | 358 found_dependency = true; |
359 | 359 |
360 if (targets[i].is_public && is_permitted_chain) { | 360 if (target.is_public && is_permitted_chain) { |
361 // This one is OK, we're done. | 361 // This one is OK, we're done. |
362 last_error = Err(); | 362 last_error = Err(); |
363 break; | 363 break; |
364 } | 364 } |
365 | 365 |
366 // Diagnose the error. | 366 // Diagnose the error. |
367 if (!targets[i].is_public) { | 367 if (!target.is_public) { |
368 // Danger: must call CreatePersistentRange to put in Err. | 368 // Danger: must call CreatePersistentRange to put in Err. |
369 last_error = Err( | 369 last_error = Err(CreatePersistentRange(source_file, range), |
370 CreatePersistentRange(source_file, range), | 370 "Including a private header.", |
371 "Including a private header.", | 371 "This file is private to the target " + |
372 "This file is private to the target " + | 372 target.target->label().GetUserVisibleName(false)); |
373 targets[i].target->label().GetUserVisibleName(false)); | |
374 } else if (!is_permitted_chain) { | 373 } else if (!is_permitted_chain) { |
375 last_error = Err( | 374 last_error = Err( |
376 CreatePersistentRange(source_file, range), | 375 CreatePersistentRange(source_file, range), |
377 "Can't include this header from here.", | 376 "Can't include this header from here.", |
378 GetDependencyChainPublicError(chain)); | 377 GetDependencyChainPublicError(chain)); |
379 } else { | 378 } else { |
380 NOTREACHED(); | 379 NOTREACHED(); |
381 } | 380 } |
382 } else if ( | 381 } else if ( |
383 to_target->allow_circular_includes_from().find(from_target->label()) != | 382 to_target->allow_circular_includes_from().find(from_target->label()) != |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 if (targets_with_other_toolchains.size() + | 559 if (targets_with_other_toolchains.size() + |
561 targets_with_matching_toolchains.size() > 1) | 560 targets_with_matching_toolchains.size() > 1) |
562 msg += "at least one of "; | 561 msg += "at least one of "; |
563 msg += "which should somehow be reachable."; | 562 msg += "which should somehow be reachable."; |
564 | 563 |
565 // Danger: must call CreatePersistentRange to put in Err. | 564 // Danger: must call CreatePersistentRange to put in Err. |
566 return Err(CreatePersistentRange(source_file, range), | 565 return Err(CreatePersistentRange(source_file, range), |
567 "Include not allowed.", msg); | 566 "Include not allowed.", msg); |
568 } | 567 } |
569 | 568 |
OLD | NEW |