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