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_
12 #include "base/gtest_prod_util.h"
13 #include "base/macros.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"
31 class HeaderChecker
: public base::RefCountedThreadSafe
<HeaderChecker
> {
33 // Represents a dependency chain.
35 ChainLink() : target(nullptr), is_public(false) {}
36 ChainLink(const Target
* t
, bool p
) : target(t
), is_public(p
) {}
40 // True when the dependency on this target is public.
44 bool operator==(const ChainLink
& other
) const {
45 return target
== other
.target
&& is_public
== other
.is_public
;
48 typedef std::vector
<ChainLink
> Chain
;
50 HeaderChecker(const BuildSettings
* build_settings
,
51 const std::vector
<const Target
*>& targets
);
53 // Runs the check. The targets in to_check will be checked.
55 // This assumes that the current thread already has a message loop. On
56 // error, fills the given vector with the errors and returns false. Returns
59 // force_check, if true, will override targets opting out of header checking
60 // with "check_includes = false" and will check them anyway.
61 bool Run(const std::vector
<const Target
*>& to_check
,
63 std::vector
<Err
>* errors
);
66 friend class base::RefCountedThreadSafe
<HeaderChecker
>;
67 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest
, IsDependencyOf
);
68 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest
, CheckInclude
);
69 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest
, PublicFirst
);
70 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest
, CheckIncludeAllowCircular
);
74 TargetInfo() : target(nullptr), is_public(false), is_generated(false) {}
75 TargetInfo(const Target
* t
, bool is_pub
, bool is_gen
)
78 is_generated(is_gen
) {
83 // True if the file is public in the given target.
86 // True if this file is generated and won't actually exist on disk.
90 typedef std::vector
<TargetInfo
> TargetVector
;
91 typedef std::map
<SourceFile
, TargetVector
> FileMap
;
93 // Backend for Run() that takes the list of files to check. The errors_ list
94 // will be populate on failure.
95 void RunCheckOverFiles(const FileMap
& flies
, bool force_check
);
97 void DoWork(const Target
* target
, const SourceFile
& file
);
99 // Adds the sources and public files from the given target to the given map.
100 static void AddTargetToFileMap(const Target
* target
, FileMap
* dest
);
102 // Returns true if the given file is in the output directory.
103 bool IsFileInOuputDir(const SourceFile
& file
) const;
105 // Resolves the contents of an include to a SourceFile.
106 SourceFile
SourceFileForInclude(const base::StringPiece
& input
) const;
108 // from_target is the target the file was defined from. It will be used in
110 bool CheckFile(const Target
* from_target
,
111 const SourceFile
& file
,
114 // Checks that the given file in the given target can include the given
115 // include file. If disallowed, returns false and sets the error. The
116 // range indicates the location of the include in the file for error
118 bool CheckInclude(const Target
* from_target
,
119 const InputFile
& source_file
,
120 const SourceFile
& include_file
,
121 const LocationRange
& range
,
124 // Returns true if the given search_for target is a dependency of
127 // If found, the vector given in "chain" will be filled with the reverse
128 // dependency chain from the dest target (chain[0] = search_for) to the src
129 // target (chain[chain.size() - 1] = search_from).
131 // Chains with permitted dependencies will be considered first. If a
132 // permitted match is found, *is_permitted will be set to true. A chain with
133 // indirect, non-public dependencies will only be considered if there are no
134 // public or direct chains. In this case, *is_permitted will be false.
136 // A permitted dependency is a sequence of public dependencies. The first
137 // one may be private, since a direct dependency always allows headers to be
139 bool IsDependencyOf(const Target
* search_for
,
140 const Target
* search_from
,
142 bool* is_permitted
) const;
144 // For internal use by the previous override of IsDependencyOf. If
145 // require_public is true, only public dependency chains are searched.
146 bool IsDependencyOf(const Target
* search_for
,
147 const Target
* search_from
,
148 bool require_permitted
,
151 // Makes a very descriptive error message for when an include is disallowed
152 // from a given from_target, with a missing dependency to one of the given
154 static Err
MakeUnreachableError(const InputFile
& source_file
,
155 const LocationRange
& range
,
156 const Target
* from_target
,
157 const TargetVector
& targets
);
159 // Non-locked variables ------------------------------------------------------
161 // These are initialized during construction (which happens on one thread)
162 // and are not modified after, so any thread can read these without locking.
164 base::MessageLoop
* main_loop_
;
165 base::RunLoop main_thread_runner_
;
167 const BuildSettings
* build_settings_
;
169 // Maps source files to targets it appears in (usually just one target).
172 // Locked variables ----------------------------------------------------------
174 // These are mutable during runtime and require locking.
178 std::vector
<Err
> errors_
;
180 DISALLOW_COPY_AND_ASSIGN(HeaderChecker
);
183 #endif // TOOLS_GN_HEADER_CHECKER_H_