blob: 9c1ee74107d8610797f0b588b29a35ee79467d4a [file] [log] [blame]
John Thompsond845bae2015-02-13 14:29:22 +00001//=====-- ModularizeUtilities.h - Utilities for modularize -*- 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 ModularizeUtilities class definition.
12///
13//===--------------------------------------------------------------------===//
14
15#ifndef MODULARIZEUTILITIES_H
16#define MODULARIZEUTILITIES_H
17
18#include "Modularize.h"
John Thompson9cb79642015-02-18 16:14:32 +000019#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/FileManager.h"
21#include "clang/Basic/LangOptions.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Basic/TargetOptions.h"
24#include "clang/Frontend/TextDiagnosticPrinter.h"
25#include "clang/Lex/HeaderSearch.h"
26#include "clang/Lex/HeaderSearchOptions.h"
27#include "clang/Lex/ModuleMap.h"
28#include "clang/Lex/Preprocessor.h"
29#include "llvm/ADT/SmallVector.h"
30#include "llvm/ADT/StringSet.h"
John Thompsond845bae2015-02-13 14:29:22 +000031#include <string>
32#include <vector>
33
34namespace Modularize {
35
36/// Modularize utilities class.
37/// Support functions and data for modularize.
38class ModularizeUtilities {
39public:
40 // Input arguments.
41
42 /// The input file paths.
43 std::vector<std::string> InputFilePaths;
44 /// The header prefix.
45 llvm::StringRef HeaderPrefix;
46
47 // Output data.
48
John Thompson9cb79642015-02-18 16:14:32 +000049 /// List of top-level header files.
John Thompsond845bae2015-02-13 14:29:22 +000050 llvm::SmallVector<std::string, 32> HeaderFileNames;
John Thompson9cb79642015-02-18 16:14:32 +000051 /// Map of top-level header file dependencies.
John Thompsond845bae2015-02-13 14:29:22 +000052 DependencyMap Dependencies;
John Thompson8eb8d932015-02-19 16:47:27 +000053 /// True if we have module maps.
54 bool HasModuleMap;
John Thompson96f55512015-06-04 23:35:19 +000055 /// Missing header count.
56 int MissingHeaderCount;
John Thompsond845bae2015-02-13 14:29:22 +000057
58 // Functions.
59
60 /// Constructor.
61 /// You can use the static createModularizeUtilities to create an instance
62 /// of this object.
63 /// \param InputPaths The input file paths.
64 /// \param Prefix The headear path prefix.
65 ModularizeUtilities(std::vector<std::string> &InputPaths,
66 llvm::StringRef Prefix);
67
68 /// Create instance of ModularizeUtilities.
69 /// \param InputPaths The input file paths.
70 /// \param Prefix The headear path prefix.
71 /// \returns Initialized ModularizeUtilities object.
72 static ModularizeUtilities *createModularizeUtilities(
73 std::vector<std::string> &InputPaths,
John Thompson8eb8d932015-02-19 16:47:27 +000074 llvm::StringRef Prefix);
John Thompsond845bae2015-02-13 14:29:22 +000075
76 /// Load header list and dependencies.
77 /// \returns std::error_code.
78 std::error_code loadAllHeaderListsAndDependencies();
79
John Thompson8eb8d932015-02-19 16:47:27 +000080 /// Do coverage checks.
81 /// For each loaded module map, do header coverage check.
82 /// Starting from the directory of the module.map file,
83 /// Find all header files, optionally looking only at files
84 /// covered by the include path options, and compare against
85 /// the headers referenced by the module.map file.
86 /// Display warnings for unaccounted-for header files.
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 /// \returns 0 if there were no errors or warnings, 1 if there
93 /// were warnings, 2 if any other problem, such as a bad
94 /// module map path argument was specified.
95 std::error_code doCoverageCheck(std::vector<std::string> &IncludePaths,
96 llvm::ArrayRef<std::string> CommandLine);
97
John Thompson9cb79642015-02-18 16:14:32 +000098 // Internal.
99
John Thompsond845bae2015-02-13 14:29:22 +0000100protected:
John Thompson9cb79642015-02-18 16:14:32 +0000101
John Thompsond845bae2015-02-13 14:29:22 +0000102 /// Load single header list and dependencies.
103 /// \param InputPath The input file path.
104 /// \returns std::error_code.
105 std::error_code loadSingleHeaderListsAndDependencies(
106 llvm::StringRef InputPath);
John Thompson3dcb3932015-02-17 20:43:47 +0000107
John Thompson9cb79642015-02-18 16:14:32 +0000108 /// Load single module map and extract header file list.
109 /// \param InputPath The input file path.
110 /// \returns std::error_code.
111 std::error_code loadModuleMap(
112 llvm::StringRef InputPath);
113
114 /// Collect module Map headers.
115 /// Walks the modules and collects referenced headers into
John Thompson3c9fb522015-02-19 14:31:48 +0000116 /// HeaderFileNames.
John Thompson9cb79642015-02-18 16:14:32 +0000117 /// \param ModMap A loaded module map object.
118 /// \return True if no errors.
119 bool collectModuleMapHeaders(clang::ModuleMap *ModMap);
120
121 /// Collect referenced headers from one module.
122 /// Collects the headers referenced in the given module into
John Thompson3c9fb522015-02-19 14:31:48 +0000123 /// HeaderFileNames.
John Thompson9cb79642015-02-18 16:14:32 +0000124 /// \param Mod The module reference.
125 /// \return True if no errors.
126 bool collectModuleHeaders(const clang::Module &Mod);
127
128 /// Collect headers from an umbrella directory.
129 /// \param UmbrellaDirName The umbrella directory name.
130 /// \return True if no errors.
131 bool collectUmbrellaHeaders(llvm::StringRef UmbrellaDirName,
132 DependentsVector &Dependents);
133
John Thompson3dcb3932015-02-17 20:43:47 +0000134public:
135
136 // Utility functions.
137
138 /// Convert header path to canonical form.
139 /// The canonical form is basically just use forward slashes,
140 /// and remove "./".
141 /// \param FilePath The file path.
142 /// \returns The file path in canonical form.
143 static std::string getCanonicalPath(llvm::StringRef FilePath);
John Thompson9cb79642015-02-18 16:14:32 +0000144
145 /// Check for header file extension.
146 /// If the file extension is .h, .inc, or missing, it's
147 /// assumed to be a header.
148 /// \param FileName The file name. Must not be a directory.
149 /// \returns true if it has a header extension or no extension.
150 static bool isHeader(llvm::StringRef FileName);
151
John Thompson8eb8d932015-02-19 16:47:27 +0000152 /// Get directory path component from file path.
153 /// \returns the component of the given path, which will be
154 /// relative if the given path is relative, absolute if the
155 /// given path is absolute, or "." if the path has no leading
156 /// path component.
157 static std::string getDirectoryFromPath(llvm::StringRef Path);
158
John Thompson9cb79642015-02-18 16:14:32 +0000159 // Internal data.
160
161 /// Options controlling the language variant.
162 std::shared_ptr<clang::LangOptions> LangOpts;
163 /// Diagnostic IDs.
164 const llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagIDs;
165 /// Options controlling the diagnostic engine.
166 llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagnosticOpts;
167 /// Diagnostic consumer.
168 clang::TextDiagnosticPrinter DC;
169 /// Diagnostic engine.
170 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> Diagnostics;
171 /// Options controlling the target.
172 std::shared_ptr<clang::TargetOptions> TargetOpts;
173 /// Target information.
174 llvm::IntrusiveRefCntPtr<clang::TargetInfo> Target;
175 /// Options controlling the file system manager.
176 clang::FileSystemOptions FileSystemOpts;
177 /// File system manager.
178 llvm::IntrusiveRefCntPtr<clang::FileManager> FileMgr;
179 /// Source manager.
180 llvm::IntrusiveRefCntPtr<clang::SourceManager> SourceMgr;
181 /// Options controlling the \#include directive.
182 llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions> HeaderSearchOpts;
183 /// Header search manager.
184 std::unique_ptr<clang::HeaderSearch> HeaderInfo;
185 // The loaded module map objects.
186 std::vector<std::unique_ptr<clang::ModuleMap>> ModuleMaps;
John Thompsond845bae2015-02-13 14:29:22 +0000187};
188
189} // end namespace Modularize
190
191#endif // MODULARIZEUTILITIES_H