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