blob: 6b0eb79a10a3f42058b0eb18c104b2346ac838a6 [file] [log] [blame]
Douglas Gregora30cfe52011-11-11 19:10:28 +00001//===--- 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 Gregora30cfe52011-11-11 19:10:28 +000015#include "clang/Basic/Diagnostic.h"
Douglas Gregor02c23eb2012-10-23 22:26:28 +000016#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregora30cfe52011-11-11 19:10:28 +000017#include "clang/Basic/FileManager.h"
18#include "clang/Basic/TargetInfo.h"
19#include "clang/Basic/TargetOptions.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#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 Gregora30cfe52011-11-11 19:10:28 +000025#include "llvm/Support/Allocator.h"
Douglas Gregorac252a32011-12-06 19:39:29 +000026#include "llvm/Support/FileSystem.h"
Douglas Gregora30cfe52011-11-11 19:10:28 +000027#include "llvm/Support/Host.h"
Douglas Gregor8b6d3de2011-11-11 21:55:48 +000028#include "llvm/Support/PathV2.h"
Douglas Gregora30cfe52011-11-11 19:10:28 +000029#include "llvm/Support/raw_ostream.h"
Douglas Gregor98cfcbf2012-09-27 14:50:15 +000030#include <stdlib.h>
Douglas Gregora30cfe52011-11-11 19:10:28 +000031using namespace clang;
32
Douglas Gregor90db2602011-12-02 01:47:07 +000033Module::ExportDecl
34ModuleMap::resolveExport(Module *Mod,
35 const Module::UnresolvedExportDecl &Unresolved,
36 bool Complain) {
Douglas Gregor0adaa882011-12-05 17:28:06 +000037 // We may have just a wildcard.
38 if (Unresolved.Id.empty()) {
39 assert(Unresolved.Wildcard && "Invalid unresolved export");
40 return Module::ExportDecl(0, true);
41 }
42
Douglas Gregor90db2602011-12-02 01:47:07 +000043 // Find the starting module.
44 Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod);
45 if (!Context) {
46 if (Complain)
47 Diags->Report(Unresolved.Id[0].second,
48 diag::err_mmap_missing_module_unqualified)
49 << Unresolved.Id[0].first << Mod->getFullModuleName();
50
51 return Module::ExportDecl();
52 }
53
54 // Dig into the module path.
55 for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) {
56 Module *Sub = lookupModuleQualified(Unresolved.Id[I].first,
57 Context);
58 if (!Sub) {
59 if (Complain)
60 Diags->Report(Unresolved.Id[I].second,
61 diag::err_mmap_missing_module_qualified)
62 << Unresolved.Id[I].first << Context->getFullModuleName()
63 << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second);
64
65 return Module::ExportDecl();
66 }
67
68 Context = Sub;
69 }
70
71 return Module::ExportDecl(Context, Unresolved.Wildcard);
72}
73
Douglas Gregor51f564f2011-12-31 04:05:44 +000074ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC,
Douglas Gregordc58aa72012-01-30 06:01:29 +000075 const LangOptions &LangOpts, const TargetInfo *Target)
Douglas Gregor2f04f182012-02-02 18:42:48 +000076 : LangOpts(LangOpts), Target(Target), BuiltinIncludeDir(0)
Douglas Gregor51f564f2011-12-31 04:05:44 +000077{
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000078 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs);
79 Diags = IntrusiveRefCntPtr<DiagnosticsEngine>(
Douglas Gregor02c23eb2012-10-23 22:26:28 +000080 new DiagnosticsEngine(DiagIDs, new DiagnosticOptions));
Douglas Gregora30cfe52011-11-11 19:10:28 +000081 Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true);
82 SourceMgr = new SourceManager(*Diags, FileMgr);
83}
84
85ModuleMap::~ModuleMap() {
Douglas Gregor09fe1bb2011-11-17 02:05:44 +000086 for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
87 IEnd = Modules.end();
88 I != IEnd; ++I) {
89 delete I->getValue();
90 }
91
Douglas Gregora30cfe52011-11-11 19:10:28 +000092 delete SourceMgr;
93}
94
Douglas Gregordc58aa72012-01-30 06:01:29 +000095void ModuleMap::setTarget(const TargetInfo &Target) {
96 assert((!this->Target || this->Target == &Target) &&
97 "Improper target override");
98 this->Target = &Target;
99}
100
Douglas Gregor8b48e082012-10-12 21:15:50 +0000101/// \brief "Sanitize" a filename so that it can be used as an identifier.
102static StringRef sanitizeFilenameAsIdentifier(StringRef Name,
103 SmallVectorImpl<char> &Buffer) {
104 if (Name.empty())
105 return Name;
106
107 // Check whether the filename is already an identifier; this is the common
108 // case.
109 bool isIdentifier = true;
110 for (unsigned I = 0, N = Name.size(); I != N; ++I) {
111 if (isalpha(Name[I]) || Name[I] == '_' || (isdigit(Name[I]) && I > 0))
112 continue;
113
114 isIdentifier = false;
115 break;
116 }
117
118 if (!isIdentifier) {
119 // If we don't already have something with the form of an identifier,
120 // create a buffer with the sanitized name.
121 Buffer.clear();
122 if (isdigit(Name[0]))
123 Buffer.push_back('_');
124 Buffer.reserve(Buffer.size() + Name.size());
125 for (unsigned I = 0, N = Name.size(); I != N; ++I) {
126 if (isalnum(Name[I]) || isspace(Name[I]))
127 Buffer.push_back(Name[I]);
128 else
129 Buffer.push_back('_');
130 }
131
132 Name = StringRef(Buffer.data(), Buffer.size());
133 }
134
135 while (llvm::StringSwitch<bool>(Name)
136#define KEYWORD(Keyword,Conditions) .Case(#Keyword, true)
137#define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true)
138#include "clang/Basic/TokenKinds.def"
139 .Default(false)) {
140 if (Name.data() != Buffer.data())
141 Buffer.append(Name.begin(), Name.end());
142 Buffer.push_back('_');
143 Name = StringRef(Buffer.data(), Buffer.size());
144 }
145
146 return Name;
147}
148
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000149Module *ModuleMap::findModuleForHeader(const FileEntry *File) {
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000150 HeadersMap::iterator Known = Headers.find(File);
Douglas Gregor51f564f2011-12-31 04:05:44 +0000151 if (Known != Headers.end()) {
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000152 // If a header is not available, don't report that it maps to anything.
153 if (!Known->second.isAvailable())
Douglas Gregor51f564f2011-12-31 04:05:44 +0000154 return 0;
155
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000156 return Known->second.getModule();
Douglas Gregor51f564f2011-12-31 04:05:44 +0000157 }
Douglas Gregor65f3b5e2011-11-11 22:18:48 +0000158
Douglas Gregoradb97992011-11-16 23:02:25 +0000159 const DirectoryEntry *Dir = File->getDir();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000160 SmallVector<const DirectoryEntry *, 2> SkippedDirs;
Douglas Gregoraa60f9c2013-01-04 19:44:26 +0000161#ifdef LLVM_ON_UNIX
162 // Note: as an egregious but useful hack we use the real path here, because
163 // frameworks moving from top-level frameworks to embedded frameworks tend
164 // to be symlinked from the top-level location to the embedded location,
165 // and we need to resolve lookups as if we had found the embedded location.
166 char RealDirName[PATH_MAX];
167 StringRef DirName;
168 if (realpath(Dir->getName(), RealDirName))
169 DirName = RealDirName;
170 else
171 DirName = Dir->getName();
172#else
Douglas Gregoradb97992011-11-16 23:02:25 +0000173 StringRef DirName = Dir->getName();
Douglas Gregoraa60f9c2013-01-04 19:44:26 +0000174#endif
Douglas Gregore209e502011-12-06 01:10:29 +0000175
176 // Keep walking up the directory hierarchy, looking for a directory with
177 // an umbrella header.
178 do {
179 llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
180 = UmbrellaDirs.find(Dir);
181 if (KnownDir != UmbrellaDirs.end()) {
182 Module *Result = KnownDir->second;
Douglas Gregor9f74f4f2011-12-06 16:17:15 +0000183
184 // Search up the module stack until we find a module with an umbrella
Douglas Gregor10694ce2011-12-08 17:39:04 +0000185 // directory.
Douglas Gregor9f74f4f2011-12-06 16:17:15 +0000186 Module *UmbrellaModule = Result;
Douglas Gregor10694ce2011-12-08 17:39:04 +0000187 while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
Douglas Gregor9f74f4f2011-12-06 16:17:15 +0000188 UmbrellaModule = UmbrellaModule->Parent;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000189
Douglas Gregor9f74f4f2011-12-06 16:17:15 +0000190 if (UmbrellaModule->InferSubmodules) {
Douglas Gregore209e502011-12-06 01:10:29 +0000191 // Infer submodules for each of the directories we found between
192 // the directory of the umbrella header and the directory where
193 // the actual header is located.
Douglas Gregor23af6d52011-12-07 22:05:21 +0000194 bool Explicit = UmbrellaModule->InferExplicitSubmodules;
Douglas Gregore209e502011-12-06 01:10:29 +0000195
Douglas Gregor6a1db482011-12-09 02:04:43 +0000196 for (unsigned I = SkippedDirs.size(); I != 0; --I) {
Douglas Gregore209e502011-12-06 01:10:29 +0000197 // Find or create the module that corresponds to this directory name.
Douglas Gregor8b48e082012-10-12 21:15:50 +0000198 SmallString<32> NameBuf;
199 StringRef Name = sanitizeFilenameAsIdentifier(
200 llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
201 NameBuf);
Douglas Gregore209e502011-12-06 01:10:29 +0000202 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
Douglas Gregor23af6d52011-12-07 22:05:21 +0000203 Explicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +0000204
205 // Associate the module and the directory.
206 UmbrellaDirs[SkippedDirs[I-1]] = Result;
207
208 // If inferred submodules export everything they import, add a
209 // wildcard to the set of exports.
Douglas Gregor9f74f4f2011-12-06 16:17:15 +0000210 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
Douglas Gregore209e502011-12-06 01:10:29 +0000211 Result->Exports.push_back(Module::ExportDecl(0, true));
212 }
213
214 // Infer a submodule with the same name as this header file.
Douglas Gregor8b48e082012-10-12 21:15:50 +0000215 SmallString<32> NameBuf;
216 StringRef Name = sanitizeFilenameAsIdentifier(
217 llvm::sys::path::stem(File->getName()), NameBuf);
Douglas Gregore209e502011-12-06 01:10:29 +0000218 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
Douglas Gregor23af6d52011-12-07 22:05:21 +0000219 Explicit).first;
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +0000220 Result->TopHeaders.insert(File);
Douglas Gregore209e502011-12-06 01:10:29 +0000221
222 // If inferred submodules export everything they import, add a
223 // wildcard to the set of exports.
Douglas Gregor9f74f4f2011-12-06 16:17:15 +0000224 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
Douglas Gregore209e502011-12-06 01:10:29 +0000225 Result->Exports.push_back(Module::ExportDecl(0, true));
226 } else {
227 // Record each of the directories we stepped through as being part of
228 // the module we found, since the umbrella header covers them all.
229 for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
230 UmbrellaDirs[SkippedDirs[I]] = Result;
231 }
232
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000233 Headers[File] = KnownHeader(Result, /*Excluded=*/false);
Douglas Gregor51f564f2011-12-31 04:05:44 +0000234
235 // If a header corresponds to an unavailable module, don't report
236 // that it maps to anything.
237 if (!Result->isAvailable())
238 return 0;
239
Douglas Gregore209e502011-12-06 01:10:29 +0000240 return Result;
241 }
242
243 SkippedDirs.push_back(Dir);
244
Douglas Gregoradb97992011-11-16 23:02:25 +0000245 // Retrieve our parent path.
246 DirName = llvm::sys::path::parent_path(DirName);
247 if (DirName.empty())
248 break;
249
250 // Resolve the parent path to a directory entry.
251 Dir = SourceMgr->getFileManager().getDirectory(DirName);
Douglas Gregore209e502011-12-06 01:10:29 +0000252 } while (Dir);
Douglas Gregoradb97992011-11-16 23:02:25 +0000253
Douglas Gregor65f3b5e2011-11-11 22:18:48 +0000254 return 0;
255}
256
Douglas Gregor51f564f2011-12-31 04:05:44 +0000257bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) {
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000258 HeadersMap::iterator Known = Headers.find(Header);
Douglas Gregor51f564f2011-12-31 04:05:44 +0000259 if (Known != Headers.end())
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000260 return !Known->second.isAvailable();
Douglas Gregor51f564f2011-12-31 04:05:44 +0000261
262 const DirectoryEntry *Dir = Header->getDir();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000263 SmallVector<const DirectoryEntry *, 2> SkippedDirs;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000264 StringRef DirName = Dir->getName();
265
266 // Keep walking up the directory hierarchy, looking for a directory with
267 // an umbrella header.
268 do {
269 llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
270 = UmbrellaDirs.find(Dir);
271 if (KnownDir != UmbrellaDirs.end()) {
272 Module *Found = KnownDir->second;
273 if (!Found->isAvailable())
274 return true;
275
276 // Search up the module stack until we find a module with an umbrella
277 // directory.
278 Module *UmbrellaModule = Found;
279 while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
280 UmbrellaModule = UmbrellaModule->Parent;
281
282 if (UmbrellaModule->InferSubmodules) {
283 for (unsigned I = SkippedDirs.size(); I != 0; --I) {
284 // Find or create the module that corresponds to this directory name.
Douglas Gregor8b48e082012-10-12 21:15:50 +0000285 SmallString<32> NameBuf;
286 StringRef Name = sanitizeFilenameAsIdentifier(
287 llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
288 NameBuf);
Douglas Gregor51f564f2011-12-31 04:05:44 +0000289 Found = lookupModuleQualified(Name, Found);
290 if (!Found)
291 return false;
292 if (!Found->isAvailable())
293 return true;
294 }
295
296 // Infer a submodule with the same name as this header file.
Douglas Gregor8b48e082012-10-12 21:15:50 +0000297 SmallString<32> NameBuf;
298 StringRef Name = sanitizeFilenameAsIdentifier(
299 llvm::sys::path::stem(Header->getName()),
300 NameBuf);
Douglas Gregor51f564f2011-12-31 04:05:44 +0000301 Found = lookupModuleQualified(Name, Found);
302 if (!Found)
303 return false;
304 }
305
306 return !Found->isAvailable();
307 }
308
309 SkippedDirs.push_back(Dir);
310
311 // Retrieve our parent path.
312 DirName = llvm::sys::path::parent_path(DirName);
313 if (DirName.empty())
314 break;
315
316 // Resolve the parent path to a directory entry.
317 Dir = SourceMgr->getFileManager().getDirectory(DirName);
318 } while (Dir);
319
320 return false;
321}
322
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000323Module *ModuleMap::findModule(StringRef Name) {
Douglas Gregor484535e2011-11-11 23:20:24 +0000324 llvm::StringMap<Module *>::iterator Known = Modules.find(Name);
325 if (Known != Modules.end())
326 return Known->getValue();
327
328 return 0;
329}
330
Douglas Gregor90db2602011-12-02 01:47:07 +0000331Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) {
332 for(; Context; Context = Context->Parent) {
333 if (Module *Sub = lookupModuleQualified(Name, Context))
334 return Sub;
335 }
336
337 return findModule(Name);
338}
339
340Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) {
341 if (!Context)
342 return findModule(Name);
343
Douglas Gregorb7a78192012-01-04 23:32:19 +0000344 return Context->findSubmodule(Name);
Douglas Gregor90db2602011-12-02 01:47:07 +0000345}
346
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000347std::pair<Module *, bool>
Douglas Gregor392ed2b2011-11-30 17:33:56 +0000348ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
349 bool IsExplicit) {
350 // Try to find an existing module with this name.
Douglas Gregorb7a78192012-01-04 23:32:19 +0000351 if (Module *Sub = lookupModuleQualified(Name, Parent))
352 return std::make_pair(Sub, false);
Douglas Gregor392ed2b2011-11-30 17:33:56 +0000353
354 // Create a new module with this name.
355 Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
356 IsExplicit);
Douglas Gregorb7a78192012-01-04 23:32:19 +0000357 if (!Parent)
Douglas Gregor392ed2b2011-11-30 17:33:56 +0000358 Modules[Name] = Result;
359 return std::make_pair(Result, true);
360}
361
Douglas Gregor82e52372012-11-06 19:39:40 +0000362bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir,
363 StringRef Name, bool &IsSystem) {
364 // Check whether we have already looked into the parent directory
365 // for a module map.
366 llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::iterator
367 inferred = InferredDirectories.find(ParentDir);
368 if (inferred == InferredDirectories.end())
369 return false;
370
371 if (!inferred->second.InferModules)
372 return false;
373
374 // We're allowed to infer for this directory, but make sure it's okay
375 // to infer this particular module.
376 bool canInfer = std::find(inferred->second.ExcludedModules.begin(),
377 inferred->second.ExcludedModules.end(),
378 Name) == inferred->second.ExcludedModules.end();
379
380 if (canInfer && inferred->second.InferSystemModules)
381 IsSystem = true;
382
383 return canInfer;
384}
385
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000386Module *
Douglas Gregor82e52372012-11-06 19:39:40 +0000387ModuleMap::inferFrameworkModule(StringRef ModuleName,
Douglas Gregorac252a32011-12-06 19:39:29 +0000388 const DirectoryEntry *FrameworkDir,
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000389 bool IsSystem,
Douglas Gregorac252a32011-12-06 19:39:29 +0000390 Module *Parent) {
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000391 // Check whether we've already found this module.
Douglas Gregorac252a32011-12-06 19:39:29 +0000392 if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
393 return Mod;
394
395 FileManager &FileMgr = SourceMgr->getFileManager();
Douglas Gregor82e52372012-11-06 19:39:40 +0000396
397 // If the framework has a parent path from which we're allowed to infer
398 // a framework module, do so.
399 if (!Parent) {
Douglas Gregor7005b902013-01-10 01:43:00 +0000400 // Determine whether we're allowed to infer a module map.
401 StringRef FrameworkDirName = FrameworkDir->getName();
402#ifdef LLVM_ON_UNIX
403 // Note: as an egregious but useful hack we use the real path here, because
404 // we might be looking at an embedded framework that symlinks out to a
405 // top-level framework, and we need to infer as if we were naming the
406 // top-level framework.
407 char RealFrameworkDirName[PATH_MAX];
408 if (realpath(FrameworkDir->getName(), RealFrameworkDirName))
409 FrameworkDirName = RealFrameworkDirName;
410#endif
411
Douglas Gregor82e52372012-11-06 19:39:40 +0000412 bool canInfer = false;
Douglas Gregor7005b902013-01-10 01:43:00 +0000413 if (llvm::sys::path::has_parent_path(FrameworkDirName)) {
Douglas Gregor82e52372012-11-06 19:39:40 +0000414 // Figure out the parent path.
Douglas Gregor7005b902013-01-10 01:43:00 +0000415 StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName);
Douglas Gregor82e52372012-11-06 19:39:40 +0000416 if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) {
417 // Check whether we have already looked into the parent directory
418 // for a module map.
419 llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::iterator
420 inferred = InferredDirectories.find(ParentDir);
421 if (inferred == InferredDirectories.end()) {
422 // We haven't looked here before. Load a module map, if there is
423 // one.
424 SmallString<128> ModMapPath = Parent;
425 llvm::sys::path::append(ModMapPath, "module.map");
426 if (const FileEntry *ModMapFile = FileMgr.getFile(ModMapPath)) {
427 parseModuleMapFile(ModMapFile);
428 inferred = InferredDirectories.find(ParentDir);
429 }
430
431 if (inferred == InferredDirectories.end())
432 inferred = InferredDirectories.insert(
433 std::make_pair(ParentDir, InferredDirectory())).first;
434 }
435
436 if (inferred->second.InferModules) {
437 // We're allowed to infer for this directory, but make sure it's okay
438 // to infer this particular module.
Douglas Gregor7005b902013-01-10 01:43:00 +0000439 StringRef Name = llvm::sys::path::stem(FrameworkDirName);
Douglas Gregor82e52372012-11-06 19:39:40 +0000440 canInfer = std::find(inferred->second.ExcludedModules.begin(),
441 inferred->second.ExcludedModules.end(),
442 Name) == inferred->second.ExcludedModules.end();
443
444 if (inferred->second.InferSystemModules)
445 IsSystem = true;
446 }
447 }
448 }
449
450 // If we're not allowed to infer a framework module, don't.
451 if (!canInfer)
452 return 0;
453 }
454
455
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000456 // Look for an umbrella header.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000457 SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000458 llvm::sys::path::append(UmbrellaName, "Headers");
459 llvm::sys::path::append(UmbrellaName, ModuleName + ".h");
Douglas Gregorac252a32011-12-06 19:39:29 +0000460 const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000461
462 // FIXME: If there's no umbrella header, we could probably scan the
463 // framework to load *everything*. But, it's not clear that this is a good
464 // idea.
465 if (!UmbrellaHeader)
466 return 0;
467
Douglas Gregorac252a32011-12-06 19:39:29 +0000468 Module *Result = new Module(ModuleName, SourceLocation(), Parent,
469 /*IsFramework=*/true, /*IsExplicit=*/false);
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000470 if (IsSystem)
471 Result->IsSystem = IsSystem;
472
Douglas Gregorb7a78192012-01-04 23:32:19 +0000473 if (!Parent)
Douglas Gregorac252a32011-12-06 19:39:29 +0000474 Modules[ModuleName] = Result;
Douglas Gregorb7a78192012-01-04 23:32:19 +0000475
Douglas Gregor489ad432011-12-08 18:00:48 +0000476 // umbrella header "umbrella-header-name"
Douglas Gregor10694ce2011-12-08 17:39:04 +0000477 Result->Umbrella = UmbrellaHeader;
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000478 Headers[UmbrellaHeader] = KnownHeader(Result, /*Excluded=*/false);
Douglas Gregor3cee31e2011-12-12 23:55:05 +0000479 UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
Douglas Gregor209977c2011-12-05 17:40:25 +0000480
481 // export *
482 Result->Exports.push_back(Module::ExportDecl(0, true));
483
Douglas Gregore209e502011-12-06 01:10:29 +0000484 // module * { export * }
485 Result->InferSubmodules = true;
486 Result->InferExportWildcard = true;
487
Douglas Gregorac252a32011-12-06 19:39:29 +0000488 // Look for subframeworks.
489 llvm::error_code EC;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000490 SmallString<128> SubframeworksDirName
Douglas Gregor52b1ed32011-12-08 16:13:24 +0000491 = StringRef(FrameworkDir->getName());
Douglas Gregorac252a32011-12-06 19:39:29 +0000492 llvm::sys::path::append(SubframeworksDirName, "Frameworks");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000493 SmallString<128> SubframeworksDirNameNative;
Douglas Gregor52b1ed32011-12-08 16:13:24 +0000494 llvm::sys::path::native(SubframeworksDirName.str(),
495 SubframeworksDirNameNative);
496 for (llvm::sys::fs::directory_iterator
497 Dir(SubframeworksDirNameNative.str(), EC), DirEnd;
Douglas Gregorac252a32011-12-06 19:39:29 +0000498 Dir != DirEnd && !EC; Dir.increment(EC)) {
499 if (!StringRef(Dir->path()).endswith(".framework"))
500 continue;
Douglas Gregor98cfcbf2012-09-27 14:50:15 +0000501
Douglas Gregorac252a32011-12-06 19:39:29 +0000502 if (const DirectoryEntry *SubframeworkDir
503 = FileMgr.getDirectory(Dir->path())) {
Douglas Gregor98cfcbf2012-09-27 14:50:15 +0000504 // Note: as an egregious but useful hack, we use the real path here and
505 // check whether it is actually a subdirectory of the parent directory.
506 // This will not be the case if the 'subframework' is actually a symlink
507 // out to a top-level framework.
508#ifdef LLVM_ON_UNIX
509 char RealSubframeworkDirName[PATH_MAX];
510 if (realpath(Dir->path().c_str(), RealSubframeworkDirName)) {
511 StringRef SubframeworkDirName = RealSubframeworkDirName;
512
513 bool FoundParent = false;
514 do {
515 // Get the parent directory name.
516 SubframeworkDirName
517 = llvm::sys::path::parent_path(SubframeworkDirName);
518 if (SubframeworkDirName.empty())
519 break;
520
521 if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) {
522 FoundParent = true;
523 break;
524 }
525 } while (true);
526
527 if (!FoundParent)
528 continue;
529 }
530#endif
531
Douglas Gregorac252a32011-12-06 19:39:29 +0000532 // FIXME: Do we want to warn about subframeworks without umbrella headers?
Douglas Gregor8b48e082012-10-12 21:15:50 +0000533 SmallString<32> NameBuf;
534 inferFrameworkModule(sanitizeFilenameAsIdentifier(
535 llvm::sys::path::stem(Dir->path()), NameBuf),
536 SubframeworkDir, IsSystem, Result);
Douglas Gregorac252a32011-12-06 19:39:29 +0000537 }
538 }
Douglas Gregor3a110f72012-01-13 16:54:27 +0000539
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000540 return Result;
541}
542
Douglas Gregore209e502011-12-06 01:10:29 +0000543void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000544 Headers[UmbrellaHeader] = KnownHeader(Mod, /*Excluded=*/false);
Douglas Gregor10694ce2011-12-08 17:39:04 +0000545 Mod->Umbrella = UmbrellaHeader;
Douglas Gregor6a1db482011-12-09 02:04:43 +0000546 UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
Douglas Gregore209e502011-12-06 01:10:29 +0000547}
548
Douglas Gregor77d029f2011-12-08 19:11:24 +0000549void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) {
550 Mod->Umbrella = UmbrellaDir;
551 UmbrellaDirs[UmbrellaDir] = Mod;
552}
553
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000554void ModuleMap::addHeader(Module *Mod, const FileEntry *Header,
555 bool Excluded) {
556 if (Excluded)
557 Mod->ExcludedHeaders.push_back(Header);
558 else
559 Mod->Headers.push_back(Header);
560 Headers[Header] = KnownHeader(Mod, Excluded);
Douglas Gregore209e502011-12-06 01:10:29 +0000561}
562
Douglas Gregorf9e357d2011-11-29 19:06:37 +0000563const FileEntry *
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000564ModuleMap::getContainingModuleMapFile(Module *Module) {
Douglas Gregorf9e357d2011-11-29 19:06:37 +0000565 if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
566 return 0;
567
568 return SourceMgr->getFileEntryForID(
569 SourceMgr->getFileID(Module->DefinitionLoc));
570}
571
Douglas Gregora30cfe52011-11-11 19:10:28 +0000572void ModuleMap::dump() {
573 llvm::errs() << "Modules:";
574 for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
575 MEnd = Modules.end();
576 M != MEnd; ++M)
Douglas Gregor804c3bf2011-11-29 18:17:59 +0000577 M->getValue()->print(llvm::errs(), 2);
Douglas Gregora30cfe52011-11-11 19:10:28 +0000578
579 llvm::errs() << "Headers:";
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000580 for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end();
Douglas Gregora30cfe52011-11-11 19:10:28 +0000581 H != HEnd; ++H) {
582 llvm::errs() << " \"" << H->first->getName() << "\" -> "
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000583 << H->second.getModule()->getFullModuleName() << "\n";
Douglas Gregora30cfe52011-11-11 19:10:28 +0000584 }
585}
586
Douglas Gregor90db2602011-12-02 01:47:07 +0000587bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
588 bool HadError = false;
589 for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
590 Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
591 Complain);
Douglas Gregor0adaa882011-12-05 17:28:06 +0000592 if (Export.getPointer() || Export.getInt())
Douglas Gregor90db2602011-12-02 01:47:07 +0000593 Mod->Exports.push_back(Export);
594 else
595 HadError = true;
596 }
597 Mod->UnresolvedExports.clear();
598 return HadError;
599}
600
Douglas Gregor55988682011-12-05 16:33:54 +0000601Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
602 if (Loc.isInvalid())
603 return 0;
604
605 // Use the expansion location to determine which module we're in.
606 FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
607 if (!ExpansionLoc.isFileID())
608 return 0;
609
610
611 const SourceManager &SrcMgr = Loc.getManager();
612 FileID ExpansionFileID = ExpansionLoc.getFileID();
Douglas Gregor55988682011-12-05 16:33:54 +0000613
Douglas Gregor303aae92012-01-06 17:19:32 +0000614 while (const FileEntry *ExpansionFile
615 = SrcMgr.getFileEntryForID(ExpansionFileID)) {
616 // Find the module that owns this header (if any).
617 if (Module *Mod = findModuleForHeader(ExpansionFile))
618 return Mod;
619
620 // No module owns this header, so look up the inclusion chain to see if
621 // any included header has an associated module.
622 SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID);
623 if (IncludeLoc.isInvalid())
624 return 0;
625
626 ExpansionFileID = SrcMgr.getFileID(IncludeLoc);
627 }
628
629 return 0;
Douglas Gregor55988682011-12-05 16:33:54 +0000630}
631
Douglas Gregora30cfe52011-11-11 19:10:28 +0000632//----------------------------------------------------------------------------//
633// Module map file parser
634//----------------------------------------------------------------------------//
635
636namespace clang {
637 /// \brief A token in a module map file.
638 struct MMToken {
639 enum TokenKind {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000640 Comma,
Douglas Gregora30cfe52011-11-11 19:10:28 +0000641 EndOfFile,
642 HeaderKeyword,
643 Identifier,
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000644 ExcludeKeyword,
Douglas Gregora30cfe52011-11-11 19:10:28 +0000645 ExplicitKeyword,
Douglas Gregor90db2602011-12-02 01:47:07 +0000646 ExportKeyword,
Douglas Gregora8654052011-11-17 22:09:43 +0000647 FrameworkKeyword,
Douglas Gregorb6cbe512013-01-14 17:21:00 +0000648 LinkKeyword,
Douglas Gregora30cfe52011-11-11 19:10:28 +0000649 ModuleKeyword,
Douglas Gregor90db2602011-12-02 01:47:07 +0000650 Period,
Douglas Gregora30cfe52011-11-11 19:10:28 +0000651 UmbrellaKeyword,
Douglas Gregor51f564f2011-12-31 04:05:44 +0000652 RequiresKeyword,
Douglas Gregor90db2602011-12-02 01:47:07 +0000653 Star,
Douglas Gregora30cfe52011-11-11 19:10:28 +0000654 StringLiteral,
655 LBrace,
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000656 RBrace,
657 LSquare,
658 RSquare
Douglas Gregora30cfe52011-11-11 19:10:28 +0000659 } Kind;
660
661 unsigned Location;
662 unsigned StringLength;
663 const char *StringData;
664
665 void clear() {
666 Kind = EndOfFile;
667 Location = 0;
668 StringLength = 0;
669 StringData = 0;
670 }
671
672 bool is(TokenKind K) const { return Kind == K; }
673
674 SourceLocation getLocation() const {
675 return SourceLocation::getFromRawEncoding(Location);
676 }
677
678 StringRef getString() const {
679 return StringRef(StringData, StringLength);
680 }
681 };
Douglas Gregor82e52372012-11-06 19:39:40 +0000682
683 /// \brief The set of attributes that can be attached to a module.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000684 struct Attributes {
685 Attributes() : IsSystem() { }
Douglas Gregor82e52372012-11-06 19:39:40 +0000686
687 /// \brief Whether this is a system module.
688 unsigned IsSystem : 1;
689 };
Douglas Gregora30cfe52011-11-11 19:10:28 +0000690
Douglas Gregor82e52372012-11-06 19:39:40 +0000691
Douglas Gregora30cfe52011-11-11 19:10:28 +0000692 class ModuleMapParser {
693 Lexer &L;
694 SourceManager &SourceMgr;
Douglas Gregor9a022bb2012-10-15 16:45:32 +0000695
696 /// \brief Default target information, used only for string literal
697 /// parsing.
698 const TargetInfo *Target;
699
Douglas Gregora30cfe52011-11-11 19:10:28 +0000700 DiagnosticsEngine &Diags;
701 ModuleMap &Map;
702
Douglas Gregor8b6d3de2011-11-11 21:55:48 +0000703 /// \brief The directory that this module map resides in.
704 const DirectoryEntry *Directory;
Douglas Gregor2f04f182012-02-02 18:42:48 +0000705
706 /// \brief The directory containing Clang-supplied headers.
707 const DirectoryEntry *BuiltinIncludeDir;
708
Douglas Gregora30cfe52011-11-11 19:10:28 +0000709 /// \brief Whether an error occurred.
710 bool HadError;
Douglas Gregor9a022bb2012-10-15 16:45:32 +0000711
Douglas Gregora30cfe52011-11-11 19:10:28 +0000712 /// \brief Stores string data for the various string literals referenced
713 /// during parsing.
714 llvm::BumpPtrAllocator StringData;
715
716 /// \brief The current token.
717 MMToken Tok;
718
719 /// \brief The active module.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000720 Module *ActiveModule;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000721
722 /// \brief Consume the current token and return its location.
723 SourceLocation consumeToken();
724
725 /// \brief Skip tokens until we reach the a token with the given kind
726 /// (or the end of the file).
727 void skipUntil(MMToken::TokenKind K);
Douglas Gregor587986e2011-12-07 02:23:45 +0000728
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000729 typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
Douglas Gregor587986e2011-12-07 02:23:45 +0000730 bool parseModuleId(ModuleId &Id);
Douglas Gregora30cfe52011-11-11 19:10:28 +0000731 void parseModuleDecl();
Douglas Gregor51f564f2011-12-31 04:05:44 +0000732 void parseRequiresDecl();
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000733 void parseHeaderDecl(SourceLocation UmbrellaLoc, SourceLocation ExcludeLoc);
Douglas Gregor77d029f2011-12-08 19:11:24 +0000734 void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
Douglas Gregor90db2602011-12-02 01:47:07 +0000735 void parseExportDecl();
Douglas Gregorb6cbe512013-01-14 17:21:00 +0000736 void parseLinkDecl();
Douglas Gregor82e52372012-11-06 19:39:40 +0000737 void parseInferredModuleDecl(bool Framework, bool Explicit);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000738 bool parseOptionalAttributes(Attributes &Attrs);
Douglas Gregor82e52372012-11-06 19:39:40 +0000739
Douglas Gregor6a1db482011-12-09 02:04:43 +0000740 const DirectoryEntry *getOverriddenHeaderSearchDir();
741
Douglas Gregora30cfe52011-11-11 19:10:28 +0000742 public:
Douglas Gregora30cfe52011-11-11 19:10:28 +0000743 explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
Douglas Gregor9a022bb2012-10-15 16:45:32 +0000744 const TargetInfo *Target,
Douglas Gregora30cfe52011-11-11 19:10:28 +0000745 DiagnosticsEngine &Diags,
Douglas Gregor8b6d3de2011-11-11 21:55:48 +0000746 ModuleMap &Map,
Douglas Gregor2f04f182012-02-02 18:42:48 +0000747 const DirectoryEntry *Directory,
748 const DirectoryEntry *BuiltinIncludeDir)
Douglas Gregor9a022bb2012-10-15 16:45:32 +0000749 : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map),
Douglas Gregor2f04f182012-02-02 18:42:48 +0000750 Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir),
751 HadError(false), ActiveModule(0)
Douglas Gregora30cfe52011-11-11 19:10:28 +0000752 {
Douglas Gregora30cfe52011-11-11 19:10:28 +0000753 Tok.clear();
754 consumeToken();
755 }
756
757 bool parseModuleMapFile();
758 };
759}
760
761SourceLocation ModuleMapParser::consumeToken() {
762retry:
763 SourceLocation Result = Tok.getLocation();
764 Tok.clear();
765
766 Token LToken;
767 L.LexFromRawLexer(LToken);
768 Tok.Location = LToken.getLocation().getRawEncoding();
769 switch (LToken.getKind()) {
770 case tok::raw_identifier:
771 Tok.StringData = LToken.getRawIdentifierData();
772 Tok.StringLength = LToken.getLength();
773 Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
774 .Case("header", MMToken::HeaderKeyword)
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000775 .Case("exclude", MMToken::ExcludeKeyword)
Douglas Gregora30cfe52011-11-11 19:10:28 +0000776 .Case("explicit", MMToken::ExplicitKeyword)
Douglas Gregor90db2602011-12-02 01:47:07 +0000777 .Case("export", MMToken::ExportKeyword)
Douglas Gregora8654052011-11-17 22:09:43 +0000778 .Case("framework", MMToken::FrameworkKeyword)
Douglas Gregorb6cbe512013-01-14 17:21:00 +0000779 .Case("link", MMToken::LinkKeyword)
Douglas Gregora30cfe52011-11-11 19:10:28 +0000780 .Case("module", MMToken::ModuleKeyword)
Douglas Gregor51f564f2011-12-31 04:05:44 +0000781 .Case("requires", MMToken::RequiresKeyword)
Douglas Gregora30cfe52011-11-11 19:10:28 +0000782 .Case("umbrella", MMToken::UmbrellaKeyword)
783 .Default(MMToken::Identifier);
784 break;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000785
786 case tok::comma:
787 Tok.Kind = MMToken::Comma;
788 break;
789
Douglas Gregora30cfe52011-11-11 19:10:28 +0000790 case tok::eof:
791 Tok.Kind = MMToken::EndOfFile;
792 break;
793
794 case tok::l_brace:
795 Tok.Kind = MMToken::LBrace;
796 break;
797
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000798 case tok::l_square:
799 Tok.Kind = MMToken::LSquare;
800 break;
801
Douglas Gregor90db2602011-12-02 01:47:07 +0000802 case tok::period:
803 Tok.Kind = MMToken::Period;
804 break;
805
Douglas Gregora30cfe52011-11-11 19:10:28 +0000806 case tok::r_brace:
807 Tok.Kind = MMToken::RBrace;
808 break;
809
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000810 case tok::r_square:
811 Tok.Kind = MMToken::RSquare;
812 break;
813
Douglas Gregor90db2602011-12-02 01:47:07 +0000814 case tok::star:
815 Tok.Kind = MMToken::Star;
816 break;
817
Douglas Gregora30cfe52011-11-11 19:10:28 +0000818 case tok::string_literal: {
Richard Smith99831e42012-03-06 03:21:47 +0000819 if (LToken.hasUDSuffix()) {
820 Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl);
821 HadError = true;
822 goto retry;
823 }
824
Douglas Gregora30cfe52011-11-11 19:10:28 +0000825 // Parse the string literal.
826 LangOptions LangOpts;
827 StringLiteralParser StringLiteral(&LToken, 1, SourceMgr, LangOpts, *Target);
828 if (StringLiteral.hadError)
829 goto retry;
830
831 // Copy the string literal into our string data allocator.
832 unsigned Length = StringLiteral.GetStringLength();
833 char *Saved = StringData.Allocate<char>(Length + 1);
834 memcpy(Saved, StringLiteral.GetString().data(), Length);
835 Saved[Length] = 0;
836
837 // Form the token.
838 Tok.Kind = MMToken::StringLiteral;
839 Tok.StringData = Saved;
840 Tok.StringLength = Length;
841 break;
842 }
843
844 case tok::comment:
845 goto retry;
846
847 default:
848 Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
849 HadError = true;
850 goto retry;
851 }
852
853 return Result;
854}
855
856void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
857 unsigned braceDepth = 0;
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000858 unsigned squareDepth = 0;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000859 do {
860 switch (Tok.Kind) {
861 case MMToken::EndOfFile:
862 return;
863
864 case MMToken::LBrace:
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000865 if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
Douglas Gregora30cfe52011-11-11 19:10:28 +0000866 return;
867
868 ++braceDepth;
869 break;
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000870
871 case MMToken::LSquare:
872 if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
873 return;
874
875 ++squareDepth;
876 break;
877
Douglas Gregora30cfe52011-11-11 19:10:28 +0000878 case MMToken::RBrace:
879 if (braceDepth > 0)
880 --braceDepth;
881 else if (Tok.is(K))
882 return;
883 break;
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000884
885 case MMToken::RSquare:
886 if (squareDepth > 0)
887 --squareDepth;
888 else if (Tok.is(K))
889 return;
890 break;
891
Douglas Gregora30cfe52011-11-11 19:10:28 +0000892 default:
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000893 if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
Douglas Gregora30cfe52011-11-11 19:10:28 +0000894 return;
895 break;
896 }
897
898 consumeToken();
899 } while (true);
900}
901
Douglas Gregor587986e2011-12-07 02:23:45 +0000902/// \brief Parse a module-id.
903///
904/// module-id:
905/// identifier
906/// identifier '.' module-id
907///
908/// \returns true if an error occurred, false otherwise.
909bool ModuleMapParser::parseModuleId(ModuleId &Id) {
910 Id.clear();
911 do {
912 if (Tok.is(MMToken::Identifier)) {
913 Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
914 consumeToken();
915 } else {
916 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
917 return true;
918 }
919
920 if (!Tok.is(MMToken::Period))
921 break;
922
923 consumeToken();
924 } while (true);
925
926 return false;
927}
928
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000929namespace {
930 /// \brief Enumerates the known attributes.
931 enum AttributeKind {
932 /// \brief An unknown attribute.
933 AT_unknown,
934 /// \brief The 'system' attribute.
935 AT_system
936 };
937}
938
Douglas Gregora30cfe52011-11-11 19:10:28 +0000939/// \brief Parse a module declaration.
940///
941/// module-declaration:
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000942/// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
943/// { module-member* }
944///
Douglas Gregora30cfe52011-11-11 19:10:28 +0000945/// module-member:
Douglas Gregor51f564f2011-12-31 04:05:44 +0000946/// requires-declaration
Douglas Gregora30cfe52011-11-11 19:10:28 +0000947/// header-declaration
Douglas Gregor587986e2011-12-07 02:23:45 +0000948/// submodule-declaration
Douglas Gregor90db2602011-12-02 01:47:07 +0000949/// export-declaration
Douglas Gregorb6cbe512013-01-14 17:21:00 +0000950/// link-declaration
Douglas Gregor1e123682011-12-05 22:27:44 +0000951///
952/// submodule-declaration:
953/// module-declaration
954/// inferred-submodule-declaration
Douglas Gregora30cfe52011-11-11 19:10:28 +0000955void ModuleMapParser::parseModuleDecl() {
Douglas Gregora8654052011-11-17 22:09:43 +0000956 assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
957 Tok.is(MMToken::FrameworkKeyword));
Douglas Gregord620a842011-12-06 17:16:41 +0000958 // Parse 'explicit' or 'framework' keyword, if present.
Douglas Gregor587986e2011-12-07 02:23:45 +0000959 SourceLocation ExplicitLoc;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000960 bool Explicit = false;
Douglas Gregord620a842011-12-06 17:16:41 +0000961 bool Framework = false;
Douglas Gregora8654052011-11-17 22:09:43 +0000962
Douglas Gregord620a842011-12-06 17:16:41 +0000963 // Parse 'explicit' keyword, if present.
964 if (Tok.is(MMToken::ExplicitKeyword)) {
Douglas Gregor587986e2011-12-07 02:23:45 +0000965 ExplicitLoc = consumeToken();
Douglas Gregord620a842011-12-06 17:16:41 +0000966 Explicit = true;
967 }
968
969 // Parse 'framework' keyword, if present.
Douglas Gregora8654052011-11-17 22:09:43 +0000970 if (Tok.is(MMToken::FrameworkKeyword)) {
971 consumeToken();
972 Framework = true;
973 }
Douglas Gregora30cfe52011-11-11 19:10:28 +0000974
975 // Parse 'module' keyword.
976 if (!Tok.is(MMToken::ModuleKeyword)) {
Douglas Gregore6fb9872011-12-06 19:57:48 +0000977 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
Douglas Gregora30cfe52011-11-11 19:10:28 +0000978 consumeToken();
979 HadError = true;
980 return;
981 }
982 consumeToken(); // 'module' keyword
Douglas Gregor1e123682011-12-05 22:27:44 +0000983
984 // If we have a wildcard for the module name, this is an inferred submodule.
985 // Parse it.
986 if (Tok.is(MMToken::Star))
Douglas Gregor82e52372012-11-06 19:39:40 +0000987 return parseInferredModuleDecl(Framework, Explicit);
Douglas Gregora30cfe52011-11-11 19:10:28 +0000988
989 // Parse the module name.
Douglas Gregor587986e2011-12-07 02:23:45 +0000990 ModuleId Id;
991 if (parseModuleId(Id)) {
Douglas Gregora30cfe52011-11-11 19:10:28 +0000992 HadError = true;
Douglas Gregor587986e2011-12-07 02:23:45 +0000993 return;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000994 }
Douglas Gregor82e52372012-11-06 19:39:40 +0000995
Douglas Gregor587986e2011-12-07 02:23:45 +0000996 if (ActiveModule) {
997 if (Id.size() > 1) {
998 Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
999 << SourceRange(Id.front().second, Id.back().second);
1000
1001 HadError = true;
1002 return;
1003 }
1004 } else if (Id.size() == 1 && Explicit) {
1005 // Top-level modules can't be explicit.
1006 Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
1007 Explicit = false;
1008 ExplicitLoc = SourceLocation();
1009 HadError = true;
1010 }
1011
1012 Module *PreviousActiveModule = ActiveModule;
1013 if (Id.size() > 1) {
1014 // This module map defines a submodule. Go find the module of which it
1015 // is a submodule.
1016 ActiveModule = 0;
1017 for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
1018 if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
1019 ActiveModule = Next;
1020 continue;
1021 }
1022
1023 if (ActiveModule) {
1024 Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
1025 << Id[I].first << ActiveModule->getTopLevelModule();
1026 } else {
1027 Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
1028 }
1029 HadError = true;
1030 return;
1031 }
1032 }
1033
1034 StringRef ModuleName = Id.back().first;
1035 SourceLocation ModuleNameLoc = Id.back().second;
Douglas Gregora30cfe52011-11-11 19:10:28 +00001036
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001037 // Parse the optional attribute list.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001038 Attributes Attrs;
Douglas Gregor82e52372012-11-06 19:39:40 +00001039 parseOptionalAttributes(Attrs);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001040
Douglas Gregora30cfe52011-11-11 19:10:28 +00001041 // Parse the opening brace.
1042 if (!Tok.is(MMToken::LBrace)) {
1043 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
1044 << ModuleName;
1045 HadError = true;
1046 return;
1047 }
1048 SourceLocation LBraceLoc = consumeToken();
1049
1050 // Determine whether this (sub)module has already been defined.
Douglas Gregorb7a78192012-01-04 23:32:19 +00001051 if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
Douglas Gregorc634f502012-01-05 00:12:00 +00001052 if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) {
1053 // Skip the module definition.
1054 skipUntil(MMToken::RBrace);
1055 if (Tok.is(MMToken::RBrace))
1056 consumeToken();
1057 else {
1058 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1059 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1060 HadError = true;
1061 }
1062 return;
1063 }
1064
Douglas Gregora30cfe52011-11-11 19:10:28 +00001065 Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
1066 << ModuleName;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001067 Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
Douglas Gregora30cfe52011-11-11 19:10:28 +00001068
1069 // Skip the module definition.
1070 skipUntil(MMToken::RBrace);
1071 if (Tok.is(MMToken::RBrace))
1072 consumeToken();
1073
1074 HadError = true;
1075 return;
1076 }
1077
1078 // Start defining this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00001079 ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
1080 Explicit).first;
1081 ActiveModule->DefinitionLoc = ModuleNameLoc;
Douglas Gregor82e52372012-11-06 19:39:40 +00001082 if (Attrs.IsSystem)
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001083 ActiveModule->IsSystem = true;
Douglas Gregora30cfe52011-11-11 19:10:28 +00001084
1085 bool Done = false;
1086 do {
1087 switch (Tok.Kind) {
1088 case MMToken::EndOfFile:
1089 case MMToken::RBrace:
1090 Done = true;
1091 break;
1092
1093 case MMToken::ExplicitKeyword:
Douglas Gregord620a842011-12-06 17:16:41 +00001094 case MMToken::FrameworkKeyword:
Douglas Gregora30cfe52011-11-11 19:10:28 +00001095 case MMToken::ModuleKeyword:
1096 parseModuleDecl();
1097 break;
1098
Douglas Gregor90db2602011-12-02 01:47:07 +00001099 case MMToken::ExportKeyword:
1100 parseExportDecl();
1101 break;
1102
Douglas Gregor51f564f2011-12-31 04:05:44 +00001103 case MMToken::RequiresKeyword:
1104 parseRequiresDecl();
1105 break;
1106
Douglas Gregor77d029f2011-12-08 19:11:24 +00001107 case MMToken::UmbrellaKeyword: {
1108 SourceLocation UmbrellaLoc = consumeToken();
1109 if (Tok.is(MMToken::HeaderKeyword))
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00001110 parseHeaderDecl(UmbrellaLoc, SourceLocation());
Douglas Gregor77d029f2011-12-08 19:11:24 +00001111 else
1112 parseUmbrellaDirDecl(UmbrellaLoc);
Douglas Gregora30cfe52011-11-11 19:10:28 +00001113 break;
Douglas Gregor77d029f2011-12-08 19:11:24 +00001114 }
Douglas Gregora30cfe52011-11-11 19:10:28 +00001115
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00001116 case MMToken::ExcludeKeyword: {
1117 SourceLocation ExcludeLoc = consumeToken();
1118 if (Tok.is(MMToken::HeaderKeyword)) {
1119 parseHeaderDecl(SourceLocation(), ExcludeLoc);
1120 } else {
1121 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1122 << "exclude";
1123 }
1124 break;
1125 }
1126
Douglas Gregor489ad432011-12-08 18:00:48 +00001127 case MMToken::HeaderKeyword:
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00001128 parseHeaderDecl(SourceLocation(), SourceLocation());
Douglas Gregora30cfe52011-11-11 19:10:28 +00001129 break;
Douglas Gregorb6cbe512013-01-14 17:21:00 +00001130
1131 case MMToken::LinkKeyword:
1132 parseLinkDecl();
1133 break;
1134
Douglas Gregora30cfe52011-11-11 19:10:28 +00001135 default:
1136 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
1137 consumeToken();
1138 break;
1139 }
1140 } while (!Done);
1141
1142 if (Tok.is(MMToken::RBrace))
1143 consumeToken();
1144 else {
1145 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1146 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1147 HadError = true;
1148 }
1149
Douglas Gregor587986e2011-12-07 02:23:45 +00001150 // We're done parsing this module. Pop back to the previous module.
1151 ActiveModule = PreviousActiveModule;
Douglas Gregora30cfe52011-11-11 19:10:28 +00001152}
Douglas Gregord620a842011-12-06 17:16:41 +00001153
Douglas Gregor51f564f2011-12-31 04:05:44 +00001154/// \brief Parse a requires declaration.
1155///
1156/// requires-declaration:
1157/// 'requires' feature-list
1158///
1159/// feature-list:
1160/// identifier ',' feature-list
1161/// identifier
1162void ModuleMapParser::parseRequiresDecl() {
1163 assert(Tok.is(MMToken::RequiresKeyword));
1164
1165 // Parse 'requires' keyword.
1166 consumeToken();
1167
1168 // Parse the feature-list.
1169 do {
1170 if (!Tok.is(MMToken::Identifier)) {
1171 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
1172 HadError = true;
1173 return;
1174 }
1175
1176 // Consume the feature name.
1177 std::string Feature = Tok.getString();
1178 consumeToken();
1179
1180 // Add this feature.
Douglas Gregordc58aa72012-01-30 06:01:29 +00001181 ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target);
Douglas Gregor51f564f2011-12-31 04:05:44 +00001182
1183 if (!Tok.is(MMToken::Comma))
1184 break;
1185
1186 // Consume the comma.
1187 consumeToken();
1188 } while (true);
1189}
1190
Douglas Gregord620a842011-12-06 17:16:41 +00001191/// \brief Append to \p Paths the set of paths needed to get to the
1192/// subframework in which the given module lives.
Benjamin Kramer5bbc3852012-02-06 11:13:08 +00001193static void appendSubframeworkPaths(Module *Mod,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001194 SmallVectorImpl<char> &Path) {
Douglas Gregord620a842011-12-06 17:16:41 +00001195 // Collect the framework names from the given module to the top-level module.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001196 SmallVector<StringRef, 2> Paths;
Douglas Gregord620a842011-12-06 17:16:41 +00001197 for (; Mod; Mod = Mod->Parent) {
1198 if (Mod->IsFramework)
1199 Paths.push_back(Mod->Name);
1200 }
1201
1202 if (Paths.empty())
1203 return;
1204
1205 // Add Frameworks/Name.framework for each subframework.
1206 for (unsigned I = Paths.size() - 1; I != 0; --I) {
1207 llvm::sys::path::append(Path, "Frameworks");
1208 llvm::sys::path::append(Path, Paths[I-1] + ".framework");
1209 }
1210}
1211
Douglas Gregor2f04f182012-02-02 18:42:48 +00001212/// \brief Determine whether the given file name is the name of a builtin
1213/// header, supplied by Clang to replace, override, or augment existing system
1214/// headers.
1215static bool isBuiltinHeader(StringRef FileName) {
1216 return llvm::StringSwitch<bool>(FileName)
1217 .Case("float.h", true)
1218 .Case("iso646.h", true)
1219 .Case("limits.h", true)
1220 .Case("stdalign.h", true)
1221 .Case("stdarg.h", true)
1222 .Case("stdbool.h", true)
1223 .Case("stddef.h", true)
1224 .Case("stdint.h", true)
1225 .Case("tgmath.h", true)
1226 .Case("unwind.h", true)
1227 .Default(false);
1228}
1229
Douglas Gregora30cfe52011-11-11 19:10:28 +00001230/// \brief Parse a header declaration.
1231///
1232/// header-declaration:
Douglas Gregor489ad432011-12-08 18:00:48 +00001233/// 'umbrella'[opt] 'header' string-literal
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00001234/// 'exclude'[opt] 'header' string-literal
1235void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc,
1236 SourceLocation ExcludeLoc) {
Douglas Gregora30cfe52011-11-11 19:10:28 +00001237 assert(Tok.is(MMToken::HeaderKeyword));
Benjamin Kramerc96c7212011-11-13 16:52:09 +00001238 consumeToken();
1239
Douglas Gregor489ad432011-12-08 18:00:48 +00001240 bool Umbrella = UmbrellaLoc.isValid();
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00001241 bool Exclude = ExcludeLoc.isValid();
1242 assert(!(Umbrella && Exclude) && "Cannot have both 'umbrella' and 'exclude'");
Douglas Gregora30cfe52011-11-11 19:10:28 +00001243 // Parse the header name.
1244 if (!Tok.is(MMToken::StringLiteral)) {
1245 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1246 << "header";
1247 HadError = true;
1248 return;
1249 }
Douglas Gregor587986e2011-12-07 02:23:45 +00001250 std::string FileName = Tok.getString();
Douglas Gregora30cfe52011-11-11 19:10:28 +00001251 SourceLocation FileNameLoc = consumeToken();
1252
Douglas Gregor77d029f2011-12-08 19:11:24 +00001253 // Check whether we already have an umbrella.
1254 if (Umbrella && ActiveModule->Umbrella) {
1255 Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash)
1256 << ActiveModule->getFullModuleName();
Douglas Gregor489ad432011-12-08 18:00:48 +00001257 HadError = true;
1258 return;
1259 }
1260
Douglas Gregor8b6d3de2011-11-11 21:55:48 +00001261 // Look for this file.
Douglas Gregor587986e2011-12-07 02:23:45 +00001262 const FileEntry *File = 0;
Douglas Gregor2f04f182012-02-02 18:42:48 +00001263 const FileEntry *BuiltinFile = 0;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001264 SmallString<128> PathName;
Douglas Gregor587986e2011-12-07 02:23:45 +00001265 if (llvm::sys::path::is_absolute(FileName)) {
1266 PathName = FileName;
1267 File = SourceMgr.getFileManager().getFile(PathName);
Douglas Gregor6a1db482011-12-09 02:04:43 +00001268 } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) {
1269 PathName = Dir->getName();
1270 llvm::sys::path::append(PathName, FileName);
1271 File = SourceMgr.getFileManager().getFile(PathName);
Douglas Gregor587986e2011-12-07 02:23:45 +00001272 } else {
1273 // Search for the header file within the search directory.
Douglas Gregor6a1db482011-12-09 02:04:43 +00001274 PathName = Directory->getName();
Douglas Gregor587986e2011-12-07 02:23:45 +00001275 unsigned PathLength = PathName.size();
Douglas Gregor18ee5472011-11-29 21:59:16 +00001276
Douglas Gregord620a842011-12-06 17:16:41 +00001277 if (ActiveModule->isPartOfFramework()) {
1278 appendSubframeworkPaths(ActiveModule, PathName);
Douglas Gregor587986e2011-12-07 02:23:45 +00001279
1280 // Check whether this file is in the public headers.
Douglas Gregor18ee5472011-11-29 21:59:16 +00001281 llvm::sys::path::append(PathName, "Headers");
Douglas Gregor587986e2011-12-07 02:23:45 +00001282 llvm::sys::path::append(PathName, FileName);
1283 File = SourceMgr.getFileManager().getFile(PathName);
1284
1285 if (!File) {
1286 // Check whether this file is in the private headers.
1287 PathName.resize(PathLength);
1288 llvm::sys::path::append(PathName, "PrivateHeaders");
1289 llvm::sys::path::append(PathName, FileName);
1290 File = SourceMgr.getFileManager().getFile(PathName);
1291 }
1292 } else {
1293 // Lookup for normal headers.
1294 llvm::sys::path::append(PathName, FileName);
1295 File = SourceMgr.getFileManager().getFile(PathName);
Douglas Gregor2f04f182012-02-02 18:42:48 +00001296
1297 // If this is a system module with a top-level header, this header
1298 // may have a counterpart (or replacement) in the set of headers
1299 // supplied by Clang. Find that builtin header.
1300 if (ActiveModule->IsSystem && !Umbrella && BuiltinIncludeDir &&
1301 BuiltinIncludeDir != Directory && isBuiltinHeader(FileName)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001302 SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName());
Douglas Gregor2f04f182012-02-02 18:42:48 +00001303 llvm::sys::path::append(BuiltinPathName, FileName);
1304 BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName);
1305
1306 // If Clang supplies this header but the underlying system does not,
1307 // just silently swap in our builtin version. Otherwise, we'll end
1308 // up adding both (later).
1309 if (!File && BuiltinFile) {
1310 File = BuiltinFile;
1311 BuiltinFile = 0;
1312 }
1313 }
Douglas Gregord620a842011-12-06 17:16:41 +00001314 }
Douglas Gregor18ee5472011-11-29 21:59:16 +00001315 }
Douglas Gregora8654052011-11-17 22:09:43 +00001316
Douglas Gregor8b6d3de2011-11-11 21:55:48 +00001317 // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
1318 // Come up with a lazy way to do this.
Douglas Gregor587986e2011-12-07 02:23:45 +00001319 if (File) {
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00001320 if (ModuleMap::KnownHeader OwningModule = Map.Headers[File]) {
Douglas Gregor8b6d3de2011-11-11 21:55:48 +00001321 Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00001322 << FileName << OwningModule.getModule()->getFullModuleName();
Douglas Gregor8b6d3de2011-11-11 21:55:48 +00001323 HadError = true;
Douglas Gregor489ad432011-12-08 18:00:48 +00001324 } else if (Umbrella) {
1325 const DirectoryEntry *UmbrellaDir = File->getDir();
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00001326 if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) {
Douglas Gregor489ad432011-12-08 18:00:48 +00001327 Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00001328 << UmbrellaModule->getFullModuleName();
Douglas Gregor489ad432011-12-08 18:00:48 +00001329 HadError = true;
1330 } else {
1331 // Record this umbrella header.
1332 Map.setUmbrellaHeader(ActiveModule, File);
1333 }
Douglas Gregor8b6d3de2011-11-11 21:55:48 +00001334 } else {
Douglas Gregor489ad432011-12-08 18:00:48 +00001335 // Record this header.
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00001336 Map.addHeader(ActiveModule, File, Exclude);
Douglas Gregor2f04f182012-02-02 18:42:48 +00001337
1338 // If there is a builtin counterpart to this file, add it now.
1339 if (BuiltinFile)
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00001340 Map.addHeader(ActiveModule, BuiltinFile, Exclude);
Douglas Gregor8b6d3de2011-11-11 21:55:48 +00001341 }
Douglas Gregor71f49f52012-11-15 19:47:16 +00001342 } else if (!Exclude) {
1343 // Ignore excluded header files. They're optional anyway.
1344
Douglas Gregor8b6d3de2011-11-11 21:55:48 +00001345 Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
Douglas Gregor77d029f2011-12-08 19:11:24 +00001346 << Umbrella << FileName;
Douglas Gregor8b6d3de2011-11-11 21:55:48 +00001347 HadError = true;
1348 }
Douglas Gregora30cfe52011-11-11 19:10:28 +00001349}
1350
Douglas Gregor77d029f2011-12-08 19:11:24 +00001351/// \brief Parse an umbrella directory declaration.
1352///
1353/// umbrella-dir-declaration:
1354/// umbrella string-literal
1355void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
1356 // Parse the directory name.
1357 if (!Tok.is(MMToken::StringLiteral)) {
1358 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1359 << "umbrella";
1360 HadError = true;
1361 return;
1362 }
1363
1364 std::string DirName = Tok.getString();
1365 SourceLocation DirNameLoc = consumeToken();
1366
1367 // Check whether we already have an umbrella.
1368 if (ActiveModule->Umbrella) {
1369 Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
1370 << ActiveModule->getFullModuleName();
1371 HadError = true;
1372 return;
1373 }
1374
1375 // Look for this file.
1376 const DirectoryEntry *Dir = 0;
1377 if (llvm::sys::path::is_absolute(DirName))
1378 Dir = SourceMgr.getFileManager().getDirectory(DirName);
1379 else {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001380 SmallString<128> PathName;
Douglas Gregor77d029f2011-12-08 19:11:24 +00001381 PathName = Directory->getName();
1382 llvm::sys::path::append(PathName, DirName);
1383 Dir = SourceMgr.getFileManager().getDirectory(PathName);
1384 }
1385
1386 if (!Dir) {
1387 Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
1388 << DirName;
1389 HadError = true;
1390 return;
1391 }
1392
1393 if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
1394 Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1395 << OwningModule->getFullModuleName();
1396 HadError = true;
1397 return;
1398 }
1399
1400 // Record this umbrella directory.
1401 Map.setUmbrellaDir(ActiveModule, Dir);
1402}
1403
Douglas Gregor90db2602011-12-02 01:47:07 +00001404/// \brief Parse a module export declaration.
1405///
1406/// export-declaration:
1407/// 'export' wildcard-module-id
1408///
1409/// wildcard-module-id:
1410/// identifier
1411/// '*'
1412/// identifier '.' wildcard-module-id
1413void ModuleMapParser::parseExportDecl() {
1414 assert(Tok.is(MMToken::ExportKeyword));
1415 SourceLocation ExportLoc = consumeToken();
1416
1417 // Parse the module-id with an optional wildcard at the end.
1418 ModuleId ParsedModuleId;
1419 bool Wildcard = false;
1420 do {
1421 if (Tok.is(MMToken::Identifier)) {
1422 ParsedModuleId.push_back(std::make_pair(Tok.getString(),
1423 Tok.getLocation()));
1424 consumeToken();
1425
1426 if (Tok.is(MMToken::Period)) {
1427 consumeToken();
1428 continue;
1429 }
1430
1431 break;
1432 }
1433
1434 if(Tok.is(MMToken::Star)) {
1435 Wildcard = true;
Douglas Gregor0adaa882011-12-05 17:28:06 +00001436 consumeToken();
Douglas Gregor90db2602011-12-02 01:47:07 +00001437 break;
1438 }
1439
1440 Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id);
1441 HadError = true;
1442 return;
1443 } while (true);
1444
1445 Module::UnresolvedExportDecl Unresolved = {
1446 ExportLoc, ParsedModuleId, Wildcard
1447 };
1448 ActiveModule->UnresolvedExports.push_back(Unresolved);
1449}
1450
Douglas Gregorb6cbe512013-01-14 17:21:00 +00001451/// \brief Parse a link declaration.
1452///
1453/// module-declaration:
1454/// 'link' 'framework'[opt] string-literal
1455void ModuleMapParser::parseLinkDecl() {
1456 assert(Tok.is(MMToken::LinkKeyword));
1457 SourceLocation LinkLoc = consumeToken();
1458
1459 // Parse the optional 'framework' keyword.
1460 bool IsFramework = false;
1461 if (Tok.is(MMToken::FrameworkKeyword)) {
1462 consumeToken();
1463 IsFramework = true;
1464 }
1465
1466 // Parse the library name
1467 if (!Tok.is(MMToken::StringLiteral)) {
1468 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name)
1469 << IsFramework << SourceRange(LinkLoc);
1470 HadError = true;
1471 return;
1472 }
1473
1474 std::string LibraryName = Tok.getString();
1475 consumeToken();
1476 ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName,
1477 IsFramework));
1478}
1479
1480/// \brief Parse an inferred module declaration (wildcard modules).
Douglas Gregor82e52372012-11-06 19:39:40 +00001481///
1482/// module-declaration:
1483/// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt]
1484/// { inferred-module-member* }
1485///
1486/// inferred-module-member:
1487/// 'export' '*'
1488/// 'exclude' identifier
1489void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) {
Douglas Gregor1e123682011-12-05 22:27:44 +00001490 assert(Tok.is(MMToken::Star));
1491 SourceLocation StarLoc = consumeToken();
1492 bool Failed = false;
Douglas Gregor82e52372012-11-06 19:39:40 +00001493
Douglas Gregor1e123682011-12-05 22:27:44 +00001494 // Inferred modules must be submodules.
Douglas Gregor82e52372012-11-06 19:39:40 +00001495 if (!ActiveModule && !Framework) {
Douglas Gregor1e123682011-12-05 22:27:44 +00001496 Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
1497 Failed = true;
1498 }
Douglas Gregor82e52372012-11-06 19:39:40 +00001499
1500 if (ActiveModule) {
1501 // Inferred modules must have umbrella directories.
1502 if (!Failed && !ActiveModule->getUmbrellaDir()) {
1503 Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
1504 Failed = true;
1505 }
1506
1507 // Check for redefinition of an inferred module.
1508 if (!Failed && ActiveModule->InferSubmodules) {
1509 Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
1510 if (ActiveModule->InferredSubmoduleLoc.isValid())
1511 Diags.Report(ActiveModule->InferredSubmoduleLoc,
1512 diag::note_mmap_prev_definition);
1513 Failed = true;
1514 }
1515
1516 // Check for the 'framework' keyword, which is not permitted here.
1517 if (Framework) {
1518 Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule);
1519 Framework = false;
1520 }
1521 } else if (Explicit) {
1522 Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework);
1523 Explicit = false;
Douglas Gregor1e123682011-12-05 22:27:44 +00001524 }
Douglas Gregor82e52372012-11-06 19:39:40 +00001525
Douglas Gregor1e123682011-12-05 22:27:44 +00001526 // If there were any problems with this inferred submodule, skip its body.
1527 if (Failed) {
1528 if (Tok.is(MMToken::LBrace)) {
1529 consumeToken();
1530 skipUntil(MMToken::RBrace);
1531 if (Tok.is(MMToken::RBrace))
1532 consumeToken();
1533 }
1534 HadError = true;
1535 return;
1536 }
Douglas Gregor82e52372012-11-06 19:39:40 +00001537
1538 // Parse optional attributes.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001539 Attributes Attrs;
Douglas Gregor82e52372012-11-06 19:39:40 +00001540 parseOptionalAttributes(Attrs);
1541
1542 if (ActiveModule) {
1543 // Note that we have an inferred submodule.
1544 ActiveModule->InferSubmodules = true;
1545 ActiveModule->InferredSubmoduleLoc = StarLoc;
1546 ActiveModule->InferExplicitSubmodules = Explicit;
1547 } else {
1548 // We'll be inferring framework modules for this directory.
1549 Map.InferredDirectories[Directory].InferModules = true;
1550 Map.InferredDirectories[Directory].InferSystemModules = Attrs.IsSystem;
1551 }
1552
Douglas Gregor1e123682011-12-05 22:27:44 +00001553 // Parse the opening brace.
1554 if (!Tok.is(MMToken::LBrace)) {
1555 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
1556 HadError = true;
1557 return;
1558 }
1559 SourceLocation LBraceLoc = consumeToken();
1560
1561 // Parse the body of the inferred submodule.
1562 bool Done = false;
1563 do {
1564 switch (Tok.Kind) {
1565 case MMToken::EndOfFile:
1566 case MMToken::RBrace:
1567 Done = true;
1568 break;
Douglas Gregor82e52372012-11-06 19:39:40 +00001569
1570 case MMToken::ExcludeKeyword: {
1571 if (ActiveModule) {
1572 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
Douglas Gregorb7ac5ac2012-11-06 19:41:11 +00001573 << (ActiveModule != 0);
Douglas Gregor82e52372012-11-06 19:39:40 +00001574 consumeToken();
1575 break;
1576 }
1577
1578 consumeToken();
1579 if (!Tok.is(MMToken::Identifier)) {
1580 Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name);
1581 break;
1582 }
1583
1584 Map.InferredDirectories[Directory].ExcludedModules
1585 .push_back(Tok.getString());
1586 consumeToken();
1587 break;
1588 }
1589
1590 case MMToken::ExportKeyword:
1591 if (!ActiveModule) {
1592 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
Douglas Gregorb7ac5ac2012-11-06 19:41:11 +00001593 << (ActiveModule != 0);
Douglas Gregor82e52372012-11-06 19:39:40 +00001594 consumeToken();
1595 break;
1596 }
1597
Douglas Gregor1e123682011-12-05 22:27:44 +00001598 consumeToken();
1599 if (Tok.is(MMToken::Star))
Douglas Gregoref85b562011-12-06 17:34:58 +00001600 ActiveModule->InferExportWildcard = true;
Douglas Gregor1e123682011-12-05 22:27:44 +00001601 else
1602 Diags.Report(Tok.getLocation(),
1603 diag::err_mmap_expected_export_wildcard);
1604 consumeToken();
1605 break;
Douglas Gregor82e52372012-11-06 19:39:40 +00001606
Douglas Gregor1e123682011-12-05 22:27:44 +00001607 case MMToken::ExplicitKeyword:
1608 case MMToken::ModuleKeyword:
1609 case MMToken::HeaderKeyword:
1610 case MMToken::UmbrellaKeyword:
1611 default:
Douglas Gregor82e52372012-11-06 19:39:40 +00001612 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
Douglas Gregorb7ac5ac2012-11-06 19:41:11 +00001613 << (ActiveModule != 0);
Douglas Gregor1e123682011-12-05 22:27:44 +00001614 consumeToken();
1615 break;
1616 }
1617 } while (!Done);
1618
1619 if (Tok.is(MMToken::RBrace))
1620 consumeToken();
1621 else {
1622 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1623 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1624 HadError = true;
1625 }
1626}
1627
Douglas Gregor82e52372012-11-06 19:39:40 +00001628/// \brief Parse optional attributes.
1629///
1630/// attributes:
1631/// attribute attributes
1632/// attribute
1633///
1634/// attribute:
1635/// [ identifier ]
1636///
1637/// \param Attrs Will be filled in with the parsed attributes.
1638///
1639/// \returns true if an error occurred, false otherwise.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001640bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) {
Douglas Gregor82e52372012-11-06 19:39:40 +00001641 bool HadError = false;
1642
1643 while (Tok.is(MMToken::LSquare)) {
1644 // Consume the '['.
1645 SourceLocation LSquareLoc = consumeToken();
1646
1647 // Check whether we have an attribute name here.
1648 if (!Tok.is(MMToken::Identifier)) {
1649 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
1650 skipUntil(MMToken::RSquare);
1651 if (Tok.is(MMToken::RSquare))
1652 consumeToken();
1653 HadError = true;
1654 }
1655
1656 // Decode the attribute name.
1657 AttributeKind Attribute
1658 = llvm::StringSwitch<AttributeKind>(Tok.getString())
1659 .Case("system", AT_system)
1660 .Default(AT_unknown);
1661 switch (Attribute) {
1662 case AT_unknown:
1663 Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
1664 << Tok.getString();
1665 break;
1666
1667 case AT_system:
1668 Attrs.IsSystem = true;
1669 break;
1670 }
1671 consumeToken();
1672
1673 // Consume the ']'.
1674 if (!Tok.is(MMToken::RSquare)) {
1675 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
1676 Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
1677 skipUntil(MMToken::RSquare);
1678 HadError = true;
1679 }
1680
1681 if (Tok.is(MMToken::RSquare))
1682 consumeToken();
1683 }
1684
1685 return HadError;
1686}
1687
Douglas Gregor6a1db482011-12-09 02:04:43 +00001688/// \brief If there is a specific header search directory due the presence
1689/// of an umbrella directory, retrieve that directory. Otherwise, returns null.
1690const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() {
1691 for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) {
1692 // If we have an umbrella directory, use that.
1693 if (Mod->hasUmbrellaDir())
1694 return Mod->getUmbrellaDir();
1695
1696 // If we have a framework directory, stop looking.
1697 if (Mod->IsFramework)
1698 return 0;
1699 }
1700
1701 return 0;
1702}
1703
Douglas Gregora30cfe52011-11-11 19:10:28 +00001704/// \brief Parse a module map file.
1705///
1706/// module-map-file:
1707/// module-declaration*
1708bool ModuleMapParser::parseModuleMapFile() {
1709 do {
1710 switch (Tok.Kind) {
1711 case MMToken::EndOfFile:
1712 return HadError;
1713
Douglas Gregor587986e2011-12-07 02:23:45 +00001714 case MMToken::ExplicitKeyword:
Douglas Gregora30cfe52011-11-11 19:10:28 +00001715 case MMToken::ModuleKeyword:
Douglas Gregora8654052011-11-17 22:09:43 +00001716 case MMToken::FrameworkKeyword:
Douglas Gregora30cfe52011-11-11 19:10:28 +00001717 parseModuleDecl();
1718 break;
Douglas Gregorb6cbe512013-01-14 17:21:00 +00001719
Douglas Gregor51f564f2011-12-31 04:05:44 +00001720 case MMToken::Comma:
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00001721 case MMToken::ExcludeKeyword:
Douglas Gregor90db2602011-12-02 01:47:07 +00001722 case MMToken::ExportKeyword:
Douglas Gregora30cfe52011-11-11 19:10:28 +00001723 case MMToken::HeaderKeyword:
1724 case MMToken::Identifier:
1725 case MMToken::LBrace:
Douglas Gregorb6cbe512013-01-14 17:21:00 +00001726 case MMToken::LinkKeyword:
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001727 case MMToken::LSquare:
Douglas Gregor90db2602011-12-02 01:47:07 +00001728 case MMToken::Period:
Douglas Gregora30cfe52011-11-11 19:10:28 +00001729 case MMToken::RBrace:
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001730 case MMToken::RSquare:
Douglas Gregor51f564f2011-12-31 04:05:44 +00001731 case MMToken::RequiresKeyword:
Douglas Gregor90db2602011-12-02 01:47:07 +00001732 case MMToken::Star:
Douglas Gregora30cfe52011-11-11 19:10:28 +00001733 case MMToken::StringLiteral:
1734 case MMToken::UmbrellaKeyword:
1735 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1736 HadError = true;
1737 consumeToken();
1738 break;
1739 }
1740 } while (true);
Douglas Gregora30cfe52011-11-11 19:10:28 +00001741}
1742
1743bool ModuleMap::parseModuleMapFile(const FileEntry *File) {
Douglas Gregor7005b902013-01-10 01:43:00 +00001744 llvm::DenseMap<const FileEntry *, bool>::iterator Known
1745 = ParsedModuleMap.find(File);
1746 if (Known != ParsedModuleMap.end())
1747 return Known->second;
1748
Douglas Gregordc58aa72012-01-30 06:01:29 +00001749 assert(Target != 0 && "Missing target information");
Douglas Gregora30cfe52011-11-11 19:10:28 +00001750 FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
1751 const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
1752 if (!Buffer)
Douglas Gregor7005b902013-01-10 01:43:00 +00001753 return ParsedModuleMap[File] = true;
Douglas Gregora30cfe52011-11-11 19:10:28 +00001754
1755 // Parse this module map file.
Douglas Gregor51f564f2011-12-31 04:05:44 +00001756 Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts);
1757 Diags->getClient()->BeginSourceFile(MMapLangOpts);
Douglas Gregor9a022bb2012-10-15 16:45:32 +00001758 ModuleMapParser Parser(L, *SourceMgr, Target, *Diags, *this, File->getDir(),
Douglas Gregor2f04f182012-02-02 18:42:48 +00001759 BuiltinIncludeDir);
Douglas Gregora30cfe52011-11-11 19:10:28 +00001760 bool Result = Parser.parseModuleMapFile();
1761 Diags->getClient()->EndSourceFile();
Douglas Gregor7005b902013-01-10 01:43:00 +00001762 ParsedModuleMap[File] = Result;
Douglas Gregora30cfe52011-11-11 19:10:28 +00001763 return Result;
1764}