clang-tools  3.8.0
CoverageChecker.h
Go to the documentation of this file.
1 //===-- CoverageChecker.h - Module map coverage checker -*- C++ -*-------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===--------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief Definitions for CoverageChecker.
12 ///
13 //===--------------------------------------------------------------------===//
14 
15 #ifndef COVERAGECHECKER_H
16 #define COVERAGECHECKER_H
17 
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/FileManager.h"
20 #include "clang/Basic/LangOptions.h"
21 #include "clang/Basic/TargetInfo.h"
22 #include "clang/Basic/TargetOptions.h"
23 #include "clang/Frontend/TextDiagnosticPrinter.h"
24 #include "clang/Lex/HeaderSearch.h"
25 #include "clang/Lex/HeaderSearchOptions.h"
26 #include "clang/Lex/ModuleMap.h"
27 #include "clang/Lex/Preprocessor.h"
28 #include "llvm/ADT/StringSet.h"
29 #include "llvm/Support/Host.h"
30 #include <string>
31 #include <vector>
32 
33 namespace Modularize {
34 
35 /// Module map checker class.
36 /// This is the heart of the checker.
37 /// The doChecks function does the main work.
38 /// The data members store the options and internally collected data.
40  // Checker arguments.
41 
42  /// The module.modulemap file path. Can be relative or absolute.
43  llvm::StringRef ModuleMapPath;
44  /// The include paths to check for files.
45  /// (Note that other directories above these paths are ignored.
46  /// To expect all files to be accounted for from the module.modulemap
47  /// file directory on down, leave this empty.)
48  std::vector<std::string> IncludePaths;
49  /// The remaining arguments, to be passed to the front end.
50  llvm::ArrayRef<std::string> CommandLine;
51  /// The module map.
52  clang::ModuleMap *ModMap;
53 
54  // Internal data.
55 
56  /// Directory containing the module map.
57  /// Might be relative to the current directory, or absolute.
58  std::string ModuleMapDirectory;
59  /// Set of all the headers found in the module map.
60  llvm::StringSet<llvm::MallocAllocator> ModuleMapHeadersSet;
61  /// All the headers found in the file system starting at the
62  /// module map, or the union of those from the include paths.
63  std::vector<std::string> FileSystemHeaders;
64  /// Headers found in file system, but not in module map.
65  std::vector<std::string> UnaccountedForHeaders;
66 
67 public:
68  /// Constructor.
69  /// You can use the static createCoverageChecker to create an instance
70  /// of this object.
71  /// \param ModuleMapPath The module.modulemap file path.
72  /// Can be relative or absolute.
73  /// \param IncludePaths The include paths to check for files.
74  /// (Note that other directories above these paths are ignored.
75  /// To expect all files to be accounted for from the module.modulemap
76  /// file directory on down, leave this empty.)
77  /// \param CommandLine Compile command line arguments.
78  /// \param ModuleMap The module map to check.
79  CoverageChecker(llvm::StringRef ModuleMapPath,
80  std::vector<std::string> &IncludePaths,
81  llvm::ArrayRef<std::string> CommandLine,
82  clang::ModuleMap *ModuleMap);
83 
84  /// Create instance of CoverageChecker.
85  /// \param ModuleMapPath The module.modulemap file path.
86  /// Can be relative or absolute.
87  /// \param IncludePaths The include paths to check for files.
88  /// (Note that other directories above these paths are ignored.
89  /// To expect all files to be accounted for from the module.modulemap
90  /// file directory on down, leave this empty.)
91  /// \param CommandLine Compile command line arguments.
92  /// \param ModuleMap The module map to check.
93  /// \returns Initialized CoverageChecker object.
95  llvm::StringRef ModuleMapPath, std::vector<std::string> &IncludePaths,
96  llvm::ArrayRef<std::string> CommandLine,
97  clang::ModuleMap *ModuleMap);
98 
99  /// Do checks.
100  /// Starting from the directory of the module.modulemap file,
101  /// Find all header files, optionally looking only at files
102  /// covered by the include path options, and compare against
103  /// the headers referenced by the module.modulemap file.
104  /// Display warnings for unaccounted-for header files.
105  /// \returns 0 if there were no errors or warnings, 1 if there
106  /// were warnings, 2 if any other problem, such as a bad
107  /// module map path argument was specified.
108  std::error_code doChecks();
109 
110  // The following functions are called by doChecks.
111 
112  /// Collect module headers.
113  /// Walks the modules and collects referenced headers into
114  /// ModuleMapHeadersSet.
115  void collectModuleHeaders();
116 
117  /// Collect referenced headers from one module.
118  /// Collects the headers referenced in the given module into
119  /// ModuleMapHeadersSet.
120  /// \param Mod The module reference.
121  /// \return True if no errors.
122  bool collectModuleHeaders(const clang::Module &Mod);
123 
124  /// Collect headers from an umbrella directory.
125  /// \param UmbrellaDirName The umbrella directory name.
126  /// \return True if no errors.
127  bool collectUmbrellaHeaders(llvm::StringRef UmbrellaDirName);
128 
129  /// Collect headers rferenced from an umbrella file.
130  /// \param UmbrellaHeaderName The umbrella file path.
131  /// \return True if no errors.
132  bool collectUmbrellaHeaderHeaders(llvm::StringRef UmbrellaHeaderName);
133 
134  /// Called from CoverageCheckerCallbacks to track a header included
135  /// from an umbrella header.
136  /// \param HeaderName The header file path.
137  void collectUmbrellaHeaderHeader(llvm::StringRef HeaderName);
138 
139  /// Collect file system header files.
140  /// This function scans the file system for header files,
141  /// starting at the directory of the module.modulemap file,
142  /// optionally filtering out all but the files covered by
143  /// the include path options.
144  /// \returns True if no errors.
146 
147  /// Collect file system header files from the given path.
148  /// This function scans the file system for header files,
149  /// starting at the given directory, which is assumed to be
150  /// relative to the directory of the module.modulemap file.
151  /// \returns True if no errors.
152  bool collectFileSystemHeaders(llvm::StringRef IncludePath);
153 
154  /// Find headers unaccounted-for in module map.
155  /// This function compares the list of collected header files
156  /// against those referenced in the module map. Display
157  /// warnings for unaccounted-for header files.
158  /// Save unaccounted-for file list for possible.
159  /// fixing action.
161 };
162 
163 } // end namespace Modularize
164 
165 #endif // COVERAGECHECKER_H
bool collectUmbrellaHeaders(llvm::StringRef UmbrellaDirName)
Collect headers from an umbrella directory.
CoverageChecker(llvm::StringRef ModuleMapPath, std::vector< std::string > &IncludePaths, llvm::ArrayRef< std::string > CommandLine, clang::ModuleMap *ModuleMap)
Constructor.
void collectModuleHeaders()
Collect module headers.
void collectUmbrellaHeaderHeader(llvm::StringRef HeaderName)
Called from CoverageCheckerCallbacks to track a header included from an umbrella header.
void findUnaccountedForHeaders()
Find headers unaccounted-for in module map.
bool collectUmbrellaHeaderHeaders(llvm::StringRef UmbrellaHeaderName)
Collect headers rferenced from an umbrella file.
std::error_code doChecks()
Do checks.
bool collectFileSystemHeaders()
Collect file system header files.
static CoverageChecker * createCoverageChecker(llvm::StringRef ModuleMapPath, std::vector< std::string > &IncludePaths, llvm::ArrayRef< std::string > CommandLine, clang::ModuleMap *ModuleMap)
Create instance of CoverageChecker.
Module map checker class.