Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: tools/gn/header_checker.h

Issue 216903004: Add optional public header checking to GN build (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 <map>
9 #include <set>
10 #include <vector>
11
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"
19
20 class BuildSettings;
21 class Label;
22 class SourceFile;
23 class Target;
24
25 namespace base {
26 class MessageLoop;
27 }
28
29 class HeaderChecker : public base::RefCountedThreadSafe<HeaderChecker> {
30 public:
31 HeaderChecker(const BuildSettings* build_settings,
32 const std::vector<const Target*>& targets);
33
34 // This assumes that the current thread already has a message loop. On
35 // error, fills the given vector with the errors and returns false. Returns
36 // true on success.
37 bool Run(std::vector<Err>* errors);
38
39 private:
40 friend class base::RefCountedThreadSafe<HeaderChecker>;
41 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest, IsDependencyOf);
42 FRIEND_TEST_ALL_PREFIXES(HeaderCheckerTest, CheckInclude);
43 ~HeaderChecker();
44
45 struct TargetInfo {
46 TargetInfo() : target(NULL), is_public(false) {}
47 TargetInfo(const Target* t, bool p) : target(t), is_public(p) {}
48
49 const Target* target;
50 bool is_public;
51 };
52
53 typedef std::vector<TargetInfo> TargetVector;
54
55 void DoWork(const Target* target, const SourceFile& file);
56
57 void OnComplete();
58
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);
62
63 // Returns true if the given file is in the output directory.
64 bool IsFileInOuputDir(const SourceFile& file) const;
65
66 // Resolves the contents of an include to a SourceFile.
67 SourceFile SourceFileForInclude(const base::StringPiece& input) const;
68
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;
74
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.
77 bool CheckInclude(const Target* from_target,
78 const SourceFile& source_file,
79 const SourceFile& include_file,
80 Err* err) const;
81
82 // Returns true if the given search_for target is a dependency of
83 // search_from. Many subtrees are duplicated so this function avoids
84 // duplicate checking across recursive calls by keeping track of checked
85 // targets in the given set. It should point to an empty set for the first
86 // call. A target is not considered to be a dependency of itself.
87 bool IsDependencyOf(const Target* search_for,
88 const Target* search_from) const;
89 bool IsDependencyOf(const Target* search_for,
90 const Target* search_from,
91 std::set<const Target*>* checked) const;
92
93 // Non-locked variables ------------------------------------------------------
94 //
95 // These are initialized during construction (which happens on one thread)
96 // and are not modified after, so any thread can read these without locking.
97
98 base::MessageLoop* main_loop_;
99 base::RunLoop main_thread_runner_;
100
101 const BuildSettings* build_settings_;
102
103 // Maps source files to targets it appears in (usually just one target).
104 typedef std::map<SourceFile, TargetVector> FileMap;
105 FileMap file_map_;
106
107 // Locked variables ----------------------------------------------------------
108 //
109 // These are mutable during runtime and require locking.
110
111 base::Lock lock_;
112
113 std::vector<Err> errors_;
114 int pending_files_;
115
116 DISALLOW_COPY_AND_ASSIGN(HeaderChecker);
117 };
118
119 #endif // TOOLS_GN_HEADER_CHECKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698