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/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"
31 class HeaderChecker
: public base::RefCountedThreadSafe
<HeaderChecker
> {
33 // Represents a dependency chain.
35 ChainLink() : target(NULL
), 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. If this list is
54 // empty, all targets will be checked.
56 // This assumes that the current thread already has a message loop. On
57 // error, fills the given vector with the errors and returns false. Returns
60 // force_check, if true, will override targets opting out of header checking
61 // with "check_includes = false" and will check them anyway.
62 bool Run(const std::vector
<const Target
*>& to_check
,
64 std::vector
<Err
>* errors
);
67 friend class base::RefCountedThreadSafe
<HeaderChecker
>;
68 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest
, IsDependencyOf
);
69 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest
, CheckInclude
);
70 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest
, PublicFirst
);
71 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest
, CheckIncludeAllowCircular
);
75 TargetInfo() : target(NULL
), is_public(false), is_generated(false) {}
76 TargetInfo(const Target
* t
, bool is_pub
, bool is_gen
)
79 is_generated(is_gen
) {
84 // True if the file is public in the given target.
87 // True if this file is generated and won't actually exist on disk.
91 typedef std::vector
<TargetInfo
> TargetVector
;
92 typedef std::map
<SourceFile
, TargetVector
> FileMap
;
94 // Backend for Run() that takes the list of files to check. The errors_ list
95 // will be populate on failure.
96 void RunCheckOverFiles(const FileMap
& flies
, bool force_check
);
98 void DoWork(const Target
* target
, const SourceFile
& file
);
100 // Adds the sources and public files from the given target to the given map.
101 static void AddTargetToFileMap(const Target
* target
, FileMap
* dest
);
103 // Returns true if the given file is in the output directory.
104 bool IsFileInOuputDir(const SourceFile
& file
) const;
106 // Resolves the contents of an include to a SourceFile.
107 SourceFile
SourceFileForInclude(const base::StringPiece
& input
) const;
109 // from_target is the target the file was defined from. It will be used in
111 bool CheckFile(const Target
* from_target
,
112 const SourceFile
& file
,
115 // Checks that the given file in the given target can include the given
116 // include file. If disallowed, returns false and sets the error. The
117 // range indicates the location of the include in the file for error
119 bool CheckInclude(const Target
* from_target
,
120 const InputFile
& source_file
,
121 const SourceFile
& include_file
,
122 const LocationRange
& range
,
125 // Returns true if the given search_for target is a dependency of
128 // If found, the vector given in "chain" will be filled with the reverse
129 // dependency chain from the dest target (chain[0] = search_for) to the src
130 // target (chain[chain.size() - 1] = search_from).
132 // Chains with permitted dependencies will be considered first. If a
133 // permitted match is found, *is_permitted will be set to true. A chain with
134 // indirect, non-public dependencies will only be considered if there are no
135 // public or direct chains. In this case, *is_permitted will be false.
137 // A permitted dependency is a sequence of public dependencies. The first
138 // one may be private, since a direct dependency always allows headers to be
140 bool IsDependencyOf(const Target
* search_for
,
141 const Target
* search_from
,
143 bool* is_permitted
) const;
145 // For internal use by the previous override of IsDependencyOf. If
146 // require_public is true, only public dependency chains are searched.
147 bool IsDependencyOf(const Target
* search_for
,
148 const Target
* search_from
,
149 bool require_permitted
,
152 // Non-locked variables ------------------------------------------------------
154 // These are initialized during construction (which happens on one thread)
155 // and are not modified after, so any thread can read these without locking.
157 base::MessageLoop
* main_loop_
;
158 base::RunLoop main_thread_runner_
;
160 const BuildSettings
* build_settings_
;
162 // Maps source files to targets it appears in (usually just one target).
165 // Locked variables ----------------------------------------------------------
167 // These are mutable during runtime and require locking.
171 std::vector
<Err
> errors_
;
173 DISALLOW_COPY_AND_ASSIGN(HeaderChecker
);
176 #endif // TOOLS_GN_HEADER_CHECKER_H_