Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / tools / gn / header_checker.h
blobf62ca2c818e0de3bb7c19b3ae0d385e9013a8475
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 // 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
38 // true on success.
39 bool Run(std::vector<Err>* errors);
41 private:
42 friend class base::RefCountedThreadSafe<HeaderChecker>;
43 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest, IsDependencyOf);
44 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest, CheckInclude);
45 ~HeaderChecker();
47 struct TargetInfo {
48 TargetInfo() : target(NULL), is_public(false) {}
49 TargetInfo(const Target* t, bool p) : target(t), is_public(p) {}
51 const Target* target;
52 bool is_public;
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
70 // error messages.
71 bool CheckFile(const Target* from_target,
72 const SourceFile& file,
73 Err* err) const;
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
78 // reporting.
79 bool CheckInclude(const Target* from_target,
80 const InputFile& source_file,
81 const SourceFile& include_file,
82 const LocationRange& range,
83 Err* err) const;
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;
108 FileMap file_map_;
110 // Locked variables ----------------------------------------------------------
112 // These are mutable during runtime and require locking.
114 base::Lock lock_;
116 std::vector<Err> errors_;
118 DISALLOW_COPY_AND_ASSIGN(HeaderChecker);
121 #endif // TOOLS_GN_HEADER_CHECKER_H_