• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef TOOLS_GN_HEADER_CHECKER_H_
6 #define TOOLS_GN_HEADER_CHECKER_H_
7 
8 #include <condition_variable>
9 #include <functional>
10 #include <map>
11 #include <mutex>
12 #include <set>
13 #include <string_view>
14 #include <vector>
15 
16 #include "base/atomic_ref_count.h"
17 #include "base/gtest_prod_util.h"
18 #include "base/memory/ref_counted.h"
19 #include "gn/c_include_iterator.h"
20 #include "gn/err.h"
21 #include "gn/source_dir.h"
22 
23 class BuildSettings;
24 class InputFile;
25 class SourceFile;
26 class Target;
27 
28 namespace base {
29 class FilePath;
30 }
31 
32 class HeaderChecker : public base::RefCountedThreadSafe<HeaderChecker> {
33  public:
34   // Represents a dependency chain.
35   struct ChainLink {
ChainLinkChainLink36     ChainLink() : target(nullptr), is_public(false) {}
ChainLinkChainLink37     ChainLink(const Target* t, bool p) : target(t), is_public(p) {}
38 
39     const Target* target;
40 
41     // True when the dependency on this target is public.
42     bool is_public;
43 
44     // Used for testing.
45     bool operator==(const ChainLink& other) const {
46       return target == other.target && is_public == other.is_public;
47     }
48   };
49   using Chain = std::vector<ChainLink>;
50 
51   // check_generated, if true, will also check generated
52   // files. Something that can only be done after running a build that
53   // has generated them.
54   HeaderChecker(const BuildSettings* build_settings,
55                 const std::vector<const Target*>& targets,
56                 bool check_generated,
57                 bool check_system);
58 
59   // Runs the check. The targets in to_check will be checked.
60   //
61   // This assumes that the current thread already has a message loop. On
62   // error, fills the given vector with the errors and returns false. Returns
63   // true on success.
64   //
65   // force_check, if true, will override targets opting out of header checking
66   // with "check_includes = false" and will check them anyway.
67   bool Run(const std::vector<const Target*>& to_check,
68            bool force_check,
69            std::vector<Err>* errors);
70 
71  private:
72   friend class base::RefCountedThreadSafe<HeaderChecker>;
73   FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest, IsDependencyOf);
74   FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest, CheckInclude);
75   FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest, PublicFirst);
76   FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest, CheckIncludeAllowCircular);
77   FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest, SourceFileForInclude);
78   FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest,
79                            SourceFileForInclude_FileNotFound);
80   FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest, Friend);
81 
82   ~HeaderChecker();
83 
84   struct TargetInfo {
TargetInfoTargetInfo85     TargetInfo() : target(nullptr), is_public(false), is_generated(false) {}
TargetInfoTargetInfo86     TargetInfo(const Target* t, bool is_pub, bool is_gen)
87         : target(t), is_public(is_pub), is_generated(is_gen) {}
88 
89     const Target* target;
90 
91     // True if the file is public in the given target.
92     bool is_public;
93 
94     // True if this file is generated and won't actually exist on disk.
95     bool is_generated;
96   };
97 
98   using TargetVector = std::vector<TargetInfo>;
99   using FileMap = std::map<SourceFile, TargetVector>;
100   using PathExistsCallback = std::function<bool(const base::FilePath& path)>;
101 
102   // Backend for Run() that takes the list of files to check. The errors_ list
103   // will be populate on failure.
104   void RunCheckOverFiles(const FileMap& flies, bool force_check);
105 
106   void DoWork(const Target* target, const SourceFile& file);
107 
108   // Adds the sources and public files from the given target to the given map.
109   static void AddTargetToFileMap(const Target* target, FileMap* dest);
110 
111   // Returns true if the given file is in the output directory.
112   bool IsFileInOuputDir(const SourceFile& file) const;
113 
114   // Resolves the contents of an include to a SourceFile.
115   SourceFile SourceFileForInclude(const IncludeStringWithLocation& include,
116                                   const std::vector<SourceDir>& include_dirs,
117                                   const InputFile& source_file,
118                                   Err* err) const;
119 
120   // from_target is the target the file was defined from. It will be used in
121   // error messages.
122   bool CheckFile(const Target* from_target,
123                  const SourceFile& file,
124                  std::vector<Err>* err) const;
125 
126   // Checks that the given file in the given target can include the
127   // given include file. If disallowed, adds the error or errors to
128   // the errors array.  The range indicates the location of the
129   // include in the file for error reporting.
130   // |no_depeency_cache| is used to cache or check whether there is no
131   // dependency from |from_target| to target having |include_file|.
132   void CheckInclude(
133       const Target* from_target,
134       const InputFile& source_file,
135       const SourceFile& include_file,
136       const LocationRange& range,
137       std::set<std::pair<const Target*, const Target*>>* no_dependency_cache,
138       std::vector<Err>* errors) const;
139 
140   // Returns true if the given search_for target is a dependency of
141   // search_from.
142   //
143   // If found, the vector given in "chain" will be filled with the reverse
144   // dependency chain from the dest target (chain[0] = search_for) to the src
145   // target (chain[chain.size() - 1] = search_from).
146   //
147   // Chains with permitted dependencies will be considered first. If a
148   // permitted match is found, *is_permitted will be set to true. A chain with
149   // indirect, non-public dependencies will only be considered if there are no
150   // public or direct chains. In this case, *is_permitted will be false.
151   //
152   // A permitted dependency is a sequence of public dependencies. The first
153   // one may be private, since a direct dependency always allows headers to be
154   // included.
155   bool IsDependencyOf(const Target* search_for,
156                       const Target* search_from,
157                       Chain* chain,
158                       bool* is_permitted) const;
159 
160   // For internal use by the previous override of IsDependencyOf.  If
161   // require_public is true, only public dependency chains are searched.
162   bool IsDependencyOf(const Target* search_for,
163                       const Target* search_from,
164                       bool require_permitted,
165                       Chain* chain) const;
166 
167   // Makes a very descriptive error message for when an include is disallowed
168   // from a given from_target, with a missing dependency to one of the given
169   // targets.
170   static Err MakeUnreachableError(const InputFile& source_file,
171                                   const LocationRange& range,
172                                   const Target* from_target,
173                                   const TargetVector& targets);
174 
175   // Non-locked variables ------------------------------------------------------
176   //
177   // These are initialized during construction (which happens on one thread)
178   // and are not modified after, so any thread can read these without locking.
179 
180   const BuildSettings* build_settings_;
181 
182   bool check_generated_;
183 
184   bool check_system_;
185 
186   // Maps source files to targets it appears in (usually just one target).
187   FileMap file_map_;
188 
189   // Number of tasks posted by RunCheckOverFiles() that haven't completed their
190   // execution.
191   base::AtomicRefCount task_count_;
192 
193   // Locked variables ----------------------------------------------------------
194   //
195   // These are mutable during runtime and require locking.
196 
197   std::mutex lock_;
198 
199   std::vector<Err> errors_;
200 
201   // Signaled when |task_count_| becomes zero.
202   std::condition_variable task_count_cv_;
203 
204   HeaderChecker(const HeaderChecker&) = delete;
205   HeaderChecker& operator=(const HeaderChecker&) = delete;
206 };
207 
208 #endif  // TOOLS_GN_HEADER_CHECKER_H_
209