blob: 31f76d09ea82e2077d462e9531babdb112b2769e [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;
53
54 // Functions.
55
56 /// Constructor.
57 /// You can use the static createModularizeUtilities to create an instance
58 /// of this object.
59 /// \param InputPaths The input file paths.
60 /// \param Prefix The headear path prefix.
61 ModularizeUtilities(std::vector<std::string> &InputPaths,
62 llvm::StringRef Prefix);
63
64 /// Create instance of ModularizeUtilities.
65 /// \param InputPaths The input file paths.
66 /// \param Prefix The headear path prefix.
67 /// \returns Initialized ModularizeUtilities object.
68 static ModularizeUtilities *createModularizeUtilities(
69 std::vector<std::string> &InputPaths,
70 llvm::StringRef Prefix);
71
72 /// Load header list and dependencies.
73 /// \returns std::error_code.
74 std::error_code loadAllHeaderListsAndDependencies();
75
John Thompson9cb79642015-02-18 16:14:32 +000076 // Internal.
77
John Thompsond845bae2015-02-13 14:29:22 +000078protected:
John Thompson9cb79642015-02-18 16:14:32 +000079
John Thompsond845bae2015-02-13 14:29:22 +000080 /// Load single header list and dependencies.
81 /// \param InputPath The input file path.
82 /// \returns std::error_code.
83 std::error_code loadSingleHeaderListsAndDependencies(
84 llvm::StringRef InputPath);
John Thompson3dcb3932015-02-17 20:43:47 +000085
John Thompson9cb79642015-02-18 16:14:32 +000086 /// Load single module map and extract header file list.
87 /// \param InputPath The input file path.
88 /// \returns std::error_code.
89 std::error_code loadModuleMap(
90 llvm::StringRef InputPath);
91
92 /// Collect module Map headers.
93 /// Walks the modules and collects referenced headers into
John Thompson3c9fb522015-02-19 14:31:48 +000094 /// HeaderFileNames.
John Thompson9cb79642015-02-18 16:14:32 +000095 /// \param ModMap A loaded module map object.
96 /// \return True if no errors.
97 bool collectModuleMapHeaders(clang::ModuleMap *ModMap);
98
99 /// Collect referenced headers from one module.
100 /// Collects the headers referenced in the given module into
John Thompson3c9fb522015-02-19 14:31:48 +0000101 /// HeaderFileNames.
John Thompson9cb79642015-02-18 16:14:32 +0000102 /// \param Mod The module reference.
103 /// \return True if no errors.
104 bool collectModuleHeaders(const clang::Module &Mod);
105
106 /// Collect headers from an umbrella directory.
107 /// \param UmbrellaDirName The umbrella directory name.
108 /// \return True if no errors.
109 bool collectUmbrellaHeaders(llvm::StringRef UmbrellaDirName,
110 DependentsVector &Dependents);
111
John Thompson3dcb3932015-02-17 20:43:47 +0000112public:
113
114 // Utility functions.
115
116 /// Convert header path to canonical form.
117 /// The canonical form is basically just use forward slashes,
118 /// and remove "./".
119 /// \param FilePath The file path.
120 /// \returns The file path in canonical form.
121 static std::string getCanonicalPath(llvm::StringRef FilePath);
John Thompson9cb79642015-02-18 16:14:32 +0000122
123 /// Check for header file extension.
124 /// If the file extension is .h, .inc, or missing, it's
125 /// assumed to be a header.
126 /// \param FileName The file name. Must not be a directory.
127 /// \returns true if it has a header extension or no extension.
128 static bool isHeader(llvm::StringRef FileName);
129
130 // Internal data.
131
132 /// Options controlling the language variant.
133 std::shared_ptr<clang::LangOptions> LangOpts;
134 /// Diagnostic IDs.
135 const llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagIDs;
136 /// Options controlling the diagnostic engine.
137 llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagnosticOpts;
138 /// Diagnostic consumer.
139 clang::TextDiagnosticPrinter DC;
140 /// Diagnostic engine.
141 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> Diagnostics;
142 /// Options controlling the target.
143 std::shared_ptr<clang::TargetOptions> TargetOpts;
144 /// Target information.
145 llvm::IntrusiveRefCntPtr<clang::TargetInfo> Target;
146 /// Options controlling the file system manager.
147 clang::FileSystemOptions FileSystemOpts;
148 /// File system manager.
149 llvm::IntrusiveRefCntPtr<clang::FileManager> FileMgr;
150 /// Source manager.
151 llvm::IntrusiveRefCntPtr<clang::SourceManager> SourceMgr;
152 /// Options controlling the \#include directive.
153 llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions> HeaderSearchOpts;
154 /// Header search manager.
155 std::unique_ptr<clang::HeaderSearch> HeaderInfo;
156 // The loaded module map objects.
157 std::vector<std::unique_ptr<clang::ModuleMap>> ModuleMaps;
John Thompsond845bae2015-02-13 14:29:22 +0000158};
159
160} // end namespace Modularize
161
162#endif // MODULARIZEUTILITIES_H