Instrumental test for BookmarksBridge. It currently tests these functionalities:...
[chromium-blink-merge.git] / tools / gn / header_checker.h
blobcad170874c8c016c937f067066a78aa8661dbee7
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.
5 #ifndef TOOLS_GN_HEADER_CHECKER_H_
6 #define TOOLS_GN_HEADER_CHECKER_H_
8 #include <map>
9 #include <set>
10 #include <vector>
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"
20 class BuildSettings;
21 class InputFile;
22 class Label;
23 class LocationRange;
24 class SourceFile;
25 class Target;
27 namespace base {
28 class MessageLoop;
31 class HeaderChecker : public base::RefCountedThreadSafe<HeaderChecker> {
32 public:
33 HeaderChecker(const BuildSettings* build_settings,
34 const std::vector<const Target*>& targets);
36 // Runs the check. The targets in to_check will be checked. If this list is
37 // empty, all targets will be checked.
39 // This assumes that the current thread already has a message loop. On
40 // error, fills the given vector with the errors and returns false. Returns
41 // true on success.
43 // force_check, if true, will override targets opting out of header checking
44 // with "check_includes = false" and will check them anyway.
45 bool Run(const std::vector<const Target*>& to_check,
46 bool force_check,
47 std::vector<Err>* errors);
49 private:
50 friend class base::RefCountedThreadSafe<HeaderChecker>;
51 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest, IsDependencyOf);
52 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest,
53 IsDependencyOf_ForwardsDirectDependentConfigs);
54 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest, CheckInclude);
55 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest, CheckIncludeAllowCircular);
56 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest,
57 GetDependentConfigChainProblemIndex);
58 ~HeaderChecker();
60 struct TargetInfo {
61 TargetInfo() : target(NULL), is_public(false), is_generated(false) {}
62 TargetInfo(const Target* t, bool is_pub, bool is_gen)
63 : target(t),
64 is_public(is_pub),
65 is_generated(is_gen) {
68 const Target* target;
70 // True if the file is public in the given target.
71 bool is_public;
73 // True if this file is generated and won't actually exist on disk.
74 bool is_generated;
77 typedef std::vector<TargetInfo> TargetVector;
78 typedef std::map<SourceFile, TargetVector> FileMap;
80 // Backend for Run() that takes the list of files to check. The errors_ list
81 // will be populate on failure.
82 void RunCheckOverFiles(const FileMap& flies, bool force_check);
84 void DoWork(const Target* target, const SourceFile& file);
86 // Adds the sources and public files from the given target to the given map.
87 static void AddTargetToFileMap(const Target* target, FileMap* dest);
89 // Returns true if the given file is in the output directory.
90 bool IsFileInOuputDir(const SourceFile& file) const;
92 // Resolves the contents of an include to a SourceFile.
93 SourceFile SourceFileForInclude(const base::StringPiece& input) const;
95 // from_target is the target the file was defined from. It will be used in
96 // error messages.
97 bool CheckFile(const Target* from_target,
98 const SourceFile& file,
99 Err* err) const;
101 // Checks that the given file in the given target can include the given
102 // include file. If disallowed, returns false and sets the error. The
103 // range indicates the location of the include in the file for error
104 // reporting.
105 bool CheckInclude(const Target* from_target,
106 const InputFile& source_file,
107 const SourceFile& include_file,
108 const LocationRange& range,
109 Err* err) const;
111 // Returns true if the given search_for target is a dependency of
112 // search_from.
114 // If found, the vector given in "chain" will be filled with the reverse
115 // dependency chain from the dest target (chain[0] = search_for) to the src
116 // target (chain[chain.size() - 1] = search_from).
118 // Chains with public dependencies will be considered first. If a public
119 // match is found, *is_public will be set to true. A chain with non-public
120 // dependencies will only be considered if there are no public chains. In
121 // this case, *is_public will be false.
122 bool IsDependencyOf(const Target* search_for,
123 const Target* search_from,
124 std::vector<const Target*>* chain,
125 bool* is_public) const;
127 // For internal use by the previous override of IsDependencyOf. If
128 // require_public is true, only public dependency chains are searched.
129 bool IsDependencyOf(const Target* search_for,
130 const Target* search_from,
131 bool require_public,
132 std::vector<const Target*>* chain) const;
134 // Non-locked variables ------------------------------------------------------
136 // These are initialized during construction (which happens on one thread)
137 // and are not modified after, so any thread can read these without locking.
139 base::MessageLoop* main_loop_;
140 base::RunLoop main_thread_runner_;
142 const BuildSettings* build_settings_;
144 // Maps source files to targets it appears in (usually just one target).
145 FileMap file_map_;
147 // Locked variables ----------------------------------------------------------
149 // These are mutable during runtime and require locking.
151 base::Lock lock_;
153 std::vector<Err> errors_;
155 DISALLOW_COPY_AND_ASSIGN(HeaderChecker);
158 #endif // TOOLS_GN_HEADER_CHECKER_H_