Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1 | //===--- ModuleMap.cpp - Describe the layout of modules ---------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the ModuleMap implementation, which describes the layout |
| 11 | // of a module as it relates to headers. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "clang/Lex/ModuleMap.h" |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 16 | #include "clang/Basic/DiagnosticOptions.h" |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 17 | #include "clang/Basic/FileManager.h" |
| 18 | #include "clang/Basic/TargetInfo.h" |
| 19 | #include "clang/Basic/TargetOptions.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Lex/LexDiagnostic.h" |
| 21 | #include "clang/Lex/Lexer.h" |
| 22 | #include "clang/Lex/LiteralSupport.h" |
| 23 | #include "llvm/ADT/StringRef.h" |
| 24 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Allocator.h" |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 26 | #include "llvm/Support/FileSystem.h" |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Host.h" |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/PathV2.h" |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 98cfcbf | 2012-09-27 14:50:15 +0000 | [diff] [blame] | 30 | #include <stdlib.h> |
Douglas Gregor | 3cc6277 | 2013-01-22 23:49:45 +0000 | [diff] [blame] | 31 | #if defined(LLVM_ON_UNIX) |
| 32 | #if defined(__linux__) |
| 33 | #include <linux/limits.h> |
| 34 | #endif |
| 35 | #endif |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 36 | using namespace clang; |
| 37 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 38 | Module::ExportDecl |
| 39 | ModuleMap::resolveExport(Module *Mod, |
| 40 | const Module::UnresolvedExportDecl &Unresolved, |
| 41 | bool Complain) { |
Douglas Gregor | 0adaa88 | 2011-12-05 17:28:06 +0000 | [diff] [blame] | 42 | // We may have just a wildcard. |
| 43 | if (Unresolved.Id.empty()) { |
| 44 | assert(Unresolved.Wildcard && "Invalid unresolved export"); |
| 45 | return Module::ExportDecl(0, true); |
| 46 | } |
| 47 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 48 | // Find the starting module. |
| 49 | Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod); |
| 50 | if (!Context) { |
| 51 | if (Complain) |
| 52 | Diags->Report(Unresolved.Id[0].second, |
| 53 | diag::err_mmap_missing_module_unqualified) |
| 54 | << Unresolved.Id[0].first << Mod->getFullModuleName(); |
| 55 | |
| 56 | return Module::ExportDecl(); |
| 57 | } |
| 58 | |
| 59 | // Dig into the module path. |
| 60 | for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) { |
| 61 | Module *Sub = lookupModuleQualified(Unresolved.Id[I].first, |
| 62 | Context); |
| 63 | if (!Sub) { |
| 64 | if (Complain) |
| 65 | Diags->Report(Unresolved.Id[I].second, |
| 66 | diag::err_mmap_missing_module_qualified) |
| 67 | << Unresolved.Id[I].first << Context->getFullModuleName() |
| 68 | << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second); |
| 69 | |
| 70 | return Module::ExportDecl(); |
| 71 | } |
| 72 | |
| 73 | Context = Sub; |
| 74 | } |
| 75 | |
| 76 | return Module::ExportDecl(Context, Unresolved.Wildcard); |
| 77 | } |
| 78 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 79 | ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC, |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 80 | const LangOptions &LangOpts, const TargetInfo *Target) |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 81 | : LangOpts(LangOpts), Target(Target), BuiltinIncludeDir(0) |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 82 | { |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 83 | IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs); |
| 84 | Diags = IntrusiveRefCntPtr<DiagnosticsEngine>( |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 85 | new DiagnosticsEngine(DiagIDs, new DiagnosticOptions)); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 86 | Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true); |
| 87 | SourceMgr = new SourceManager(*Diags, FileMgr); |
| 88 | } |
| 89 | |
| 90 | ModuleMap::~ModuleMap() { |
Douglas Gregor | 09fe1bb | 2011-11-17 02:05:44 +0000 | [diff] [blame] | 91 | for (llvm::StringMap<Module *>::iterator I = Modules.begin(), |
| 92 | IEnd = Modules.end(); |
| 93 | I != IEnd; ++I) { |
| 94 | delete I->getValue(); |
| 95 | } |
| 96 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 97 | delete SourceMgr; |
| 98 | } |
| 99 | |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 100 | void ModuleMap::setTarget(const TargetInfo &Target) { |
| 101 | assert((!this->Target || this->Target == &Target) && |
| 102 | "Improper target override"); |
| 103 | this->Target = &Target; |
| 104 | } |
| 105 | |
Douglas Gregor | 8b48e08 | 2012-10-12 21:15:50 +0000 | [diff] [blame] | 106 | /// \brief "Sanitize" a filename so that it can be used as an identifier. |
| 107 | static StringRef sanitizeFilenameAsIdentifier(StringRef Name, |
| 108 | SmallVectorImpl<char> &Buffer) { |
| 109 | if (Name.empty()) |
| 110 | return Name; |
| 111 | |
| 112 | // Check whether the filename is already an identifier; this is the common |
| 113 | // case. |
| 114 | bool isIdentifier = true; |
| 115 | for (unsigned I = 0, N = Name.size(); I != N; ++I) { |
| 116 | if (isalpha(Name[I]) || Name[I] == '_' || (isdigit(Name[I]) && I > 0)) |
| 117 | continue; |
| 118 | |
| 119 | isIdentifier = false; |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | if (!isIdentifier) { |
| 124 | // If we don't already have something with the form of an identifier, |
| 125 | // create a buffer with the sanitized name. |
| 126 | Buffer.clear(); |
| 127 | if (isdigit(Name[0])) |
| 128 | Buffer.push_back('_'); |
| 129 | Buffer.reserve(Buffer.size() + Name.size()); |
| 130 | for (unsigned I = 0, N = Name.size(); I != N; ++I) { |
| 131 | if (isalnum(Name[I]) || isspace(Name[I])) |
| 132 | Buffer.push_back(Name[I]); |
| 133 | else |
| 134 | Buffer.push_back('_'); |
| 135 | } |
| 136 | |
| 137 | Name = StringRef(Buffer.data(), Buffer.size()); |
| 138 | } |
| 139 | |
| 140 | while (llvm::StringSwitch<bool>(Name) |
| 141 | #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true) |
| 142 | #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true) |
| 143 | #include "clang/Basic/TokenKinds.def" |
| 144 | .Default(false)) { |
| 145 | if (Name.data() != Buffer.data()) |
| 146 | Buffer.append(Name.begin(), Name.end()); |
| 147 | Buffer.push_back('_'); |
| 148 | Name = StringRef(Buffer.data(), Buffer.size()); |
| 149 | } |
| 150 | |
| 151 | return Name; |
| 152 | } |
| 153 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 154 | Module *ModuleMap::findModuleForHeader(const FileEntry *File) { |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 155 | HeadersMap::iterator Known = Headers.find(File); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 156 | if (Known != Headers.end()) { |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 157 | // If a header is not available, don't report that it maps to anything. |
| 158 | if (!Known->second.isAvailable()) |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 159 | return 0; |
| 160 | |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 161 | return Known->second.getModule(); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 162 | } |
Douglas Gregor | 65f3b5e | 2011-11-11 22:18:48 +0000 | [diff] [blame] | 163 | |
Douglas Gregor | adb9799 | 2011-11-16 23:02:25 +0000 | [diff] [blame] | 164 | const DirectoryEntry *Dir = File->getDir(); |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 165 | SmallVector<const DirectoryEntry *, 2> SkippedDirs; |
Douglas Gregor | 713b7c0 | 2013-01-26 00:55:12 +0000 | [diff] [blame^] | 166 | |
Douglas Gregor | aa60f9c | 2013-01-04 19:44:26 +0000 | [diff] [blame] | 167 | // Note: as an egregious but useful hack we use the real path here, because |
| 168 | // frameworks moving from top-level frameworks to embedded frameworks tend |
| 169 | // to be symlinked from the top-level location to the embedded location, |
| 170 | // and we need to resolve lookups as if we had found the embedded location. |
Douglas Gregor | 713b7c0 | 2013-01-26 00:55:12 +0000 | [diff] [blame^] | 171 | StringRef DirName = SourceMgr->getFileManager().getCanonicalName(Dir); |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 172 | |
| 173 | // Keep walking up the directory hierarchy, looking for a directory with |
| 174 | // an umbrella header. |
| 175 | do { |
| 176 | llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir |
| 177 | = UmbrellaDirs.find(Dir); |
| 178 | if (KnownDir != UmbrellaDirs.end()) { |
| 179 | Module *Result = KnownDir->second; |
Douglas Gregor | 9f74f4f | 2011-12-06 16:17:15 +0000 | [diff] [blame] | 180 | |
| 181 | // Search up the module stack until we find a module with an umbrella |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 182 | // directory. |
Douglas Gregor | 9f74f4f | 2011-12-06 16:17:15 +0000 | [diff] [blame] | 183 | Module *UmbrellaModule = Result; |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 184 | while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) |
Douglas Gregor | 9f74f4f | 2011-12-06 16:17:15 +0000 | [diff] [blame] | 185 | UmbrellaModule = UmbrellaModule->Parent; |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 186 | |
Douglas Gregor | 9f74f4f | 2011-12-06 16:17:15 +0000 | [diff] [blame] | 187 | if (UmbrellaModule->InferSubmodules) { |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 188 | // Infer submodules for each of the directories we found between |
| 189 | // the directory of the umbrella header and the directory where |
| 190 | // the actual header is located. |
Douglas Gregor | 23af6d5 | 2011-12-07 22:05:21 +0000 | [diff] [blame] | 191 | bool Explicit = UmbrellaModule->InferExplicitSubmodules; |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 192 | |
Douglas Gregor | 6a1db48 | 2011-12-09 02:04:43 +0000 | [diff] [blame] | 193 | for (unsigned I = SkippedDirs.size(); I != 0; --I) { |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 194 | // Find or create the module that corresponds to this directory name. |
Douglas Gregor | 8b48e08 | 2012-10-12 21:15:50 +0000 | [diff] [blame] | 195 | SmallString<32> NameBuf; |
| 196 | StringRef Name = sanitizeFilenameAsIdentifier( |
| 197 | llvm::sys::path::stem(SkippedDirs[I-1]->getName()), |
| 198 | NameBuf); |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 199 | Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, |
Douglas Gregor | 23af6d5 | 2011-12-07 22:05:21 +0000 | [diff] [blame] | 200 | Explicit).first; |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 201 | |
| 202 | // Associate the module and the directory. |
| 203 | UmbrellaDirs[SkippedDirs[I-1]] = Result; |
| 204 | |
| 205 | // If inferred submodules export everything they import, add a |
| 206 | // wildcard to the set of exports. |
Douglas Gregor | 9f74f4f | 2011-12-06 16:17:15 +0000 | [diff] [blame] | 207 | if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 208 | Result->Exports.push_back(Module::ExportDecl(0, true)); |
| 209 | } |
| 210 | |
| 211 | // Infer a submodule with the same name as this header file. |
Douglas Gregor | 8b48e08 | 2012-10-12 21:15:50 +0000 | [diff] [blame] | 212 | SmallString<32> NameBuf; |
| 213 | StringRef Name = sanitizeFilenameAsIdentifier( |
| 214 | llvm::sys::path::stem(File->getName()), NameBuf); |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 215 | Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, |
Douglas Gregor | 23af6d5 | 2011-12-07 22:05:21 +0000 | [diff] [blame] | 216 | Explicit).first; |
Argyrios Kyrtzidis | c7782d9 | 2012-10-05 00:22:33 +0000 | [diff] [blame] | 217 | Result->TopHeaders.insert(File); |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 218 | |
| 219 | // If inferred submodules export everything they import, add a |
| 220 | // wildcard to the set of exports. |
Douglas Gregor | 9f74f4f | 2011-12-06 16:17:15 +0000 | [diff] [blame] | 221 | if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 222 | Result->Exports.push_back(Module::ExportDecl(0, true)); |
| 223 | } else { |
| 224 | // Record each of the directories we stepped through as being part of |
| 225 | // the module we found, since the umbrella header covers them all. |
| 226 | for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) |
| 227 | UmbrellaDirs[SkippedDirs[I]] = Result; |
| 228 | } |
| 229 | |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 230 | Headers[File] = KnownHeader(Result, /*Excluded=*/false); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 231 | |
| 232 | // If a header corresponds to an unavailable module, don't report |
| 233 | // that it maps to anything. |
| 234 | if (!Result->isAvailable()) |
| 235 | return 0; |
| 236 | |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 237 | return Result; |
| 238 | } |
| 239 | |
| 240 | SkippedDirs.push_back(Dir); |
| 241 | |
Douglas Gregor | adb9799 | 2011-11-16 23:02:25 +0000 | [diff] [blame] | 242 | // Retrieve our parent path. |
| 243 | DirName = llvm::sys::path::parent_path(DirName); |
| 244 | if (DirName.empty()) |
| 245 | break; |
| 246 | |
| 247 | // Resolve the parent path to a directory entry. |
| 248 | Dir = SourceMgr->getFileManager().getDirectory(DirName); |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 249 | } while (Dir); |
Douglas Gregor | adb9799 | 2011-11-16 23:02:25 +0000 | [diff] [blame] | 250 | |
Douglas Gregor | 65f3b5e | 2011-11-11 22:18:48 +0000 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 254 | bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) { |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 255 | HeadersMap::iterator Known = Headers.find(Header); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 256 | if (Known != Headers.end()) |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 257 | return !Known->second.isAvailable(); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 258 | |
| 259 | const DirectoryEntry *Dir = Header->getDir(); |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 260 | SmallVector<const DirectoryEntry *, 2> SkippedDirs; |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 261 | StringRef DirName = Dir->getName(); |
| 262 | |
| 263 | // Keep walking up the directory hierarchy, looking for a directory with |
| 264 | // an umbrella header. |
| 265 | do { |
| 266 | llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir |
| 267 | = UmbrellaDirs.find(Dir); |
| 268 | if (KnownDir != UmbrellaDirs.end()) { |
| 269 | Module *Found = KnownDir->second; |
| 270 | if (!Found->isAvailable()) |
| 271 | return true; |
| 272 | |
| 273 | // Search up the module stack until we find a module with an umbrella |
| 274 | // directory. |
| 275 | Module *UmbrellaModule = Found; |
| 276 | while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) |
| 277 | UmbrellaModule = UmbrellaModule->Parent; |
| 278 | |
| 279 | if (UmbrellaModule->InferSubmodules) { |
| 280 | for (unsigned I = SkippedDirs.size(); I != 0; --I) { |
| 281 | // Find or create the module that corresponds to this directory name. |
Douglas Gregor | 8b48e08 | 2012-10-12 21:15:50 +0000 | [diff] [blame] | 282 | SmallString<32> NameBuf; |
| 283 | StringRef Name = sanitizeFilenameAsIdentifier( |
| 284 | llvm::sys::path::stem(SkippedDirs[I-1]->getName()), |
| 285 | NameBuf); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 286 | Found = lookupModuleQualified(Name, Found); |
| 287 | if (!Found) |
| 288 | return false; |
| 289 | if (!Found->isAvailable()) |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | // Infer a submodule with the same name as this header file. |
Douglas Gregor | 8b48e08 | 2012-10-12 21:15:50 +0000 | [diff] [blame] | 294 | SmallString<32> NameBuf; |
| 295 | StringRef Name = sanitizeFilenameAsIdentifier( |
| 296 | llvm::sys::path::stem(Header->getName()), |
| 297 | NameBuf); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 298 | Found = lookupModuleQualified(Name, Found); |
| 299 | if (!Found) |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | return !Found->isAvailable(); |
| 304 | } |
| 305 | |
| 306 | SkippedDirs.push_back(Dir); |
| 307 | |
| 308 | // Retrieve our parent path. |
| 309 | DirName = llvm::sys::path::parent_path(DirName); |
| 310 | if (DirName.empty()) |
| 311 | break; |
| 312 | |
| 313 | // Resolve the parent path to a directory entry. |
| 314 | Dir = SourceMgr->getFileManager().getDirectory(DirName); |
| 315 | } while (Dir); |
| 316 | |
| 317 | return false; |
| 318 | } |
| 319 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 320 | Module *ModuleMap::findModule(StringRef Name) { |
Douglas Gregor | 484535e | 2011-11-11 23:20:24 +0000 | [diff] [blame] | 321 | llvm::StringMap<Module *>::iterator Known = Modules.find(Name); |
| 322 | if (Known != Modules.end()) |
| 323 | return Known->getValue(); |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 328 | Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) { |
| 329 | for(; Context; Context = Context->Parent) { |
| 330 | if (Module *Sub = lookupModuleQualified(Name, Context)) |
| 331 | return Sub; |
| 332 | } |
| 333 | |
| 334 | return findModule(Name); |
| 335 | } |
| 336 | |
| 337 | Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) { |
| 338 | if (!Context) |
| 339 | return findModule(Name); |
| 340 | |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 341 | return Context->findSubmodule(Name); |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 344 | std::pair<Module *, bool> |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 345 | ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, |
| 346 | bool IsExplicit) { |
| 347 | // Try to find an existing module with this name. |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 348 | if (Module *Sub = lookupModuleQualified(Name, Parent)) |
| 349 | return std::make_pair(Sub, false); |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 350 | |
| 351 | // Create a new module with this name. |
| 352 | Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework, |
| 353 | IsExplicit); |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 354 | if (!Parent) |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 355 | Modules[Name] = Result; |
| 356 | return std::make_pair(Result, true); |
| 357 | } |
| 358 | |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 359 | bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir, |
| 360 | StringRef Name, bool &IsSystem) { |
| 361 | // Check whether we have already looked into the parent directory |
| 362 | // for a module map. |
| 363 | llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::iterator |
| 364 | inferred = InferredDirectories.find(ParentDir); |
| 365 | if (inferred == InferredDirectories.end()) |
| 366 | return false; |
| 367 | |
| 368 | if (!inferred->second.InferModules) |
| 369 | return false; |
| 370 | |
| 371 | // We're allowed to infer for this directory, but make sure it's okay |
| 372 | // to infer this particular module. |
| 373 | bool canInfer = std::find(inferred->second.ExcludedModules.begin(), |
| 374 | inferred->second.ExcludedModules.end(), |
| 375 | Name) == inferred->second.ExcludedModules.end(); |
| 376 | |
| 377 | if (canInfer && inferred->second.InferSystemModules) |
| 378 | IsSystem = true; |
| 379 | |
| 380 | return canInfer; |
| 381 | } |
| 382 | |
Douglas Gregor | 8767dc2 | 2013-01-14 17:57:51 +0000 | [diff] [blame] | 383 | /// \brief For a framework module, infer the framework against which we |
| 384 | /// should link. |
| 385 | static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir, |
| 386 | FileManager &FileMgr) { |
| 387 | assert(Mod->IsFramework && "Can only infer linking for framework modules"); |
| 388 | assert(!Mod->isSubFramework() && |
| 389 | "Can only infer linking for top-level frameworks"); |
| 390 | |
| 391 | SmallString<128> LibName; |
| 392 | LibName += FrameworkDir->getName(); |
| 393 | llvm::sys::path::append(LibName, Mod->Name); |
| 394 | if (FileMgr.getFile(LibName)) { |
| 395 | Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name, |
| 396 | /*IsFramework=*/true)); |
| 397 | } |
| 398 | } |
| 399 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 400 | Module * |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 401 | ModuleMap::inferFrameworkModule(StringRef ModuleName, |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 402 | const DirectoryEntry *FrameworkDir, |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 403 | bool IsSystem, |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 404 | Module *Parent) { |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 405 | // Check whether we've already found this module. |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 406 | if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) |
| 407 | return Mod; |
| 408 | |
| 409 | FileManager &FileMgr = SourceMgr->getFileManager(); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 410 | |
| 411 | // If the framework has a parent path from which we're allowed to infer |
| 412 | // a framework module, do so. |
| 413 | if (!Parent) { |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 414 | // Determine whether we're allowed to infer a module map. |
Douglas Gregor | 713b7c0 | 2013-01-26 00:55:12 +0000 | [diff] [blame^] | 415 | |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 416 | // Note: as an egregious but useful hack we use the real path here, because |
| 417 | // we might be looking at an embedded framework that symlinks out to a |
| 418 | // top-level framework, and we need to infer as if we were naming the |
| 419 | // top-level framework. |
Douglas Gregor | 713b7c0 | 2013-01-26 00:55:12 +0000 | [diff] [blame^] | 420 | StringRef FrameworkDirName |
| 421 | = SourceMgr->getFileManager().getCanonicalName(FrameworkDir); |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 422 | |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 423 | bool canInfer = false; |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 424 | if (llvm::sys::path::has_parent_path(FrameworkDirName)) { |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 425 | // Figure out the parent path. |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 426 | StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 427 | if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) { |
| 428 | // Check whether we have already looked into the parent directory |
| 429 | // for a module map. |
| 430 | llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::iterator |
| 431 | inferred = InferredDirectories.find(ParentDir); |
| 432 | if (inferred == InferredDirectories.end()) { |
| 433 | // We haven't looked here before. Load a module map, if there is |
| 434 | // one. |
| 435 | SmallString<128> ModMapPath = Parent; |
| 436 | llvm::sys::path::append(ModMapPath, "module.map"); |
| 437 | if (const FileEntry *ModMapFile = FileMgr.getFile(ModMapPath)) { |
| 438 | parseModuleMapFile(ModMapFile); |
| 439 | inferred = InferredDirectories.find(ParentDir); |
| 440 | } |
| 441 | |
| 442 | if (inferred == InferredDirectories.end()) |
| 443 | inferred = InferredDirectories.insert( |
| 444 | std::make_pair(ParentDir, InferredDirectory())).first; |
| 445 | } |
| 446 | |
| 447 | if (inferred->second.InferModules) { |
| 448 | // We're allowed to infer for this directory, but make sure it's okay |
| 449 | // to infer this particular module. |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 450 | StringRef Name = llvm::sys::path::stem(FrameworkDirName); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 451 | canInfer = std::find(inferred->second.ExcludedModules.begin(), |
| 452 | inferred->second.ExcludedModules.end(), |
| 453 | Name) == inferred->second.ExcludedModules.end(); |
| 454 | |
| 455 | if (inferred->second.InferSystemModules) |
| 456 | IsSystem = true; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // If we're not allowed to infer a framework module, don't. |
| 462 | if (!canInfer) |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 467 | // Look for an umbrella header. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 468 | SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 469 | llvm::sys::path::append(UmbrellaName, "Headers"); |
| 470 | llvm::sys::path::append(UmbrellaName, ModuleName + ".h"); |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 471 | const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 472 | |
| 473 | // FIXME: If there's no umbrella header, we could probably scan the |
| 474 | // framework to load *everything*. But, it's not clear that this is a good |
| 475 | // idea. |
| 476 | if (!UmbrellaHeader) |
| 477 | return 0; |
| 478 | |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 479 | Module *Result = new Module(ModuleName, SourceLocation(), Parent, |
| 480 | /*IsFramework=*/true, /*IsExplicit=*/false); |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 481 | if (IsSystem) |
| 482 | Result->IsSystem = IsSystem; |
| 483 | |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 484 | if (!Parent) |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 485 | Modules[ModuleName] = Result; |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 486 | |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 487 | // umbrella header "umbrella-header-name" |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 488 | Result->Umbrella = UmbrellaHeader; |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 489 | Headers[UmbrellaHeader] = KnownHeader(Result, /*Excluded=*/false); |
Douglas Gregor | 3cee31e | 2011-12-12 23:55:05 +0000 | [diff] [blame] | 490 | UmbrellaDirs[UmbrellaHeader->getDir()] = Result; |
Douglas Gregor | 209977c | 2011-12-05 17:40:25 +0000 | [diff] [blame] | 491 | |
| 492 | // export * |
| 493 | Result->Exports.push_back(Module::ExportDecl(0, true)); |
| 494 | |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 495 | // module * { export * } |
| 496 | Result->InferSubmodules = true; |
| 497 | Result->InferExportWildcard = true; |
| 498 | |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 499 | // Look for subframeworks. |
| 500 | llvm::error_code EC; |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 501 | SmallString<128> SubframeworksDirName |
Douglas Gregor | 52b1ed3 | 2011-12-08 16:13:24 +0000 | [diff] [blame] | 502 | = StringRef(FrameworkDir->getName()); |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 503 | llvm::sys::path::append(SubframeworksDirName, "Frameworks"); |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 504 | SmallString<128> SubframeworksDirNameNative; |
Douglas Gregor | 52b1ed3 | 2011-12-08 16:13:24 +0000 | [diff] [blame] | 505 | llvm::sys::path::native(SubframeworksDirName.str(), |
| 506 | SubframeworksDirNameNative); |
| 507 | for (llvm::sys::fs::directory_iterator |
| 508 | Dir(SubframeworksDirNameNative.str(), EC), DirEnd; |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 509 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
| 510 | if (!StringRef(Dir->path()).endswith(".framework")) |
| 511 | continue; |
Douglas Gregor | 98cfcbf | 2012-09-27 14:50:15 +0000 | [diff] [blame] | 512 | |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 513 | if (const DirectoryEntry *SubframeworkDir |
| 514 | = FileMgr.getDirectory(Dir->path())) { |
Douglas Gregor | 98cfcbf | 2012-09-27 14:50:15 +0000 | [diff] [blame] | 515 | // Note: as an egregious but useful hack, we use the real path here and |
| 516 | // check whether it is actually a subdirectory of the parent directory. |
| 517 | // This will not be the case if the 'subframework' is actually a symlink |
| 518 | // out to a top-level framework. |
Douglas Gregor | 713b7c0 | 2013-01-26 00:55:12 +0000 | [diff] [blame^] | 519 | StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir); |
| 520 | bool FoundParent = false; |
| 521 | do { |
| 522 | // Get the parent directory name. |
| 523 | SubframeworkDirName |
| 524 | = llvm::sys::path::parent_path(SubframeworkDirName); |
| 525 | if (SubframeworkDirName.empty()) |
| 526 | break; |
Douglas Gregor | 98cfcbf | 2012-09-27 14:50:15 +0000 | [diff] [blame] | 527 | |
Douglas Gregor | 713b7c0 | 2013-01-26 00:55:12 +0000 | [diff] [blame^] | 528 | if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { |
| 529 | FoundParent = true; |
| 530 | break; |
| 531 | } |
| 532 | } while (true); |
Douglas Gregor | 98cfcbf | 2012-09-27 14:50:15 +0000 | [diff] [blame] | 533 | |
Douglas Gregor | 713b7c0 | 2013-01-26 00:55:12 +0000 | [diff] [blame^] | 534 | if (!FoundParent) |
| 535 | continue; |
Douglas Gregor | 98cfcbf | 2012-09-27 14:50:15 +0000 | [diff] [blame] | 536 | |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 537 | // FIXME: Do we want to warn about subframeworks without umbrella headers? |
Douglas Gregor | 8b48e08 | 2012-10-12 21:15:50 +0000 | [diff] [blame] | 538 | SmallString<32> NameBuf; |
| 539 | inferFrameworkModule(sanitizeFilenameAsIdentifier( |
| 540 | llvm::sys::path::stem(Dir->path()), NameBuf), |
| 541 | SubframeworkDir, IsSystem, Result); |
Douglas Gregor | ac252a3 | 2011-12-06 19:39:29 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
Douglas Gregor | 3a110f7 | 2012-01-13 16:54:27 +0000 | [diff] [blame] | 544 | |
Douglas Gregor | 8767dc2 | 2013-01-14 17:57:51 +0000 | [diff] [blame] | 545 | // If the module is a top-level framework, automatically link against the |
| 546 | // framework. |
| 547 | if (!Result->isSubFramework()) { |
| 548 | inferFrameworkLink(Result, FrameworkDir, FileMgr); |
| 549 | } |
| 550 | |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 551 | return Result; |
| 552 | } |
| 553 | |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 554 | void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 555 | Headers[UmbrellaHeader] = KnownHeader(Mod, /*Excluded=*/false); |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 556 | Mod->Umbrella = UmbrellaHeader; |
Douglas Gregor | 6a1db48 | 2011-12-09 02:04:43 +0000 | [diff] [blame] | 557 | UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 560 | void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) { |
| 561 | Mod->Umbrella = UmbrellaDir; |
| 562 | UmbrellaDirs[UmbrellaDir] = Mod; |
| 563 | } |
| 564 | |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 565 | void ModuleMap::addHeader(Module *Mod, const FileEntry *Header, |
| 566 | bool Excluded) { |
| 567 | if (Excluded) |
| 568 | Mod->ExcludedHeaders.push_back(Header); |
| 569 | else |
| 570 | Mod->Headers.push_back(Header); |
| 571 | Headers[Header] = KnownHeader(Mod, Excluded); |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Douglas Gregor | f9e357d | 2011-11-29 19:06:37 +0000 | [diff] [blame] | 574 | const FileEntry * |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 575 | ModuleMap::getContainingModuleMapFile(Module *Module) { |
Douglas Gregor | f9e357d | 2011-11-29 19:06:37 +0000 | [diff] [blame] | 576 | if (Module->DefinitionLoc.isInvalid() || !SourceMgr) |
| 577 | return 0; |
| 578 | |
| 579 | return SourceMgr->getFileEntryForID( |
| 580 | SourceMgr->getFileID(Module->DefinitionLoc)); |
| 581 | } |
| 582 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 583 | void ModuleMap::dump() { |
| 584 | llvm::errs() << "Modules:"; |
| 585 | for (llvm::StringMap<Module *>::iterator M = Modules.begin(), |
| 586 | MEnd = Modules.end(); |
| 587 | M != MEnd; ++M) |
Douglas Gregor | 804c3bf | 2011-11-29 18:17:59 +0000 | [diff] [blame] | 588 | M->getValue()->print(llvm::errs(), 2); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 589 | |
| 590 | llvm::errs() << "Headers:"; |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 591 | for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 592 | H != HEnd; ++H) { |
| 593 | llvm::errs() << " \"" << H->first->getName() << "\" -> " |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 594 | << H->second.getModule()->getFullModuleName() << "\n"; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 598 | bool ModuleMap::resolveExports(Module *Mod, bool Complain) { |
| 599 | bool HadError = false; |
| 600 | for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { |
| 601 | Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], |
| 602 | Complain); |
Douglas Gregor | 0adaa88 | 2011-12-05 17:28:06 +0000 | [diff] [blame] | 603 | if (Export.getPointer() || Export.getInt()) |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 604 | Mod->Exports.push_back(Export); |
| 605 | else |
| 606 | HadError = true; |
| 607 | } |
| 608 | Mod->UnresolvedExports.clear(); |
| 609 | return HadError; |
| 610 | } |
| 611 | |
Douglas Gregor | 5598868 | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 612 | Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { |
| 613 | if (Loc.isInvalid()) |
| 614 | return 0; |
| 615 | |
| 616 | // Use the expansion location to determine which module we're in. |
| 617 | FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); |
| 618 | if (!ExpansionLoc.isFileID()) |
| 619 | return 0; |
| 620 | |
| 621 | |
| 622 | const SourceManager &SrcMgr = Loc.getManager(); |
| 623 | FileID ExpansionFileID = ExpansionLoc.getFileID(); |
Douglas Gregor | 5598868 | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 624 | |
Douglas Gregor | 303aae9 | 2012-01-06 17:19:32 +0000 | [diff] [blame] | 625 | while (const FileEntry *ExpansionFile |
| 626 | = SrcMgr.getFileEntryForID(ExpansionFileID)) { |
| 627 | // Find the module that owns this header (if any). |
| 628 | if (Module *Mod = findModuleForHeader(ExpansionFile)) |
| 629 | return Mod; |
| 630 | |
| 631 | // No module owns this header, so look up the inclusion chain to see if |
| 632 | // any included header has an associated module. |
| 633 | SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); |
| 634 | if (IncludeLoc.isInvalid()) |
| 635 | return 0; |
| 636 | |
| 637 | ExpansionFileID = SrcMgr.getFileID(IncludeLoc); |
| 638 | } |
| 639 | |
| 640 | return 0; |
Douglas Gregor | 5598868 | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 643 | //----------------------------------------------------------------------------// |
| 644 | // Module map file parser |
| 645 | //----------------------------------------------------------------------------// |
| 646 | |
| 647 | namespace clang { |
| 648 | /// \brief A token in a module map file. |
| 649 | struct MMToken { |
| 650 | enum TokenKind { |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 651 | Comma, |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 652 | EndOfFile, |
| 653 | HeaderKeyword, |
| 654 | Identifier, |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 655 | ExcludeKeyword, |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 656 | ExplicitKeyword, |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 657 | ExportKeyword, |
Douglas Gregor | a865405 | 2011-11-17 22:09:43 +0000 | [diff] [blame] | 658 | FrameworkKeyword, |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 659 | LinkKeyword, |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 660 | ModuleKeyword, |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 661 | Period, |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 662 | UmbrellaKeyword, |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 663 | RequiresKeyword, |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 664 | Star, |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 665 | StringLiteral, |
| 666 | LBrace, |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 667 | RBrace, |
| 668 | LSquare, |
| 669 | RSquare |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 670 | } Kind; |
| 671 | |
| 672 | unsigned Location; |
| 673 | unsigned StringLength; |
| 674 | const char *StringData; |
| 675 | |
| 676 | void clear() { |
| 677 | Kind = EndOfFile; |
| 678 | Location = 0; |
| 679 | StringLength = 0; |
| 680 | StringData = 0; |
| 681 | } |
| 682 | |
| 683 | bool is(TokenKind K) const { return Kind == K; } |
| 684 | |
| 685 | SourceLocation getLocation() const { |
| 686 | return SourceLocation::getFromRawEncoding(Location); |
| 687 | } |
| 688 | |
| 689 | StringRef getString() const { |
| 690 | return StringRef(StringData, StringLength); |
| 691 | } |
| 692 | }; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 693 | |
| 694 | /// \brief The set of attributes that can be attached to a module. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 695 | struct Attributes { |
| 696 | Attributes() : IsSystem() { } |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 697 | |
| 698 | /// \brief Whether this is a system module. |
| 699 | unsigned IsSystem : 1; |
| 700 | }; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 701 | |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 702 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 703 | class ModuleMapParser { |
| 704 | Lexer &L; |
| 705 | SourceManager &SourceMgr; |
Douglas Gregor | 9a022bb | 2012-10-15 16:45:32 +0000 | [diff] [blame] | 706 | |
| 707 | /// \brief Default target information, used only for string literal |
| 708 | /// parsing. |
| 709 | const TargetInfo *Target; |
| 710 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 711 | DiagnosticsEngine &Diags; |
| 712 | ModuleMap ⤅ |
| 713 | |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 714 | /// \brief The directory that this module map resides in. |
| 715 | const DirectoryEntry *Directory; |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 716 | |
| 717 | /// \brief The directory containing Clang-supplied headers. |
| 718 | const DirectoryEntry *BuiltinIncludeDir; |
| 719 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 720 | /// \brief Whether an error occurred. |
| 721 | bool HadError; |
Douglas Gregor | 9a022bb | 2012-10-15 16:45:32 +0000 | [diff] [blame] | 722 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 723 | /// \brief Stores string data for the various string literals referenced |
| 724 | /// during parsing. |
| 725 | llvm::BumpPtrAllocator StringData; |
| 726 | |
| 727 | /// \brief The current token. |
| 728 | MMToken Tok; |
| 729 | |
| 730 | /// \brief The active module. |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 731 | Module *ActiveModule; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 732 | |
| 733 | /// \brief Consume the current token and return its location. |
| 734 | SourceLocation consumeToken(); |
| 735 | |
| 736 | /// \brief Skip tokens until we reach the a token with the given kind |
| 737 | /// (or the end of the file). |
| 738 | void skipUntil(MMToken::TokenKind K); |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 739 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 740 | typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 741 | bool parseModuleId(ModuleId &Id); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 742 | void parseModuleDecl(); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 743 | void parseRequiresDecl(); |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 744 | void parseHeaderDecl(SourceLocation UmbrellaLoc, SourceLocation ExcludeLoc); |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 745 | void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 746 | void parseExportDecl(); |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 747 | void parseLinkDecl(); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 748 | void parseInferredModuleDecl(bool Framework, bool Explicit); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 749 | bool parseOptionalAttributes(Attributes &Attrs); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 750 | |
Douglas Gregor | 6a1db48 | 2011-12-09 02:04:43 +0000 | [diff] [blame] | 751 | const DirectoryEntry *getOverriddenHeaderSearchDir(); |
| 752 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 753 | public: |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 754 | explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, |
Douglas Gregor | 9a022bb | 2012-10-15 16:45:32 +0000 | [diff] [blame] | 755 | const TargetInfo *Target, |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 756 | DiagnosticsEngine &Diags, |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 757 | ModuleMap &Map, |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 758 | const DirectoryEntry *Directory, |
| 759 | const DirectoryEntry *BuiltinIncludeDir) |
Douglas Gregor | 9a022bb | 2012-10-15 16:45:32 +0000 | [diff] [blame] | 760 | : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 761 | Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir), |
| 762 | HadError(false), ActiveModule(0) |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 763 | { |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 764 | Tok.clear(); |
| 765 | consumeToken(); |
| 766 | } |
| 767 | |
| 768 | bool parseModuleMapFile(); |
| 769 | }; |
| 770 | } |
| 771 | |
| 772 | SourceLocation ModuleMapParser::consumeToken() { |
| 773 | retry: |
| 774 | SourceLocation Result = Tok.getLocation(); |
| 775 | Tok.clear(); |
| 776 | |
| 777 | Token LToken; |
| 778 | L.LexFromRawLexer(LToken); |
| 779 | Tok.Location = LToken.getLocation().getRawEncoding(); |
| 780 | switch (LToken.getKind()) { |
| 781 | case tok::raw_identifier: |
| 782 | Tok.StringData = LToken.getRawIdentifierData(); |
| 783 | Tok.StringLength = LToken.getLength(); |
| 784 | Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString()) |
| 785 | .Case("header", MMToken::HeaderKeyword) |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 786 | .Case("exclude", MMToken::ExcludeKeyword) |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 787 | .Case("explicit", MMToken::ExplicitKeyword) |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 788 | .Case("export", MMToken::ExportKeyword) |
Douglas Gregor | a865405 | 2011-11-17 22:09:43 +0000 | [diff] [blame] | 789 | .Case("framework", MMToken::FrameworkKeyword) |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 790 | .Case("link", MMToken::LinkKeyword) |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 791 | .Case("module", MMToken::ModuleKeyword) |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 792 | .Case("requires", MMToken::RequiresKeyword) |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 793 | .Case("umbrella", MMToken::UmbrellaKeyword) |
| 794 | .Default(MMToken::Identifier); |
| 795 | break; |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 796 | |
| 797 | case tok::comma: |
| 798 | Tok.Kind = MMToken::Comma; |
| 799 | break; |
| 800 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 801 | case tok::eof: |
| 802 | Tok.Kind = MMToken::EndOfFile; |
| 803 | break; |
| 804 | |
| 805 | case tok::l_brace: |
| 806 | Tok.Kind = MMToken::LBrace; |
| 807 | break; |
| 808 | |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 809 | case tok::l_square: |
| 810 | Tok.Kind = MMToken::LSquare; |
| 811 | break; |
| 812 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 813 | case tok::period: |
| 814 | Tok.Kind = MMToken::Period; |
| 815 | break; |
| 816 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 817 | case tok::r_brace: |
| 818 | Tok.Kind = MMToken::RBrace; |
| 819 | break; |
| 820 | |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 821 | case tok::r_square: |
| 822 | Tok.Kind = MMToken::RSquare; |
| 823 | break; |
| 824 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 825 | case tok::star: |
| 826 | Tok.Kind = MMToken::Star; |
| 827 | break; |
| 828 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 829 | case tok::string_literal: { |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 830 | if (LToken.hasUDSuffix()) { |
| 831 | Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); |
| 832 | HadError = true; |
| 833 | goto retry; |
| 834 | } |
| 835 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 836 | // Parse the string literal. |
| 837 | LangOptions LangOpts; |
| 838 | StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target); |
| 839 | if (StringLiteral.hadError) |
| 840 | goto retry; |
| 841 | |
| 842 | // Copy the string literal into our string data allocator. |
| 843 | unsigned Length = StringLiteral.GetStringLength(); |
| 844 | char *Saved = StringData.Allocate<char>(Length + 1); |
| 845 | memcpy(Saved, StringLiteral.GetString().data(), Length); |
| 846 | Saved[Length] = 0; |
| 847 | |
| 848 | // Form the token. |
| 849 | Tok.Kind = MMToken::StringLiteral; |
| 850 | Tok.StringData = Saved; |
| 851 | Tok.StringLength = Length; |
| 852 | break; |
| 853 | } |
| 854 | |
| 855 | case tok::comment: |
| 856 | goto retry; |
| 857 | |
| 858 | default: |
| 859 | Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); |
| 860 | HadError = true; |
| 861 | goto retry; |
| 862 | } |
| 863 | |
| 864 | return Result; |
| 865 | } |
| 866 | |
| 867 | void ModuleMapParser::skipUntil(MMToken::TokenKind K) { |
| 868 | unsigned braceDepth = 0; |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 869 | unsigned squareDepth = 0; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 870 | do { |
| 871 | switch (Tok.Kind) { |
| 872 | case MMToken::EndOfFile: |
| 873 | return; |
| 874 | |
| 875 | case MMToken::LBrace: |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 876 | if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 877 | return; |
| 878 | |
| 879 | ++braceDepth; |
| 880 | break; |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 881 | |
| 882 | case MMToken::LSquare: |
| 883 | if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) |
| 884 | return; |
| 885 | |
| 886 | ++squareDepth; |
| 887 | break; |
| 888 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 889 | case MMToken::RBrace: |
| 890 | if (braceDepth > 0) |
| 891 | --braceDepth; |
| 892 | else if (Tok.is(K)) |
| 893 | return; |
| 894 | break; |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 895 | |
| 896 | case MMToken::RSquare: |
| 897 | if (squareDepth > 0) |
| 898 | --squareDepth; |
| 899 | else if (Tok.is(K)) |
| 900 | return; |
| 901 | break; |
| 902 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 903 | default: |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 904 | if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 905 | return; |
| 906 | break; |
| 907 | } |
| 908 | |
| 909 | consumeToken(); |
| 910 | } while (true); |
| 911 | } |
| 912 | |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 913 | /// \brief Parse a module-id. |
| 914 | /// |
| 915 | /// module-id: |
| 916 | /// identifier |
| 917 | /// identifier '.' module-id |
| 918 | /// |
| 919 | /// \returns true if an error occurred, false otherwise. |
| 920 | bool ModuleMapParser::parseModuleId(ModuleId &Id) { |
| 921 | Id.clear(); |
| 922 | do { |
| 923 | if (Tok.is(MMToken::Identifier)) { |
| 924 | Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); |
| 925 | consumeToken(); |
| 926 | } else { |
| 927 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); |
| 928 | return true; |
| 929 | } |
| 930 | |
| 931 | if (!Tok.is(MMToken::Period)) |
| 932 | break; |
| 933 | |
| 934 | consumeToken(); |
| 935 | } while (true); |
| 936 | |
| 937 | return false; |
| 938 | } |
| 939 | |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 940 | namespace { |
| 941 | /// \brief Enumerates the known attributes. |
| 942 | enum AttributeKind { |
| 943 | /// \brief An unknown attribute. |
| 944 | AT_unknown, |
| 945 | /// \brief The 'system' attribute. |
| 946 | AT_system |
| 947 | }; |
| 948 | } |
| 949 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 950 | /// \brief Parse a module declaration. |
| 951 | /// |
| 952 | /// module-declaration: |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 953 | /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] |
| 954 | /// { module-member* } |
| 955 | /// |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 956 | /// module-member: |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 957 | /// requires-declaration |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 958 | /// header-declaration |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 959 | /// submodule-declaration |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 960 | /// export-declaration |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 961 | /// link-declaration |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 962 | /// |
| 963 | /// submodule-declaration: |
| 964 | /// module-declaration |
| 965 | /// inferred-submodule-declaration |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 966 | void ModuleMapParser::parseModuleDecl() { |
Douglas Gregor | a865405 | 2011-11-17 22:09:43 +0000 | [diff] [blame] | 967 | assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || |
| 968 | Tok.is(MMToken::FrameworkKeyword)); |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 969 | // Parse 'explicit' or 'framework' keyword, if present. |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 970 | SourceLocation ExplicitLoc; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 971 | bool Explicit = false; |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 972 | bool Framework = false; |
Douglas Gregor | a865405 | 2011-11-17 22:09:43 +0000 | [diff] [blame] | 973 | |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 974 | // Parse 'explicit' keyword, if present. |
| 975 | if (Tok.is(MMToken::ExplicitKeyword)) { |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 976 | ExplicitLoc = consumeToken(); |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 977 | Explicit = true; |
| 978 | } |
| 979 | |
| 980 | // Parse 'framework' keyword, if present. |
Douglas Gregor | a865405 | 2011-11-17 22:09:43 +0000 | [diff] [blame] | 981 | if (Tok.is(MMToken::FrameworkKeyword)) { |
| 982 | consumeToken(); |
| 983 | Framework = true; |
| 984 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 985 | |
| 986 | // Parse 'module' keyword. |
| 987 | if (!Tok.is(MMToken::ModuleKeyword)) { |
Douglas Gregor | e6fb987 | 2011-12-06 19:57:48 +0000 | [diff] [blame] | 988 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 989 | consumeToken(); |
| 990 | HadError = true; |
| 991 | return; |
| 992 | } |
| 993 | consumeToken(); // 'module' keyword |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 994 | |
| 995 | // If we have a wildcard for the module name, this is an inferred submodule. |
| 996 | // Parse it. |
| 997 | if (Tok.is(MMToken::Star)) |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 998 | return parseInferredModuleDecl(Framework, Explicit); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 999 | |
| 1000 | // Parse the module name. |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1001 | ModuleId Id; |
| 1002 | if (parseModuleId(Id)) { |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1003 | HadError = true; |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1004 | return; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1005 | } |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1006 | |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1007 | if (ActiveModule) { |
| 1008 | if (Id.size() > 1) { |
| 1009 | Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) |
| 1010 | << SourceRange(Id.front().second, Id.back().second); |
| 1011 | |
| 1012 | HadError = true; |
| 1013 | return; |
| 1014 | } |
| 1015 | } else if (Id.size() == 1 && Explicit) { |
| 1016 | // Top-level modules can't be explicit. |
| 1017 | Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); |
| 1018 | Explicit = false; |
| 1019 | ExplicitLoc = SourceLocation(); |
| 1020 | HadError = true; |
| 1021 | } |
| 1022 | |
| 1023 | Module *PreviousActiveModule = ActiveModule; |
| 1024 | if (Id.size() > 1) { |
| 1025 | // This module map defines a submodule. Go find the module of which it |
| 1026 | // is a submodule. |
| 1027 | ActiveModule = 0; |
| 1028 | for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { |
| 1029 | if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { |
| 1030 | ActiveModule = Next; |
| 1031 | continue; |
| 1032 | } |
| 1033 | |
| 1034 | if (ActiveModule) { |
| 1035 | Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) |
| 1036 | << Id[I].first << ActiveModule->getTopLevelModule(); |
| 1037 | } else { |
| 1038 | Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); |
| 1039 | } |
| 1040 | HadError = true; |
| 1041 | return; |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | StringRef ModuleName = Id.back().first; |
| 1046 | SourceLocation ModuleNameLoc = Id.back().second; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1047 | |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1048 | // Parse the optional attribute list. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1049 | Attributes Attrs; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1050 | parseOptionalAttributes(Attrs); |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1051 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1052 | // Parse the opening brace. |
| 1053 | if (!Tok.is(MMToken::LBrace)) { |
| 1054 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) |
| 1055 | << ModuleName; |
| 1056 | HadError = true; |
| 1057 | return; |
| 1058 | } |
| 1059 | SourceLocation LBraceLoc = consumeToken(); |
| 1060 | |
| 1061 | // Determine whether this (sub)module has already been defined. |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1062 | if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { |
Douglas Gregor | c634f50 | 2012-01-05 00:12:00 +0000 | [diff] [blame] | 1063 | if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { |
| 1064 | // Skip the module definition. |
| 1065 | skipUntil(MMToken::RBrace); |
| 1066 | if (Tok.is(MMToken::RBrace)) |
| 1067 | consumeToken(); |
| 1068 | else { |
| 1069 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); |
| 1070 | Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); |
| 1071 | HadError = true; |
| 1072 | } |
| 1073 | return; |
| 1074 | } |
| 1075 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1076 | Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) |
| 1077 | << ModuleName; |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1078 | Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1079 | |
| 1080 | // Skip the module definition. |
| 1081 | skipUntil(MMToken::RBrace); |
| 1082 | if (Tok.is(MMToken::RBrace)) |
| 1083 | consumeToken(); |
| 1084 | |
| 1085 | HadError = true; |
| 1086 | return; |
| 1087 | } |
| 1088 | |
| 1089 | // Start defining this module. |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1090 | ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, |
| 1091 | Explicit).first; |
| 1092 | ActiveModule->DefinitionLoc = ModuleNameLoc; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1093 | if (Attrs.IsSystem) |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1094 | ActiveModule->IsSystem = true; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1095 | |
| 1096 | bool Done = false; |
| 1097 | do { |
| 1098 | switch (Tok.Kind) { |
| 1099 | case MMToken::EndOfFile: |
| 1100 | case MMToken::RBrace: |
| 1101 | Done = true; |
| 1102 | break; |
| 1103 | |
| 1104 | case MMToken::ExplicitKeyword: |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1105 | case MMToken::FrameworkKeyword: |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1106 | case MMToken::ModuleKeyword: |
| 1107 | parseModuleDecl(); |
| 1108 | break; |
| 1109 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1110 | case MMToken::ExportKeyword: |
| 1111 | parseExportDecl(); |
| 1112 | break; |
| 1113 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1114 | case MMToken::RequiresKeyword: |
| 1115 | parseRequiresDecl(); |
| 1116 | break; |
| 1117 | |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1118 | case MMToken::UmbrellaKeyword: { |
| 1119 | SourceLocation UmbrellaLoc = consumeToken(); |
| 1120 | if (Tok.is(MMToken::HeaderKeyword)) |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1121 | parseHeaderDecl(UmbrellaLoc, SourceLocation()); |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1122 | else |
| 1123 | parseUmbrellaDirDecl(UmbrellaLoc); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1124 | break; |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1125 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1126 | |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1127 | case MMToken::ExcludeKeyword: { |
| 1128 | SourceLocation ExcludeLoc = consumeToken(); |
| 1129 | if (Tok.is(MMToken::HeaderKeyword)) { |
| 1130 | parseHeaderDecl(SourceLocation(), ExcludeLoc); |
| 1131 | } else { |
| 1132 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) |
| 1133 | << "exclude"; |
| 1134 | } |
| 1135 | break; |
| 1136 | } |
| 1137 | |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 1138 | case MMToken::HeaderKeyword: |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1139 | parseHeaderDecl(SourceLocation(), SourceLocation()); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1140 | break; |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 1141 | |
| 1142 | case MMToken::LinkKeyword: |
| 1143 | parseLinkDecl(); |
| 1144 | break; |
| 1145 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1146 | default: |
| 1147 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); |
| 1148 | consumeToken(); |
| 1149 | break; |
| 1150 | } |
| 1151 | } while (!Done); |
| 1152 | |
| 1153 | if (Tok.is(MMToken::RBrace)) |
| 1154 | consumeToken(); |
| 1155 | else { |
| 1156 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); |
| 1157 | Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); |
| 1158 | HadError = true; |
| 1159 | } |
| 1160 | |
Douglas Gregor | 8767dc2 | 2013-01-14 17:57:51 +0000 | [diff] [blame] | 1161 | // If the active module is a top-level framework, and there are no link |
| 1162 | // libraries, automatically link against the framework. |
| 1163 | if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() && |
| 1164 | ActiveModule->LinkLibraries.empty()) { |
| 1165 | inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager()); |
| 1166 | } |
| 1167 | |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1168 | // We're done parsing this module. Pop back to the previous module. |
| 1169 | ActiveModule = PreviousActiveModule; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1170 | } |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1171 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1172 | /// \brief Parse a requires declaration. |
| 1173 | /// |
| 1174 | /// requires-declaration: |
| 1175 | /// 'requires' feature-list |
| 1176 | /// |
| 1177 | /// feature-list: |
| 1178 | /// identifier ',' feature-list |
| 1179 | /// identifier |
| 1180 | void ModuleMapParser::parseRequiresDecl() { |
| 1181 | assert(Tok.is(MMToken::RequiresKeyword)); |
| 1182 | |
| 1183 | // Parse 'requires' keyword. |
| 1184 | consumeToken(); |
| 1185 | |
| 1186 | // Parse the feature-list. |
| 1187 | do { |
| 1188 | if (!Tok.is(MMToken::Identifier)) { |
| 1189 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); |
| 1190 | HadError = true; |
| 1191 | return; |
| 1192 | } |
| 1193 | |
| 1194 | // Consume the feature name. |
| 1195 | std::string Feature = Tok.getString(); |
| 1196 | consumeToken(); |
| 1197 | |
| 1198 | // Add this feature. |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 1199 | ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1200 | |
| 1201 | if (!Tok.is(MMToken::Comma)) |
| 1202 | break; |
| 1203 | |
| 1204 | // Consume the comma. |
| 1205 | consumeToken(); |
| 1206 | } while (true); |
| 1207 | } |
| 1208 | |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1209 | /// \brief Append to \p Paths the set of paths needed to get to the |
| 1210 | /// subframework in which the given module lives. |
Benjamin Kramer | 5bbc385 | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 1211 | static void appendSubframeworkPaths(Module *Mod, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1212 | SmallVectorImpl<char> &Path) { |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1213 | // Collect the framework names from the given module to the top-level module. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1214 | SmallVector<StringRef, 2> Paths; |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1215 | for (; Mod; Mod = Mod->Parent) { |
| 1216 | if (Mod->IsFramework) |
| 1217 | Paths.push_back(Mod->Name); |
| 1218 | } |
| 1219 | |
| 1220 | if (Paths.empty()) |
| 1221 | return; |
| 1222 | |
| 1223 | // Add Frameworks/Name.framework for each subframework. |
| 1224 | for (unsigned I = Paths.size() - 1; I != 0; --I) { |
| 1225 | llvm::sys::path::append(Path, "Frameworks"); |
| 1226 | llvm::sys::path::append(Path, Paths[I-1] + ".framework"); |
| 1227 | } |
| 1228 | } |
| 1229 | |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 1230 | /// \brief Determine whether the given file name is the name of a builtin |
| 1231 | /// header, supplied by Clang to replace, override, or augment existing system |
| 1232 | /// headers. |
| 1233 | static bool isBuiltinHeader(StringRef FileName) { |
| 1234 | return llvm::StringSwitch<bool>(FileName) |
| 1235 | .Case("float.h", true) |
| 1236 | .Case("iso646.h", true) |
| 1237 | .Case("limits.h", true) |
| 1238 | .Case("stdalign.h", true) |
| 1239 | .Case("stdarg.h", true) |
| 1240 | .Case("stdbool.h", true) |
| 1241 | .Case("stddef.h", true) |
| 1242 | .Case("stdint.h", true) |
| 1243 | .Case("tgmath.h", true) |
| 1244 | .Case("unwind.h", true) |
| 1245 | .Default(false); |
| 1246 | } |
| 1247 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1248 | /// \brief Parse a header declaration. |
| 1249 | /// |
| 1250 | /// header-declaration: |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 1251 | /// 'umbrella'[opt] 'header' string-literal |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1252 | /// 'exclude'[opt] 'header' string-literal |
| 1253 | void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc, |
| 1254 | SourceLocation ExcludeLoc) { |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1255 | assert(Tok.is(MMToken::HeaderKeyword)); |
Benjamin Kramer | c96c721 | 2011-11-13 16:52:09 +0000 | [diff] [blame] | 1256 | consumeToken(); |
| 1257 | |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 1258 | bool Umbrella = UmbrellaLoc.isValid(); |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1259 | bool Exclude = ExcludeLoc.isValid(); |
| 1260 | assert(!(Umbrella && Exclude) && "Cannot have both 'umbrella' and 'exclude'"); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1261 | // Parse the header name. |
| 1262 | if (!Tok.is(MMToken::StringLiteral)) { |
| 1263 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) |
| 1264 | << "header"; |
| 1265 | HadError = true; |
| 1266 | return; |
| 1267 | } |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1268 | std::string FileName = Tok.getString(); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1269 | SourceLocation FileNameLoc = consumeToken(); |
| 1270 | |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1271 | // Check whether we already have an umbrella. |
| 1272 | if (Umbrella && ActiveModule->Umbrella) { |
| 1273 | Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash) |
| 1274 | << ActiveModule->getFullModuleName(); |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 1275 | HadError = true; |
| 1276 | return; |
| 1277 | } |
| 1278 | |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 1279 | // Look for this file. |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1280 | const FileEntry *File = 0; |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 1281 | const FileEntry *BuiltinFile = 0; |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1282 | SmallString<128> PathName; |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1283 | if (llvm::sys::path::is_absolute(FileName)) { |
| 1284 | PathName = FileName; |
| 1285 | File = SourceMgr.getFileManager().getFile(PathName); |
Douglas Gregor | 6a1db48 | 2011-12-09 02:04:43 +0000 | [diff] [blame] | 1286 | } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) { |
| 1287 | PathName = Dir->getName(); |
| 1288 | llvm::sys::path::append(PathName, FileName); |
| 1289 | File = SourceMgr.getFileManager().getFile(PathName); |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1290 | } else { |
| 1291 | // Search for the header file within the search directory. |
Douglas Gregor | 6a1db48 | 2011-12-09 02:04:43 +0000 | [diff] [blame] | 1292 | PathName = Directory->getName(); |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1293 | unsigned PathLength = PathName.size(); |
Douglas Gregor | 18ee547 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1294 | |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1295 | if (ActiveModule->isPartOfFramework()) { |
| 1296 | appendSubframeworkPaths(ActiveModule, PathName); |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1297 | |
| 1298 | // Check whether this file is in the public headers. |
Douglas Gregor | 18ee547 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1299 | llvm::sys::path::append(PathName, "Headers"); |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1300 | llvm::sys::path::append(PathName, FileName); |
| 1301 | File = SourceMgr.getFileManager().getFile(PathName); |
| 1302 | |
| 1303 | if (!File) { |
| 1304 | // Check whether this file is in the private headers. |
| 1305 | PathName.resize(PathLength); |
| 1306 | llvm::sys::path::append(PathName, "PrivateHeaders"); |
| 1307 | llvm::sys::path::append(PathName, FileName); |
| 1308 | File = SourceMgr.getFileManager().getFile(PathName); |
| 1309 | } |
| 1310 | } else { |
| 1311 | // Lookup for normal headers. |
| 1312 | llvm::sys::path::append(PathName, FileName); |
| 1313 | File = SourceMgr.getFileManager().getFile(PathName); |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 1314 | |
| 1315 | // If this is a system module with a top-level header, this header |
| 1316 | // may have a counterpart (or replacement) in the set of headers |
| 1317 | // supplied by Clang. Find that builtin header. |
| 1318 | if (ActiveModule->IsSystem && !Umbrella && BuiltinIncludeDir && |
| 1319 | BuiltinIncludeDir != Directory && isBuiltinHeader(FileName)) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1320 | SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 1321 | llvm::sys::path::append(BuiltinPathName, FileName); |
| 1322 | BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); |
| 1323 | |
| 1324 | // If Clang supplies this header but the underlying system does not, |
| 1325 | // just silently swap in our builtin version. Otherwise, we'll end |
| 1326 | // up adding both (later). |
| 1327 | if (!File && BuiltinFile) { |
| 1328 | File = BuiltinFile; |
| 1329 | BuiltinFile = 0; |
| 1330 | } |
| 1331 | } |
Douglas Gregor | d620a84 | 2011-12-06 17:16:41 +0000 | [diff] [blame] | 1332 | } |
Douglas Gregor | 18ee547 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1333 | } |
Douglas Gregor | a865405 | 2011-11-17 22:09:43 +0000 | [diff] [blame] | 1334 | |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 1335 | // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. |
| 1336 | // Come up with a lazy way to do this. |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1337 | if (File) { |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1338 | if (ModuleMap::KnownHeader OwningModule = Map.Headers[File]) { |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 1339 | Diags.Report(FileNameLoc, diag::err_mmap_header_conflict) |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1340 | << FileName << OwningModule.getModule()->getFullModuleName(); |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 1341 | HadError = true; |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 1342 | } else if (Umbrella) { |
| 1343 | const DirectoryEntry *UmbrellaDir = File->getDir(); |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1344 | if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 1345 | Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1346 | << UmbrellaModule->getFullModuleName(); |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 1347 | HadError = true; |
| 1348 | } else { |
| 1349 | // Record this umbrella header. |
| 1350 | Map.setUmbrellaHeader(ActiveModule, File); |
| 1351 | } |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 1352 | } else { |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 1353 | // Record this header. |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1354 | Map.addHeader(ActiveModule, File, Exclude); |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 1355 | |
| 1356 | // If there is a builtin counterpart to this file, add it now. |
| 1357 | if (BuiltinFile) |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1358 | Map.addHeader(ActiveModule, BuiltinFile, Exclude); |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 1359 | } |
Douglas Gregor | 71f49f5 | 2012-11-15 19:47:16 +0000 | [diff] [blame] | 1360 | } else if (!Exclude) { |
| 1361 | // Ignore excluded header files. They're optional anyway. |
| 1362 | |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 1363 | Diags.Report(FileNameLoc, diag::err_mmap_header_not_found) |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1364 | << Umbrella << FileName; |
Douglas Gregor | 8b6d3de | 2011-11-11 21:55:48 +0000 | [diff] [blame] | 1365 | HadError = true; |
| 1366 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1369 | /// \brief Parse an umbrella directory declaration. |
| 1370 | /// |
| 1371 | /// umbrella-dir-declaration: |
| 1372 | /// umbrella string-literal |
| 1373 | void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { |
| 1374 | // Parse the directory name. |
| 1375 | if (!Tok.is(MMToken::StringLiteral)) { |
| 1376 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) |
| 1377 | << "umbrella"; |
| 1378 | HadError = true; |
| 1379 | return; |
| 1380 | } |
| 1381 | |
| 1382 | std::string DirName = Tok.getString(); |
| 1383 | SourceLocation DirNameLoc = consumeToken(); |
| 1384 | |
| 1385 | // Check whether we already have an umbrella. |
| 1386 | if (ActiveModule->Umbrella) { |
| 1387 | Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) |
| 1388 | << ActiveModule->getFullModuleName(); |
| 1389 | HadError = true; |
| 1390 | return; |
| 1391 | } |
| 1392 | |
| 1393 | // Look for this file. |
| 1394 | const DirectoryEntry *Dir = 0; |
| 1395 | if (llvm::sys::path::is_absolute(DirName)) |
| 1396 | Dir = SourceMgr.getFileManager().getDirectory(DirName); |
| 1397 | else { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1398 | SmallString<128> PathName; |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1399 | PathName = Directory->getName(); |
| 1400 | llvm::sys::path::append(PathName, DirName); |
| 1401 | Dir = SourceMgr.getFileManager().getDirectory(PathName); |
| 1402 | } |
| 1403 | |
| 1404 | if (!Dir) { |
| 1405 | Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) |
| 1406 | << DirName; |
| 1407 | HadError = true; |
| 1408 | return; |
| 1409 | } |
| 1410 | |
| 1411 | if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { |
| 1412 | Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) |
| 1413 | << OwningModule->getFullModuleName(); |
| 1414 | HadError = true; |
| 1415 | return; |
| 1416 | } |
| 1417 | |
| 1418 | // Record this umbrella directory. |
| 1419 | Map.setUmbrellaDir(ActiveModule, Dir); |
| 1420 | } |
| 1421 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1422 | /// \brief Parse a module export declaration. |
| 1423 | /// |
| 1424 | /// export-declaration: |
| 1425 | /// 'export' wildcard-module-id |
| 1426 | /// |
| 1427 | /// wildcard-module-id: |
| 1428 | /// identifier |
| 1429 | /// '*' |
| 1430 | /// identifier '.' wildcard-module-id |
| 1431 | void ModuleMapParser::parseExportDecl() { |
| 1432 | assert(Tok.is(MMToken::ExportKeyword)); |
| 1433 | SourceLocation ExportLoc = consumeToken(); |
| 1434 | |
| 1435 | // Parse the module-id with an optional wildcard at the end. |
| 1436 | ModuleId ParsedModuleId; |
| 1437 | bool Wildcard = false; |
| 1438 | do { |
| 1439 | if (Tok.is(MMToken::Identifier)) { |
| 1440 | ParsedModuleId.push_back(std::make_pair(Tok.getString(), |
| 1441 | Tok.getLocation())); |
| 1442 | consumeToken(); |
| 1443 | |
| 1444 | if (Tok.is(MMToken::Period)) { |
| 1445 | consumeToken(); |
| 1446 | continue; |
| 1447 | } |
| 1448 | |
| 1449 | break; |
| 1450 | } |
| 1451 | |
| 1452 | if(Tok.is(MMToken::Star)) { |
| 1453 | Wildcard = true; |
Douglas Gregor | 0adaa88 | 2011-12-05 17:28:06 +0000 | [diff] [blame] | 1454 | consumeToken(); |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1455 | break; |
| 1456 | } |
| 1457 | |
| 1458 | Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id); |
| 1459 | HadError = true; |
| 1460 | return; |
| 1461 | } while (true); |
| 1462 | |
| 1463 | Module::UnresolvedExportDecl Unresolved = { |
| 1464 | ExportLoc, ParsedModuleId, Wildcard |
| 1465 | }; |
| 1466 | ActiveModule->UnresolvedExports.push_back(Unresolved); |
| 1467 | } |
| 1468 | |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 1469 | /// \brief Parse a link declaration. |
| 1470 | /// |
| 1471 | /// module-declaration: |
| 1472 | /// 'link' 'framework'[opt] string-literal |
| 1473 | void ModuleMapParser::parseLinkDecl() { |
| 1474 | assert(Tok.is(MMToken::LinkKeyword)); |
| 1475 | SourceLocation LinkLoc = consumeToken(); |
| 1476 | |
| 1477 | // Parse the optional 'framework' keyword. |
| 1478 | bool IsFramework = false; |
| 1479 | if (Tok.is(MMToken::FrameworkKeyword)) { |
| 1480 | consumeToken(); |
| 1481 | IsFramework = true; |
| 1482 | } |
| 1483 | |
| 1484 | // Parse the library name |
| 1485 | if (!Tok.is(MMToken::StringLiteral)) { |
| 1486 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) |
| 1487 | << IsFramework << SourceRange(LinkLoc); |
| 1488 | HadError = true; |
| 1489 | return; |
| 1490 | } |
| 1491 | |
| 1492 | std::string LibraryName = Tok.getString(); |
| 1493 | consumeToken(); |
| 1494 | ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, |
| 1495 | IsFramework)); |
| 1496 | } |
| 1497 | |
| 1498 | /// \brief Parse an inferred module declaration (wildcard modules). |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1499 | /// |
| 1500 | /// module-declaration: |
| 1501 | /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] |
| 1502 | /// { inferred-module-member* } |
| 1503 | /// |
| 1504 | /// inferred-module-member: |
| 1505 | /// 'export' '*' |
| 1506 | /// 'exclude' identifier |
| 1507 | void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1508 | assert(Tok.is(MMToken::Star)); |
| 1509 | SourceLocation StarLoc = consumeToken(); |
| 1510 | bool Failed = false; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1511 | |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1512 | // Inferred modules must be submodules. |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1513 | if (!ActiveModule && !Framework) { |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1514 | Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); |
| 1515 | Failed = true; |
| 1516 | } |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1517 | |
| 1518 | if (ActiveModule) { |
| 1519 | // Inferred modules must have umbrella directories. |
| 1520 | if (!Failed && !ActiveModule->getUmbrellaDir()) { |
| 1521 | Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); |
| 1522 | Failed = true; |
| 1523 | } |
| 1524 | |
| 1525 | // Check for redefinition of an inferred module. |
| 1526 | if (!Failed && ActiveModule->InferSubmodules) { |
| 1527 | Diags.Report(StarLoc, diag::err_mmap_inferred_redef); |
| 1528 | if (ActiveModule->InferredSubmoduleLoc.isValid()) |
| 1529 | Diags.Report(ActiveModule->InferredSubmoduleLoc, |
| 1530 | diag::note_mmap_prev_definition); |
| 1531 | Failed = true; |
| 1532 | } |
| 1533 | |
| 1534 | // Check for the 'framework' keyword, which is not permitted here. |
| 1535 | if (Framework) { |
| 1536 | Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); |
| 1537 | Framework = false; |
| 1538 | } |
| 1539 | } else if (Explicit) { |
| 1540 | Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); |
| 1541 | Explicit = false; |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1542 | } |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1543 | |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1544 | // If there were any problems with this inferred submodule, skip its body. |
| 1545 | if (Failed) { |
| 1546 | if (Tok.is(MMToken::LBrace)) { |
| 1547 | consumeToken(); |
| 1548 | skipUntil(MMToken::RBrace); |
| 1549 | if (Tok.is(MMToken::RBrace)) |
| 1550 | consumeToken(); |
| 1551 | } |
| 1552 | HadError = true; |
| 1553 | return; |
| 1554 | } |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1555 | |
| 1556 | // Parse optional attributes. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1557 | Attributes Attrs; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1558 | parseOptionalAttributes(Attrs); |
| 1559 | |
| 1560 | if (ActiveModule) { |
| 1561 | // Note that we have an inferred submodule. |
| 1562 | ActiveModule->InferSubmodules = true; |
| 1563 | ActiveModule->InferredSubmoduleLoc = StarLoc; |
| 1564 | ActiveModule->InferExplicitSubmodules = Explicit; |
| 1565 | } else { |
| 1566 | // We'll be inferring framework modules for this directory. |
| 1567 | Map.InferredDirectories[Directory].InferModules = true; |
| 1568 | Map.InferredDirectories[Directory].InferSystemModules = Attrs.IsSystem; |
| 1569 | } |
| 1570 | |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1571 | // Parse the opening brace. |
| 1572 | if (!Tok.is(MMToken::LBrace)) { |
| 1573 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); |
| 1574 | HadError = true; |
| 1575 | return; |
| 1576 | } |
| 1577 | SourceLocation LBraceLoc = consumeToken(); |
| 1578 | |
| 1579 | // Parse the body of the inferred submodule. |
| 1580 | bool Done = false; |
| 1581 | do { |
| 1582 | switch (Tok.Kind) { |
| 1583 | case MMToken::EndOfFile: |
| 1584 | case MMToken::RBrace: |
| 1585 | Done = true; |
| 1586 | break; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1587 | |
| 1588 | case MMToken::ExcludeKeyword: { |
| 1589 | if (ActiveModule) { |
| 1590 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) |
Douglas Gregor | b7ac5ac | 2012-11-06 19:41:11 +0000 | [diff] [blame] | 1591 | << (ActiveModule != 0); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1592 | consumeToken(); |
| 1593 | break; |
| 1594 | } |
| 1595 | |
| 1596 | consumeToken(); |
| 1597 | if (!Tok.is(MMToken::Identifier)) { |
| 1598 | Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); |
| 1599 | break; |
| 1600 | } |
| 1601 | |
| 1602 | Map.InferredDirectories[Directory].ExcludedModules |
| 1603 | .push_back(Tok.getString()); |
| 1604 | consumeToken(); |
| 1605 | break; |
| 1606 | } |
| 1607 | |
| 1608 | case MMToken::ExportKeyword: |
| 1609 | if (!ActiveModule) { |
| 1610 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) |
Douglas Gregor | b7ac5ac | 2012-11-06 19:41:11 +0000 | [diff] [blame] | 1611 | << (ActiveModule != 0); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1612 | consumeToken(); |
| 1613 | break; |
| 1614 | } |
| 1615 | |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1616 | consumeToken(); |
| 1617 | if (Tok.is(MMToken::Star)) |
Douglas Gregor | ef85b56 | 2011-12-06 17:34:58 +0000 | [diff] [blame] | 1618 | ActiveModule->InferExportWildcard = true; |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1619 | else |
| 1620 | Diags.Report(Tok.getLocation(), |
| 1621 | diag::err_mmap_expected_export_wildcard); |
| 1622 | consumeToken(); |
| 1623 | break; |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1624 | |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1625 | case MMToken::ExplicitKeyword: |
| 1626 | case MMToken::ModuleKeyword: |
| 1627 | case MMToken::HeaderKeyword: |
| 1628 | case MMToken::UmbrellaKeyword: |
| 1629 | default: |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1630 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) |
Douglas Gregor | b7ac5ac | 2012-11-06 19:41:11 +0000 | [diff] [blame] | 1631 | << (ActiveModule != 0); |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1632 | consumeToken(); |
| 1633 | break; |
| 1634 | } |
| 1635 | } while (!Done); |
| 1636 | |
| 1637 | if (Tok.is(MMToken::RBrace)) |
| 1638 | consumeToken(); |
| 1639 | else { |
| 1640 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); |
| 1641 | Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); |
| 1642 | HadError = true; |
| 1643 | } |
| 1644 | } |
| 1645 | |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1646 | /// \brief Parse optional attributes. |
| 1647 | /// |
| 1648 | /// attributes: |
| 1649 | /// attribute attributes |
| 1650 | /// attribute |
| 1651 | /// |
| 1652 | /// attribute: |
| 1653 | /// [ identifier ] |
| 1654 | /// |
| 1655 | /// \param Attrs Will be filled in with the parsed attributes. |
| 1656 | /// |
| 1657 | /// \returns true if an error occurred, false otherwise. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1658 | bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1659 | bool HadError = false; |
| 1660 | |
| 1661 | while (Tok.is(MMToken::LSquare)) { |
| 1662 | // Consume the '['. |
| 1663 | SourceLocation LSquareLoc = consumeToken(); |
| 1664 | |
| 1665 | // Check whether we have an attribute name here. |
| 1666 | if (!Tok.is(MMToken::Identifier)) { |
| 1667 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); |
| 1668 | skipUntil(MMToken::RSquare); |
| 1669 | if (Tok.is(MMToken::RSquare)) |
| 1670 | consumeToken(); |
| 1671 | HadError = true; |
| 1672 | } |
| 1673 | |
| 1674 | // Decode the attribute name. |
| 1675 | AttributeKind Attribute |
| 1676 | = llvm::StringSwitch<AttributeKind>(Tok.getString()) |
| 1677 | .Case("system", AT_system) |
| 1678 | .Default(AT_unknown); |
| 1679 | switch (Attribute) { |
| 1680 | case AT_unknown: |
| 1681 | Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) |
| 1682 | << Tok.getString(); |
| 1683 | break; |
| 1684 | |
| 1685 | case AT_system: |
| 1686 | Attrs.IsSystem = true; |
| 1687 | break; |
| 1688 | } |
| 1689 | consumeToken(); |
| 1690 | |
| 1691 | // Consume the ']'. |
| 1692 | if (!Tok.is(MMToken::RSquare)) { |
| 1693 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); |
| 1694 | Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); |
| 1695 | skipUntil(MMToken::RSquare); |
| 1696 | HadError = true; |
| 1697 | } |
| 1698 | |
| 1699 | if (Tok.is(MMToken::RSquare)) |
| 1700 | consumeToken(); |
| 1701 | } |
| 1702 | |
| 1703 | return HadError; |
| 1704 | } |
| 1705 | |
Douglas Gregor | 6a1db48 | 2011-12-09 02:04:43 +0000 | [diff] [blame] | 1706 | /// \brief If there is a specific header search directory due the presence |
| 1707 | /// of an umbrella directory, retrieve that directory. Otherwise, returns null. |
| 1708 | const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() { |
| 1709 | for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) { |
| 1710 | // If we have an umbrella directory, use that. |
| 1711 | if (Mod->hasUmbrellaDir()) |
| 1712 | return Mod->getUmbrellaDir(); |
| 1713 | |
| 1714 | // If we have a framework directory, stop looking. |
| 1715 | if (Mod->IsFramework) |
| 1716 | return 0; |
| 1717 | } |
| 1718 | |
| 1719 | return 0; |
| 1720 | } |
| 1721 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1722 | /// \brief Parse a module map file. |
| 1723 | /// |
| 1724 | /// module-map-file: |
| 1725 | /// module-declaration* |
| 1726 | bool ModuleMapParser::parseModuleMapFile() { |
| 1727 | do { |
| 1728 | switch (Tok.Kind) { |
| 1729 | case MMToken::EndOfFile: |
| 1730 | return HadError; |
| 1731 | |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1732 | case MMToken::ExplicitKeyword: |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1733 | case MMToken::ModuleKeyword: |
Douglas Gregor | a865405 | 2011-11-17 22:09:43 +0000 | [diff] [blame] | 1734 | case MMToken::FrameworkKeyword: |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1735 | parseModuleDecl(); |
| 1736 | break; |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 1737 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1738 | case MMToken::Comma: |
Douglas Gregor | 2b49d1f | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 1739 | case MMToken::ExcludeKeyword: |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1740 | case MMToken::ExportKeyword: |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1741 | case MMToken::HeaderKeyword: |
| 1742 | case MMToken::Identifier: |
| 1743 | case MMToken::LBrace: |
Douglas Gregor | b6cbe51 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 1744 | case MMToken::LinkKeyword: |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1745 | case MMToken::LSquare: |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1746 | case MMToken::Period: |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1747 | case MMToken::RBrace: |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1748 | case MMToken::RSquare: |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1749 | case MMToken::RequiresKeyword: |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1750 | case MMToken::Star: |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1751 | case MMToken::StringLiteral: |
| 1752 | case MMToken::UmbrellaKeyword: |
| 1753 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); |
| 1754 | HadError = true; |
| 1755 | consumeToken(); |
| 1756 | break; |
| 1757 | } |
| 1758 | } while (true); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | bool ModuleMap::parseModuleMapFile(const FileEntry *File) { |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 1762 | llvm::DenseMap<const FileEntry *, bool>::iterator Known |
| 1763 | = ParsedModuleMap.find(File); |
| 1764 | if (Known != ParsedModuleMap.end()) |
| 1765 | return Known->second; |
| 1766 | |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 1767 | assert(Target != 0 && "Missing target information"); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1768 | FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User); |
| 1769 | const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID); |
| 1770 | if (!Buffer) |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 1771 | return ParsedModuleMap[File] = true; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1772 | |
| 1773 | // Parse this module map file. |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1774 | Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts); |
| 1775 | Diags->getClient()->BeginSourceFile(MMapLangOpts); |
Douglas Gregor | 9a022bb | 2012-10-15 16:45:32 +0000 | [diff] [blame] | 1776 | ModuleMapParser Parser(L, *SourceMgr, Target, *Diags, *this, File->getDir(), |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 1777 | BuiltinIncludeDir); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1778 | bool Result = Parser.parseModuleMapFile(); |
| 1779 | Diags->getClient()->EndSourceFile(); |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 1780 | ParsedModuleMap[File] = Result; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1781 | return Result; |
| 1782 | } |