blob: 9e4b68232a14f28d0e99385a3d3f59dd008e3fdb [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- HeaderSearch.cpp - Resolve Header File Locations ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the DirectoryLookup and HeaderSearch interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer5f016e22007-07-11 17:01:13 +000014#include "clang/Lex/HeaderSearch.h"
Douglas Gregora30cfe52011-11-11 19:10:28 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000016#include "clang/Basic/FileManager.h"
17#include "clang/Basic/IdentifierTable.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/Lex/HeaderMap.h"
19#include "clang/Lex/HeaderSearchOptions.h"
20#include "clang/Lex/Lexer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallString.h"
Ted Kremenekeabea452011-07-27 18:41:18 +000022#include "llvm/Support/Capacity.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "llvm/Support/FileSystem.h"
24#include "llvm/Support/Path.h"
Chris Lattner3daed522009-03-02 22:20:04 +000025#include <cstdio>
Douglas Gregor3cc62772013-01-22 23:49:45 +000026#if defined(LLVM_ON_UNIX)
Dmitri Gribenkoadeb7822013-01-26 16:29:36 +000027#include <limits.h>
Douglas Gregor3cc62772013-01-22 23:49:45 +000028#endif
Reid Spencer5f016e22007-07-11 17:01:13 +000029using namespace clang;
30
Douglas Gregor8c5a7602009-04-25 23:30:02 +000031const IdentifierInfo *
32HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) {
33 if (ControllingMacro)
34 return ControllingMacro;
35
36 if (!ControllingMacroID || !External)
37 return 0;
38
39 ControllingMacro = External->GetIdentifier(ControllingMacroID);
40 return ControllingMacro;
41}
42
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000043ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
44
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +000045HeaderSearch::HeaderSearch(IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts,
Douglas Gregorc042edd2012-10-24 16:19:39 +000046 FileManager &FM, DiagnosticsEngine &Diags,
Douglas Gregordc58aa72012-01-30 06:01:29 +000047 const LangOptions &LangOpts,
48 const TargetInfo *Target)
Douglas Gregorc042edd2012-10-24 16:19:39 +000049 : HSOpts(HSOpts), FileMgr(FM), FrameworkMap(64),
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +000050 ModMap(FileMgr, *Diags.getClient(), LangOpts, Target, *this)
Douglas Gregor8e238062011-11-11 00:35:06 +000051{
Nico Weber74a5fd82011-05-24 04:31:14 +000052 AngledDirIdx = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000053 SystemDirIdx = 0;
54 NoCurDirSearch = false;
Mike Stump1eb44332009-09-09 15:08:12 +000055
Douglas Gregor8c5a7602009-04-25 23:30:02 +000056 ExternalLookup = 0;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000057 ExternalSource = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000058 NumIncluded = 0;
59 NumMultiIncludeFileOptzn = 0;
60 NumFrameworkLookups = NumSubFrameworkLookups = 0;
61}
62
Chris Lattner822da612007-12-17 06:36:45 +000063HeaderSearch::~HeaderSearch() {
64 // Delete headermaps.
65 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
66 delete HeaderMaps[i].second;
67}
Mike Stump1eb44332009-09-09 15:08:12 +000068
Reid Spencer5f016e22007-07-11 17:01:13 +000069void HeaderSearch::PrintStats() {
70 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
71 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
72 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
73 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
74 NumOnceOnlyFiles += FileInfo[i].isImport;
75 if (MaxNumIncludes < FileInfo[i].NumIncludes)
76 MaxNumIncludes = FileInfo[i].NumIncludes;
77 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
78 }
79 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
80 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
81 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Mike Stump1eb44332009-09-09 15:08:12 +000082
Reid Spencer5f016e22007-07-11 17:01:13 +000083 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
84 fprintf(stderr, " %d #includes skipped due to"
85 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Mike Stump1eb44332009-09-09 15:08:12 +000086
Reid Spencer5f016e22007-07-11 17:01:13 +000087 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
88 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
89}
90
Chris Lattner822da612007-12-17 06:36:45 +000091/// CreateHeaderMap - This method returns a HeaderMap for the specified
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +000092/// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000093const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattner822da612007-12-17 06:36:45 +000094 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerdf772332007-12-17 07:52:39 +000095 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattner822da612007-12-17 06:36:45 +000096 if (!HeaderMaps.empty()) {
97 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerdf772332007-12-17 07:52:39 +000098 // Pointer equality comparison of FileEntries works because they are
99 // already uniqued by inode.
Mike Stump1eb44332009-09-09 15:08:12 +0000100 if (HeaderMaps[i].first == FE)
Chris Lattner822da612007-12-17 06:36:45 +0000101 return HeaderMaps[i].second;
102 }
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Chris Lattner39b49bc2010-11-23 08:35:12 +0000104 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattner822da612007-12-17 06:36:45 +0000105 HeaderMaps.push_back(std::make_pair(FE, HM));
106 return HM;
107 }
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattner822da612007-12-17 06:36:45 +0000109 return 0;
110}
111
Douglas Gregore434ec72012-01-29 17:08:11 +0000112std::string HeaderSearch::getModuleFileName(Module *Module) {
Douglas Gregor9a6da692011-09-12 20:41:59 +0000113 // If we don't have a module cache path, we can't do anything.
Douglas Gregore434ec72012-01-29 17:08:11 +0000114 if (ModuleCachePath.empty())
115 return std::string();
116
117
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000118 SmallString<256> Result(ModuleCachePath);
Douglas Gregore434ec72012-01-29 17:08:11 +0000119 llvm::sys::path::append(Result, Module->getTopLevelModule()->Name + ".pcm");
120 return Result.str().str();
121}
122
123std::string HeaderSearch::getModuleFileName(StringRef ModuleName) {
124 // If we don't have a module cache path, we can't do anything.
125 if (ModuleCachePath.empty())
126 return std::string();
Douglas Gregor6e975c42011-09-13 23:15:45 +0000127
Douglas Gregore434ec72012-01-29 17:08:11 +0000128
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000129 SmallString<256> Result(ModuleCachePath);
Douglas Gregore434ec72012-01-29 17:08:11 +0000130 llvm::sys::path::append(Result, ModuleName + ".pcm");
131 return Result.str().str();
132}
133
134Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) {
Douglas Gregorcf70d782011-11-12 00:05:07 +0000135 // Look in the module map to determine if there is a module by this name.
Douglas Gregore434ec72012-01-29 17:08:11 +0000136 Module *Module = ModMap.findModule(ModuleName);
137 if (Module || !AllowSearch)
138 return Module;
139
Douglas Gregor7005b902013-01-10 01:43:00 +0000140 // Look through the various header search paths to load any available module
Douglas Gregore434ec72012-01-29 17:08:11 +0000141 // maps, searching for a module map that describes this module.
142 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
143 if (SearchDirs[Idx].isFramework()) {
144 // Search for or infer a module map for a framework.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000145 SmallString<128> FrameworkDirName;
Douglas Gregore434ec72012-01-29 17:08:11 +0000146 FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
147 llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
148 if (const DirectoryEntry *FrameworkDir
149 = FileMgr.getDirectory(FrameworkDirName)) {
150 bool IsSystem
151 = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
152 Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem);
Douglas Gregorcf70d782011-11-12 00:05:07 +0000153 if (Module)
154 break;
155 }
Douglas Gregore434ec72012-01-29 17:08:11 +0000156 }
157
158 // FIXME: Figure out how header maps and module maps will work together.
159
160 // Only deal with normal search directories.
161 if (!SearchDirs[Idx].isNormalDir())
162 continue;
163
164 // Search for a module map file in this directory.
165 if (loadModuleMapFile(SearchDirs[Idx].getDir()) == LMM_NewlyLoaded) {
166 // We just loaded a module map file; check whether the module is
167 // available now.
168 Module = ModMap.findModule(ModuleName);
169 if (Module)
170 break;
171 }
172
173 // Search for a module map in a subdirectory with the same name as the
174 // module.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000175 SmallString<128> NestedModuleMapDirName;
Douglas Gregore434ec72012-01-29 17:08:11 +0000176 NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
177 llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
178 if (loadModuleMapFile(NestedModuleMapDirName) == LMM_NewlyLoaded) {
179 // If we just loaded a module map file, look for the module again.
180 Module = ModMap.findModule(ModuleName);
181 if (Module)
182 break;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000183 }
184 }
Douglas Gregore434ec72012-01-29 17:08:11 +0000185
186 return Module;
Douglas Gregor9a6da692011-09-12 20:41:59 +0000187}
188
Chris Lattnerdf772332007-12-17 07:52:39 +0000189//===----------------------------------------------------------------------===//
190// File lookup within a DirectoryLookup scope
191//===----------------------------------------------------------------------===//
192
Chris Lattner3af66a92007-12-17 17:57:27 +0000193/// getName - Return the directory or filename corresponding to this lookup
194/// object.
195const char *DirectoryLookup::getName() const {
196 if (isNormalDir())
197 return getDir()->getName();
198 if (isFramework())
199 return getFrameworkDir()->getName();
200 assert(isHeaderMap() && "Unknown DirectoryLookup");
201 return getHeaderMap()->getFileName();
202}
203
204
Chris Lattnerdf772332007-12-17 07:52:39 +0000205/// LookupFile - Lookup the specified file in this search path, returning it
206/// if it exists or returning null if not.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000207const FileEntry *DirectoryLookup::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000208 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000209 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000210 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000211 SmallVectorImpl<char> *RelativePath,
Daniel Dunbar85ff9692012-04-05 17:10:06 +0000212 Module **SuggestedModule,
213 bool &InUserSpecifiedSystemFramework) const {
214 InUserSpecifiedSystemFramework = false;
215
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000216 SmallString<1024> TmpDir;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000217 if (isNormalDir()) {
218 // Concatenate the requested file onto the directory.
Eli Friedmana6e023c2011-07-08 20:17:28 +0000219 TmpDir = getDir()->getName();
220 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek74124942011-04-26 21:50:03 +0000221 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000222 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000223 SearchPath->clear();
224 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
225 }
226 if (RelativePath != NULL) {
227 RelativePath->clear();
228 RelativePath->append(Filename.begin(), Filename.end());
229 }
Douglas Gregora30cfe52011-11-11 19:10:28 +0000230
231 // If we have a module map that might map this header, load it and
232 // check whether we'll have a suggestion for a module.
233 if (SuggestedModule && HS.hasModuleMap(TmpDir, getDir())) {
234 const FileEntry *File = HS.getFileMgr().getFile(TmpDir.str(),
235 /*openFile=*/false);
236 if (!File)
237 return File;
238
239 // If there is a module that corresponds to this header,
240 // suggest it.
Douglas Gregor5e3f9222011-12-08 17:01:29 +0000241 *SuggestedModule = HS.findModuleForHeader(File);
Douglas Gregora30cfe52011-11-11 19:10:28 +0000242 return File;
243 }
244
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000245 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000246 }
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Chris Lattnerafded5b2007-12-17 08:13:48 +0000248 if (isFramework())
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000249 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
Daniel Dunbar85ff9692012-04-05 17:10:06 +0000250 SuggestedModule, InUserSpecifiedSystemFramework);
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000252 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek74124942011-04-26 21:50:03 +0000253 const FileEntry * const Result = getHeaderMap()->LookupFile(
254 Filename, HS.getFileMgr());
255 if (Result) {
256 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000257 StringRef SearchPathRef(getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000258 SearchPath->clear();
259 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
260 }
261 if (RelativePath != NULL) {
262 RelativePath->clear();
263 RelativePath->append(Filename.begin(), Filename.end());
264 }
265 }
266 return Result;
Chris Lattnerdf772332007-12-17 07:52:39 +0000267}
268
Douglas Gregor7005b902013-01-10 01:43:00 +0000269/// \brief Given a framework directory, find the top-most framework directory.
270///
271/// \param FileMgr The file manager to use for directory lookups.
272/// \param DirName The name of the framework directory.
273/// \param SubmodulePath Will be populated with the submodule path from the
274/// returned top-level module to the originally named framework.
275static const DirectoryEntry *
276getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
277 SmallVectorImpl<std::string> &SubmodulePath) {
278 assert(llvm::sys::path::extension(DirName) == ".framework" &&
279 "Not a framework directory");
280
Douglas Gregor7005b902013-01-10 01:43:00 +0000281 // Note: as an egregious but useful hack we use the real path here, because
282 // frameworks moving between top-level frameworks to embedded frameworks tend
283 // to be symlinked, and we base the logical structure of modules on the
284 // physical layout. In particular, we need to deal with crazy includes like
285 //
286 // #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
287 //
288 // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
289 // which one should access with, e.g.,
290 //
291 // #include <Bar/Wibble.h>
292 //
293 // Similar issues occur when a top-level framework has moved into an
294 // embedded framework.
Douglas Gregor7005b902013-01-10 01:43:00 +0000295 const DirectoryEntry *TopFrameworkDir = FileMgr.getDirectory(DirName);
Douglas Gregor713b7c02013-01-26 00:55:12 +0000296 DirName = FileMgr.getCanonicalName(TopFrameworkDir);
Douglas Gregor7005b902013-01-10 01:43:00 +0000297 do {
298 // Get the parent directory name.
299 DirName = llvm::sys::path::parent_path(DirName);
300 if (DirName.empty())
301 break;
302
303 // Determine whether this directory exists.
304 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
305 if (!Dir)
306 break;
307
308 // If this is a framework directory, then we're a subframework of this
309 // framework.
310 if (llvm::sys::path::extension(DirName) == ".framework") {
311 SubmodulePath.push_back(llvm::sys::path::stem(DirName));
312 TopFrameworkDir = Dir;
313 }
314 } while (true);
315
316 return TopFrameworkDir;
317}
Chris Lattnerdf772332007-12-17 07:52:39 +0000318
Chris Lattnerafded5b2007-12-17 08:13:48 +0000319/// DoFrameworkLookup - Do a lookup of the specified file in the current
320/// DirectoryLookup, which is a framework directory.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000321const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000322 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000323 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000324 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000325 SmallVectorImpl<char> *RelativePath,
Daniel Dunbar85ff9692012-04-05 17:10:06 +0000326 Module **SuggestedModule,
327 bool &InUserSpecifiedSystemFramework) const
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000328{
Chris Lattnerafded5b2007-12-17 08:13:48 +0000329 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000332 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000333 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Chris Lattnerafded5b2007-12-17 08:13:48 +0000335 // Find out if this is the home for the specified framework, by checking
Daniel Dunbar9ee35f92012-04-05 17:09:40 +0000336 // HeaderSearch. Possible answers are yes/no and unknown.
337 HeaderSearch::FrameworkCacheEntry &CacheEntry =
Chris Lattnera1394812010-01-10 01:35:12 +0000338 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Chris Lattnerafded5b2007-12-17 08:13:48 +0000340 // If it is known and in some other directory, fail.
Daniel Dunbar9ee35f92012-04-05 17:09:40 +0000341 if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Chris Lattnerafded5b2007-12-17 08:13:48 +0000344 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Reid Spencer5f016e22007-07-11 17:01:13 +0000346 // FrameworkName = "/System/Library/Frameworks/"
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000347 SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000348 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 if (FrameworkName.empty() || FrameworkName.back() != '/')
350 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000353 StringRef ModuleName(Filename.begin(), SlashPos);
354 FrameworkName += ModuleName;
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
357 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Daniel Dunbar9ee35f92012-04-05 17:09:40 +0000359 // If the cache entry was unresolved, populate it now.
360 if (CacheEntry.Directory == 0) {
Chris Lattnerafded5b2007-12-17 08:13:48 +0000361 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 // If the framework dir doesn't exist, we fail.
Daniel Dunbar85ff9692012-04-05 17:10:06 +0000364 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
365 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 // Otherwise, if it does, remember that this is the right direntry for this
368 // framework.
Daniel Dunbar9ee35f92012-04-05 17:09:40 +0000369 CacheEntry.Directory = getFrameworkDir();
Daniel Dunbar85ff9692012-04-05 17:10:06 +0000370
371 // If this is a user search directory, check if the framework has been
372 // user-specified as a system framework.
373 if (getDirCharacteristic() == SrcMgr::C_User) {
374 SmallString<1024> SystemFrameworkMarker(FrameworkName);
375 SystemFrameworkMarker += ".system_framework";
376 if (llvm::sys::fs::exists(SystemFrameworkMarker.str())) {
377 CacheEntry.IsUserSpecifiedSystemFramework = true;
378 }
379 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Daniel Dunbar85ff9692012-04-05 17:10:06 +0000382 // Set the 'user-specified system framework' flag.
383 InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
384
Manuel Klimek74124942011-04-26 21:50:03 +0000385 if (RelativePath != NULL) {
386 RelativePath->clear();
387 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
388 }
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000389
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
391 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 FrameworkName += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000394
395 if (SearchPath != NULL) {
396 SearchPath->clear();
397 // Without trailing '/'.
398 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
399 }
400
Chris Lattnera1394812010-01-10 01:35:12 +0000401 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Douglas Gregor7005b902013-01-10 01:43:00 +0000402 const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
403 /*openFile=*/!SuggestedModule);
404 if (!FE) {
405 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
406 const char *Private = "Private";
407 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
408 Private+strlen(Private));
409 if (SearchPath != NULL)
410 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
411 Private+strlen(Private));
412
413 FE = FileMgr.getFile(FrameworkName.str(), /*openFile=*/!SuggestedModule);
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000414 }
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Douglas Gregor7005b902013-01-10 01:43:00 +0000416 // If we found the header and are allowed to suggest a module, do so now.
417 if (FE && SuggestedModule) {
418 // Find the framework in which this header occurs.
419 StringRef FrameworkPath = FE->getName();
420 bool FoundFramework = false;
421 do {
422 // Get the parent directory name.
423 FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
424 if (FrameworkPath.empty())
425 break;
Manuel Klimek74124942011-04-26 21:50:03 +0000426
Douglas Gregor7005b902013-01-10 01:43:00 +0000427 // Determine whether this directory exists.
428 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath);
429 if (!Dir)
430 break;
431
432 // If this is a framework directory, then we're a subframework of this
433 // framework.
434 if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
435 FoundFramework = true;
436 break;
437 }
438 } while (true);
439
440 if (FoundFramework) {
441 // Find the top-level framework based on this framework.
442 SmallVector<std::string, 4> SubmodulePath;
443 const DirectoryEntry *TopFrameworkDir
444 = ::getTopFrameworkDir(FileMgr, FrameworkPath, SubmodulePath);
445
446 // Determine the name of the top-level framework.
447 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
448
449 // Load this framework module. If that succeeds, find the suggested module
450 // for this header, if any.
451 bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
452 if (HS.loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem)) {
453 *SuggestedModule = HS.findModuleForHeader(FE);
454 }
455 } else {
456 *SuggestedModule = HS.findModuleForHeader(FE);
457 }
458 }
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000459 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000460}
461
Douglas Gregordc58aa72012-01-30 06:01:29 +0000462void HeaderSearch::setTarget(const TargetInfo &Target) {
463 ModMap.setTarget(Target);
464}
465
Chris Lattnerdf772332007-12-17 07:52:39 +0000466
Chris Lattnerafded5b2007-12-17 08:13:48 +0000467//===----------------------------------------------------------------------===//
468// Header File Location.
469//===----------------------------------------------------------------------===//
470
471
James Dennett853519c2012-06-20 00:56:32 +0000472/// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
Reid Spencer5f016e22007-07-11 17:01:13 +0000473/// return null on failure. isAngled indicates whether the file reference is
James Dennett853519c2012-06-20 00:56:32 +0000474/// for system \#include's or not (i.e. using <> instead of ""). CurFileEnt, if
475/// non-null, indicates where the \#including file is, in case a relative search
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000476/// is needed.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000477const FileEntry *HeaderSearch::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000478 StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000479 bool isAngled,
480 const DirectoryLookup *FromDir,
481 const DirectoryLookup *&CurDir,
482 const FileEntry *CurFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000483 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000484 SmallVectorImpl<char> *RelativePath,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000485 Module **SuggestedModule,
Douglas Gregor1c2e9332011-11-20 17:46:46 +0000486 bool SkipCache)
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000487{
488 if (SuggestedModule)
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000489 *SuggestedModule = 0;
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000490
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000492 if (llvm::sys::path::is_absolute(Filename)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 CurDir = 0;
494
495 // If this was an #include_next "/absolute/file", fail.
496 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Manuel Klimek74124942011-04-26 21:50:03 +0000498 if (SearchPath != NULL)
499 SearchPath->clear();
500 if (RelativePath != NULL) {
501 RelativePath->clear();
502 RelativePath->append(Filename.begin(), Filename.end());
503 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 // Otherwise, just return the file.
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000505 return FileMgr.getFile(Filename, /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 }
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000508 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000509 // directory. This has to be based on CurFileEnt, not CurDir, because
510 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000511 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
512 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000513 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000514 SmallString<1024> TmpDir;
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000515 // Concatenate the requested file onto the directory.
516 // FIXME: Portability. Filename concatenation should be in sys::Path.
517 TmpDir += CurFileEnt->getDir()->getName();
518 TmpDir.push_back('/');
519 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000520 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000522 // This file is a system header or C++ unfriendly if the old file is.
523 //
Douglas Gregor21efbb62012-08-13 15:47:39 +0000524 // Note that we only use one of FromHFI/ToHFI at once, due to potential
525 // reallocation of the underlying vector potentially making the first
526 // reference binding dangling.
527 HeaderFileInfo &FromHFI = getFileInfo(CurFileEnt);
528 unsigned DirInfo = FromHFI.DirInfo;
529 bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
530 StringRef Framework = FromHFI.Framework;
531
532 HeaderFileInfo &ToHFI = getFileInfo(FE);
533 ToHFI.DirInfo = DirInfo;
534 ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
535 ToHFI.Framework = Framework;
536
Manuel Klimek74124942011-04-26 21:50:03 +0000537 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000538 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000539 SearchPath->clear();
540 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
541 }
542 if (RelativePath != NULL) {
543 RelativePath->clear();
544 RelativePath->append(Filename.begin(), Filename.end());
545 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 return FE;
547 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 }
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 CurDir = 0;
551
552 // If this is a system #include, ignore the user #include locs.
Nico Weber74a5fd82011-05-24 04:31:14 +0000553 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 // If this is a #include_next request, start searching after the directory the
556 // file was found in.
557 if (FromDir)
558 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Chris Lattner9960ae82007-07-22 07:28:00 +0000560 // Cache all of the lookups performed by this method. Many headers are
561 // multiply included, and the "pragma once" optimization prevents them from
562 // being relex/pp'd, but they would still have to search through a
563 // (potentially huge) series of SearchDirs to find it.
564 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000565 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000566
567 // If the entry has been previously looked up, the first value will be
568 // non-zero. If the value is equal to i (the start point of our search), then
569 // this is a matching hit.
Douglas Gregor1c2e9332011-11-20 17:46:46 +0000570 if (!SkipCache && CacheLookup.first == i+1) {
Chris Lattner9960ae82007-07-22 07:28:00 +0000571 // Skip querying potentially lots of directories for this lookup.
572 i = CacheLookup.second;
573 } else {
574 // Otherwise, this is the first query, or the previous query didn't match
575 // our search start. We will fill in our found location below, so prime the
576 // start point value.
577 CacheLookup.first = i+1;
578 }
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 // Check each directory in sequence to see if it contains this file.
581 for (; i != SearchDirs.size(); ++i) {
Daniel Dunbar85ff9692012-04-05 17:10:06 +0000582 bool InUserSpecifiedSystemFramework = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000583 const FileEntry *FE =
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000584 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath,
Daniel Dunbar85ff9692012-04-05 17:10:06 +0000585 SuggestedModule, InUserSpecifiedSystemFramework);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000586 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Chris Lattnerafded5b2007-12-17 08:13:48 +0000588 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Chris Lattnerafded5b2007-12-17 08:13:48 +0000590 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000591 HeaderFileInfo &HFI = getFileInfo(FE);
592 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Daniel Dunbar85ff9692012-04-05 17:10:06 +0000594 // If the directory characteristic is User but this framework was
595 // user-specified to be treated as a system framework, promote the
596 // characteristic.
597 if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
598 HFI.DirInfo = SrcMgr::C_System;
599
Richard Smithf122a132012-06-13 20:27:03 +0000600 // If the filename matches a known system header prefix, override
601 // whether the file is a system header.
Richard Trieu4ef2f6a2012-06-13 20:52:36 +0000602 for (unsigned j = SystemHeaderPrefixes.size(); j; --j) {
603 if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
604 HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
Richard Smithf122a132012-06-13 20:27:03 +0000605 : SrcMgr::C_User;
606 break;
607 }
608 }
609
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000610 // If this file is found in a header map and uses the framework style of
611 // includes, then this header is part of a framework we're building.
612 if (CurDir->isIndexHeaderMap()) {
613 size_t SlashPos = Filename.find('/');
614 if (SlashPos != StringRef::npos) {
615 HFI.IndexHeaderMapHeader = 1;
616 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
617 SlashPos));
618 }
619 }
620
Chris Lattnerafded5b2007-12-17 08:13:48 +0000621 // Remember this location for the next lookup we do.
622 CacheLookup.second = i;
623 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 }
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000626 // If we are including a file with a quoted include "foo.h" from inside
627 // a header in a framework that is currently being built, and we couldn't
628 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
629 // "Foo" is the name of the framework in which the including header was found.
630 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) {
631 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt);
632 if (IncludingHFI.IndexHeaderMapHeader) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000633 SmallString<128> ScratchFilename;
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000634 ScratchFilename += IncludingHFI.Framework;
635 ScratchFilename += '/';
636 ScratchFilename += Filename;
637
638 const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true,
639 FromDir, CurDir, CurFileEnt,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000640 SearchPath, RelativePath,
641 SuggestedModule);
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000642 std::pair<unsigned, unsigned> &CacheLookup
643 = LookupFileCache.GetOrCreateValue(Filename).getValue();
644 CacheLookup.second
645 = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second;
646 return Result;
647 }
648 }
649
Chris Lattner9960ae82007-07-22 07:28:00 +0000650 // Otherwise, didn't find it. Remember we didn't find this.
651 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 return 0;
653}
654
655/// LookupSubframeworkHeader - Look up a subframework for the specified
James Dennett853519c2012-06-20 00:56:32 +0000656/// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from
Reid Spencer5f016e22007-07-11 17:01:13 +0000657/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
658/// is a subframework within Carbon.framework. If so, return the FileEntry
659/// for the designated file, otherwise return null.
660const FileEntry *HeaderSearch::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000661LookupSubframeworkHeader(StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000662 const FileEntry *ContextFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000663 SmallVectorImpl<char> *SearchPath,
Douglas Gregor1b58c742013-02-08 00:10:48 +0000664 SmallVectorImpl<char> *RelativePath,
665 Module **SuggestedModule) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000666 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 // Framework names must have a '/' in the filename. Find it.
Douglas Gregorefda0e82011-12-09 16:48:01 +0000669 // FIXME: Should we permit '\' on Windows?
Chris Lattnera1394812010-01-10 01:35:12 +0000670 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000671 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 // Look up the base framework name of the ContextFileEnt.
674 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 // If the context info wasn't a framework, couldn't be a subframework.
Douglas Gregorefda0e82011-12-09 16:48:01 +0000677 const unsigned DotFrameworkLen = 10;
678 const char *FrameworkPos = strstr(ContextName, ".framework");
679 if (FrameworkPos == 0 ||
680 (FrameworkPos[DotFrameworkLen] != '/' &&
681 FrameworkPos[DotFrameworkLen] != '\\'))
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Daniel Dunbar9ee35f92012-04-05 17:09:40 +0000684 SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000685
686 // Append Frameworks/HIToolbox.framework/
687 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000688 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 FrameworkName += ".framework/";
690
Daniel Dunbar9ee35f92012-04-05 17:09:40 +0000691 llvm::StringMapEntry<FrameworkCacheEntry> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000692 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 // Some other location?
Daniel Dunbar9ee35f92012-04-05 17:09:40 +0000695 if (CacheLookup.getValue().Directory &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 CacheLookup.getKeyLength() == FrameworkName.size() &&
697 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
698 CacheLookup.getKeyLength()) != 0)
699 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 // Cache subframework.
Daniel Dunbar9ee35f92012-04-05 17:09:40 +0000702 if (CacheLookup.getValue().Directory == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000706 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000707 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 // Otherwise, if it does, remember that this is the right direntry for this
710 // framework.
Daniel Dunbar9ee35f92012-04-05 17:09:40 +0000711 CacheLookup.getValue().Directory = Dir;
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 const FileEntry *FE = 0;
715
Manuel Klimek74124942011-04-26 21:50:03 +0000716 if (RelativePath != NULL) {
717 RelativePath->clear();
718 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
719 }
720
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000722 SmallString<1024> HeadersFilename(FrameworkName);
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000724 if (SearchPath != NULL) {
725 SearchPath->clear();
726 // Without trailing '/'.
727 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
728 }
729
Chris Lattnera1394812010-01-10 01:35:12 +0000730 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000731 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
734 HeadersFilename = FrameworkName;
735 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000736 if (SearchPath != NULL) {
737 SearchPath->clear();
738 // Without trailing '/'.
739 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
740 }
741
Chris Lattnera1394812010-01-10 01:35:12 +0000742 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000743 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 return 0;
745 }
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000748 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000749 // Note that the temporary 'DirInfo' is required here, as either call to
750 // getFileInfo could resize the vector and we don't want to rely on order
751 // of evaluation.
752 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
753 getFileInfo(FE).DirInfo = DirInfo;
Douglas Gregor1b58c742013-02-08 00:10:48 +0000754
755 // If we're supposed to suggest a module, look for one now.
756 if (SuggestedModule) {
757 // Find the top-level framework based on this framework.
758 FrameworkName.pop_back(); // remove the trailing '/'
759 SmallVector<std::string, 4> SubmodulePath;
760 const DirectoryEntry *TopFrameworkDir
761 = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
762
763 // Determine the name of the top-level framework.
764 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
765
766 // Load this framework module. If that succeeds, find the suggested module
767 // for this header, if any.
768 bool IsSystem = false;
769 if (loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem)) {
770 *SuggestedModule = findModuleForHeader(FE);
771 }
772 }
773
Reid Spencer5f016e22007-07-11 17:01:13 +0000774 return FE;
775}
776
Chandler Carruthcb381ea2011-12-09 01:33:57 +0000777/// \brief Helper static function to normalize a path for injection into
778/// a synthetic header.
779/*static*/ std::string
780HeaderSearch::NormalizeDashIncludePath(StringRef File, FileManager &FileMgr) {
781 // Implicit include paths should be resolved relative to the current
782 // working directory first, and then use the regular header search
783 // mechanism. The proper way to handle this is to have the
784 // predefines buffer located at the current working directory, but
785 // it has no file entry. For now, workaround this by using an
786 // absolute path if we find the file here, and otherwise letting
787 // header search handle it.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000788 SmallString<128> Path(File);
Chandler Carruthcb381ea2011-12-09 01:33:57 +0000789 llvm::sys::fs::make_absolute(Path);
790 bool exists;
791 if (llvm::sys::fs::exists(Path.str(), exists) || !exists)
792 Path = File;
793 else if (exists)
794 FileMgr.getFile(File);
795
796 return Lexer::Stringify(Path.str());
797}
798
Reid Spencer5f016e22007-07-11 17:01:13 +0000799//===----------------------------------------------------------------------===//
800// File Info Management.
801//===----------------------------------------------------------------------===//
802
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000803/// \brief Merge the header file info provided by \p OtherHFI into the current
804/// header file info (\p HFI)
805static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
806 const HeaderFileInfo &OtherHFI) {
807 HFI.isImport |= OtherHFI.isImport;
808 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +0000809 HFI.isModuleHeader |= OtherHFI.isModuleHeader;
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000810 HFI.NumIncludes += OtherHFI.NumIncludes;
811
812 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
813 HFI.ControllingMacro = OtherHFI.ControllingMacro;
814 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
815 }
816
817 if (OtherHFI.External) {
818 HFI.DirInfo = OtherHFI.DirInfo;
819 HFI.External = OtherHFI.External;
820 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
821 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000822
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000823 if (HFI.Framework.empty())
824 HFI.Framework = OtherHFI.Framework;
825
826 HFI.Resolved = true;
827}
828
Steve Naroff83d63c72009-04-24 20:03:17 +0000829/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000830/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000831HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 if (FE->getUID() >= FileInfo.size())
833 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000834
835 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000836 if (ExternalSource && !HFI.Resolved)
837 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000838 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000839}
Reid Spencer5f016e22007-07-11 17:01:13 +0000840
Douglas Gregordd3e5542011-05-04 00:14:37 +0000841bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
842 // Check if we've ever seen this file as a header.
843 if (File->getUID() >= FileInfo.size())
844 return false;
845
846 // Resolve header file info from the external source, if needed.
847 HeaderFileInfo &HFI = FileInfo[File->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000848 if (ExternalSource && !HFI.Resolved)
849 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File));
Douglas Gregordd3e5542011-05-04 00:14:37 +0000850
Argyrios Kyrtzidis44dfff62012-12-10 20:08:37 +0000851 return HFI.isPragmaOnce || HFI.isImport ||
852 HFI.ControllingMacro || HFI.ControllingMacroID;
Douglas Gregordd3e5542011-05-04 00:14:37 +0000853}
854
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +0000855void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE) {
856 if (FE->getUID() >= FileInfo.size())
857 FileInfo.resize(FE->getUID()+1);
858
859 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
860 HFI.isModuleHeader = true;
861}
862
Steve Naroff83d63c72009-04-24 20:03:17 +0000863void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
864 if (UID >= FileInfo.size())
865 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000866 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000867 FileInfo[UID] = HFI;
868}
869
Reid Spencer5f016e22007-07-11 17:01:13 +0000870bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
871 ++NumIncluded; // Count # of attempted #includes.
872
873 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000874 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 // If this is a #import directive, check that we have not already imported
877 // this header.
878 if (isImport) {
879 // If this has already been imported, don't import it again.
880 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 // Has this already been #import'ed or #include'd?
883 if (FileInfo.NumIncludes) return false;
884 } else {
885 // Otherwise, if this is a #include of a file that was previously #import'd
886 // or if this is the second #include of a #pragma once file, ignore it.
887 if (FileInfo.isImport)
888 return false;
889 }
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
892 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000893 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000894 = FileInfo.getControllingMacro(ExternalLookup))
895 if (ControllingMacro->hasMacroDefinition()) {
896 ++NumMultiIncludeFileOptzn;
897 return false;
898 }
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 // Increment the number of times this file has been included.
901 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 return true;
904}
905
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000906size_t HeaderSearch::getTotalMemory() const {
907 return SearchDirs.capacity()
Ted Kremenekeabea452011-07-27 18:41:18 +0000908 + llvm::capacity_in_bytes(FileInfo)
909 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000910 + LookupFileCache.getAllocator().getTotalMemory()
911 + FrameworkMap.getAllocator().getTotalMemory();
912}
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000913
914StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
915 return FrameworkNames.GetOrCreateValue(Framework).getKey();
916}
Douglas Gregora30cfe52011-11-11 19:10:28 +0000917
918bool HeaderSearch::hasModuleMap(StringRef FileName,
919 const DirectoryEntry *Root) {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000920 SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000921
922 StringRef DirName = FileName;
923 do {
924 // Get the parent directory name.
925 DirName = llvm::sys::path::parent_path(DirName);
926 if (DirName.empty())
927 return false;
928
929 // Determine whether this directory exists.
930 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
931 if (!Dir)
932 return false;
933
Douglas Gregorcf70d782011-11-12 00:05:07 +0000934 // Try to load the module map file in this directory.
Douglas Gregor26697972011-11-12 00:22:19 +0000935 switch (loadModuleMapFile(Dir)) {
936 case LMM_NewlyLoaded:
937 case LMM_AlreadyLoaded:
Douglas Gregorcf70d782011-11-12 00:05:07 +0000938 // Success. All of the directories we stepped through inherit this module
939 // map file.
Douglas Gregora30cfe52011-11-11 19:10:28 +0000940 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
941 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
942
943 return true;
Douglas Gregor26697972011-11-12 00:22:19 +0000944
945 case LMM_NoDirectory:
946 case LMM_InvalidModuleMap:
947 break;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000948 }
Douglas Gregora30cfe52011-11-11 19:10:28 +0000949
Douglas Gregorcf70d782011-11-12 00:05:07 +0000950 // If we hit the top of our search, we're done.
951 if (Dir == Root)
952 return false;
953
Douglas Gregora30cfe52011-11-11 19:10:28 +0000954 // Keep track of all of the directories we checked, so we can mark them as
955 // having module maps if we eventually do find a module map.
956 FixUpDirectories.push_back(Dir);
957 } while (true);
Douglas Gregora30cfe52011-11-11 19:10:28 +0000958}
959
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +0000960Module *HeaderSearch::findModuleForHeader(const FileEntry *File) const {
961 if (ExternalSource) {
962 // Make sure the external source has handled header info about this file,
963 // which includes whether the file is part of a module.
964 (void)getFileInfo(File);
965 }
Douglas Gregor51f564f2011-12-31 04:05:44 +0000966 if (Module *Mod = ModMap.findModuleForHeader(File))
967 return Mod;
Douglas Gregor65f3b5e2011-11-11 22:18:48 +0000968
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000969 return 0;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000970}
971
Douglas Gregordb1cde72011-11-16 00:09:06 +0000972bool HeaderSearch::loadModuleMapFile(const FileEntry *File) {
973 const DirectoryEntry *Dir = File->getDir();
974
975 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
976 = DirectoryHasModuleMap.find(Dir);
977 if (KnownDir != DirectoryHasModuleMap.end())
978 return !KnownDir->second;
979
980 bool Result = ModMap.parseModuleMapFile(File);
Douglas Gregor4813442c2011-12-07 21:25:07 +0000981 if (!Result && llvm::sys::path::filename(File->getName()) == "module.map") {
982 // If the file we loaded was a module.map, look for the corresponding
983 // module_private.map.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000984 SmallString<128> PrivateFilename(Dir->getName());
Douglas Gregor4813442c2011-12-07 21:25:07 +0000985 llvm::sys::path::append(PrivateFilename, "module_private.map");
986 if (const FileEntry *PrivateFile = FileMgr.getFile(PrivateFilename))
987 Result = ModMap.parseModuleMapFile(PrivateFile);
988 }
989
990 DirectoryHasModuleMap[Dir] = !Result;
Douglas Gregordb1cde72011-11-16 00:09:06 +0000991 return Result;
992}
993
Douglas Gregore434ec72012-01-29 17:08:11 +0000994Module *HeaderSearch::loadFrameworkModule(StringRef Name,
995 const DirectoryEntry *Dir,
996 bool IsSystem) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000997 if (Module *Module = ModMap.findModule(Name))
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000998 return Module;
999
1000 // Try to load a module map file.
1001 switch (loadModuleMapFile(Dir)) {
1002 case LMM_InvalidModuleMap:
1003 break;
1004
1005 case LMM_AlreadyLoaded:
1006 case LMM_NoDirectory:
1007 return 0;
1008
1009 case LMM_NewlyLoaded:
1010 return ModMap.findModule(Name);
1011 }
Douglas Gregora8c6fea2012-01-13 22:31:52 +00001012
Douglas Gregor7005b902013-01-10 01:43:00 +00001013 // Figure out the top-level framework directory and the submodule path from
1014 // that top-level framework to the requested framework.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001015 SmallVector<std::string, 2> SubmodulePath;
Douglas Gregora8c6fea2012-01-13 22:31:52 +00001016 SubmodulePath.push_back(Name);
Douglas Gregor7005b902013-01-10 01:43:00 +00001017 const DirectoryEntry *TopFrameworkDir
1018 = ::getTopFrameworkDir(FileMgr, Dir->getName(), SubmodulePath);
Douglas Gregor82e52372012-11-06 19:39:40 +00001019
Douglas Gregor82e52372012-11-06 19:39:40 +00001020
Douglas Gregora8c6fea2012-01-13 22:31:52 +00001021 // Try to infer a module map from the top-level framework directory.
1022 Module *Result = ModMap.inferFrameworkModule(SubmodulePath.back(),
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001023 TopFrameworkDir,
1024 IsSystem,
Douglas Gregora8c6fea2012-01-13 22:31:52 +00001025 /*Parent=*/0);
Douglas Gregor7005b902013-01-10 01:43:00 +00001026 if (!Result)
1027 return 0;
Douglas Gregora8c6fea2012-01-13 22:31:52 +00001028
1029 // Follow the submodule path to find the requested (sub)framework module
1030 // within the top-level framework module.
1031 SubmodulePath.pop_back();
1032 while (!SubmodulePath.empty() && Result) {
1033 Result = ModMap.lookupModuleQualified(SubmodulePath.back(), Result);
1034 SubmodulePath.pop_back();
1035 }
1036 return Result;
Douglas Gregor2821c7f2011-11-17 01:41:17 +00001037}
1038
Douglas Gregordb1cde72011-11-16 00:09:06 +00001039
Douglas Gregor26697972011-11-12 00:22:19 +00001040HeaderSearch::LoadModuleMapResult
1041HeaderSearch::loadModuleMapFile(StringRef DirName) {
Douglas Gregorcf70d782011-11-12 00:05:07 +00001042 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
1043 return loadModuleMapFile(Dir);
1044
Douglas Gregor26697972011-11-12 00:22:19 +00001045 return LMM_NoDirectory;
Douglas Gregorcf70d782011-11-12 00:05:07 +00001046}
1047
Douglas Gregor26697972011-11-12 00:22:19 +00001048HeaderSearch::LoadModuleMapResult
1049HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) {
Douglas Gregorcf70d782011-11-12 00:05:07 +00001050 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
1051 = DirectoryHasModuleMap.find(Dir);
1052 if (KnownDir != DirectoryHasModuleMap.end())
Douglas Gregor26697972011-11-12 00:22:19 +00001053 return KnownDir->second? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +00001054
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001055 SmallString<128> ModuleMapFileName;
Douglas Gregorcf70d782011-11-12 00:05:07 +00001056 ModuleMapFileName += Dir->getName();
Douglas Gregor587986e2011-12-07 02:23:45 +00001057 unsigned ModuleMapDirNameLen = ModuleMapFileName.size();
Douglas Gregorcf70d782011-11-12 00:05:07 +00001058 llvm::sys::path::append(ModuleMapFileName, "module.map");
1059 if (const FileEntry *ModuleMapFile = FileMgr.getFile(ModuleMapFileName)) {
1060 // We have found a module map file. Try to parse it.
Douglas Gregor587986e2011-12-07 02:23:45 +00001061 if (ModMap.parseModuleMapFile(ModuleMapFile)) {
1062 // No suitable module map.
1063 DirectoryHasModuleMap[Dir] = false;
1064 return LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +00001065 }
Douglas Gregor587986e2011-12-07 02:23:45 +00001066
1067 // This directory has a module map.
1068 DirectoryHasModuleMap[Dir] = true;
1069
1070 // Check whether there is a private module map that we need to load as well.
1071 ModuleMapFileName.erase(ModuleMapFileName.begin() + ModuleMapDirNameLen,
1072 ModuleMapFileName.end());
1073 llvm::sys::path::append(ModuleMapFileName, "module_private.map");
1074 if (const FileEntry *PrivateModuleMapFile
1075 = FileMgr.getFile(ModuleMapFileName)) {
1076 if (ModMap.parseModuleMapFile(PrivateModuleMapFile)) {
1077 // No suitable module map.
1078 DirectoryHasModuleMap[Dir] = false;
1079 return LMM_InvalidModuleMap;
1080 }
1081 }
1082
1083 return LMM_NewlyLoaded;
Douglas Gregorcf70d782011-11-12 00:05:07 +00001084 }
1085
1086 // No suitable module map.
1087 DirectoryHasModuleMap[Dir] = false;
Douglas Gregor26697972011-11-12 00:22:19 +00001088 return LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +00001089}
Douglas Gregora30cfe52011-11-11 19:10:28 +00001090
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001091void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
Douglas Gregorc5b2e582012-01-29 18:15:03 +00001092 Modules.clear();
1093
1094 // Load module maps for each of the header search directories.
1095 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
1096 if (SearchDirs[Idx].isFramework()) {
1097 llvm::error_code EC;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001098 SmallString<128> DirNative;
Douglas Gregorc5b2e582012-01-29 18:15:03 +00001099 llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
1100 DirNative);
1101
1102 // Search each of the ".framework" directories to load them as modules.
1103 bool IsSystem = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
1104 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
1105 Dir != DirEnd && !EC; Dir.increment(EC)) {
1106 if (llvm::sys::path::extension(Dir->path()) != ".framework")
1107 continue;
1108
1109 const DirectoryEntry *FrameworkDir = FileMgr.getDirectory(Dir->path());
1110 if (!FrameworkDir)
1111 continue;
1112
1113 // Load this framework module.
1114 loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir,
1115 IsSystem);
1116 }
1117 continue;
1118 }
1119
1120 // FIXME: Deal with header maps.
1121 if (SearchDirs[Idx].isHeaderMap())
1122 continue;
1123
1124 // Try to load a module map file for the search directory.
1125 loadModuleMapFile(SearchDirs[Idx].getDir());
1126
1127 // Try to load module map files for immediate subdirectories of this search
1128 // directory.
1129 llvm::error_code EC;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001130 SmallString<128> DirNative;
Douglas Gregorc5b2e582012-01-29 18:15:03 +00001131 llvm::sys::path::native(SearchDirs[Idx].getDir()->getName(), DirNative);
1132 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
1133 Dir != DirEnd && !EC; Dir.increment(EC)) {
1134 loadModuleMapFile(Dir->path());
1135 }
1136 }
1137
1138 // Populate the list of modules.
1139 for (ModuleMap::module_iterator M = ModMap.module_begin(),
1140 MEnd = ModMap.module_end();
1141 M != MEnd; ++M) {
1142 Modules.push_back(M->getValue());
1143 }
1144}