xref: /llvm-project/clang-tools-extra/modularize/ModularizeUtilities.h (revision a171d248ca34b8b6f8de11d42a83ad981285963a)
1d845baecSJohn Thompson //=====-- ModularizeUtilities.h - Utilities for modularize -*- C++ -*-======//
2d845baecSJohn Thompson //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d845baecSJohn Thompson //
7d845baecSJohn Thompson //===--------------------------------------------------------------------===//
8d845baecSJohn Thompson ///
9d845baecSJohn Thompson /// \file
10282dc72cSDmitri Gribenko /// ModularizeUtilities class definition.
11d845baecSJohn Thompson ///
12d845baecSJohn Thompson //===--------------------------------------------------------------------===//
13d845baecSJohn Thompson 
14d845baecSJohn Thompson #ifndef MODULARIZEUTILITIES_H
15d845baecSJohn Thompson #define MODULARIZEUTILITIES_H
16d845baecSJohn Thompson 
17d845baecSJohn Thompson #include "Modularize.h"
189cb79646SJohn Thompson #include "clang/Basic/Diagnostic.h"
199cb79646SJohn Thompson #include "clang/Basic/FileManager.h"
209cb79646SJohn Thompson #include "clang/Basic/LangOptions.h"
219cb79646SJohn Thompson #include "clang/Basic/TargetInfo.h"
229cb79646SJohn Thompson #include "clang/Basic/TargetOptions.h"
239cb79646SJohn Thompson #include "clang/Frontend/TextDiagnosticPrinter.h"
249cb79646SJohn Thompson #include "clang/Lex/HeaderSearch.h"
259cb79646SJohn Thompson #include "clang/Lex/HeaderSearchOptions.h"
269cb79646SJohn Thompson #include "clang/Lex/ModuleMap.h"
279cb79646SJohn Thompson #include "clang/Lex/Preprocessor.h"
289cb79646SJohn Thompson #include "llvm/ADT/SmallVector.h"
299cb79646SJohn Thompson #include "llvm/ADT/StringSet.h"
30d845baecSJohn Thompson #include <string>
31d845baecSJohn Thompson #include <vector>
32d845baecSJohn Thompson 
33d845baecSJohn Thompson namespace Modularize {
34d845baecSJohn Thompson 
35d845baecSJohn Thompson /// Modularize utilities class.
36d845baecSJohn Thompson /// Support functions and data for modularize.
37d845baecSJohn Thompson class ModularizeUtilities {
38d845baecSJohn Thompson public:
39d845baecSJohn Thompson   // Input arguments.
40d845baecSJohn Thompson 
41d845baecSJohn Thompson   /// The input file paths.
42d845baecSJohn Thompson   std::vector<std::string> InputFilePaths;
43d845baecSJohn Thompson   /// The header prefix.
44d845baecSJohn Thompson   llvm::StringRef HeaderPrefix;
454018c624SJohn Thompson   /// The path of problem files list file.
464018c624SJohn Thompson   llvm::StringRef ProblemFilesPath;
47d845baecSJohn Thompson 
48d845baecSJohn Thompson   // Output data.
49d845baecSJohn Thompson 
509cb79646SJohn Thompson   /// List of top-level header files.
51d845baecSJohn Thompson   llvm::SmallVector<std::string, 32> HeaderFileNames;
529cb79646SJohn Thompson   /// Map of top-level header file dependencies.
53d845baecSJohn Thompson   DependencyMap Dependencies;
548eb8d936SJohn Thompson   /// True if we have module maps.
558eb8d936SJohn Thompson   bool HasModuleMap;
5696f5551bSJohn Thompson   /// Missing header count.
5796f5551bSJohn Thompson   int MissingHeaderCount;
584018c624SJohn Thompson   /// List of header files with no problems during the first pass,
594018c624SJohn Thompson   /// that is, no compile errors.
604018c624SJohn Thompson   llvm::SmallVector<std::string, 32> GoodFileNames;
614018c624SJohn Thompson   /// List of header files with problems.
624018c624SJohn Thompson   llvm::SmallVector<std::string, 32> ProblemFileNames;
63d845baecSJohn Thompson 
64d845baecSJohn Thompson   // Functions.
65d845baecSJohn Thompson 
66d845baecSJohn Thompson   /// Constructor.
67d845baecSJohn Thompson   /// You can use the static createModularizeUtilities to create an instance
68d845baecSJohn Thompson   /// of this object.
69d845baecSJohn Thompson   /// \param InputPaths The input file paths.
70d845baecSJohn Thompson   /// \param Prefix The headear path prefix.
714018c624SJohn Thompson   /// \param ProblemFilesListPath The problem header list path.
72d845baecSJohn Thompson   ModularizeUtilities(std::vector<std::string> &InputPaths,
734018c624SJohn Thompson                       llvm::StringRef Prefix,
744018c624SJohn Thompson                       llvm::StringRef ProblemFilesListPath);
75d845baecSJohn Thompson 
76d845baecSJohn Thompson   /// Create instance of ModularizeUtilities.
77d845baecSJohn Thompson   /// \param InputPaths The input file paths.
78d845baecSJohn Thompson   /// \param Prefix The headear path prefix.
794018c624SJohn Thompson   /// \param ProblemFilesListPath The problem header list path.
80d845baecSJohn Thompson   /// \returns Initialized ModularizeUtilities object.
81d845baecSJohn Thompson   static ModularizeUtilities *createModularizeUtilities(
82d845baecSJohn Thompson       std::vector<std::string> &InputPaths,
834018c624SJohn Thompson       llvm::StringRef Prefix,
844018c624SJohn Thompson       llvm::StringRef ProblemFilesListPath);
85d845baecSJohn Thompson 
86d845baecSJohn Thompson   /// Load header list and dependencies.
87d845baecSJohn Thompson   /// \returns std::error_code.
88d845baecSJohn Thompson   std::error_code loadAllHeaderListsAndDependencies();
89d845baecSJohn Thompson 
908eb8d936SJohn Thompson   /// Do coverage checks.
918eb8d936SJohn Thompson   /// For each loaded module map, do header coverage check.
92*a171d248SMichael Spencer   /// Starting from the directory of the module.modulemap file,
938eb8d936SJohn Thompson   /// Find all header files, optionally looking only at files
948eb8d936SJohn Thompson   /// covered by the include path options, and compare against
95*a171d248SMichael Spencer   /// the headers referenced by the module.modulemap file.
968eb8d936SJohn Thompson   /// Display warnings for unaccounted-for header files.
978eb8d936SJohn Thompson   /// \param IncludePaths The include paths to check for files.
988eb8d936SJohn Thompson   ///   (Note that other directories above these paths are ignored.
998eb8d936SJohn Thompson   ///   To expect all files to be accounted for from the module.modulemap
1008eb8d936SJohn Thompson   ///   file directory on down, leave this empty.)
1018eb8d936SJohn Thompson   /// \param CommandLine Compile command line arguments.
1028eb8d936SJohn Thompson   /// \returns 0 if there were no errors or warnings, 1 if there
1038eb8d936SJohn Thompson   ///   were warnings, 2 if any other problem, such as a bad
1048eb8d936SJohn Thompson   ///   module map path argument was specified.
1058eb8d936SJohn Thompson   std::error_code doCoverageCheck(std::vector<std::string> &IncludePaths,
1068eb8d936SJohn Thompson                                   llvm::ArrayRef<std::string> CommandLine);
1078eb8d936SJohn Thompson 
1084018c624SJohn Thompson   /// Add unique problem file.
1094018c624SJohn Thompson   /// Also standardizes the path.
1104018c624SJohn Thompson   /// \param FilePath Problem file path.
1114018c624SJohn Thompson   void addUniqueProblemFile(std::string FilePath);
1124018c624SJohn Thompson 
1134018c624SJohn Thompson   /// Add file with no compile errors.
1144018c624SJohn Thompson   /// Also standardizes the path.
1154018c624SJohn Thompson   /// \param FilePath Problem file path.
1164018c624SJohn Thompson   void addNoCompileErrorsFile(std::string FilePath);
1174018c624SJohn Thompson 
1184018c624SJohn Thompson   /// List problem files.
1194018c624SJohn Thompson   void displayProblemFiles();
1204018c624SJohn Thompson 
1214018c624SJohn Thompson   /// List files with no problems.
1224018c624SJohn Thompson   void displayGoodFiles();
1234018c624SJohn Thompson 
1244018c624SJohn Thompson   /// List files with problem files commented out.
1254018c624SJohn Thompson   void displayCombinedFiles();
1264018c624SJohn Thompson 
1279cb79646SJohn Thompson   // Internal.
1289cb79646SJohn Thompson 
129d845baecSJohn Thompson protected:
1309cb79646SJohn Thompson 
131d845baecSJohn Thompson   /// Load single header list and dependencies.
132d845baecSJohn Thompson   /// \param InputPath The input file path.
133d845baecSJohn Thompson   /// \returns std::error_code.
134d845baecSJohn Thompson   std::error_code loadSingleHeaderListsAndDependencies(
135d845baecSJohn Thompson       llvm::StringRef InputPath);
1363dcb3934SJohn Thompson 
1374018c624SJohn Thompson   /// Load problem header list.
1384018c624SJohn Thompson   /// \param InputPath The input file path.
1394018c624SJohn Thompson   /// \returns std::error_code.
1404018c624SJohn Thompson   std::error_code loadProblemHeaderList(
1414018c624SJohn Thompson     llvm::StringRef InputPath);
1424018c624SJohn Thompson 
1439cb79646SJohn Thompson   /// Load single module map and extract header file list.
1449cb79646SJohn Thompson   /// \param InputPath The input file path.
1459cb79646SJohn Thompson   /// \returns std::error_code.
1469cb79646SJohn Thompson   std::error_code loadModuleMap(
1479cb79646SJohn Thompson     llvm::StringRef InputPath);
1489cb79646SJohn Thompson 
1499cb79646SJohn Thompson   /// Collect module Map headers.
1509cb79646SJohn Thompson   /// Walks the modules and collects referenced headers into
1513c9fb522SJohn Thompson   /// HeaderFileNames.
1529cb79646SJohn Thompson   /// \param ModMap A loaded module map object.
1539cb79646SJohn Thompson   /// \return True if no errors.
1549cb79646SJohn Thompson   bool collectModuleMapHeaders(clang::ModuleMap *ModMap);
1559cb79646SJohn Thompson 
1569cb79646SJohn Thompson   /// Collect referenced headers from one module.
1579cb79646SJohn Thompson   /// Collects the headers referenced in the given module into
1583c9fb522SJohn Thompson   /// HeaderFileNames.
1599cb79646SJohn Thompson   /// \param Mod The module reference.
1609cb79646SJohn Thompson   /// \return True if no errors.
1619cb79646SJohn Thompson   bool collectModuleHeaders(const clang::Module &Mod);
1629cb79646SJohn Thompson 
1639cb79646SJohn Thompson   /// Collect headers from an umbrella directory.
1649cb79646SJohn Thompson   /// \param UmbrellaDirName The umbrella directory name.
1659cb79646SJohn Thompson   /// \return True if no errors.
1669cb79646SJohn Thompson   bool collectUmbrellaHeaders(llvm::StringRef UmbrellaDirName,
1679cb79646SJohn Thompson     DependentsVector &Dependents);
1689cb79646SJohn Thompson 
1693dcb3934SJohn Thompson public:
1703dcb3934SJohn Thompson 
1713dcb3934SJohn Thompson   // Utility functions.
1723dcb3934SJohn Thompson 
1733dcb3934SJohn Thompson   /// Convert header path to canonical form.
1743dcb3934SJohn Thompson   /// The canonical form is basically just use forward slashes,
1753dcb3934SJohn Thompson   /// and remove "./".
1763dcb3934SJohn Thompson   /// \param FilePath The file path.
1773dcb3934SJohn Thompson   /// \returns The file path in canonical form.
1783dcb3934SJohn Thompson   static std::string getCanonicalPath(llvm::StringRef FilePath);
1799cb79646SJohn Thompson 
1809cb79646SJohn Thompson   /// Check for header file extension.
1819cb79646SJohn Thompson   /// If the file extension is .h, .inc, or missing, it's
1829cb79646SJohn Thompson   /// assumed to be a header.
1839cb79646SJohn Thompson   /// \param FileName The file name.  Must not be a directory.
1849cb79646SJohn Thompson   /// \returns true if it has a header extension or no extension.
1859cb79646SJohn Thompson   static bool isHeader(llvm::StringRef FileName);
1869cb79646SJohn Thompson 
1878eb8d936SJohn Thompson   /// Get directory path component from file path.
1888eb8d936SJohn Thompson   /// \returns the component of the given path, which will be
1898eb8d936SJohn Thompson   /// relative if the given path is relative, absolute if the
1908eb8d936SJohn Thompson   /// given path is absolute, or "." if the path has no leading
1918eb8d936SJohn Thompson   /// path component.
1928eb8d936SJohn Thompson   static std::string getDirectoryFromPath(llvm::StringRef Path);
1938eb8d936SJohn Thompson 
1949cb79646SJohn Thompson   // Internal data.
1959cb79646SJohn Thompson 
1969cb79646SJohn Thompson   /// Options controlling the language variant.
1979cb79646SJohn Thompson   std::shared_ptr<clang::LangOptions> LangOpts;
1989cb79646SJohn Thompson   /// Diagnostic IDs.
1999cb79646SJohn Thompson   const llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagIDs;
2009cb79646SJohn Thompson   /// Options controlling the diagnostic engine.
2019cb79646SJohn Thompson   llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagnosticOpts;
2029cb79646SJohn Thompson   /// Diagnostic consumer.
2039cb79646SJohn Thompson   clang::TextDiagnosticPrinter DC;
2049cb79646SJohn Thompson   /// Diagnostic engine.
2059cb79646SJohn Thompson   llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> Diagnostics;
2069cb79646SJohn Thompson   /// Options controlling the target.
2079cb79646SJohn Thompson   std::shared_ptr<clang::TargetOptions> TargetOpts;
2089cb79646SJohn Thompson   /// Target information.
2099cb79646SJohn Thompson   llvm::IntrusiveRefCntPtr<clang::TargetInfo> Target;
2109cb79646SJohn Thompson   /// Options controlling the file system manager.
2119cb79646SJohn Thompson   clang::FileSystemOptions FileSystemOpts;
2129cb79646SJohn Thompson   /// File system manager.
2139cb79646SJohn Thompson   llvm::IntrusiveRefCntPtr<clang::FileManager> FileMgr;
2149cb79646SJohn Thompson   /// Source manager.
2159cb79646SJohn Thompson   llvm::IntrusiveRefCntPtr<clang::SourceManager> SourceMgr;
2169cb79646SJohn Thompson   /// Header search manager.
2179cb79646SJohn Thompson   std::unique_ptr<clang::HeaderSearch> HeaderInfo;
2189cb79646SJohn Thompson   // The loaded module map objects.
2199cb79646SJohn Thompson   std::vector<std::unique_ptr<clang::ModuleMap>> ModuleMaps;
220d845baecSJohn Thompson };
221d845baecSJohn Thompson 
222d845baecSJohn Thompson } // end namespace Modularize
223d845baecSJohn Thompson 
224d845baecSJohn Thompson #endif // MODULARIZEUTILITIES_H
225