John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 1 | //===--- extra/modularize/ModularizeUtilities.cpp -------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements a class for loading and validating a module map or |
| 10 | // header list by checking that all headers in the corresponding directories |
| 11 | // are accounted for. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceManager.h" |
| 16 | #include "clang/Driver/Options.h" |
| 17 | #include "clang/Frontend/CompilerInstance.h" |
| 18 | #include "clang/Frontend/FrontendActions.h" |
John Thompson | 8eb8d93 | 2015-02-19 16:47:27 +0000 | [diff] [blame] | 19 | #include "CoverageChecker.h" |
Nick Lewycky | d07cfbd | 2015-02-20 07:05:56 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
| 21 | #include "llvm/Support/FileUtilities.h" |
| 22 | #include "llvm/Support/MemoryBuffer.h" |
| 23 | #include "llvm/Support/Path.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
John Thompson | 8eb8d93 | 2015-02-19 16:47:27 +0000 | [diff] [blame] | 25 | #include "ModularizeUtilities.h" |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 26 | |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 27 | using namespace clang; |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | using namespace Modularize; |
| 30 | |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 31 | namespace { |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 32 | // Subclass TargetOptions so we can construct it inline with |
| 33 | // the minimal option, the triple. |
| 34 | class ModuleMapTargetOptions : public clang::TargetOptions { |
| 35 | public: |
| 36 | ModuleMapTargetOptions() { Triple = llvm::sys::getDefaultTargetTriple(); } |
| 37 | }; |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 38 | } // namespace |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 39 | |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 40 | // ModularizeUtilities class implementation. |
| 41 | |
| 42 | // Constructor. |
| 43 | ModularizeUtilities::ModularizeUtilities(std::vector<std::string> &InputPaths, |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 44 | llvm::StringRef Prefix, |
| 45 | llvm::StringRef ProblemFilesListPath) |
David Blaikie | 201f0f5 | 2017-01-06 01:09:06 +0000 | [diff] [blame] | 46 | : InputFilePaths(InputPaths), HeaderPrefix(Prefix), |
| 47 | ProblemFilesPath(ProblemFilesListPath), HasModuleMap(false), |
| 48 | MissingHeaderCount(0), |
| 49 | // Init clang stuff needed for loading the module map and preprocessing. |
| 50 | LangOpts(new LangOptions()), DiagIDs(new DiagnosticIDs()), |
| 51 | DiagnosticOpts(new DiagnosticOptions()), |
| 52 | DC(llvm::errs(), DiagnosticOpts.get()), |
| 53 | Diagnostics( |
| 54 | new DiagnosticsEngine(DiagIDs, DiagnosticOpts.get(), &DC, false)), |
| 55 | TargetOpts(new ModuleMapTargetOptions()), |
| 56 | Target(TargetInfo::CreateTargetInfo(*Diagnostics, TargetOpts)), |
| 57 | FileMgr(new FileManager(FileSystemOpts)), |
| 58 | SourceMgr(new SourceManager(*Diagnostics, *FileMgr, false)), |
| 59 | HeaderInfo(new HeaderSearch(std::make_shared<HeaderSearchOptions>(), |
| 60 | *SourceMgr, *Diagnostics, *LangOpts, |
| 61 | Target.get())) {} |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 62 | |
| 63 | // Create instance of ModularizeUtilities, to simplify setting up |
| 64 | // subordinate objects. |
| 65 | ModularizeUtilities *ModularizeUtilities::createModularizeUtilities( |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 66 | std::vector<std::string> &InputPaths, llvm::StringRef Prefix, |
| 67 | llvm::StringRef ProblemFilesListPath) { |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 68 | |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 69 | return new ModularizeUtilities(InputPaths, Prefix, ProblemFilesListPath); |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // Load all header lists and dependencies. |
| 73 | std::error_code ModularizeUtilities::loadAllHeaderListsAndDependencies() { |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 74 | // For each input file. |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 75 | for (auto I = InputFilePaths.begin(), E = InputFilePaths.end(); I != E; ++I) { |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 76 | llvm::StringRef InputPath = *I; |
| 77 | // If it's a module map. |
| 78 | if (InputPath.endswith(".modulemap")) { |
| 79 | // Load the module map. |
| 80 | if (std::error_code EC = loadModuleMap(InputPath)) |
| 81 | return EC; |
| 82 | } |
| 83 | else { |
| 84 | // Else we assume it's a header list and load it. |
| 85 | if (std::error_code EC = loadSingleHeaderListsAndDependencies(InputPath)) { |
| 86 | errs() << "modularize: error: Unable to get header list '" << InputPath |
| 87 | << "': " << EC.message() << '\n'; |
| 88 | return EC; |
| 89 | } |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 92 | // If we have a problem files list. |
| 93 | if (ProblemFilesPath.size() != 0) { |
| 94 | // Load problem files list. |
| 95 | if (std::error_code EC = loadProblemHeaderList(ProblemFilesPath)) { |
| 96 | errs() << "modularize: error: Unable to get problem header list '" << ProblemFilesPath |
| 97 | << "': " << EC.message() << '\n'; |
| 98 | return EC; |
| 99 | } |
| 100 | } |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 101 | return std::error_code(); |
| 102 | } |
John Thompson | 8eb8d93 | 2015-02-19 16:47:27 +0000 | [diff] [blame] | 103 | |
| 104 | // Do coverage checks. |
| 105 | // For each loaded module map, do header coverage check. |
| 106 | // Starting from the directory of the module.map file, |
| 107 | // Find all header files, optionally looking only at files |
| 108 | // covered by the include path options, and compare against |
| 109 | // the headers referenced by the module.map file. |
| 110 | // Display warnings for unaccounted-for header files. |
| 111 | // Returns 0 if there were no errors or warnings, 1 if there |
| 112 | // were warnings, 2 if any other problem, such as a bad |
| 113 | // module map path argument was specified. |
| 114 | std::error_code ModularizeUtilities::doCoverageCheck( |
| 115 | std::vector<std::string> &IncludePaths, |
| 116 | llvm::ArrayRef<std::string> CommandLine) { |
| 117 | int ModuleMapCount = ModuleMaps.size(); |
| 118 | int ModuleMapIndex; |
| 119 | std::error_code EC; |
| 120 | for (ModuleMapIndex = 0; ModuleMapIndex < ModuleMapCount; ++ModuleMapIndex) { |
| 121 | std::unique_ptr<clang::ModuleMap> &ModMap = ModuleMaps[ModuleMapIndex]; |
David Blaikie | a67cf00 | 2017-02-11 05:25:21 +0000 | [diff] [blame] | 122 | auto Checker = CoverageChecker::createCoverageChecker( |
| 123 | InputFilePaths[ModuleMapIndex], IncludePaths, CommandLine, |
| 124 | ModMap.get()); |
John Thompson | 8eb8d93 | 2015-02-19 16:47:27 +0000 | [diff] [blame] | 125 | std::error_code LocalEC = Checker->doChecks(); |
| 126 | if (LocalEC.value() > 0) |
| 127 | EC = LocalEC; |
| 128 | } |
| 129 | return EC; |
| 130 | } |
| 131 | |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 132 | // Load single header list and dependencies. |
| 133 | std::error_code ModularizeUtilities::loadSingleHeaderListsAndDependencies( |
| 134 | llvm::StringRef InputPath) { |
| 135 | |
| 136 | // By default, use the path component of the list file name. |
| 137 | SmallString<256> HeaderDirectory(InputPath); |
| 138 | llvm::sys::path::remove_filename(HeaderDirectory); |
| 139 | SmallString<256> CurrentDirectory; |
| 140 | llvm::sys::fs::current_path(CurrentDirectory); |
| 141 | |
| 142 | // Get the prefix if we have one. |
| 143 | if (HeaderPrefix.size() != 0) |
| 144 | HeaderDirectory = HeaderPrefix; |
| 145 | |
| 146 | // Read the header list file into a buffer. |
| 147 | ErrorOr<std::unique_ptr<MemoryBuffer>> listBuffer = |
| 148 | MemoryBuffer::getFile(InputPath); |
| 149 | if (std::error_code EC = listBuffer.getError()) |
| 150 | return EC; |
| 151 | |
| 152 | // Parse the header list into strings. |
| 153 | SmallVector<StringRef, 32> Strings; |
| 154 | listBuffer.get()->getBuffer().split(Strings, "\n", -1, false); |
| 155 | |
| 156 | // Collect the header file names from the string list. |
| 157 | for (SmallVectorImpl<StringRef>::iterator I = Strings.begin(), |
| 158 | E = Strings.end(); |
| 159 | I != E; ++I) { |
| 160 | StringRef Line = I->trim(); |
| 161 | // Ignore comments and empty lines. |
| 162 | if (Line.empty() || (Line[0] == '#')) |
| 163 | continue; |
| 164 | std::pair<StringRef, StringRef> TargetAndDependents = Line.split(':'); |
| 165 | SmallString<256> HeaderFileName; |
| 166 | // Prepend header file name prefix if it's not absolute. |
| 167 | if (llvm::sys::path::is_absolute(TargetAndDependents.first)) |
| 168 | llvm::sys::path::native(TargetAndDependents.first, HeaderFileName); |
| 169 | else { |
| 170 | if (HeaderDirectory.size() != 0) |
| 171 | HeaderFileName = HeaderDirectory; |
| 172 | else |
| 173 | HeaderFileName = CurrentDirectory; |
| 174 | llvm::sys::path::append(HeaderFileName, TargetAndDependents.first); |
| 175 | llvm::sys::path::native(HeaderFileName); |
| 176 | } |
| 177 | // Handle optional dependencies. |
| 178 | DependentsVector Dependents; |
| 179 | SmallVector<StringRef, 4> DependentsList; |
| 180 | TargetAndDependents.second.split(DependentsList, " ", -1, false); |
| 181 | int Count = DependentsList.size(); |
| 182 | for (int Index = 0; Index < Count; ++Index) { |
| 183 | SmallString<256> Dependent; |
| 184 | if (llvm::sys::path::is_absolute(DependentsList[Index])) |
| 185 | Dependent = DependentsList[Index]; |
| 186 | else { |
| 187 | if (HeaderDirectory.size() != 0) |
| 188 | Dependent = HeaderDirectory; |
| 189 | else |
| 190 | Dependent = CurrentDirectory; |
| 191 | llvm::sys::path::append(Dependent, DependentsList[Index]); |
| 192 | } |
| 193 | llvm::sys::path::native(Dependent); |
John Thompson | 3dcb393 | 2015-02-17 20:43:47 +0000 | [diff] [blame] | 194 | Dependents.push_back(getCanonicalPath(Dependent.str())); |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 195 | } |
John Thompson | 3dcb393 | 2015-02-17 20:43:47 +0000 | [diff] [blame] | 196 | // Get canonical form. |
| 197 | HeaderFileName = getCanonicalPath(HeaderFileName); |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 198 | // Save the resulting header file path and dependencies. |
| 199 | HeaderFileNames.push_back(HeaderFileName.str()); |
| 200 | Dependencies[HeaderFileName.str()] = Dependents; |
| 201 | } |
| 202 | return std::error_code(); |
| 203 | } |
John Thompson | 3dcb393 | 2015-02-17 20:43:47 +0000 | [diff] [blame] | 204 | |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 205 | // Load problem header list. |
| 206 | std::error_code ModularizeUtilities::loadProblemHeaderList( |
| 207 | llvm::StringRef InputPath) { |
| 208 | |
| 209 | // By default, use the path component of the list file name. |
| 210 | SmallString<256> HeaderDirectory(InputPath); |
| 211 | llvm::sys::path::remove_filename(HeaderDirectory); |
| 212 | SmallString<256> CurrentDirectory; |
| 213 | llvm::sys::fs::current_path(CurrentDirectory); |
| 214 | |
| 215 | // Get the prefix if we have one. |
| 216 | if (HeaderPrefix.size() != 0) |
| 217 | HeaderDirectory = HeaderPrefix; |
| 218 | |
| 219 | // Read the header list file into a buffer. |
| 220 | ErrorOr<std::unique_ptr<MemoryBuffer>> listBuffer = |
| 221 | MemoryBuffer::getFile(InputPath); |
| 222 | if (std::error_code EC = listBuffer.getError()) |
| 223 | return EC; |
| 224 | |
| 225 | // Parse the header list into strings. |
| 226 | SmallVector<StringRef, 32> Strings; |
| 227 | listBuffer.get()->getBuffer().split(Strings, "\n", -1, false); |
| 228 | |
| 229 | // Collect the header file names from the string list. |
| 230 | for (SmallVectorImpl<StringRef>::iterator I = Strings.begin(), |
| 231 | E = Strings.end(); |
| 232 | I != E; ++I) { |
| 233 | StringRef Line = I->trim(); |
| 234 | // Ignore comments and empty lines. |
| 235 | if (Line.empty() || (Line[0] == '#')) |
| 236 | continue; |
| 237 | SmallString<256> HeaderFileName; |
| 238 | // Prepend header file name prefix if it's not absolute. |
| 239 | if (llvm::sys::path::is_absolute(Line)) |
| 240 | llvm::sys::path::native(Line, HeaderFileName); |
| 241 | else { |
| 242 | if (HeaderDirectory.size() != 0) |
| 243 | HeaderFileName = HeaderDirectory; |
| 244 | else |
| 245 | HeaderFileName = CurrentDirectory; |
| 246 | llvm::sys::path::append(HeaderFileName, Line); |
| 247 | llvm::sys::path::native(HeaderFileName); |
| 248 | } |
| 249 | // Get canonical form. |
| 250 | HeaderFileName = getCanonicalPath(HeaderFileName); |
| 251 | // Save the resulting header file path. |
| 252 | ProblemFileNames.push_back(HeaderFileName.str()); |
| 253 | } |
| 254 | return std::error_code(); |
| 255 | } |
| 256 | |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 257 | // Load single module map and extract header file list. |
| 258 | std::error_code ModularizeUtilities::loadModuleMap( |
| 259 | llvm::StringRef InputPath) { |
| 260 | // Get file entry for module.modulemap file. |
| 261 | const FileEntry *ModuleMapEntry = |
| 262 | SourceMgr->getFileManager().getFile(InputPath); |
| 263 | |
| 264 | // return error if not found. |
| 265 | if (!ModuleMapEntry) { |
| 266 | llvm::errs() << "error: File \"" << InputPath << "\" not found.\n"; |
| 267 | return std::error_code(1, std::generic_category()); |
| 268 | } |
| 269 | |
| 270 | // Because the module map parser uses a ForwardingDiagnosticConsumer, |
| 271 | // which doesn't forward the BeginSourceFile call, we do it explicitly here. |
| 272 | DC.BeginSourceFile(*LangOpts, nullptr); |
| 273 | |
| 274 | // Figure out the home directory for the module map file. |
| 275 | const DirectoryEntry *Dir = ModuleMapEntry->getDir(); |
| 276 | StringRef DirName(Dir->getName()); |
| 277 | if (llvm::sys::path::filename(DirName) == "Modules") { |
| 278 | DirName = llvm::sys::path::parent_path(DirName); |
| 279 | if (DirName.endswith(".framework")) |
| 280 | Dir = FileMgr->getDirectory(DirName); |
| 281 | // FIXME: This assert can fail if there's a race between the above check |
| 282 | // and the removal of the directory. |
| 283 | assert(Dir && "parent must exist"); |
| 284 | } |
| 285 | |
| 286 | std::unique_ptr<ModuleMap> ModMap; |
| 287 | ModMap.reset(new ModuleMap(*SourceMgr, *Diagnostics, *LangOpts, |
| 288 | Target.get(), *HeaderInfo)); |
| 289 | |
| 290 | // Parse module.modulemap file into module map. |
| 291 | if (ModMap->parseModuleMapFile(ModuleMapEntry, false, Dir)) { |
| 292 | return std::error_code(1, std::generic_category()); |
| 293 | } |
| 294 | |
| 295 | // Do matching end call. |
| 296 | DC.EndSourceFile(); |
| 297 | |
John Thompson | 96f5551 | 2015-06-04 23:35:19 +0000 | [diff] [blame] | 298 | // Reset missing header count. |
| 299 | MissingHeaderCount = 0; |
| 300 | |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 301 | if (!collectModuleMapHeaders(ModMap.get())) |
| 302 | return std::error_code(1, std::generic_category()); |
| 303 | |
| 304 | // Save module map. |
| 305 | ModuleMaps.push_back(std::move(ModMap)); |
| 306 | |
John Thompson | 8eb8d93 | 2015-02-19 16:47:27 +0000 | [diff] [blame] | 307 | // Indicate we are using module maps. |
| 308 | HasModuleMap = true; |
| 309 | |
John Thompson | 96f5551 | 2015-06-04 23:35:19 +0000 | [diff] [blame] | 310 | // Return code of 1 for missing headers. |
| 311 | if (MissingHeaderCount) |
| 312 | return std::error_code(1, std::generic_category()); |
| 313 | |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 314 | return std::error_code(); |
| 315 | } |
| 316 | |
| 317 | // Collect module map headers. |
| 318 | // Walks the modules and collects referenced headers into |
John Thompson | 3c9fb52 | 2015-02-19 14:31:48 +0000 | [diff] [blame] | 319 | // HeaderFileNames. |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 320 | bool ModularizeUtilities::collectModuleMapHeaders(clang::ModuleMap *ModMap) { |
| 321 | for (ModuleMap::module_iterator I = ModMap->module_begin(), |
| 322 | E = ModMap->module_end(); |
| 323 | I != E; ++I) { |
| 324 | if (!collectModuleHeaders(*I->second)) |
| 325 | return false; |
| 326 | } |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | // Collect referenced headers from one module. |
| 331 | // Collects the headers referenced in the given module into |
John Thompson | 3c9fb52 | 2015-02-19 14:31:48 +0000 | [diff] [blame] | 332 | // HeaderFileNames. |
Paul Robinson | f868d0b | 2016-02-05 23:20:02 +0000 | [diff] [blame] | 333 | bool ModularizeUtilities::collectModuleHeaders(const clang::Module &Mod) { |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 334 | |
| 335 | // Ignore explicit modules because they often have dependencies |
| 336 | // we can't know. |
| 337 | if (Mod.IsExplicit) |
| 338 | return true; |
| 339 | |
| 340 | // Treat headers in umbrella directory as dependencies. |
| 341 | DependentsVector UmbrellaDependents; |
| 342 | |
| 343 | // Recursively do submodules. |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 344 | for (auto MI = Mod.submodule_begin(), MIEnd = Mod.submodule_end(); |
| 345 | MI != MIEnd; ++MI) |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 346 | collectModuleHeaders(**MI); |
| 347 | |
Richard Smith | 9d5ae21 | 2015-05-16 03:10:31 +0000 | [diff] [blame] | 348 | if (const FileEntry *UmbrellaHeader = Mod.getUmbrellaHeader().Entry) { |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 349 | std::string HeaderPath = getCanonicalPath(UmbrellaHeader->getName()); |
| 350 | // Collect umbrella header. |
| 351 | HeaderFileNames.push_back(HeaderPath); |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 352 | |
| 353 | // FUTURE: When needed, umbrella header header collection goes here. |
| 354 | } |
Richard Smith | 9d5ae21 | 2015-05-16 03:10:31 +0000 | [diff] [blame] | 355 | else if (const DirectoryEntry *UmbrellaDir = Mod.getUmbrellaDir().Entry) { |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 356 | // If there normal headers, assume these are umbrellas and skip collection. |
| 357 | if (Mod.Headers->size() == 0) { |
| 358 | // Collect headers in umbrella directory. |
| 359 | if (!collectUmbrellaHeaders(UmbrellaDir->getName(), UmbrellaDependents)) |
| 360 | return false; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // We ignore HK_Private, HK_Textual, HK_PrivateTextual, and HK_Excluded, |
| 365 | // assuming they are marked as such either because of unsuitability for |
| 366 | // modules or because they are meant to be included by another header, |
| 367 | // and thus should be ignored by modularize. |
| 368 | |
| 369 | int NormalHeaderCount = Mod.Headers[clang::Module::HK_Normal].size(); |
| 370 | |
| 371 | for (int Index = 0; Index < NormalHeaderCount; ++Index) { |
| 372 | DependentsVector NormalDependents; |
| 373 | // Collect normal header. |
| 374 | const clang::Module::Header &Header( |
| 375 | Mod.Headers[clang::Module::HK_Normal][Index]); |
| 376 | std::string HeaderPath = getCanonicalPath(Header.Entry->getName()); |
| 377 | HeaderFileNames.push_back(HeaderPath); |
| 378 | } |
| 379 | |
John Thompson | 96f5551 | 2015-06-04 23:35:19 +0000 | [diff] [blame] | 380 | int MissingCountThisModule = Mod.MissingHeaders.size(); |
| 381 | |
| 382 | for (int Index = 0; Index < MissingCountThisModule; ++Index) { |
| 383 | std::string MissingFile = Mod.MissingHeaders[Index].FileName; |
| 384 | SourceLocation Loc = Mod.MissingHeaders[Index].FileNameLoc; |
| 385 | errs() << Loc.printToString(*SourceMgr) |
| 386 | << ": error : Header not found: " << MissingFile << "\n"; |
| 387 | } |
| 388 | |
| 389 | MissingHeaderCount += MissingCountThisModule; |
| 390 | |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 391 | return true; |
| 392 | } |
| 393 | |
| 394 | // Collect headers from an umbrella directory. |
| 395 | bool ModularizeUtilities::collectUmbrellaHeaders(StringRef UmbrellaDirName, |
| 396 | DependentsVector &Dependents) { |
| 397 | // Initialize directory name. |
| 398 | SmallString<256> Directory(UmbrellaDirName); |
| 399 | // Walk the directory. |
| 400 | std::error_code EC; |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 401 | for (llvm::sys::fs::directory_iterator I(Directory.str(), EC), E; I != E; |
| 402 | I.increment(EC)) { |
| 403 | if (EC) |
| 404 | return false; |
| 405 | std::string File(I->path()); |
Peter Collingbourne | 0dfdb44 | 2017-10-10 22:19:46 +0000 | [diff] [blame] | 406 | llvm::ErrorOr<llvm::sys::fs::basic_file_status> Status = I->status(); |
| 407 | if (!Status) |
| 408 | return false; |
| 409 | llvm::sys::fs::file_type Type = Status->type(); |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 410 | // If the file is a directory, ignore the name and recurse. |
| 411 | if (Type == llvm::sys::fs::file_type::directory_file) { |
| 412 | if (!collectUmbrellaHeaders(File, Dependents)) |
| 413 | return false; |
| 414 | continue; |
| 415 | } |
| 416 | // If the file does not have a common header extension, ignore it. |
| 417 | if (!isHeader(File)) |
| 418 | continue; |
| 419 | // Save header name. |
| 420 | std::string HeaderPath = getCanonicalPath(File); |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 421 | Dependents.push_back(HeaderPath); |
| 422 | } |
| 423 | return true; |
| 424 | } |
John Thompson | 84ced5c | 2015-03-06 00:39:42 +0000 | [diff] [blame] | 425 | |
| 426 | // Replace .. embedded in path for purposes of having |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 427 | // a canonical path. |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 428 | static std::string replaceDotDot(StringRef Path) { |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 429 | SmallString<128> Buffer; |
| 430 | llvm::sys::path::const_iterator B = llvm::sys::path::begin(Path), |
| 431 | E = llvm::sys::path::end(Path); |
| 432 | while (B != E) { |
| 433 | if (B->compare(".") == 0) { |
| 434 | } |
| 435 | else if (B->compare("..") == 0) |
| 436 | llvm::sys::path::remove_filename(Buffer); |
| 437 | else |
| 438 | llvm::sys::path::append(Buffer, *B); |
| 439 | ++B; |
| 440 | } |
| 441 | if (Path.endswith("/") || Path.endswith("\\")) |
| 442 | Buffer.append(1, Path.back()); |
| 443 | return Buffer.c_str(); |
| 444 | } |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 445 | |
John Thompson | 3dcb393 | 2015-02-17 20:43:47 +0000 | [diff] [blame] | 446 | // Convert header path to canonical form. |
| 447 | // The canonical form is basically just use forward slashes, and remove "./". |
| 448 | // \param FilePath The file path, relative to the module map directory. |
| 449 | // \returns The file path in canonical form. |
| 450 | std::string ModularizeUtilities::getCanonicalPath(StringRef FilePath) { |
John Thompson | 84ced5c | 2015-03-06 00:39:42 +0000 | [diff] [blame] | 451 | std::string Tmp(replaceDotDot(FilePath)); |
John Thompson | 3dcb393 | 2015-02-17 20:43:47 +0000 | [diff] [blame] | 452 | std::replace(Tmp.begin(), Tmp.end(), '\\', '/'); |
| 453 | StringRef Tmp2(Tmp); |
| 454 | if (Tmp2.startswith("./")) |
| 455 | Tmp = Tmp2.substr(2); |
| 456 | return Tmp; |
| 457 | } |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 458 | |
| 459 | // Check for header file extension. |
| 460 | // If the file extension is .h, .inc, or missing, it's |
| 461 | // assumed to be a header. |
| 462 | // \param FileName The file name. Must not be a directory. |
| 463 | // \returns true if it has a header extension or no extension. |
| 464 | bool ModularizeUtilities::isHeader(StringRef FileName) { |
| 465 | StringRef Extension = llvm::sys::path::extension(FileName); |
| 466 | if (Extension.size() == 0) |
John Thompson | b3eef01 | 2015-12-04 22:42:18 +0000 | [diff] [blame] | 467 | return true; |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 468 | if (Extension.equals_lower(".h")) |
| 469 | return true; |
| 470 | if (Extension.equals_lower(".inc")) |
| 471 | return true; |
| 472 | return false; |
| 473 | } |
John Thompson | 8eb8d93 | 2015-02-19 16:47:27 +0000 | [diff] [blame] | 474 | |
| 475 | // Get directory path component from file path. |
| 476 | // \returns the component of the given path, which will be |
| 477 | // relative if the given path is relative, absolute if the |
| 478 | // given path is absolute, or "." if the path has no leading |
| 479 | // path component. |
| 480 | std::string ModularizeUtilities::getDirectoryFromPath(StringRef Path) { |
| 481 | SmallString<256> Directory(Path); |
| 482 | sys::path::remove_filename(Directory); |
| 483 | if (Directory.size() == 0) |
| 484 | return "."; |
| 485 | return Directory.str(); |
| 486 | } |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 487 | |
| 488 | // Add unique problem file. |
| 489 | // Also standardizes the path. |
| 490 | void ModularizeUtilities::addUniqueProblemFile(std::string FilePath) { |
| 491 | FilePath = getCanonicalPath(FilePath); |
| 492 | // Don't add if already present. |
| 493 | for(auto &TestFilePath : ProblemFileNames) { |
| 494 | if (TestFilePath == FilePath) |
| 495 | return; |
| 496 | } |
| 497 | ProblemFileNames.push_back(FilePath); |
| 498 | } |
| 499 | |
| 500 | // Add file with no compile errors. |
| 501 | // Also standardizes the path. |
| 502 | void ModularizeUtilities::addNoCompileErrorsFile(std::string FilePath) { |
| 503 | FilePath = getCanonicalPath(FilePath); |
| 504 | GoodFileNames.push_back(FilePath); |
| 505 | } |
| 506 | |
| 507 | // List problem files. |
| 508 | void ModularizeUtilities::displayProblemFiles() { |
| 509 | errs() << "\nThese are the files with possible errors:\n\n"; |
| 510 | for (auto &ProblemFile : ProblemFileNames) { |
| 511 | errs() << ProblemFile << "\n"; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | // List files with no problems. |
| 516 | void ModularizeUtilities::displayGoodFiles() { |
| 517 | errs() << "\nThese are the files with no detected errors:\n\n"; |
| 518 | for (auto &GoodFile : HeaderFileNames) { |
| 519 | bool Good = true; |
| 520 | for (auto &ProblemFile : ProblemFileNames) { |
| 521 | if (ProblemFile == GoodFile) { |
| 522 | Good = false; |
| 523 | break; |
| 524 | } |
| 525 | } |
| 526 | if (Good) |
| 527 | errs() << GoodFile << "\n"; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | // List files with problem files commented out. |
| 532 | void ModularizeUtilities::displayCombinedFiles() { |
| 533 | errs() << |
| 534 | "\nThese are the combined files, with problem files preceded by #:\n\n"; |
| 535 | for (auto &File : HeaderFileNames) { |
| 536 | bool Good = true; |
| 537 | for (auto &ProblemFile : ProblemFileNames) { |
| 538 | if (ProblemFile == File) { |
| 539 | Good = false; |
| 540 | break; |
| 541 | } |
| 542 | } |
| 543 | errs() << (Good ? "" : "#") << File << "\n"; |
| 544 | } |
| 545 | } |