OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef TOOLS_GN_HEADER_CHECKER_H_ |
| 6 #define TOOLS_GN_HEADER_CHECKER_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <set> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/run_loop.h" |
| 16 #include "base/strings/string_piece.h" |
| 17 #include "base/synchronization/lock.h" |
| 18 #include "tools/gn/err.h" |
| 19 |
| 20 class BuildSettings; |
| 21 class Label; |
| 22 class SourceFile; |
| 23 class Target; |
| 24 |
| 25 namespace base { |
| 26 class MessageLoop; |
| 27 } |
| 28 |
| 29 class HeaderChecker : public base::RefCountedThreadSafe<HeaderChecker> { |
| 30 public: |
| 31 HeaderChecker(const BuildSettings* build_settings, |
| 32 const std::vector<const Target*>& targets); |
| 33 |
| 34 // This assumes that the current thread already has a message loop. On |
| 35 // error, fills the given vector with the errors and returns false. Returns |
| 36 // true on success. |
| 37 bool Run(std::vector<Err>* errors); |
| 38 |
| 39 private: |
| 40 friend class base::RefCountedThreadSafe<HeaderChecker>; |
| 41 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest, IsDependencyOf); |
| 42 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest, CheckInclude); |
| 43 ~HeaderChecker(); |
| 44 |
| 45 struct TargetInfo { |
| 46 TargetInfo() : target(NULL), is_public(false) {} |
| 47 TargetInfo(const Target* t, bool p) : target(t), is_public(p) {} |
| 48 |
| 49 const Target* target; |
| 50 bool is_public; |
| 51 }; |
| 52 |
| 53 typedef std::vector<TargetInfo> TargetVector; |
| 54 |
| 55 void DoWork(const Target* target, const SourceFile& file); |
| 56 |
| 57 // Adds the sources and public files from the given target to the file_map_. |
| 58 // Not threadsafe! Called only during init. |
| 59 void AddTargetToFileMap(const Target* target); |
| 60 |
| 61 // Returns true if the given file is in the output directory. |
| 62 bool IsFileInOuputDir(const SourceFile& file) const; |
| 63 |
| 64 // Resolves the contents of an include to a SourceFile. |
| 65 SourceFile SourceFileForInclude(const base::StringPiece& input) const; |
| 66 |
| 67 // from_target is the target the file was defined from. It will be used in |
| 68 // error messages. |
| 69 bool CheckFile(const Target* from_target, |
| 70 const SourceFile& file, |
| 71 Err* err) const; |
| 72 |
| 73 // Checks that the given file in the given target can include the given |
| 74 // include file. If disallowed, returns false and sets the error. |
| 75 bool CheckInclude(const Target* from_target, |
| 76 const SourceFile& source_file, |
| 77 const SourceFile& include_file, |
| 78 Err* err) const; |
| 79 |
| 80 // Returns true if the given search_for target is a dependency of |
| 81 // search_from. Many subtrees are duplicated so this function avoids |
| 82 // duplicate checking across recursive calls by keeping track of checked |
| 83 // targets in the given set. It should point to an empty set for the first |
| 84 // call. A target is not considered to be a dependency of itself. |
| 85 bool IsDependencyOf(const Target* search_for, |
| 86 const Target* search_from) const; |
| 87 bool IsDependencyOf(const Target* search_for, |
| 88 const Target* search_from, |
| 89 std::set<const Target*>* checked) const; |
| 90 |
| 91 // Non-locked variables ------------------------------------------------------ |
| 92 // |
| 93 // These are initialized during construction (which happens on one thread) |
| 94 // and are not modified after, so any thread can read these without locking. |
| 95 |
| 96 base::MessageLoop* main_loop_; |
| 97 base::RunLoop main_thread_runner_; |
| 98 |
| 99 const BuildSettings* build_settings_; |
| 100 |
| 101 // Maps source files to targets it appears in (usually just one target). |
| 102 typedef std::map<SourceFile, TargetVector> FileMap; |
| 103 FileMap file_map_; |
| 104 |
| 105 // Locked variables ---------------------------------------------------------- |
| 106 // |
| 107 // These are mutable during runtime and require locking. |
| 108 |
| 109 base::Lock lock_; |
| 110 |
| 111 std::vector<Err> errors_; |
| 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(HeaderChecker); |
| 114 }; |
| 115 |
| 116 #endif // TOOLS_GN_HEADER_CHECKER_H_ |
OLD | NEW |