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 HeaderChecker(const BuildSettings
* build_settings
,
34 const std::vector
<const Target
*>& targets
);
36 // This assumes that the current thread already has a message loop. On
37 // error, fills the given vector with the errors and returns false. Returns
39 bool Run(std::vector
<Err
>* errors
);
42 friend class base::RefCountedThreadSafe
<HeaderChecker
>;
43 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest
, IsDependencyOf
);
44 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest
, CheckInclude
);
48 TargetInfo() : target(NULL
), is_public(false) {}
49 TargetInfo(const Target
* t
, bool p
) : target(t
), is_public(p
) {}
55 typedef std::vector
<TargetInfo
> TargetVector
;
57 void DoWork(const Target
* target
, const SourceFile
& file
);
59 // Adds the sources and public files from the given target to the file_map_.
60 // Not threadsafe! Called only during init.
61 void AddTargetToFileMap(const Target
* target
);
63 // Returns true if the given file is in the output directory.
64 bool IsFileInOuputDir(const SourceFile
& file
) const;
66 // Resolves the contents of an include to a SourceFile.
67 SourceFile
SourceFileForInclude(const base::StringPiece
& input
) const;
69 // from_target is the target the file was defined from. It will be used in
71 bool CheckFile(const Target
* from_target
,
72 const SourceFile
& file
,
75 // Checks that the given file in the given target can include the given
76 // include file. If disallowed, returns false and sets the error. The
77 // range indicates the location of the include in the file for error
79 bool CheckInclude(const Target
* from_target
,
80 const InputFile
& source_file
,
81 const SourceFile
& include_file
,
82 const LocationRange
& range
,
85 // Returns true if the given search_for target is a dependency of
86 // search_from. Many subtrees are duplicated so this function avoids
87 // duplicate checking across recursive calls by keeping track of checked
88 // targets in the given set. It should point to an empty set for the first
89 // call. A target is not considered to be a dependency of itself.
90 bool IsDependencyOf(const Target
* search_for
,
91 const Target
* search_from
) const;
92 bool IsDependencyOf(const Target
* search_for
,
93 const Target
* search_from
,
94 std::set
<const Target
*>* checked
) const;
96 // Non-locked variables ------------------------------------------------------
98 // These are initialized during construction (which happens on one thread)
99 // and are not modified after, so any thread can read these without locking.
101 base::MessageLoop
* main_loop_
;
102 base::RunLoop main_thread_runner_
;
104 const BuildSettings
* build_settings_
;
106 // Maps source files to targets it appears in (usually just one target).
107 typedef std::map
<SourceFile
, TargetVector
> FileMap
;
110 // Locked variables ----------------------------------------------------------
112 // These are mutable during runtime and require locking.
116 std::vector
<Err
> errors_
;
118 DISALLOW_COPY_AND_ASSIGN(HeaderChecker
);
121 #endif // TOOLS_GN_HEADER_CHECKER_H_