Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1 | //===--- ModuleMap.cpp - Describe the layout of modules ---------*- 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 | // This file defines the ModuleMap implementation, which describes the layout |
| 11 | // of a module as it relates to headers. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "clang/Lex/ModuleMap.h" |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 15 | #include "clang/Basic/CharInfo.h" |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 17 | #include "clang/Basic/DiagnosticOptions.h" |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 18 | #include "clang/Basic/FileManager.h" |
| 19 | #include "clang/Basic/TargetInfo.h" |
| 20 | #include "clang/Basic/TargetOptions.h" |
Argyrios Kyrtzidis | 55ea75b | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 21 | #include "clang/Lex/HeaderSearch.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Lex/LexDiagnostic.h" |
| 23 | #include "clang/Lex/Lexer.h" |
| 24 | #include "clang/Lex/LiteralSupport.h" |
| 25 | #include "llvm/ADT/StringRef.h" |
| 26 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Allocator.h" |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 28 | #include "llvm/Support/FileSystem.h" |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Host.h" |
Rafael Espindola | 8229d22 | 2013-06-11 22:15:02 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Path.h" |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 98cfcbf | 2012-09-27 14:50:15 +0000 | [diff] [blame] | 32 | #include <stdlib.h> |
Douglas Gregor | 3cc6277 | 2013-01-22 23:49:45 +0000 | [diff] [blame] | 33 | #if defined(LLVM_ON_UNIX) |
Dmitri Gribenko | adeb782 | 2013-01-26 16:29:36 +0000 | [diff] [blame] | 34 | #include <limits.h> |
Douglas Gregor | 3cc6277 | 2013-01-22 23:49:45 +0000 | [diff] [blame] | 35 | #endif |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 36 | using namespace clang; |
| 37 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 38 | Module::ExportDecl |
| 39 | ModuleMap::resolveExport(Module *Mod, |
| 40 | const Module::UnresolvedExportDecl &Unresolved, |
Argyrios Kyrtzidis | 0be5e56 | 2013-02-19 19:58:45 +0000 | [diff] [blame] | 41 | bool Complain) const { |
Douglas Gregor | 0adaa88 | 2011-12-05 17:28:06 +0000 | [diff] [blame] | 42 | // We may have just a wildcard. |
| 43 | if (Unresolved.Id.empty()) { |
| 44 | assert(Unresolved.Wildcard && "Invalid unresolved export"); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 45 | return Module::ExportDecl(nullptr, true); |
Douglas Gregor | 0adaa88 | 2011-12-05 17:28:06 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 48 | // Resolve the module-id. |
| 49 | Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain); |
| 50 | if (!Context) |
| 51 | return Module::ExportDecl(); |
| 52 | |
| 53 | return Module::ExportDecl(Context, Unresolved.Wildcard); |
| 54 | } |
| 55 | |
| 56 | Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod, |
| 57 | bool Complain) const { |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 58 | // Find the starting module. |
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 59 | Module *Context = lookupModuleUnqualified(Id[0].first, Mod); |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 60 | if (!Context) { |
| 61 | if (Complain) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 62 | Diags.Report(Id[0].second, diag::err_mmap_missing_module_unqualified) |
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 63 | << Id[0].first << Mod->getFullModuleName(); |
| 64 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 65 | return nullptr; |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // Dig into the module path. |
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 69 | for (unsigned I = 1, N = Id.size(); I != N; ++I) { |
| 70 | Module *Sub = lookupModuleQualified(Id[I].first, Context); |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 71 | if (!Sub) { |
| 72 | if (Complain) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 73 | Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) |
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 74 | << Id[I].first << Context->getFullModuleName() |
| 75 | << SourceRange(Id[0].second, Id[I-1].second); |
| 76 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 77 | return nullptr; |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 78 | } |
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 79 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 80 | Context = Sub; |
| 81 | } |
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 82 | |
| 83 | return Context; |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 86 | ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags, |
Argyrios Kyrtzidis | 55ea75b | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 87 | const LangOptions &LangOpts, const TargetInfo *Target, |
| 88 | HeaderSearch &HeaderInfo) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 89 | : SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 90 | HeaderInfo(HeaderInfo), BuiltinIncludeDir(nullptr), |
| 91 | CompilingModule(nullptr), SourceModule(nullptr) {} |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 92 | |
| 93 | ModuleMap::~ModuleMap() { |
Douglas Gregor | 09fe1bb | 2011-11-17 02:05:44 +0000 | [diff] [blame] | 94 | for (llvm::StringMap<Module *>::iterator I = Modules.begin(), |
| 95 | IEnd = Modules.end(); |
| 96 | I != IEnd; ++I) { |
| 97 | delete I->getValue(); |
| 98 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 101 | void ModuleMap::setTarget(const TargetInfo &Target) { |
| 102 | assert((!this->Target || this->Target == &Target) && |
| 103 | "Improper target override"); |
| 104 | this->Target = &Target; |
| 105 | } |
| 106 | |
Douglas Gregor | 8b48e08 | 2012-10-12 21:15:50 +0000 | [diff] [blame] | 107 | /// \brief "Sanitize" a filename so that it can be used as an identifier. |
| 108 | static StringRef sanitizeFilenameAsIdentifier(StringRef Name, |
| 109 | SmallVectorImpl<char> &Buffer) { |
| 110 | if (Name.empty()) |
| 111 | return Name; |
| 112 | |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 113 | if (!isValidIdentifier(Name)) { |
Douglas Gregor | 8b48e08 | 2012-10-12 21:15:50 +0000 | [diff] [blame] | 114 | // If we don't already have something with the form of an identifier, |
| 115 | // create a buffer with the sanitized name. |
| 116 | Buffer.clear(); |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 117 | if (isDigit(Name[0])) |
Douglas Gregor | 8b48e08 | 2012-10-12 21:15:50 +0000 | [diff] [blame] | 118 | Buffer.push_back('_'); |
| 119 | Buffer.reserve(Buffer.size() + Name.size()); |
| 120 | for (unsigned I = 0, N = Name.size(); I != N; ++I) { |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 121 | if (isIdentifierBody(Name[I])) |
Douglas Gregor | 8b48e08 | 2012-10-12 21:15:50 +0000 | [diff] [blame] | 122 | Buffer.push_back(Name[I]); |
| 123 | else |
| 124 | Buffer.push_back('_'); |
| 125 | } |
| 126 | |
| 127 | Name = StringRef(Buffer.data(), Buffer.size()); |
| 128 | } |
| 129 | |
| 130 | while (llvm::StringSwitch<bool>(Name) |
| 131 | #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true) |
| 132 | #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true) |
| 133 | #include "clang/Basic/TokenKinds.def" |
| 134 | .Default(false)) { |
| 135 | if (Name.data() != Buffer.data()) |
| 136 | Buffer.append(Name.begin(), Name.end()); |
| 137 | Buffer.push_back('_'); |
| 138 | Name = StringRef(Buffer.data(), Buffer.size()); |
| 139 | } |
| 140 | |
| 141 | return Name; |
| 142 | } |
| 143 | |
Douglas Gregor | db3910b | 2013-05-02 17:58:30 +0000 | [diff] [blame] | 144 | /// \brief Determine whether the given file name is the name of a builtin |
| 145 | /// header, supplied by Clang to replace, override, or augment existing system |
| 146 | /// headers. |
| 147 | static bool isBuiltinHeader(StringRef FileName) { |
| 148 | return llvm::StringSwitch<bool>(FileName) |
| 149 | .Case("float.h", true) |
| 150 | .Case("iso646.h", true) |
| 151 | .Case("limits.h", true) |
| 152 | .Case("stdalign.h", true) |
| 153 | .Case("stdarg.h", true) |
| 154 | .Case("stdbool.h", true) |
| 155 | .Case("stddef.h", true) |
| 156 | .Case("stdint.h", true) |
| 157 | .Case("tgmath.h", true) |
| 158 | .Case("unwind.h", true) |
| 159 | .Default(false); |
| 160 | } |
| 161 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 162 | ModuleMap::HeadersMap::iterator |
| 163 | ModuleMap::findKnownHeader(const FileEntry *File) { |
| 164 | HeadersMap::iterator Known = Headers.find(File); |
| 165 | if (Known == Headers.end() && File->getDir() == BuiltinIncludeDir && |
| 166 | isBuiltinHeader(llvm::sys::path::filename(File->getName()))) { |
| 167 | HeaderInfo.loadTopLevelSystemModules(); |
| 168 | return Headers.find(File); |
| 169 | } |
| 170 | return Known; |
| 171 | } |
| 172 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 173 | ModuleMap::KnownHeader |
| 174 | ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File, |
| 175 | SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs) { |
| 176 | const DirectoryEntry *Dir = File->getDir(); |
| 177 | assert(Dir && "file in no directory"); |
| 178 | |
| 179 | // Note: as an egregious but useful hack we use the real path here, because |
| 180 | // frameworks moving from top-level frameworks to embedded frameworks tend |
| 181 | // to be symlinked from the top-level location to the embedded location, |
| 182 | // and we need to resolve lookups as if we had found the embedded location. |
| 183 | StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir); |
| 184 | |
| 185 | // Keep walking up the directory hierarchy, looking for a directory with |
| 186 | // an umbrella header. |
| 187 | do { |
| 188 | auto KnownDir = UmbrellaDirs.find(Dir); |
| 189 | if (KnownDir != UmbrellaDirs.end()) |
| 190 | return KnownHeader(KnownDir->second, NormalHeader); |
| 191 | |
| 192 | IntermediateDirs.push_back(Dir); |
| 193 | |
| 194 | // Retrieve our parent path. |
| 195 | DirName = llvm::sys::path::parent_path(DirName); |
| 196 | if (DirName.empty()) |
| 197 | break; |
| 198 | |
| 199 | // Resolve the parent path to a directory entry. |
| 200 | Dir = SourceMgr.getFileManager().getDirectory(DirName); |
| 201 | } while (Dir); |
| 202 | return KnownHeader(); |
| 203 | } |
| 204 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 205 | // Returns 'true' if 'RequestingModule directly uses 'RequestedModule'. |
| 206 | static bool directlyUses(const Module *RequestingModule, |
| 207 | const Module *RequestedModule) { |
| 208 | return std::find(RequestingModule->DirectUses.begin(), |
| 209 | RequestingModule->DirectUses.end(), |
| 210 | RequestedModule) != RequestingModule->DirectUses.end(); |
| 211 | } |
| 212 | |
| 213 | static bool violatesPrivateInclude(Module *RequestingModule, |
| 214 | const FileEntry *IncFileEnt, |
| 215 | ModuleMap::ModuleHeaderRole Role, |
| 216 | Module *RequestedModule) { |
| 217 | #ifndef NDEBUG |
| 218 | // Check for consistency between the module header role |
| 219 | // as obtained from the lookup and as obtained from the module. |
| 220 | // This check is not cheap, so enable it only for debugging. |
| 221 | SmallVectorImpl<const FileEntry *> &PvtHdrs |
| 222 | = RequestedModule->PrivateHeaders; |
| 223 | SmallVectorImpl<const FileEntry *>::iterator Look |
| 224 | = std::find(PvtHdrs.begin(), PvtHdrs.end(), IncFileEnt); |
| 225 | bool IsPrivate = Look != PvtHdrs.end(); |
| 226 | assert((IsPrivate && Role == ModuleMap::PrivateHeader) |
| 227 | || (!IsPrivate && Role != ModuleMap::PrivateHeader)); |
| 228 | #endif |
| 229 | return Role == ModuleMap::PrivateHeader && |
| 230 | RequestedModule->getTopLevelModule() != RequestingModule; |
| 231 | } |
| 232 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 233 | static Module *getTopLevelOrNull(Module *M) { |
| 234 | return M ? M->getTopLevelModule() : nullptr; |
| 235 | } |
| 236 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 237 | void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule, |
| 238 | SourceLocation FilenameLoc, |
| 239 | StringRef Filename, |
| 240 | const FileEntry *File) { |
| 241 | // No errors for indirect modules. This may be a bit of a problem for modules |
| 242 | // with no source files. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 243 | if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 244 | return; |
| 245 | |
| 246 | if (RequestingModule) |
| 247 | resolveUses(RequestingModule, /*Complain=*/false); |
| 248 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 249 | bool Excluded = false; |
| 250 | Module *Private = nullptr; |
| 251 | Module *NotUsed = nullptr; |
| 252 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 253 | HeadersMap::iterator Known = findKnownHeader(File); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 254 | if (Known != Headers.end()) { |
| 255 | for (const KnownHeader &Header : Known->second) { |
| 256 | // Excluded headers don't really belong to a module. |
| 257 | if (Header.getRole() == ModuleMap::ExcludedHeader) { |
| 258 | Excluded = true; |
| 259 | continue; |
| 260 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 261 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 262 | // If 'File' is part of 'RequestingModule' we can definitely include it. |
| 263 | if (Header.getModule() == RequestingModule) |
| 264 | return; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 265 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 266 | // Remember private headers for later printing of a diagnostic. |
| 267 | if (violatesPrivateInclude(RequestingModule, File, Header.getRole(), |
| 268 | Header.getModule())) { |
| 269 | Private = Header.getModule(); |
| 270 | continue; |
| 271 | } |
| 272 | |
| 273 | // If uses need to be specified explicitly, we are only allowed to return |
| 274 | // modules that are explicitly used by the requesting module. |
| 275 | if (RequestingModule && LangOpts.ModulesDeclUse && |
| 276 | !directlyUses(RequestingModule, Header.getModule())) { |
| 277 | NotUsed = Header.getModule(); |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | // We have found a module that we can happily use. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 282 | return; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 283 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // We have found a header, but it is private. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 287 | if (Private) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 288 | Diags.Report(FilenameLoc, diag::error_use_of_private_header_outside_module) |
| 289 | << Filename; |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | // We have found a module, but we don't use it. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 294 | if (NotUsed) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 295 | Diags.Report(FilenameLoc, diag::error_undeclared_use_of_module) |
| 296 | << RequestingModule->getFullModuleName() << Filename; |
| 297 | return; |
| 298 | } |
| 299 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 300 | if (Excluded || isHeaderInUmbrellaDirs(File)) |
| 301 | return; |
| 302 | |
| 303 | // At this point, only non-modular includes remain. |
| 304 | |
| 305 | if (LangOpts.ModulesStrictDeclUse) { |
| 306 | Diags.Report(FilenameLoc, diag::error_undeclared_use_of_module) |
| 307 | << RequestingModule->getFullModuleName() << Filename; |
| 308 | } else if (RequestingModule) { |
| 309 | diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ? |
| 310 | diag::warn_non_modular_include_in_framework_module : |
| 311 | diag::warn_non_modular_include_in_module; |
| 312 | Diags.Report(FilenameLoc, DiagID) << RequestingModule->getFullModuleName(); |
| 313 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Daniel Jasper | c641709 | 2013-10-22 08:09:47 +0000 | [diff] [blame] | 316 | ModuleMap::KnownHeader |
| 317 | ModuleMap::findModuleForHeader(const FileEntry *File, |
| 318 | Module *RequestingModule) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 319 | HeadersMap::iterator Known = findKnownHeader(File); |
| 320 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 321 | if (Known != Headers.end()) { |
Daniel Jasper | c641709 | 2013-10-22 08:09:47 +0000 | [diff] [blame] | 322 | ModuleMap::KnownHeader Result = KnownHeader(); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 323 | |
Daniel Jasper | c641709 | 2013-10-22 08:09:47 +0000 | [diff] [blame] | 324 | // Iterate over all modules that 'File' is part of to find the best fit. |
| 325 | for (SmallVectorImpl<KnownHeader>::iterator I = Known->second.begin(), |
| 326 | E = Known->second.end(); |
| 327 | I != E; ++I) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 328 | // Cannot use a module if the header is excluded in it. |
| 329 | if (I->getRole() == ModuleMap::ExcludedHeader) |
| 330 | continue; |
| 331 | |
| 332 | // Cannot use a module if it is unavailable. |
| 333 | if (!I->getModule()->isAvailable()) |
Daniel Jasper | c641709 | 2013-10-22 08:09:47 +0000 | [diff] [blame] | 334 | continue; |
| 335 | |
| 336 | // If 'File' is part of 'RequestingModule', 'RequestingModule' is the |
| 337 | // module we are looking for. |
| 338 | if (I->getModule() == RequestingModule) |
| 339 | return *I; |
| 340 | |
| 341 | // If uses need to be specified explicitly, we are only allowed to return |
| 342 | // modules that are explicitly used by the requesting module. |
| 343 | if (RequestingModule && LangOpts.ModulesDeclUse && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 344 | !directlyUses(RequestingModule, I->getModule())) |
Daniel Jasper | c641709 | 2013-10-22 08:09:47 +0000 | [diff] [blame] | 345 | continue; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 346 | |
Daniel Jasper | c641709 | 2013-10-22 08:09:47 +0000 | [diff] [blame] | 347 | Result = *I; |
| 348 | // If 'File' is a public header of this module, this is as good as we |
| 349 | // are going to get. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 350 | // FIXME: If we have a RequestingModule, we should prefer the header from |
| 351 | // that module. |
Daniel Jasper | c641709 | 2013-10-22 08:09:47 +0000 | [diff] [blame] | 352 | if (I->getRole() == ModuleMap::NormalHeader) |
| 353 | break; |
| 354 | } |
| 355 | return Result; |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 356 | } |
Douglas Gregor | db3910b | 2013-05-02 17:58:30 +0000 | [diff] [blame] | 357 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 358 | SmallVector<const DirectoryEntry *, 2> SkippedDirs; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 359 | KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs); |
| 360 | if (H) { |
| 361 | Module *Result = H.getModule(); |
Douglas Gregor | 713b7c0 | 2013-01-26 00:55:12 +0000 | [diff] [blame] | 362 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 363 | // Search up the module stack until we find a module with an umbrella |
| 364 | // directory. |
| 365 | Module *UmbrellaModule = Result; |
| 366 | while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) |
| 367 | UmbrellaModule = UmbrellaModule->Parent; |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 368 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 369 | if (UmbrellaModule->InferSubmodules) { |
| 370 | // Infer submodules for each of the directories we found between |
| 371 | // the directory of the umbrella header and the directory where |
| 372 | // the actual header is located. |
| 373 | bool Explicit = UmbrellaModule->InferExplicitSubmodules; |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 374 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 375 | for (unsigned I = SkippedDirs.size(); I != 0; --I) { |
| 376 | // Find or create the module that corresponds to this directory name. |
Douglas Gregor | 8b48e08 | 2012-10-12 21:15:50 +0000 | [diff] [blame] | 377 | SmallString<32> NameBuf; |
| 378 | StringRef Name = sanitizeFilenameAsIdentifier( |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 379 | llvm::sys::path::stem(SkippedDirs[I-1]->getName()), NameBuf); |
| 380 | Result = findOrCreateModule(Name, Result, UmbrellaModule->ModuleMap, |
| 381 | /*IsFramework=*/false, Explicit).first; |
| 382 | Result->IsInferred = true; |
| 383 | |
| 384 | // Associate the module and the directory. |
| 385 | UmbrellaDirs[SkippedDirs[I-1]] = Result; |
| 386 | |
| 387 | // If inferred submodules export everything they import, add a |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 388 | // wildcard to the set of exports. |
Douglas Gregor | 9f74f4f | 2011-12-06 16:17:15 +0000 | [diff] [blame] | 389 | if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 390 | Result->Exports.push_back(Module::ExportDecl(nullptr, true)); |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 391 | } |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 392 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 393 | // Infer a submodule with the same name as this header file. |
| 394 | SmallString<32> NameBuf; |
| 395 | StringRef Name = sanitizeFilenameAsIdentifier( |
| 396 | llvm::sys::path::stem(File->getName()), NameBuf); |
| 397 | Result = findOrCreateModule(Name, Result, UmbrellaModule->ModuleMap, |
| 398 | /*IsFramework=*/false, Explicit).first; |
| 399 | Result->IsInferred = true; |
| 400 | Result->addTopHeader(File); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 401 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 402 | // If inferred submodules export everything they import, add a |
| 403 | // wildcard to the set of exports. |
| 404 | if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) |
| 405 | Result->Exports.push_back(Module::ExportDecl(nullptr, true)); |
| 406 | } else { |
| 407 | // Record each of the directories we stepped through as being part of |
| 408 | // the module we found, since the umbrella header covers them all. |
| 409 | for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) |
| 410 | UmbrellaDirs[SkippedDirs[I]] = Result; |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 411 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 412 | |
| 413 | Headers[File].push_back(KnownHeader(Result, NormalHeader)); |
| 414 | |
| 415 | // If a header corresponds to an unavailable module, don't report |
| 416 | // that it maps to anything. |
| 417 | if (!Result->isAvailable()) |
| 418 | return KnownHeader(); |
| 419 | |
| 420 | return Headers[File].back(); |
| 421 | } |
Douglas Gregor | adb9799 | 2011-11-16 23:02:25 +0000 | [diff] [blame] | 422 | |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 423 | return KnownHeader(); |
Douglas Gregor | 65f3b5e | 2011-11-11 22:18:48 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Argyrios Kyrtzidis | 0be5e56 | 2013-02-19 19:58:45 +0000 | [diff] [blame] | 426 | bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 427 | return isHeaderUnavailableInModule(Header, nullptr); |
| 428 | } |
| 429 | |
| 430 | bool |
| 431 | ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header, |
| 432 | const Module *RequestingModule) const { |
Argyrios Kyrtzidis | 0be5e56 | 2013-02-19 19:58:45 +0000 | [diff] [blame] | 433 | HeadersMap::const_iterator Known = Headers.find(Header); |
Daniel Jasper | c641709 | 2013-10-22 08:09:47 +0000 | [diff] [blame] | 434 | if (Known != Headers.end()) { |
| 435 | for (SmallVectorImpl<KnownHeader>::const_iterator |
| 436 | I = Known->second.begin(), |
| 437 | E = Known->second.end(); |
| 438 | I != E; ++I) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 439 | if (I->isAvailable() && (!RequestingModule || |
| 440 | I->getModule()->isSubModuleOf(RequestingModule))) |
Daniel Jasper | c641709 | 2013-10-22 08:09:47 +0000 | [diff] [blame] | 441 | return false; |
| 442 | } |
| 443 | return true; |
| 444 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 445 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 446 | const DirectoryEntry *Dir = Header->getDir(); |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 447 | SmallVector<const DirectoryEntry *, 2> SkippedDirs; |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 448 | StringRef DirName = Dir->getName(); |
| 449 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 450 | auto IsUnavailable = [&](const Module *M) { |
| 451 | return !M->isAvailable() && (!RequestingModule || |
| 452 | M->isSubModuleOf(RequestingModule)); |
| 453 | }; |
| 454 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 455 | // Keep walking up the directory hierarchy, looking for a directory with |
| 456 | // an umbrella header. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 457 | do { |
Argyrios Kyrtzidis | 0be5e56 | 2013-02-19 19:58:45 +0000 | [diff] [blame] | 458 | llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 459 | = UmbrellaDirs.find(Dir); |
| 460 | if (KnownDir != UmbrellaDirs.end()) { |
| 461 | Module *Found = KnownDir->second; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 462 | if (IsUnavailable(Found)) |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 463 | return true; |
| 464 | |
| 465 | // Search up the module stack until we find a module with an umbrella |
| 466 | // directory. |
| 467 | Module *UmbrellaModule = Found; |
| 468 | while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) |
| 469 | UmbrellaModule = UmbrellaModule->Parent; |
| 470 | |
| 471 | if (UmbrellaModule->InferSubmodules) { |
| 472 | for (unsigned I = SkippedDirs.size(); I != 0; --I) { |
| 473 | // Find or create the module that corresponds to this directory name. |
Douglas Gregor | 8b48e08 | 2012-10-12 21:15:50 +0000 | [diff] [blame] | 474 | SmallString<32> NameBuf; |
| 475 | StringRef Name = sanitizeFilenameAsIdentifier( |
| 476 | llvm::sys::path::stem(SkippedDirs[I-1]->getName()), |
| 477 | NameBuf); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 478 | Found = lookupModuleQualified(Name, Found); |
| 479 | if (!Found) |
| 480 | return false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 481 | if (IsUnavailable(Found)) |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 482 | return true; |
| 483 | } |
| 484 | |
| 485 | // Infer a submodule with the same name as this header file. |
Douglas Gregor | 8b48e08 | 2012-10-12 21:15:50 +0000 | [diff] [blame] | 486 | SmallString<32> NameBuf; |
| 487 | StringRef Name = sanitizeFilenameAsIdentifier( |
| 488 | llvm::sys::path::stem(Header->getName()), |
| 489 | NameBuf); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 490 | Found = lookupModuleQualified(Name, Found); |
| 491 | if (!Found) |
| 492 | return false; |
| 493 | } |
| 494 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 495 | return IsUnavailable(Found); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | SkippedDirs.push_back(Dir); |
| 499 | |
| 500 | // Retrieve our parent path. |
| 501 | DirName = llvm::sys::path::parent_path(DirName); |
| 502 | if (DirName.empty()) |
| 503 | break; |
| 504 | |
| 505 | // Resolve the parent path to a directory entry. |
Manuel Klimek | ee0cd37 | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 506 | Dir = SourceMgr.getFileManager().getDirectory(DirName); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 507 | } while (Dir); |
| 508 | |
| 509 | return false; |
| 510 | } |
| 511 | |
Argyrios Kyrtzidis | 0be5e56 | 2013-02-19 19:58:45 +0000 | [diff] [blame] | 512 | Module *ModuleMap::findModule(StringRef Name) const { |
| 513 | llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name); |
Douglas Gregor | 484535e | 2011-11-11 23:20:24 +0000 | [diff] [blame] | 514 | if (Known != Modules.end()) |
| 515 | return Known->getValue(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 516 | |
| 517 | return nullptr; |
Douglas Gregor | 484535e | 2011-11-11 23:20:24 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Argyrios Kyrtzidis | 0be5e56 | 2013-02-19 19:58:45 +0000 | [diff] [blame] | 520 | Module *ModuleMap::lookupModuleUnqualified(StringRef Name, |
| 521 | Module *Context) const { |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 522 | for(; Context; Context = Context->Parent) { |
| 523 | if (Module *Sub = lookupModuleQualified(Name, Context)) |
| 524 | return Sub; |
| 525 | } |
| 526 | |
| 527 | return findModule(Name); |
| 528 | } |
| 529 | |
Argyrios Kyrtzidis | 0be5e56 | 2013-02-19 19:58:45 +0000 | [diff] [blame] | 530 | Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{ |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 531 | if (!Context) |
| 532 | return findModule(Name); |
| 533 | |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 534 | return Context->findSubmodule(Name); |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 537 | std::pair<Module *, bool> |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 538 | ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, |
| 539 | const FileEntry *ModuleMap, bool IsFramework, |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 540 | bool IsExplicit) { |
| 541 | // Try to find an existing module with this name. |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 542 | if (Module *Sub = lookupModuleQualified(Name, Parent)) |
| 543 | return std::make_pair(Sub, false); |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 544 | |
| 545 | // Create a new module with this name. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 546 | Module *Result = new Module(Name, SourceLocation(), Parent, ModuleMap, |
| 547 | IsFramework, IsExplicit); |
Daniel Jasper | ddd2dfc | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 548 | if (LangOpts.CurrentModule == Name) { |
| 549 | SourceModule = Result; |
| 550 | SourceModuleName = Name; |
| 551 | } |
Argyrios Kyrtzidis | d3220db | 2013-05-08 23:46:46 +0000 | [diff] [blame] | 552 | if (!Parent) { |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 553 | Modules[Name] = Result; |
Argyrios Kyrtzidis | d3220db | 2013-05-08 23:46:46 +0000 | [diff] [blame] | 554 | if (!LangOpts.CurrentModule.empty() && !CompilingModule && |
| 555 | Name == LangOpts.CurrentModule) { |
| 556 | CompilingModule = Result; |
| 557 | } |
| 558 | } |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 559 | return std::make_pair(Result, true); |
| 560 | } |
| 561 | |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 562 | bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir, |
Argyrios Kyrtzidis | 0be5e56 | 2013-02-19 19:58:45 +0000 | [diff] [blame] | 563 | StringRef Name, bool &IsSystem) const { |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 564 | // Check whether we have already looked into the parent directory |
| 565 | // for a module map. |
Argyrios Kyrtzidis | 0be5e56 | 2013-02-19 19:58:45 +0000 | [diff] [blame] | 566 | llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 567 | inferred = InferredDirectories.find(ParentDir); |
| 568 | if (inferred == InferredDirectories.end()) |
| 569 | return false; |
| 570 | |
| 571 | if (!inferred->second.InferModules) |
| 572 | return false; |
| 573 | |
| 574 | // We're allowed to infer for this directory, but make sure it's okay |
| 575 | // to infer this particular module. |
| 576 | bool canInfer = std::find(inferred->second.ExcludedModules.begin(), |
| 577 | inferred->second.ExcludedModules.end(), |
| 578 | Name) == inferred->second.ExcludedModules.end(); |
| 579 | |
| 580 | if (canInfer && inferred->second.InferSystemModules) |
| 581 | IsSystem = true; |
| 582 | |
| 583 | return canInfer; |
| 584 | } |
| 585 | |
Douglas Gregor | 8767dc2 | 2013-01-14 17:57:51 +0000 | [diff] [blame] | 586 | /// \brief For a framework module, infer the framework against which we |
| 587 | /// should link. |
| 588 | static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir, |
| 589 | FileManager &FileMgr) { |
| 590 | assert(Mod->IsFramework && "Can only infer linking for framework modules"); |
| 591 | assert(!Mod->isSubFramework() && |
| 592 | "Can only infer linking for top-level frameworks"); |
| 593 | |
| 594 | SmallString<128> LibName; |
| 595 | LibName += FrameworkDir->getName(); |
| 596 | llvm::sys::path::append(LibName, Mod->Name); |
| 597 | if (FileMgr.getFile(LibName)) { |
| 598 | Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name, |
| 599 | /*IsFramework=*/true)); |
| 600 | } |
| 601 | } |
| 602 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 603 | Module * |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 604 | ModuleMap::inferFrameworkModule(StringRef ModuleName, |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 605 | const DirectoryEntry *FrameworkDir, |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 606 | bool IsSystem, |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 607 | Module *Parent) { |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 608 | // Check whether we've already found this module. |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 609 | if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) |
| 610 | return Mod; |
| 611 | |
Manuel Klimek | ee0cd37 | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 612 | FileManager &FileMgr = SourceMgr.getFileManager(); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 613 | |
| 614 | // If the framework has a parent path from which we're allowed to infer |
| 615 | // a framework module, do so. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 616 | const FileEntry *ModuleMapFile = nullptr; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 617 | if (!Parent) { |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 618 | // Determine whether we're allowed to infer a module map. |
Douglas Gregor | 713b7c0 | 2013-01-26 00:55:12 +0000 | [diff] [blame] | 619 | |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 620 | // Note: as an egregious but useful hack we use the real path here, because |
| 621 | // we might be looking at an embedded framework that symlinks out to a |
| 622 | // top-level framework, and we need to infer as if we were naming the |
| 623 | // top-level framework. |
Douglas Gregor | 713b7c0 | 2013-01-26 00:55:12 +0000 | [diff] [blame] | 624 | StringRef FrameworkDirName |
Manuel Klimek | ee0cd37 | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 625 | = SourceMgr.getFileManager().getCanonicalName(FrameworkDir); |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 626 | |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 627 | bool canInfer = false; |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 628 | if (llvm::sys::path::has_parent_path(FrameworkDirName)) { |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 629 | // Figure out the parent path. |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 630 | StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 631 | if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) { |
| 632 | // Check whether we have already looked into the parent directory |
| 633 | // for a module map. |
Argyrios Kyrtzidis | 0be5e56 | 2013-02-19 19:58:45 +0000 | [diff] [blame] | 634 | llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 635 | inferred = InferredDirectories.find(ParentDir); |
| 636 | if (inferred == InferredDirectories.end()) { |
| 637 | // We haven't looked here before. Load a module map, if there is |
| 638 | // one. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 639 | bool IsFrameworkDir = Parent.endswith(".framework"); |
| 640 | if (const FileEntry *ModMapFile = |
| 641 | HeaderInfo.lookupModuleMapFile(ParentDir, IsFrameworkDir)) { |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame] | 642 | parseModuleMapFile(ModMapFile, IsSystem); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 643 | inferred = InferredDirectories.find(ParentDir); |
| 644 | } |
| 645 | |
| 646 | if (inferred == InferredDirectories.end()) |
| 647 | inferred = InferredDirectories.insert( |
| 648 | std::make_pair(ParentDir, InferredDirectory())).first; |
| 649 | } |
| 650 | |
| 651 | if (inferred->second.InferModules) { |
| 652 | // We're allowed to infer for this directory, but make sure it's okay |
| 653 | // to infer this particular module. |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 654 | StringRef Name = llvm::sys::path::stem(FrameworkDirName); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 655 | canInfer = std::find(inferred->second.ExcludedModules.begin(), |
| 656 | inferred->second.ExcludedModules.end(), |
| 657 | Name) == inferred->second.ExcludedModules.end(); |
| 658 | |
| 659 | if (inferred->second.InferSystemModules) |
| 660 | IsSystem = true; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 661 | ModuleMapFile = inferred->second.ModuleMapFile; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | // If we're not allowed to infer a framework module, don't. |
| 667 | if (!canInfer) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 668 | return nullptr; |
| 669 | } else |
| 670 | ModuleMapFile = Parent->ModuleMap; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 671 | |
| 672 | |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 673 | // Look for an umbrella header. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 674 | SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); |
Benjamin Kramer | ceb6dc8 | 2013-06-28 16:25:46 +0000 | [diff] [blame] | 675 | llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h"); |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 676 | const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 677 | |
| 678 | // FIXME: If there's no umbrella header, we could probably scan the |
| 679 | // framework to load *everything*. But, it's not clear that this is a good |
| 680 | // idea. |
| 681 | if (!UmbrellaHeader) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 682 | return nullptr; |
| 683 | |
| 684 | Module *Result = new Module(ModuleName, SourceLocation(), Parent, ModuleMapFile, |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 685 | /*IsFramework=*/true, /*IsExplicit=*/false); |
Daniel Jasper | ddd2dfc | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 686 | if (LangOpts.CurrentModule == ModuleName) { |
| 687 | SourceModule = Result; |
| 688 | SourceModuleName = ModuleName; |
| 689 | } |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 690 | if (IsSystem) |
| 691 | Result->IsSystem = IsSystem; |
| 692 | |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 693 | if (!Parent) |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 694 | Modules[ModuleName] = Result; |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 695 | |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 696 | // umbrella header "umbrella-header-name" |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 697 | Result->Umbrella = UmbrellaHeader; |
Daniel Jasper | c641709 | 2013-10-22 08:09:47 +0000 | [diff] [blame] | 698 | Headers[UmbrellaHeader].push_back(KnownHeader(Result, NormalHeader)); |
Douglas Gregor | 3cee31e | 2011-12-12 23:55:05 +0000 | [diff] [blame] | 699 | UmbrellaDirs[UmbrellaHeader->getDir()] = Result; |
Douglas Gregor | 209977c | 2011-12-05 17:40:25 +0000 | [diff] [blame] | 700 | |
| 701 | // export * |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 702 | Result->Exports.push_back(Module::ExportDecl(nullptr, true)); |
| 703 | |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 704 | // module * { export * } |
| 705 | Result->InferSubmodules = true; |
| 706 | Result->InferExportWildcard = true; |
| 707 | |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 708 | // Look for subframeworks. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 709 | std::error_code EC; |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 710 | SmallString<128> SubframeworksDirName |
Douglas Gregor | 52b1ed3 | 2011-12-08 16:13:24 +0000 | [diff] [blame] | 711 | = StringRef(FrameworkDir->getName()); |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 712 | llvm::sys::path::append(SubframeworksDirName, "Frameworks"); |
Benjamin Kramer | 0f599ac | 2013-09-11 11:23:15 +0000 | [diff] [blame] | 713 | llvm::sys::path::native(SubframeworksDirName); |
Douglas Gregor | 52b1ed3 | 2011-12-08 16:13:24 +0000 | [diff] [blame] | 714 | for (llvm::sys::fs::directory_iterator |
Benjamin Kramer | 0f599ac | 2013-09-11 11:23:15 +0000 | [diff] [blame] | 715 | Dir(SubframeworksDirName.str(), EC), DirEnd; |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 716 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
| 717 | if (!StringRef(Dir->path()).endswith(".framework")) |
| 718 | continue; |
Douglas Gregor | 98cfcbf | 2012-09-27 14:50:15 +0000 | [diff] [blame] | 719 | |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 720 | if (const DirectoryEntry *SubframeworkDir |
| 721 | = FileMgr.getDirectory(Dir->path())) { |
Douglas Gregor | 98cfcbf | 2012-09-27 14:50:15 +0000 | [diff] [blame] | 722 | // Note: as an egregious but useful hack, we use the real path here and |
| 723 | // check whether it is actually a subdirectory of the parent directory. |
| 724 | // This will not be the case if the 'subframework' is actually a symlink |
| 725 | // out to a top-level framework. |
Douglas Gregor | 713b7c0 | 2013-01-26 00:55:12 +0000 | [diff] [blame] | 726 | StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir); |
| 727 | bool FoundParent = false; |
| 728 | do { |
| 729 | // Get the parent directory name. |
| 730 | SubframeworkDirName |
| 731 | = llvm::sys::path::parent_path(SubframeworkDirName); |
| 732 | if (SubframeworkDirName.empty()) |
| 733 | break; |
Douglas Gregor | 98cfcbf | 2012-09-27 14:50:15 +0000 | [diff] [blame] | 734 | |
Douglas Gregor | 713b7c0 | 2013-01-26 00:55:12 +0000 | [diff] [blame] | 735 | if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { |
| 736 | FoundParent = true; |
| 737 | break; |
| 738 | } |
| 739 | } while (true); |
Douglas Gregor | 98cfcbf | 2012-09-27 14:50:15 +0000 | [diff] [blame] | 740 | |
Douglas Gregor | 713b7c0 | 2013-01-26 00:55:12 +0000 | [diff] [blame] | 741 | if (!FoundParent) |
| 742 | continue; |
Douglas Gregor | 98cfcbf | 2012-09-27 14:50:15 +0000 | [diff] [blame] | 743 | |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 744 | // FIXME: Do we want to warn about subframeworks without umbrella headers? |
Douglas Gregor | 8b48e08 | 2012-10-12 21:15:50 +0000 | [diff] [blame] | 745 | SmallString<32> NameBuf; |
| 746 | inferFrameworkModule(sanitizeFilenameAsIdentifier( |
| 747 | llvm::sys::path::stem(Dir->path()), NameBuf), |
| 748 | SubframeworkDir, IsSystem, Result); |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
Douglas Gregor | 3a110f7 | 2012-01-13 16:54:27 +0000 | [diff] [blame] | 751 | |
Douglas Gregor | 8767dc2 | 2013-01-14 17:57:51 +0000 | [diff] [blame] | 752 | // If the module is a top-level framework, automatically link against the |
| 753 | // framework. |
| 754 | if (!Result->isSubFramework()) { |
| 755 | inferFrameworkLink(Result, FrameworkDir, FileMgr); |
| 756 | } |
| 757 | |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 758 | return Result; |
| 759 | } |
| 760 | |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 761 | void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ |
Daniel Jasper | c641709 | 2013-10-22 08:09:47 +0000 | [diff] [blame] | 762 | Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader)); |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 763 | Mod->Umbrella = UmbrellaHeader; |
Douglas Gregor | 6a1db48 | 2011-12-09 02:04:43 +0000 | [diff] [blame] | 764 | UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 767 | void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) { |
| 768 | Mod->Umbrella = UmbrellaDir; |
| 769 | UmbrellaDirs[UmbrellaDir] = Mod; |
| 770 | } |
| 771 | |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 772 | void ModuleMap::addHeader(Module *Mod, const FileEntry *Header, |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 773 | ModuleHeaderRole Role) { |
| 774 | if (Role == ExcludedHeader) { |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 775 | Mod->ExcludedHeaders.push_back(Header); |
Argyrios Kyrtzidis | 55ea75b | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 776 | } else { |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 777 | if (Role == PrivateHeader) |
| 778 | Mod->PrivateHeaders.push_back(Header); |
| 779 | else |
| 780 | Mod->NormalHeaders.push_back(Header); |
Argyrios Kyrtzidis | d3220db | 2013-05-08 23:46:46 +0000 | [diff] [blame] | 781 | bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule; |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 782 | HeaderInfo.MarkFileModuleHeader(Header, Role, isCompilingModuleHeader); |
Argyrios Kyrtzidis | 55ea75b | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 783 | } |
Daniel Jasper | c641709 | 2013-10-22 08:09:47 +0000 | [diff] [blame] | 784 | Headers[Header].push_back(KnownHeader(Mod, Role)); |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Douglas Gregor | f9e357d | 2011-11-29 19:06:37 +0000 | [diff] [blame] | 787 | const FileEntry * |
Argyrios Kyrtzidis | 0be5e56 | 2013-02-19 19:58:45 +0000 | [diff] [blame] | 788 | ModuleMap::getContainingModuleMapFile(Module *Module) const { |
Manuel Klimek | ee0cd37 | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 789 | if (Module->DefinitionLoc.isInvalid()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 790 | return nullptr; |
Douglas Gregor | f9e357d | 2011-11-29 19:06:37 +0000 | [diff] [blame] | 791 | |
Manuel Klimek | ee0cd37 | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 792 | return SourceMgr.getFileEntryForID( |
| 793 | SourceMgr.getFileID(Module->DefinitionLoc)); |
Douglas Gregor | f9e357d | 2011-11-29 19:06:37 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 796 | void ModuleMap::dump() { |
| 797 | llvm::errs() << "Modules:"; |
| 798 | for (llvm::StringMap<Module *>::iterator M = Modules.begin(), |
| 799 | MEnd = Modules.end(); |
| 800 | M != MEnd; ++M) |
Douglas Gregor | 804c3bf | 2011-11-29 18:17:59 +0000 | [diff] [blame] | 801 | M->getValue()->print(llvm::errs(), 2); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 802 | |
| 803 | llvm::errs() << "Headers:"; |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 804 | for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 805 | H != HEnd; ++H) { |
Daniel Jasper | c641709 | 2013-10-22 08:09:47 +0000 | [diff] [blame] | 806 | llvm::errs() << " \"" << H->first->getName() << "\" -> "; |
| 807 | for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(), |
| 808 | E = H->second.end(); |
| 809 | I != E; ++I) { |
| 810 | if (I != H->second.begin()) |
| 811 | llvm::errs() << ","; |
| 812 | llvm::errs() << I->getModule()->getFullModuleName(); |
| 813 | } |
| 814 | llvm::errs() << "\n"; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 818 | bool ModuleMap::resolveExports(Module *Mod, bool Complain) { |
| 819 | bool HadError = false; |
| 820 | for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { |
| 821 | Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], |
| 822 | Complain); |
Douglas Gregor | 0adaa88 | 2011-12-05 17:28:06 +0000 | [diff] [blame] | 823 | if (Export.getPointer() || Export.getInt()) |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 824 | Mod->Exports.push_back(Export); |
| 825 | else |
| 826 | HadError = true; |
| 827 | } |
| 828 | Mod->UnresolvedExports.clear(); |
| 829 | return HadError; |
| 830 | } |
| 831 | |
Daniel Jasper | ddd2dfc | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 832 | bool ModuleMap::resolveUses(Module *Mod, bool Complain) { |
| 833 | bool HadError = false; |
| 834 | for (unsigned I = 0, N = Mod->UnresolvedDirectUses.size(); I != N; ++I) { |
| 835 | Module *DirectUse = |
| 836 | resolveModuleId(Mod->UnresolvedDirectUses[I], Mod, Complain); |
| 837 | if (DirectUse) |
| 838 | Mod->DirectUses.push_back(DirectUse); |
| 839 | else |
| 840 | HadError = true; |
| 841 | } |
| 842 | Mod->UnresolvedDirectUses.clear(); |
| 843 | return HadError; |
| 844 | } |
| 845 | |
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 846 | bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) { |
| 847 | bool HadError = false; |
| 848 | for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) { |
| 849 | Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id, |
| 850 | Mod, Complain); |
| 851 | if (!OtherMod) { |
| 852 | HadError = true; |
| 853 | continue; |
| 854 | } |
| 855 | |
| 856 | Module::Conflict Conflict; |
| 857 | Conflict.Other = OtherMod; |
| 858 | Conflict.Message = Mod->UnresolvedConflicts[I].Message; |
| 859 | Mod->Conflicts.push_back(Conflict); |
| 860 | } |
| 861 | Mod->UnresolvedConflicts.clear(); |
| 862 | return HadError; |
| 863 | } |
| 864 | |
Douglas Gregor | 5598868 | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 865 | Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { |
| 866 | if (Loc.isInvalid()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 867 | return nullptr; |
| 868 | |
Douglas Gregor | 5598868 | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 869 | // Use the expansion location to determine which module we're in. |
| 870 | FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); |
| 871 | if (!ExpansionLoc.isFileID()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 872 | return nullptr; |
| 873 | |
Douglas Gregor | 5598868 | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 874 | const SourceManager &SrcMgr = Loc.getManager(); |
| 875 | FileID ExpansionFileID = ExpansionLoc.getFileID(); |
Douglas Gregor | 5598868 | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 876 | |
Douglas Gregor | 303aae9 | 2012-01-06 17:19:32 +0000 | [diff] [blame] | 877 | while (const FileEntry *ExpansionFile |
| 878 | = SrcMgr.getFileEntryForID(ExpansionFileID)) { |
| 879 | // Find the module that owns this header (if any). |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 880 | if (Module *Mod = findModuleForHeader(ExpansionFile).getModule()) |
Douglas Gregor | 303aae9 | 2012-01-06 17:19:32 +0000 | [diff] [blame] | 881 | return Mod; |
| 882 | |
| 883 | // No module owns this header, so look up the inclusion chain to see if |
| 884 | // any included header has an associated module. |
| 885 | SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); |
| 886 | if (IncludeLoc.isInvalid()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 887 | return nullptr; |
| 888 | |
Douglas Gregor | 303aae9 | 2012-01-06 17:19:32 +0000 | [diff] [blame] | 889 | ExpansionFileID = SrcMgr.getFileID(IncludeLoc); |
| 890 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 891 | |
| 892 | return nullptr; |
Douglas Gregor | 5598868 | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 895 | //----------------------------------------------------------------------------// |
| 896 | // Module map file parser |
| 897 | //----------------------------------------------------------------------------// |
| 898 | |
| 899 | namespace clang { |
| 900 | /// \brief A token in a module map file. |
| 901 | struct MMToken { |
| 902 | enum TokenKind { |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 903 | Comma, |
Douglas Gregor | 63a7268 | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 904 | ConfigMacros, |
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 905 | Conflict, |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 906 | EndOfFile, |
| 907 | HeaderKeyword, |
| 908 | Identifier, |
Richard Smith | 5794b53 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 909 | Exclaim, |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 910 | ExcludeKeyword, |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 911 | ExplicitKeyword, |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 912 | ExportKeyword, |
Daniel Jasper | 5f0a352 | 2013-09-11 07:20:44 +0000 | [diff] [blame] | 913 | ExternKeyword, |
Douglas Gregor | a865405 | 2011-11-17 22:09:43 +0000 | [diff] [blame] | 914 | FrameworkKeyword, |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 915 | LinkKeyword, |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 916 | ModuleKeyword, |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 917 | Period, |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 918 | PrivateKeyword, |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 919 | UmbrellaKeyword, |
Daniel Jasper | ddd2dfc | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 920 | UseKeyword, |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 921 | RequiresKeyword, |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 922 | Star, |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 923 | StringLiteral, |
| 924 | LBrace, |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 925 | RBrace, |
| 926 | LSquare, |
| 927 | RSquare |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 928 | } Kind; |
| 929 | |
| 930 | unsigned Location; |
| 931 | unsigned StringLength; |
| 932 | const char *StringData; |
| 933 | |
| 934 | void clear() { |
| 935 | Kind = EndOfFile; |
| 936 | Location = 0; |
| 937 | StringLength = 0; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 938 | StringData = nullptr; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | bool is(TokenKind K) const { return Kind == K; } |
| 942 | |
| 943 | SourceLocation getLocation() const { |
| 944 | return SourceLocation::getFromRawEncoding(Location); |
| 945 | } |
| 946 | |
| 947 | StringRef getString() const { |
| 948 | return StringRef(StringData, StringLength); |
| 949 | } |
| 950 | }; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 951 | |
| 952 | /// \brief The set of attributes that can be attached to a module. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 953 | struct Attributes { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 954 | Attributes() : IsSystem(), IsExternC(), IsExhaustive() { } |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 955 | |
| 956 | /// \brief Whether this is a system module. |
| 957 | unsigned IsSystem : 1; |
Douglas Gregor | 63a7268 | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 958 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 959 | /// \brief Whether this is an extern "C" module. |
| 960 | unsigned IsExternC : 1; |
| 961 | |
Douglas Gregor | 63a7268 | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 962 | /// \brief Whether this is an exhaustive set of configuration macros. |
| 963 | unsigned IsExhaustive : 1; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 964 | }; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 965 | |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 966 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 967 | class ModuleMapParser { |
| 968 | Lexer &L; |
| 969 | SourceManager &SourceMgr; |
Douglas Gregor | 9a022bb | 2012-10-15 16:45:32 +0000 | [diff] [blame] | 970 | |
| 971 | /// \brief Default target information, used only for string literal |
| 972 | /// parsing. |
| 973 | const TargetInfo *Target; |
| 974 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 975 | DiagnosticsEngine &Diags; |
| 976 | ModuleMap ⤅ |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 977 | |
| 978 | /// \brief The current module map file. |
| 979 | const FileEntry *ModuleMapFile; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 980 | |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 981 | /// \brief The directory that this module map resides in. |
| 982 | const DirectoryEntry *Directory; |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 983 | |
| 984 | /// \brief The directory containing Clang-supplied headers. |
| 985 | const DirectoryEntry *BuiltinIncludeDir; |
| 986 | |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame] | 987 | /// \brief Whether this module map is in a system header directory. |
| 988 | bool IsSystem; |
| 989 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 990 | /// \brief Whether an error occurred. |
| 991 | bool HadError; |
Douglas Gregor | 9a022bb | 2012-10-15 16:45:32 +0000 | [diff] [blame] | 992 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 993 | /// \brief Stores string data for the various string literals referenced |
| 994 | /// during parsing. |
| 995 | llvm::BumpPtrAllocator StringData; |
| 996 | |
| 997 | /// \brief The current token. |
| 998 | MMToken Tok; |
| 999 | |
| 1000 | /// \brief The active module. |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 1001 | Module *ActiveModule; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1002 | |
| 1003 | /// \brief Consume the current token and return its location. |
| 1004 | SourceLocation consumeToken(); |
| 1005 | |
| 1006 | /// \brief Skip tokens until we reach the a token with the given kind |
| 1007 | /// (or the end of the file). |
| 1008 | void skipUntil(MMToken::TokenKind K); |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1009 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1010 | typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1011 | bool parseModuleId(ModuleId &Id); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1012 | void parseModuleDecl(); |
Daniel Jasper | 5f0a352 | 2013-09-11 07:20:44 +0000 | [diff] [blame] | 1013 | void parseExternModuleDecl(); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1014 | void parseRequiresDecl(); |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 1015 | void parseHeaderDecl(clang::MMToken::TokenKind, |
| 1016 | SourceLocation LeadingLoc); |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1017 | void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1018 | void parseExportDecl(); |
Daniel Jasper | ddd2dfc | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 1019 | void parseUseDecl(); |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 1020 | void parseLinkDecl(); |
Douglas Gregor | 63a7268 | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1021 | void parseConfigMacros(); |
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 1022 | void parseConflict(); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1023 | void parseInferredModuleDecl(bool Framework, bool Explicit); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1024 | bool parseOptionalAttributes(Attributes &Attrs); |
Douglas Gregor | 6a1db48 | 2011-12-09 02:04:43 +0000 | [diff] [blame] | 1025 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1026 | public: |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1027 | explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, |
Douglas Gregor | 9a022bb | 2012-10-15 16:45:32 +0000 | [diff] [blame] | 1028 | const TargetInfo *Target, |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1029 | DiagnosticsEngine &Diags, |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 1030 | ModuleMap &Map, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1031 | const FileEntry *ModuleMapFile, |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 1032 | const DirectoryEntry *Directory, |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame] | 1033 | const DirectoryEntry *BuiltinIncludeDir, |
| 1034 | bool IsSystem) |
Douglas Gregor | 9a022bb | 2012-10-15 16:45:32 +0000 | [diff] [blame] | 1035 | : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1036 | ModuleMapFile(ModuleMapFile), Directory(Directory), |
| 1037 | BuiltinIncludeDir(BuiltinIncludeDir), IsSystem(IsSystem), |
| 1038 | HadError(false), ActiveModule(nullptr) |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1039 | { |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1040 | Tok.clear(); |
| 1041 | consumeToken(); |
| 1042 | } |
| 1043 | |
| 1044 | bool parseModuleMapFile(); |
| 1045 | }; |
| 1046 | } |
| 1047 | |
| 1048 | SourceLocation ModuleMapParser::consumeToken() { |
| 1049 | retry: |
| 1050 | SourceLocation Result = Tok.getLocation(); |
| 1051 | Tok.clear(); |
| 1052 | |
| 1053 | Token LToken; |
| 1054 | L.LexFromRawLexer(LToken); |
| 1055 | Tok.Location = LToken.getLocation().getRawEncoding(); |
| 1056 | switch (LToken.getKind()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1057 | case tok::raw_identifier: { |
| 1058 | StringRef RI = LToken.getRawIdentifier(); |
| 1059 | Tok.StringData = RI.data(); |
| 1060 | Tok.StringLength = RI.size(); |
| 1061 | Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI) |
Douglas Gregor | 63a7268 | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1062 | .Case("config_macros", MMToken::ConfigMacros) |
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 1063 | .Case("conflict", MMToken::Conflict) |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1064 | .Case("exclude", MMToken::ExcludeKeyword) |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1065 | .Case("explicit", MMToken::ExplicitKeyword) |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1066 | .Case("export", MMToken::ExportKeyword) |
Daniel Jasper | 5f0a352 | 2013-09-11 07:20:44 +0000 | [diff] [blame] | 1067 | .Case("extern", MMToken::ExternKeyword) |
Douglas Gregor | a865405 | 2011-11-17 22:09:43 +0000 | [diff] [blame] | 1068 | .Case("framework", MMToken::FrameworkKeyword) |
Douglas Gregor | 63a7268 | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1069 | .Case("header", MMToken::HeaderKeyword) |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 1070 | .Case("link", MMToken::LinkKeyword) |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1071 | .Case("module", MMToken::ModuleKeyword) |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 1072 | .Case("private", MMToken::PrivateKeyword) |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1073 | .Case("requires", MMToken::RequiresKeyword) |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1074 | .Case("umbrella", MMToken::UmbrellaKeyword) |
Daniel Jasper | ddd2dfc | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 1075 | .Case("use", MMToken::UseKeyword) |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1076 | .Default(MMToken::Identifier); |
| 1077 | break; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1078 | } |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1079 | |
| 1080 | case tok::comma: |
| 1081 | Tok.Kind = MMToken::Comma; |
| 1082 | break; |
| 1083 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1084 | case tok::eof: |
| 1085 | Tok.Kind = MMToken::EndOfFile; |
| 1086 | break; |
| 1087 | |
| 1088 | case tok::l_brace: |
| 1089 | Tok.Kind = MMToken::LBrace; |
| 1090 | break; |
| 1091 | |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1092 | case tok::l_square: |
| 1093 | Tok.Kind = MMToken::LSquare; |
| 1094 | break; |
| 1095 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1096 | case tok::period: |
| 1097 | Tok.Kind = MMToken::Period; |
| 1098 | break; |
| 1099 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1100 | case tok::r_brace: |
| 1101 | Tok.Kind = MMToken::RBrace; |
| 1102 | break; |
| 1103 | |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1104 | case tok::r_square: |
| 1105 | Tok.Kind = MMToken::RSquare; |
| 1106 | break; |
| 1107 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1108 | case tok::star: |
| 1109 | Tok.Kind = MMToken::Star; |
| 1110 | break; |
| 1111 | |
Richard Smith | 5794b53 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 1112 | case tok::exclaim: |
| 1113 | Tok.Kind = MMToken::Exclaim; |
| 1114 | break; |
| 1115 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1116 | case tok::string_literal: { |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 1117 | if (LToken.hasUDSuffix()) { |
| 1118 | Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); |
| 1119 | HadError = true; |
| 1120 | goto retry; |
| 1121 | } |
| 1122 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1123 | // Parse the string literal. |
| 1124 | LangOptions LangOpts; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1125 | StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1126 | if (StringLiteral.hadError) |
| 1127 | goto retry; |
| 1128 | |
| 1129 | // Copy the string literal into our string data allocator. |
| 1130 | unsigned Length = StringLiteral.GetStringLength(); |
| 1131 | char *Saved = StringData.Allocate<char>(Length + 1); |
| 1132 | memcpy(Saved, StringLiteral.GetString().data(), Length); |
| 1133 | Saved[Length] = 0; |
| 1134 | |
| 1135 | // Form the token. |
| 1136 | Tok.Kind = MMToken::StringLiteral; |
| 1137 | Tok.StringData = Saved; |
| 1138 | Tok.StringLength = Length; |
| 1139 | break; |
| 1140 | } |
| 1141 | |
| 1142 | case tok::comment: |
| 1143 | goto retry; |
| 1144 | |
| 1145 | default: |
| 1146 | Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); |
| 1147 | HadError = true; |
| 1148 | goto retry; |
| 1149 | } |
| 1150 | |
| 1151 | return Result; |
| 1152 | } |
| 1153 | |
| 1154 | void ModuleMapParser::skipUntil(MMToken::TokenKind K) { |
| 1155 | unsigned braceDepth = 0; |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1156 | unsigned squareDepth = 0; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1157 | do { |
| 1158 | switch (Tok.Kind) { |
| 1159 | case MMToken::EndOfFile: |
| 1160 | return; |
| 1161 | |
| 1162 | case MMToken::LBrace: |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1163 | if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1164 | return; |
| 1165 | |
| 1166 | ++braceDepth; |
| 1167 | break; |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1168 | |
| 1169 | case MMToken::LSquare: |
| 1170 | if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) |
| 1171 | return; |
| 1172 | |
| 1173 | ++squareDepth; |
| 1174 | break; |
| 1175 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1176 | case MMToken::RBrace: |
| 1177 | if (braceDepth > 0) |
| 1178 | --braceDepth; |
| 1179 | else if (Tok.is(K)) |
| 1180 | return; |
| 1181 | break; |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1182 | |
| 1183 | case MMToken::RSquare: |
| 1184 | if (squareDepth > 0) |
| 1185 | --squareDepth; |
| 1186 | else if (Tok.is(K)) |
| 1187 | return; |
| 1188 | break; |
| 1189 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1190 | default: |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1191 | if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1192 | return; |
| 1193 | break; |
| 1194 | } |
| 1195 | |
| 1196 | consumeToken(); |
| 1197 | } while (true); |
| 1198 | } |
| 1199 | |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1200 | /// \brief Parse a module-id. |
| 1201 | /// |
| 1202 | /// module-id: |
| 1203 | /// identifier |
| 1204 | /// identifier '.' module-id |
| 1205 | /// |
| 1206 | /// \returns true if an error occurred, false otherwise. |
| 1207 | bool ModuleMapParser::parseModuleId(ModuleId &Id) { |
| 1208 | Id.clear(); |
| 1209 | do { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1210 | if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) { |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1211 | Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); |
| 1212 | consumeToken(); |
| 1213 | } else { |
| 1214 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); |
| 1215 | return true; |
| 1216 | } |
| 1217 | |
| 1218 | if (!Tok.is(MMToken::Period)) |
| 1219 | break; |
| 1220 | |
| 1221 | consumeToken(); |
| 1222 | } while (true); |
| 1223 | |
| 1224 | return false; |
| 1225 | } |
| 1226 | |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1227 | namespace { |
| 1228 | /// \brief Enumerates the known attributes. |
| 1229 | enum AttributeKind { |
| 1230 | /// \brief An unknown attribute. |
| 1231 | AT_unknown, |
| 1232 | /// \brief The 'system' attribute. |
Douglas Gregor | 63a7268 | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1233 | AT_system, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1234 | /// \brief The 'extern_c' attribute. |
| 1235 | AT_extern_c, |
Douglas Gregor | 63a7268 | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1236 | /// \brief The 'exhaustive' attribute. |
| 1237 | AT_exhaustive |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1238 | }; |
| 1239 | } |
| 1240 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1241 | /// \brief Parse a module declaration. |
| 1242 | /// |
| 1243 | /// module-declaration: |
Daniel Jasper | 5f0a352 | 2013-09-11 07:20:44 +0000 | [diff] [blame] | 1244 | /// 'extern' 'module' module-id string-literal |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1245 | /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] |
| 1246 | /// { module-member* } |
| 1247 | /// |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1248 | /// module-member: |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1249 | /// requires-declaration |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1250 | /// header-declaration |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1251 | /// submodule-declaration |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1252 | /// export-declaration |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 1253 | /// link-declaration |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1254 | /// |
| 1255 | /// submodule-declaration: |
| 1256 | /// module-declaration |
| 1257 | /// inferred-submodule-declaration |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1258 | void ModuleMapParser::parseModuleDecl() { |
Douglas Gregor | a865405 | 2011-11-17 22:09:43 +0000 | [diff] [blame] | 1259 | assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || |
Daniel Jasper | 5f0a352 | 2013-09-11 07:20:44 +0000 | [diff] [blame] | 1260 | Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword)); |
| 1261 | if (Tok.is(MMToken::ExternKeyword)) { |
| 1262 | parseExternModuleDecl(); |
| 1263 | return; |
| 1264 | } |
| 1265 | |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1266 | // Parse 'explicit' or 'framework' keyword, if present. |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1267 | SourceLocation ExplicitLoc; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1268 | bool Explicit = false; |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1269 | bool Framework = false; |
Douglas Gregor | a865405 | 2011-11-17 22:09:43 +0000 | [diff] [blame] | 1270 | |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1271 | // Parse 'explicit' keyword, if present. |
| 1272 | if (Tok.is(MMToken::ExplicitKeyword)) { |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1273 | ExplicitLoc = consumeToken(); |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1274 | Explicit = true; |
| 1275 | } |
| 1276 | |
| 1277 | // Parse 'framework' keyword, if present. |
Douglas Gregor | a865405 | 2011-11-17 22:09:43 +0000 | [diff] [blame] | 1278 | if (Tok.is(MMToken::FrameworkKeyword)) { |
| 1279 | consumeToken(); |
| 1280 | Framework = true; |
| 1281 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1282 | |
| 1283 | // Parse 'module' keyword. |
| 1284 | if (!Tok.is(MMToken::ModuleKeyword)) { |
Douglas Gregor | e6fb987 | 2011-12-06 19:57:48 +0000 | [diff] [blame] | 1285 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1286 | consumeToken(); |
| 1287 | HadError = true; |
| 1288 | return; |
| 1289 | } |
| 1290 | consumeToken(); // 'module' keyword |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1291 | |
| 1292 | // If we have a wildcard for the module name, this is an inferred submodule. |
| 1293 | // Parse it. |
| 1294 | if (Tok.is(MMToken::Star)) |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1295 | return parseInferredModuleDecl(Framework, Explicit); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1296 | |
| 1297 | // Parse the module name. |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1298 | ModuleId Id; |
| 1299 | if (parseModuleId(Id)) { |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1300 | HadError = true; |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1301 | return; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1302 | } |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1303 | |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1304 | if (ActiveModule) { |
| 1305 | if (Id.size() > 1) { |
| 1306 | Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) |
| 1307 | << SourceRange(Id.front().second, Id.back().second); |
| 1308 | |
| 1309 | HadError = true; |
| 1310 | return; |
| 1311 | } |
| 1312 | } else if (Id.size() == 1 && Explicit) { |
| 1313 | // Top-level modules can't be explicit. |
| 1314 | Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); |
| 1315 | Explicit = false; |
| 1316 | ExplicitLoc = SourceLocation(); |
| 1317 | HadError = true; |
| 1318 | } |
| 1319 | |
| 1320 | Module *PreviousActiveModule = ActiveModule; |
| 1321 | if (Id.size() > 1) { |
| 1322 | // This module map defines a submodule. Go find the module of which it |
| 1323 | // is a submodule. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1324 | ActiveModule = nullptr; |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1325 | for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { |
| 1326 | if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { |
| 1327 | ActiveModule = Next; |
| 1328 | continue; |
| 1329 | } |
| 1330 | |
| 1331 | if (ActiveModule) { |
| 1332 | Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1333 | << Id[I].first |
| 1334 | << ActiveModule->getTopLevelModule()->getFullModuleName(); |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1335 | } else { |
| 1336 | Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); |
| 1337 | } |
| 1338 | HadError = true; |
| 1339 | return; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | StringRef ModuleName = Id.back().first; |
| 1344 | SourceLocation ModuleNameLoc = Id.back().second; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1345 | |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1346 | // Parse the optional attribute list. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1347 | Attributes Attrs; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1348 | parseOptionalAttributes(Attrs); |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1349 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1350 | // Parse the opening brace. |
| 1351 | if (!Tok.is(MMToken::LBrace)) { |
| 1352 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) |
| 1353 | << ModuleName; |
| 1354 | HadError = true; |
| 1355 | return; |
| 1356 | } |
| 1357 | SourceLocation LBraceLoc = consumeToken(); |
| 1358 | |
| 1359 | // Determine whether this (sub)module has already been defined. |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1360 | if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { |
Douglas Gregor | c634f50 | 2012-01-05 00:12:00 +0000 | [diff] [blame] | 1361 | if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { |
| 1362 | // Skip the module definition. |
| 1363 | skipUntil(MMToken::RBrace); |
| 1364 | if (Tok.is(MMToken::RBrace)) |
| 1365 | consumeToken(); |
| 1366 | else { |
| 1367 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); |
| 1368 | Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); |
| 1369 | HadError = true; |
| 1370 | } |
| 1371 | return; |
| 1372 | } |
| 1373 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1374 | Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) |
| 1375 | << ModuleName; |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1376 | Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1377 | |
| 1378 | // Skip the module definition. |
| 1379 | skipUntil(MMToken::RBrace); |
| 1380 | if (Tok.is(MMToken::RBrace)) |
| 1381 | consumeToken(); |
| 1382 | |
| 1383 | HadError = true; |
| 1384 | return; |
| 1385 | } |
| 1386 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1387 | // If this is a submodule, use the parent's module map, since we don't want |
| 1388 | // the private module map file. |
| 1389 | const FileEntry *ModuleMap = ActiveModule ? ActiveModule->ModuleMap |
| 1390 | : ModuleMapFile; |
| 1391 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1392 | // Start defining this module. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1393 | ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, ModuleMap, |
| 1394 | Framework, Explicit).first; |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1395 | ActiveModule->DefinitionLoc = ModuleNameLoc; |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame] | 1396 | if (Attrs.IsSystem || IsSystem) |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1397 | ActiveModule->IsSystem = true; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1398 | if (Attrs.IsExternC) |
| 1399 | ActiveModule->IsExternC = true; |
| 1400 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1401 | bool Done = false; |
| 1402 | do { |
| 1403 | switch (Tok.Kind) { |
| 1404 | case MMToken::EndOfFile: |
| 1405 | case MMToken::RBrace: |
| 1406 | Done = true; |
| 1407 | break; |
Douglas Gregor | 63a7268 | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1408 | |
| 1409 | case MMToken::ConfigMacros: |
| 1410 | parseConfigMacros(); |
| 1411 | break; |
| 1412 | |
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 1413 | case MMToken::Conflict: |
| 1414 | parseConflict(); |
| 1415 | break; |
| 1416 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1417 | case MMToken::ExplicitKeyword: |
Daniel Jasper | 5f0a352 | 2013-09-11 07:20:44 +0000 | [diff] [blame] | 1418 | case MMToken::ExternKeyword: |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1419 | case MMToken::FrameworkKeyword: |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1420 | case MMToken::ModuleKeyword: |
| 1421 | parseModuleDecl(); |
| 1422 | break; |
Daniel Jasper | 5f0a352 | 2013-09-11 07:20:44 +0000 | [diff] [blame] | 1423 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1424 | case MMToken::ExportKeyword: |
| 1425 | parseExportDecl(); |
| 1426 | break; |
Daniel Jasper | ddd2dfc | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 1427 | |
| 1428 | case MMToken::UseKeyword: |
| 1429 | parseUseDecl(); |
| 1430 | break; |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1431 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1432 | case MMToken::RequiresKeyword: |
| 1433 | parseRequiresDecl(); |
| 1434 | break; |
| 1435 | |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1436 | case MMToken::UmbrellaKeyword: { |
| 1437 | SourceLocation UmbrellaLoc = consumeToken(); |
| 1438 | if (Tok.is(MMToken::HeaderKeyword)) |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 1439 | parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc); |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1440 | else |
| 1441 | parseUmbrellaDirDecl(UmbrellaLoc); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1442 | break; |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1443 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1444 | |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1445 | case MMToken::ExcludeKeyword: { |
| 1446 | SourceLocation ExcludeLoc = consumeToken(); |
| 1447 | if (Tok.is(MMToken::HeaderKeyword)) { |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 1448 | parseHeaderDecl(MMToken::ExcludeKeyword, ExcludeLoc); |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1449 | } else { |
| 1450 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) |
| 1451 | << "exclude"; |
| 1452 | } |
| 1453 | break; |
| 1454 | } |
| 1455 | |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 1456 | case MMToken::PrivateKeyword: { |
| 1457 | SourceLocation PrivateLoc = consumeToken(); |
| 1458 | if (Tok.is(MMToken::HeaderKeyword)) { |
| 1459 | parseHeaderDecl(MMToken::PrivateKeyword, PrivateLoc); |
| 1460 | } else { |
| 1461 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) |
| 1462 | << "private"; |
| 1463 | } |
| 1464 | break; |
| 1465 | } |
| 1466 | |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 1467 | case MMToken::HeaderKeyword: |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 1468 | parseHeaderDecl(MMToken::HeaderKeyword, SourceLocation()); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1469 | break; |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 1470 | |
| 1471 | case MMToken::LinkKeyword: |
| 1472 | parseLinkDecl(); |
| 1473 | break; |
| 1474 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1475 | default: |
| 1476 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); |
| 1477 | consumeToken(); |
| 1478 | break; |
| 1479 | } |
| 1480 | } while (!Done); |
| 1481 | |
| 1482 | if (Tok.is(MMToken::RBrace)) |
| 1483 | consumeToken(); |
| 1484 | else { |
| 1485 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); |
| 1486 | Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); |
| 1487 | HadError = true; |
| 1488 | } |
| 1489 | |
Douglas Gregor | 8767dc2 | 2013-01-14 17:57:51 +0000 | [diff] [blame] | 1490 | // If the active module is a top-level framework, and there are no link |
| 1491 | // libraries, automatically link against the framework. |
| 1492 | if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() && |
| 1493 | ActiveModule->LinkLibraries.empty()) { |
| 1494 | inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager()); |
| 1495 | } |
| 1496 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1497 | // If the module meets all requirements but is still unavailable, mark the |
| 1498 | // whole tree as unavailable to prevent it from building. |
| 1499 | if (!ActiveModule->IsAvailable && !ActiveModule->IsMissingRequirement && |
| 1500 | ActiveModule->Parent) { |
| 1501 | ActiveModule->getTopLevelModule()->markUnavailable(); |
| 1502 | ActiveModule->getTopLevelModule()->MissingHeaders.append( |
| 1503 | ActiveModule->MissingHeaders.begin(), ActiveModule->MissingHeaders.end()); |
| 1504 | } |
| 1505 | |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1506 | // We're done parsing this module. Pop back to the previous module. |
| 1507 | ActiveModule = PreviousActiveModule; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1508 | } |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1509 | |
Daniel Jasper | 5f0a352 | 2013-09-11 07:20:44 +0000 | [diff] [blame] | 1510 | /// \brief Parse an extern module declaration. |
| 1511 | /// |
| 1512 | /// extern module-declaration: |
| 1513 | /// 'extern' 'module' module-id string-literal |
| 1514 | void ModuleMapParser::parseExternModuleDecl() { |
| 1515 | assert(Tok.is(MMToken::ExternKeyword)); |
| 1516 | consumeToken(); // 'extern' keyword |
| 1517 | |
| 1518 | // Parse 'module' keyword. |
| 1519 | if (!Tok.is(MMToken::ModuleKeyword)) { |
| 1520 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); |
| 1521 | consumeToken(); |
| 1522 | HadError = true; |
| 1523 | return; |
| 1524 | } |
| 1525 | consumeToken(); // 'module' keyword |
| 1526 | |
| 1527 | // Parse the module name. |
| 1528 | ModuleId Id; |
| 1529 | if (parseModuleId(Id)) { |
| 1530 | HadError = true; |
| 1531 | return; |
| 1532 | } |
| 1533 | |
| 1534 | // Parse the referenced module map file name. |
| 1535 | if (!Tok.is(MMToken::StringLiteral)) { |
| 1536 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file); |
| 1537 | HadError = true; |
| 1538 | return; |
| 1539 | } |
| 1540 | std::string FileName = Tok.getString(); |
| 1541 | consumeToken(); // filename |
| 1542 | |
| 1543 | StringRef FileNameRef = FileName; |
| 1544 | SmallString<128> ModuleMapFileName; |
| 1545 | if (llvm::sys::path::is_relative(FileNameRef)) { |
| 1546 | ModuleMapFileName += Directory->getName(); |
| 1547 | llvm::sys::path::append(ModuleMapFileName, FileName); |
| 1548 | FileNameRef = ModuleMapFileName.str(); |
| 1549 | } |
| 1550 | if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef)) |
| 1551 | Map.parseModuleMapFile(File, /*IsSystem=*/false); |
| 1552 | } |
| 1553 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1554 | /// \brief Parse a requires declaration. |
| 1555 | /// |
| 1556 | /// requires-declaration: |
| 1557 | /// 'requires' feature-list |
| 1558 | /// |
| 1559 | /// feature-list: |
Richard Smith | 5794b53 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 1560 | /// feature ',' feature-list |
| 1561 | /// feature |
| 1562 | /// |
| 1563 | /// feature: |
| 1564 | /// '!'[opt] identifier |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1565 | void ModuleMapParser::parseRequiresDecl() { |
| 1566 | assert(Tok.is(MMToken::RequiresKeyword)); |
| 1567 | |
| 1568 | // Parse 'requires' keyword. |
| 1569 | consumeToken(); |
| 1570 | |
| 1571 | // Parse the feature-list. |
| 1572 | do { |
Richard Smith | 5794b53 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 1573 | bool RequiredState = true; |
| 1574 | if (Tok.is(MMToken::Exclaim)) { |
| 1575 | RequiredState = false; |
| 1576 | consumeToken(); |
| 1577 | } |
| 1578 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1579 | if (!Tok.is(MMToken::Identifier)) { |
| 1580 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); |
| 1581 | HadError = true; |
| 1582 | return; |
| 1583 | } |
| 1584 | |
| 1585 | // Consume the feature name. |
| 1586 | std::string Feature = Tok.getString(); |
| 1587 | consumeToken(); |
| 1588 | |
| 1589 | // Add this feature. |
Richard Smith | 5794b53 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 1590 | ActiveModule->addRequirement(Feature, RequiredState, |
| 1591 | Map.LangOpts, *Map.Target); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1592 | |
| 1593 | if (!Tok.is(MMToken::Comma)) |
| 1594 | break; |
| 1595 | |
| 1596 | // Consume the comma. |
| 1597 | consumeToken(); |
| 1598 | } while (true); |
| 1599 | } |
| 1600 | |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1601 | /// \brief Append to \p Paths the set of paths needed to get to the |
| 1602 | /// subframework in which the given module lives. |
Benjamin Kramer | 5bbc385 | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 1603 | static void appendSubframeworkPaths(Module *Mod, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1604 | SmallVectorImpl<char> &Path) { |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1605 | // Collect the framework names from the given module to the top-level module. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1606 | SmallVector<StringRef, 2> Paths; |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1607 | for (; Mod; Mod = Mod->Parent) { |
| 1608 | if (Mod->IsFramework) |
| 1609 | Paths.push_back(Mod->Name); |
| 1610 | } |
| 1611 | |
| 1612 | if (Paths.empty()) |
| 1613 | return; |
| 1614 | |
| 1615 | // Add Frameworks/Name.framework for each subframework. |
Benjamin Kramer | ceb6dc8 | 2013-06-28 16:25:46 +0000 | [diff] [blame] | 1616 | for (unsigned I = Paths.size() - 1; I != 0; --I) |
| 1617 | llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework"); |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1620 | /// \brief Parse a header declaration. |
| 1621 | /// |
| 1622 | /// header-declaration: |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 1623 | /// 'umbrella'[opt] 'header' string-literal |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1624 | /// 'exclude'[opt] 'header' string-literal |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 1625 | void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken, |
| 1626 | SourceLocation LeadingLoc) { |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1627 | assert(Tok.is(MMToken::HeaderKeyword)); |
Benjamin Kramer | c96c721 | 2011-11-13 16:52:09 +0000 | [diff] [blame] | 1628 | consumeToken(); |
| 1629 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1630 | // Parse the header name. |
| 1631 | if (!Tok.is(MMToken::StringLiteral)) { |
| 1632 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) |
| 1633 | << "header"; |
| 1634 | HadError = true; |
| 1635 | return; |
| 1636 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1637 | Module::HeaderDirective Header; |
| 1638 | Header.FileName = Tok.getString(); |
| 1639 | Header.FileNameLoc = consumeToken(); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1640 | |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1641 | // Check whether we already have an umbrella. |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 1642 | if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1643 | Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash) |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1644 | << ActiveModule->getFullModuleName(); |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 1645 | HadError = true; |
| 1646 | return; |
| 1647 | } |
| 1648 | |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 1649 | // Look for this file. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1650 | const FileEntry *File = nullptr; |
| 1651 | const FileEntry *BuiltinFile = nullptr; |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1652 | SmallString<128> PathName; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1653 | if (llvm::sys::path::is_absolute(Header.FileName)) { |
| 1654 | PathName = Header.FileName; |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1655 | File = SourceMgr.getFileManager().getFile(PathName); |
| 1656 | } else { |
| 1657 | // Search for the header file within the search directory. |
Douglas Gregor | 6a1db48 | 2011-12-09 02:04:43 +0000 | [diff] [blame] | 1658 | PathName = Directory->getName(); |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1659 | unsigned PathLength = PathName.size(); |
Douglas Gregor | 18ee547 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1660 | |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1661 | if (ActiveModule->isPartOfFramework()) { |
| 1662 | appendSubframeworkPaths(ActiveModule, PathName); |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1663 | |
| 1664 | // Check whether this file is in the public headers. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1665 | llvm::sys::path::append(PathName, "Headers", Header.FileName); |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1666 | File = SourceMgr.getFileManager().getFile(PathName); |
| 1667 | |
| 1668 | if (!File) { |
| 1669 | // Check whether this file is in the private headers. |
| 1670 | PathName.resize(PathLength); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1671 | llvm::sys::path::append(PathName, "PrivateHeaders", Header.FileName); |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1672 | File = SourceMgr.getFileManager().getFile(PathName); |
| 1673 | } |
| 1674 | } else { |
| 1675 | // Lookup for normal headers. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1676 | llvm::sys::path::append(PathName, Header.FileName); |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1677 | File = SourceMgr.getFileManager().getFile(PathName); |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 1678 | |
| 1679 | // If this is a system module with a top-level header, this header |
| 1680 | // may have a counterpart (or replacement) in the set of headers |
| 1681 | // supplied by Clang. Find that builtin header. |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 1682 | if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword && |
| 1683 | BuiltinIncludeDir && BuiltinIncludeDir != Directory && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1684 | isBuiltinHeader(Header.FileName)) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1685 | SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1686 | llvm::sys::path::append(BuiltinPathName, Header.FileName); |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 1687 | BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); |
| 1688 | |
| 1689 | // If Clang supplies this header but the underlying system does not, |
| 1690 | // just silently swap in our builtin version. Otherwise, we'll end |
| 1691 | // up adding both (later). |
| 1692 | if (!File && BuiltinFile) { |
| 1693 | File = BuiltinFile; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1694 | BuiltinFile = nullptr; |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 1695 | } |
| 1696 | } |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1697 | } |
Douglas Gregor | 18ee547 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1698 | } |
Douglas Gregor | a865405 | 2011-11-17 22:09:43 +0000 | [diff] [blame] | 1699 | |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 1700 | // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. |
| 1701 | // Come up with a lazy way to do this. |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1702 | if (File) { |
Daniel Jasper | c641709 | 2013-10-22 08:09:47 +0000 | [diff] [blame] | 1703 | if (LeadingToken == MMToken::UmbrellaKeyword) { |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 1704 | const DirectoryEntry *UmbrellaDir = File->getDir(); |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1705 | if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 1706 | Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash) |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1707 | << UmbrellaModule->getFullModuleName(); |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 1708 | HadError = true; |
| 1709 | } else { |
| 1710 | // Record this umbrella header. |
| 1711 | Map.setUmbrellaHeader(ActiveModule, File); |
| 1712 | } |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 1713 | } else { |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 1714 | // Record this header. |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 1715 | ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader; |
| 1716 | if (LeadingToken == MMToken::ExcludeKeyword) |
| 1717 | Role = ModuleMap::ExcludedHeader; |
| 1718 | else if (LeadingToken == MMToken::PrivateKeyword) |
| 1719 | Role = ModuleMap::PrivateHeader; |
| 1720 | else |
| 1721 | assert(LeadingToken == MMToken::HeaderKeyword); |
| 1722 | |
| 1723 | Map.addHeader(ActiveModule, File, Role); |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 1724 | |
| 1725 | // If there is a builtin counterpart to this file, add it now. |
| 1726 | if (BuiltinFile) |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 1727 | Map.addHeader(ActiveModule, BuiltinFile, Role); |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 1728 | } |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 1729 | } else if (LeadingToken != MMToken::ExcludeKeyword) { |
Douglas Gregor | 71f49f5 | 2012-11-15 19:47:16 +0000 | [diff] [blame] | 1730 | // Ignore excluded header files. They're optional anyway. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1731 | |
| 1732 | // If we find a module that has a missing header, we mark this module as |
| 1733 | // unavailable and store the header directive for displaying diagnostics. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1734 | Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1735 | ActiveModule->markUnavailable(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1736 | ActiveModule->MissingHeaders.push_back(Header); |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 1737 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1740 | /// \brief Parse an umbrella directory declaration. |
| 1741 | /// |
| 1742 | /// umbrella-dir-declaration: |
| 1743 | /// umbrella string-literal |
| 1744 | void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { |
| 1745 | // Parse the directory name. |
| 1746 | if (!Tok.is(MMToken::StringLiteral)) { |
| 1747 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) |
| 1748 | << "umbrella"; |
| 1749 | HadError = true; |
| 1750 | return; |
| 1751 | } |
| 1752 | |
| 1753 | std::string DirName = Tok.getString(); |
| 1754 | SourceLocation DirNameLoc = consumeToken(); |
| 1755 | |
| 1756 | // Check whether we already have an umbrella. |
| 1757 | if (ActiveModule->Umbrella) { |
| 1758 | Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) |
| 1759 | << ActiveModule->getFullModuleName(); |
| 1760 | HadError = true; |
| 1761 | return; |
| 1762 | } |
| 1763 | |
| 1764 | // Look for this file. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1765 | const DirectoryEntry *Dir = nullptr; |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1766 | if (llvm::sys::path::is_absolute(DirName)) |
| 1767 | Dir = SourceMgr.getFileManager().getDirectory(DirName); |
| 1768 | else { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1769 | SmallString<128> PathName; |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1770 | PathName = Directory->getName(); |
| 1771 | llvm::sys::path::append(PathName, DirName); |
| 1772 | Dir = SourceMgr.getFileManager().getDirectory(PathName); |
| 1773 | } |
| 1774 | |
| 1775 | if (!Dir) { |
| 1776 | Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) |
| 1777 | << DirName; |
| 1778 | HadError = true; |
| 1779 | return; |
| 1780 | } |
| 1781 | |
| 1782 | if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { |
| 1783 | Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) |
| 1784 | << OwningModule->getFullModuleName(); |
| 1785 | HadError = true; |
| 1786 | return; |
| 1787 | } |
| 1788 | |
| 1789 | // Record this umbrella directory. |
| 1790 | Map.setUmbrellaDir(ActiveModule, Dir); |
| 1791 | } |
| 1792 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1793 | /// \brief Parse a module export declaration. |
| 1794 | /// |
| 1795 | /// export-declaration: |
| 1796 | /// 'export' wildcard-module-id |
| 1797 | /// |
| 1798 | /// wildcard-module-id: |
| 1799 | /// identifier |
| 1800 | /// '*' |
| 1801 | /// identifier '.' wildcard-module-id |
| 1802 | void ModuleMapParser::parseExportDecl() { |
| 1803 | assert(Tok.is(MMToken::ExportKeyword)); |
| 1804 | SourceLocation ExportLoc = consumeToken(); |
| 1805 | |
| 1806 | // Parse the module-id with an optional wildcard at the end. |
| 1807 | ModuleId ParsedModuleId; |
| 1808 | bool Wildcard = false; |
| 1809 | do { |
| 1810 | if (Tok.is(MMToken::Identifier)) { |
| 1811 | ParsedModuleId.push_back(std::make_pair(Tok.getString(), |
| 1812 | Tok.getLocation())); |
| 1813 | consumeToken(); |
| 1814 | |
| 1815 | if (Tok.is(MMToken::Period)) { |
| 1816 | consumeToken(); |
| 1817 | continue; |
| 1818 | } |
| 1819 | |
| 1820 | break; |
| 1821 | } |
| 1822 | |
| 1823 | if(Tok.is(MMToken::Star)) { |
| 1824 | Wildcard = true; |
Douglas Gregor | 0adaa88 | 2011-12-05 17:28:06 +0000 | [diff] [blame] | 1825 | consumeToken(); |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1826 | break; |
| 1827 | } |
| 1828 | |
Daniel Jasper | ddd2dfc | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 1829 | Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1830 | HadError = true; |
| 1831 | return; |
| 1832 | } while (true); |
| 1833 | |
| 1834 | Module::UnresolvedExportDecl Unresolved = { |
| 1835 | ExportLoc, ParsedModuleId, Wildcard |
| 1836 | }; |
| 1837 | ActiveModule->UnresolvedExports.push_back(Unresolved); |
| 1838 | } |
| 1839 | |
Daniel Jasper | ddd2dfc | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 1840 | /// \brief Parse a module uses declaration. |
| 1841 | /// |
| 1842 | /// uses-declaration: |
| 1843 | /// 'uses' wildcard-module-id |
| 1844 | void ModuleMapParser::parseUseDecl() { |
| 1845 | assert(Tok.is(MMToken::UseKeyword)); |
| 1846 | consumeToken(); |
| 1847 | // Parse the module-id. |
| 1848 | ModuleId ParsedModuleId; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1849 | parseModuleId(ParsedModuleId); |
Daniel Jasper | ddd2dfc | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 1850 | |
| 1851 | ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId); |
| 1852 | } |
| 1853 | |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 1854 | /// \brief Parse a link declaration. |
| 1855 | /// |
| 1856 | /// module-declaration: |
| 1857 | /// 'link' 'framework'[opt] string-literal |
| 1858 | void ModuleMapParser::parseLinkDecl() { |
| 1859 | assert(Tok.is(MMToken::LinkKeyword)); |
| 1860 | SourceLocation LinkLoc = consumeToken(); |
| 1861 | |
| 1862 | // Parse the optional 'framework' keyword. |
| 1863 | bool IsFramework = false; |
| 1864 | if (Tok.is(MMToken::FrameworkKeyword)) { |
| 1865 | consumeToken(); |
| 1866 | IsFramework = true; |
| 1867 | } |
| 1868 | |
| 1869 | // Parse the library name |
| 1870 | if (!Tok.is(MMToken::StringLiteral)) { |
| 1871 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) |
| 1872 | << IsFramework << SourceRange(LinkLoc); |
| 1873 | HadError = true; |
| 1874 | return; |
| 1875 | } |
| 1876 | |
| 1877 | std::string LibraryName = Tok.getString(); |
| 1878 | consumeToken(); |
| 1879 | ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, |
| 1880 | IsFramework)); |
| 1881 | } |
| 1882 | |
Douglas Gregor | 63a7268 | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1883 | /// \brief Parse a configuration macro declaration. |
| 1884 | /// |
| 1885 | /// module-declaration: |
| 1886 | /// 'config_macros' attributes[opt] config-macro-list? |
| 1887 | /// |
| 1888 | /// config-macro-list: |
| 1889 | /// identifier (',' identifier)? |
| 1890 | void ModuleMapParser::parseConfigMacros() { |
| 1891 | assert(Tok.is(MMToken::ConfigMacros)); |
| 1892 | SourceLocation ConfigMacrosLoc = consumeToken(); |
| 1893 | |
| 1894 | // Only top-level modules can have configuration macros. |
| 1895 | if (ActiveModule->Parent) { |
| 1896 | Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule); |
| 1897 | } |
| 1898 | |
| 1899 | // Parse the optional attributes. |
| 1900 | Attributes Attrs; |
| 1901 | parseOptionalAttributes(Attrs); |
| 1902 | if (Attrs.IsExhaustive && !ActiveModule->Parent) { |
| 1903 | ActiveModule->ConfigMacrosExhaustive = true; |
| 1904 | } |
| 1905 | |
| 1906 | // If we don't have an identifier, we're done. |
| 1907 | if (!Tok.is(MMToken::Identifier)) |
| 1908 | return; |
| 1909 | |
| 1910 | // Consume the first identifier. |
| 1911 | if (!ActiveModule->Parent) { |
| 1912 | ActiveModule->ConfigMacros.push_back(Tok.getString().str()); |
| 1913 | } |
| 1914 | consumeToken(); |
| 1915 | |
| 1916 | do { |
| 1917 | // If there's a comma, consume it. |
| 1918 | if (!Tok.is(MMToken::Comma)) |
| 1919 | break; |
| 1920 | consumeToken(); |
| 1921 | |
| 1922 | // We expect to see a macro name here. |
| 1923 | if (!Tok.is(MMToken::Identifier)) { |
| 1924 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro); |
| 1925 | break; |
| 1926 | } |
| 1927 | |
| 1928 | // Consume the macro name. |
| 1929 | if (!ActiveModule->Parent) { |
| 1930 | ActiveModule->ConfigMacros.push_back(Tok.getString().str()); |
| 1931 | } |
| 1932 | consumeToken(); |
| 1933 | } while (true); |
| 1934 | } |
| 1935 | |
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 1936 | /// \brief Format a module-id into a string. |
| 1937 | static std::string formatModuleId(const ModuleId &Id) { |
| 1938 | std::string result; |
| 1939 | { |
| 1940 | llvm::raw_string_ostream OS(result); |
| 1941 | |
| 1942 | for (unsigned I = 0, N = Id.size(); I != N; ++I) { |
| 1943 | if (I) |
| 1944 | OS << "."; |
| 1945 | OS << Id[I].first; |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | return result; |
| 1950 | } |
| 1951 | |
| 1952 | /// \brief Parse a conflict declaration. |
| 1953 | /// |
| 1954 | /// module-declaration: |
| 1955 | /// 'conflict' module-id ',' string-literal |
| 1956 | void ModuleMapParser::parseConflict() { |
| 1957 | assert(Tok.is(MMToken::Conflict)); |
| 1958 | SourceLocation ConflictLoc = consumeToken(); |
| 1959 | Module::UnresolvedConflict Conflict; |
| 1960 | |
| 1961 | // Parse the module-id. |
| 1962 | if (parseModuleId(Conflict.Id)) |
| 1963 | return; |
| 1964 | |
| 1965 | // Parse the ','. |
| 1966 | if (!Tok.is(MMToken::Comma)) { |
| 1967 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma) |
| 1968 | << SourceRange(ConflictLoc); |
| 1969 | return; |
| 1970 | } |
| 1971 | consumeToken(); |
| 1972 | |
| 1973 | // Parse the message. |
| 1974 | if (!Tok.is(MMToken::StringLiteral)) { |
| 1975 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message) |
| 1976 | << formatModuleId(Conflict.Id); |
| 1977 | return; |
| 1978 | } |
| 1979 | Conflict.Message = Tok.getString().str(); |
| 1980 | consumeToken(); |
| 1981 | |
| 1982 | // Add this unresolved conflict. |
| 1983 | ActiveModule->UnresolvedConflicts.push_back(Conflict); |
| 1984 | } |
| 1985 | |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 1986 | /// \brief Parse an inferred module declaration (wildcard modules). |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1987 | /// |
| 1988 | /// module-declaration: |
| 1989 | /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] |
| 1990 | /// { inferred-module-member* } |
| 1991 | /// |
| 1992 | /// inferred-module-member: |
| 1993 | /// 'export' '*' |
| 1994 | /// 'exclude' identifier |
| 1995 | void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1996 | assert(Tok.is(MMToken::Star)); |
| 1997 | SourceLocation StarLoc = consumeToken(); |
| 1998 | bool Failed = false; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1999 | |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 2000 | // Inferred modules must be submodules. |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2001 | if (!ActiveModule && !Framework) { |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 2002 | Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); |
| 2003 | Failed = true; |
| 2004 | } |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2005 | |
| 2006 | if (ActiveModule) { |
| 2007 | // Inferred modules must have umbrella directories. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2008 | if (!Failed && ActiveModule->IsAvailable && |
| 2009 | !ActiveModule->getUmbrellaDir()) { |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2010 | Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); |
| 2011 | Failed = true; |
| 2012 | } |
| 2013 | |
| 2014 | // Check for redefinition of an inferred module. |
| 2015 | if (!Failed && ActiveModule->InferSubmodules) { |
| 2016 | Diags.Report(StarLoc, diag::err_mmap_inferred_redef); |
| 2017 | if (ActiveModule->InferredSubmoduleLoc.isValid()) |
| 2018 | Diags.Report(ActiveModule->InferredSubmoduleLoc, |
| 2019 | diag::note_mmap_prev_definition); |
| 2020 | Failed = true; |
| 2021 | } |
| 2022 | |
| 2023 | // Check for the 'framework' keyword, which is not permitted here. |
| 2024 | if (Framework) { |
| 2025 | Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); |
| 2026 | Framework = false; |
| 2027 | } |
| 2028 | } else if (Explicit) { |
| 2029 | Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); |
| 2030 | Explicit = false; |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 2031 | } |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2032 | |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 2033 | // If there were any problems with this inferred submodule, skip its body. |
| 2034 | if (Failed) { |
| 2035 | if (Tok.is(MMToken::LBrace)) { |
| 2036 | consumeToken(); |
| 2037 | skipUntil(MMToken::RBrace); |
| 2038 | if (Tok.is(MMToken::RBrace)) |
| 2039 | consumeToken(); |
| 2040 | } |
| 2041 | HadError = true; |
| 2042 | return; |
| 2043 | } |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2044 | |
| 2045 | // Parse optional attributes. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2046 | Attributes Attrs; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2047 | parseOptionalAttributes(Attrs); |
| 2048 | |
| 2049 | if (ActiveModule) { |
| 2050 | // Note that we have an inferred submodule. |
| 2051 | ActiveModule->InferSubmodules = true; |
| 2052 | ActiveModule->InferredSubmoduleLoc = StarLoc; |
| 2053 | ActiveModule->InferExplicitSubmodules = Explicit; |
| 2054 | } else { |
| 2055 | // We'll be inferring framework modules for this directory. |
| 2056 | Map.InferredDirectories[Directory].InferModules = true; |
| 2057 | Map.InferredDirectories[Directory].InferSystemModules = Attrs.IsSystem; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2058 | Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2059 | // FIXME: Handle the 'framework' keyword. |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 2062 | // Parse the opening brace. |
| 2063 | if (!Tok.is(MMToken::LBrace)) { |
| 2064 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); |
| 2065 | HadError = true; |
| 2066 | return; |
| 2067 | } |
| 2068 | SourceLocation LBraceLoc = consumeToken(); |
| 2069 | |
| 2070 | // Parse the body of the inferred submodule. |
| 2071 | bool Done = false; |
| 2072 | do { |
| 2073 | switch (Tok.Kind) { |
| 2074 | case MMToken::EndOfFile: |
| 2075 | case MMToken::RBrace: |
| 2076 | Done = true; |
| 2077 | break; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2078 | |
| 2079 | case MMToken::ExcludeKeyword: { |
| 2080 | if (ActiveModule) { |
| 2081 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2082 | << (ActiveModule != nullptr); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2083 | consumeToken(); |
| 2084 | break; |
| 2085 | } |
| 2086 | |
| 2087 | consumeToken(); |
| 2088 | if (!Tok.is(MMToken::Identifier)) { |
| 2089 | Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); |
| 2090 | break; |
| 2091 | } |
| 2092 | |
| 2093 | Map.InferredDirectories[Directory].ExcludedModules |
| 2094 | .push_back(Tok.getString()); |
| 2095 | consumeToken(); |
| 2096 | break; |
| 2097 | } |
| 2098 | |
| 2099 | case MMToken::ExportKeyword: |
| 2100 | if (!ActiveModule) { |
| 2101 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2102 | << (ActiveModule != nullptr); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2103 | consumeToken(); |
| 2104 | break; |
| 2105 | } |
| 2106 | |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 2107 | consumeToken(); |
| 2108 | if (Tok.is(MMToken::Star)) |
Douglas Gregor | ef85b56 | 2011-12-06 17:34:58 +0000 | [diff] [blame] | 2109 | ActiveModule->InferExportWildcard = true; |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 2110 | else |
| 2111 | Diags.Report(Tok.getLocation(), |
| 2112 | diag::err_mmap_expected_export_wildcard); |
| 2113 | consumeToken(); |
| 2114 | break; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2115 | |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 2116 | case MMToken::ExplicitKeyword: |
| 2117 | case MMToken::ModuleKeyword: |
| 2118 | case MMToken::HeaderKeyword: |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 2119 | case MMToken::PrivateKeyword: |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 2120 | case MMToken::UmbrellaKeyword: |
| 2121 | default: |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2122 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2123 | << (ActiveModule != nullptr); |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 2124 | consumeToken(); |
| 2125 | break; |
| 2126 | } |
| 2127 | } while (!Done); |
| 2128 | |
| 2129 | if (Tok.is(MMToken::RBrace)) |
| 2130 | consumeToken(); |
| 2131 | else { |
| 2132 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); |
| 2133 | Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); |
| 2134 | HadError = true; |
| 2135 | } |
| 2136 | } |
| 2137 | |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2138 | /// \brief Parse optional attributes. |
| 2139 | /// |
| 2140 | /// attributes: |
| 2141 | /// attribute attributes |
| 2142 | /// attribute |
| 2143 | /// |
| 2144 | /// attribute: |
| 2145 | /// [ identifier ] |
| 2146 | /// |
| 2147 | /// \param Attrs Will be filled in with the parsed attributes. |
| 2148 | /// |
| 2149 | /// \returns true if an error occurred, false otherwise. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2150 | bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2151 | bool HadError = false; |
| 2152 | |
| 2153 | while (Tok.is(MMToken::LSquare)) { |
| 2154 | // Consume the '['. |
| 2155 | SourceLocation LSquareLoc = consumeToken(); |
| 2156 | |
| 2157 | // Check whether we have an attribute name here. |
| 2158 | if (!Tok.is(MMToken::Identifier)) { |
| 2159 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); |
| 2160 | skipUntil(MMToken::RSquare); |
| 2161 | if (Tok.is(MMToken::RSquare)) |
| 2162 | consumeToken(); |
| 2163 | HadError = true; |
| 2164 | } |
| 2165 | |
| 2166 | // Decode the attribute name. |
| 2167 | AttributeKind Attribute |
| 2168 | = llvm::StringSwitch<AttributeKind>(Tok.getString()) |
Douglas Gregor | 63a7268 | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 2169 | .Case("exhaustive", AT_exhaustive) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2170 | .Case("extern_c", AT_extern_c) |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2171 | .Case("system", AT_system) |
| 2172 | .Default(AT_unknown); |
| 2173 | switch (Attribute) { |
| 2174 | case AT_unknown: |
| 2175 | Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) |
| 2176 | << Tok.getString(); |
| 2177 | break; |
| 2178 | |
| 2179 | case AT_system: |
| 2180 | Attrs.IsSystem = true; |
| 2181 | break; |
Douglas Gregor | 63a7268 | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 2182 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2183 | case AT_extern_c: |
| 2184 | Attrs.IsExternC = true; |
| 2185 | break; |
| 2186 | |
Douglas Gregor | 63a7268 | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 2187 | case AT_exhaustive: |
| 2188 | Attrs.IsExhaustive = true; |
| 2189 | break; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 2190 | } |
| 2191 | consumeToken(); |
| 2192 | |
| 2193 | // Consume the ']'. |
| 2194 | if (!Tok.is(MMToken::RSquare)) { |
| 2195 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); |
| 2196 | Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); |
| 2197 | skipUntil(MMToken::RSquare); |
| 2198 | HadError = true; |
| 2199 | } |
| 2200 | |
| 2201 | if (Tok.is(MMToken::RSquare)) |
| 2202 | consumeToken(); |
| 2203 | } |
| 2204 | |
| 2205 | return HadError; |
| 2206 | } |
| 2207 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 2208 | /// \brief Parse a module map file. |
| 2209 | /// |
| 2210 | /// module-map-file: |
| 2211 | /// module-declaration* |
| 2212 | bool ModuleMapParser::parseModuleMapFile() { |
| 2213 | do { |
| 2214 | switch (Tok.Kind) { |
| 2215 | case MMToken::EndOfFile: |
| 2216 | return HadError; |
| 2217 | |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 2218 | case MMToken::ExplicitKeyword: |
Daniel Jasper | 5f0a352 | 2013-09-11 07:20:44 +0000 | [diff] [blame] | 2219 | case MMToken::ExternKeyword: |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 2220 | case MMToken::ModuleKeyword: |
Douglas Gregor | a865405 | 2011-11-17 22:09:43 +0000 | [diff] [blame] | 2221 | case MMToken::FrameworkKeyword: |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 2222 | parseModuleDecl(); |
| 2223 | break; |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 2224 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 2225 | case MMToken::Comma: |
Douglas Gregor | 63a7268 | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 2226 | case MMToken::ConfigMacros: |
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 2227 | case MMToken::Conflict: |
Richard Smith | 5794b53 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 2228 | case MMToken::Exclaim: |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 2229 | case MMToken::ExcludeKeyword: |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 2230 | case MMToken::ExportKeyword: |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 2231 | case MMToken::HeaderKeyword: |
| 2232 | case MMToken::Identifier: |
| 2233 | case MMToken::LBrace: |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 2234 | case MMToken::LinkKeyword: |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 2235 | case MMToken::LSquare: |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 2236 | case MMToken::Period: |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 2237 | case MMToken::PrivateKeyword: |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 2238 | case MMToken::RBrace: |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 2239 | case MMToken::RSquare: |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 2240 | case MMToken::RequiresKeyword: |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 2241 | case MMToken::Star: |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 2242 | case MMToken::StringLiteral: |
| 2243 | case MMToken::UmbrellaKeyword: |
Daniel Jasper | ddd2dfc | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 2244 | case MMToken::UseKeyword: |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 2245 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); |
| 2246 | HadError = true; |
| 2247 | consumeToken(); |
| 2248 | break; |
| 2249 | } |
| 2250 | } while (true); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame] | 2253 | bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem) { |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 2254 | llvm::DenseMap<const FileEntry *, bool>::iterator Known |
| 2255 | = ParsedModuleMap.find(File); |
| 2256 | if (Known != ParsedModuleMap.end()) |
| 2257 | return Known->second; |
| 2258 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2259 | assert(Target && "Missing target information"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2260 | auto FileCharacter = IsSystem ? SrcMgr::C_System : SrcMgr::C_User; |
| 2261 | FileID ID = SourceMgr.createFileID(File, SourceLocation(), FileCharacter); |
Manuel Klimek | ee0cd37 | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 2262 | const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 2263 | if (!Buffer) |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 2264 | return ParsedModuleMap[File] = true; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2265 | |
| 2266 | // Find the directory for the module. For frameworks, that may require going |
| 2267 | // up from the 'Modules' directory. |
| 2268 | const DirectoryEntry *Dir = File->getDir(); |
| 2269 | StringRef DirName(Dir->getName()); |
| 2270 | if (llvm::sys::path::filename(DirName) == "Modules") { |
| 2271 | DirName = llvm::sys::path::parent_path(DirName); |
| 2272 | if (DirName.endswith(".framework")) |
| 2273 | Dir = SourceMgr.getFileManager().getDirectory(DirName); |
| 2274 | assert(Dir && "parent must exist"); |
| 2275 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 2276 | |
| 2277 | // Parse this module map file. |
Manuel Klimek | ee0cd37 | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 2278 | Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2279 | ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir, |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame] | 2280 | BuiltinIncludeDir, IsSystem); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 2281 | bool Result = Parser.parseModuleMapFile(); |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 2282 | ParsedModuleMap[File] = Result; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 2283 | return Result; |
| 2284 | } |