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