1 //===--- HeaderSearch.cpp - Resolve Header File Locations ---===// 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 implements the DirectoryLookup and HeaderSearch interfaces. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Lex/HeaderSearch.h" 15 #include "clang/Lex/HeaderMap.h" 16 #include "clang/Basic/FileManager.h" 17 #include "clang/Basic/IdentifierTable.h" 18 #include "llvm/Support/FileSystem.h" 19 #include "llvm/Support/Path.h" 20 #include "llvm/ADT/SmallString.h" 21 #include <cstdio> 22 using namespace clang; 23 24 const IdentifierInfo * 25 HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) { 26 if (ControllingMacro) 27 return ControllingMacro; 28 29 if (!ControllingMacroID || !External) 30 return 0; 31 32 ControllingMacro = External->GetIdentifier(ControllingMacroID); 33 return ControllingMacro; 34 } 35 36 ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {} 37 38 HeaderSearch::HeaderSearch(FileManager &FM) 39 : FileMgr(FM), FrameworkMap(64) { 40 SystemDirIdx = 0; 41 NoCurDirSearch = false; 42 43 ExternalLookup = 0; 44 ExternalSource = 0; 45 NumIncluded = 0; 46 NumMultiIncludeFileOptzn = 0; 47 NumFrameworkLookups = NumSubFrameworkLookups = 0; 48 } 49 50 HeaderSearch::~HeaderSearch() { 51 // Delete headermaps. 52 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) 53 delete HeaderMaps[i].second; 54 } 55 56 void HeaderSearch::PrintStats() { 57 fprintf(stderr, "\n*** HeaderSearch Stats:\n"); 58 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size()); 59 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0; 60 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) { 61 NumOnceOnlyFiles += FileInfo[i].isImport; 62 if (MaxNumIncludes < FileInfo[i].NumIncludes) 63 MaxNumIncludes = FileInfo[i].NumIncludes; 64 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1; 65 } 66 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles); 67 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles); 68 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes); 69 70 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded); 71 fprintf(stderr, " %d #includes skipped due to" 72 " the multi-include optimization.\n", NumMultiIncludeFileOptzn); 73 74 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups); 75 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups); 76 } 77 78 /// CreateHeaderMap - This method returns a HeaderMap for the specified 79 /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure. 80 const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) { 81 // We expect the number of headermaps to be small, and almost always empty. 82 // If it ever grows, use of a linear search should be re-evaluated. 83 if (!HeaderMaps.empty()) { 84 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) 85 // Pointer equality comparison of FileEntries works because they are 86 // already uniqued by inode. 87 if (HeaderMaps[i].first == FE) 88 return HeaderMaps[i].second; 89 } 90 91 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) { 92 HeaderMaps.push_back(std::make_pair(FE, HM)); 93 return HM; 94 } 95 96 return 0; 97 } 98 99 //===----------------------------------------------------------------------===// 100 // File lookup within a DirectoryLookup scope 101 //===----------------------------------------------------------------------===// 102 103 /// getName - Return the directory or filename corresponding to this lookup 104 /// object. 105 const char *DirectoryLookup::getName() const { 106 if (isNormalDir()) 107 return getDir()->getName(); 108 if (isFramework()) 109 return getFrameworkDir()->getName(); 110 assert(isHeaderMap() && "Unknown DirectoryLookup"); 111 return getHeaderMap()->getFileName(); 112 } 113 114 115 /// LookupFile - Lookup the specified file in this search path, returning it 116 /// if it exists or returning null if not. 117 const FileEntry *DirectoryLookup::LookupFile( 118 llvm::StringRef Filename, 119 HeaderSearch &HS, llvm::SmallVectorImpl<char> *RawPath) const { 120 llvm::SmallString<1024> TmpDir; 121 if (isNormalDir()) { 122 // Concatenate the requested file onto the directory. 123 // FIXME: Portability. Filename concatenation should be in sys::Path. 124 TmpDir += getDir()->getName(); 125 TmpDir.push_back('/'); 126 TmpDir.append(Filename.begin(), Filename.end()); 127 if (RawPath != NULL) 128 *RawPath = TmpDir; 129 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true); 130 } 131 132 if (isFramework()) 133 return DoFrameworkLookup(Filename, HS, RawPath); 134 135 assert(isHeaderMap() && "Unknown directory lookup"); 136 return getHeaderMap()->LookupFile(Filename, HS.getFileMgr(), RawPath); 137 } 138 139 140 /// DoFrameworkLookup - Do a lookup of the specified file in the current 141 /// DirectoryLookup, which is a framework directory. 142 const FileEntry *DirectoryLookup::DoFrameworkLookup( 143 llvm::StringRef Filename, 144 HeaderSearch &HS, llvm::SmallVectorImpl<char> *RawPath) const { 145 FileManager &FileMgr = HS.getFileMgr(); 146 147 // Framework names must have a '/' in the filename. 148 size_t SlashPos = Filename.find('/'); 149 if (SlashPos == llvm::StringRef::npos) return 0; 150 151 // Find out if this is the home for the specified framework, by checking 152 // HeaderSearch. Possible answer are yes/no and unknown. 153 const DirectoryEntry *&FrameworkDirCache = 154 HS.LookupFrameworkCache(Filename.substr(0, SlashPos)); 155 156 // If it is known and in some other directory, fail. 157 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir()) 158 return 0; 159 160 // Otherwise, construct the path to this framework dir. 161 162 // FrameworkName = "/System/Library/Frameworks/" 163 llvm::SmallString<1024> FrameworkName; 164 FrameworkName += getFrameworkDir()->getName(); 165 if (FrameworkName.empty() || FrameworkName.back() != '/') 166 FrameworkName.push_back('/'); 167 168 // FrameworkName = "/System/Library/Frameworks/Cocoa" 169 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos); 170 171 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/" 172 FrameworkName += ".framework/"; 173 174 // If the cache entry is still unresolved, query to see if the cache entry is 175 // still unresolved. If so, check its existence now. 176 if (FrameworkDirCache == 0) { 177 HS.IncrementFrameworkLookupCount(); 178 179 // If the framework dir doesn't exist, we fail. 180 // FIXME: It's probably more efficient to query this with FileMgr.getDir. 181 bool Exists; 182 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists) 183 return 0; 184 185 // Otherwise, if it does, remember that this is the right direntry for this 186 // framework. 187 FrameworkDirCache = getFrameworkDir(); 188 } 189 190 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h" 191 unsigned OrigSize = FrameworkName.size(); 192 193 FrameworkName += "Headers/"; 194 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end()); 195 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(), 196 /*openFile=*/true)) { 197 if (RawPath != NULL) 198 *RawPath = FrameworkName; 199 return FE; 200 } 201 202 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h" 203 const char *Private = "Private"; 204 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private, 205 Private+strlen(Private)); 206 if (RawPath != NULL) 207 *RawPath = FrameworkName; 208 return FileMgr.getFile(FrameworkName.str(), /*openFile=*/true); 209 } 210 211 212 //===----------------------------------------------------------------------===// 213 // Header File Location. 214 //===----------------------------------------------------------------------===// 215 216 217 /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, 218 /// return null on failure. isAngled indicates whether the file reference is 219 /// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if 220 /// non-null, indicates where the #including file is, in case a relative search 221 /// is needed. 222 const FileEntry *HeaderSearch::LookupFile( 223 llvm::StringRef Filename, 224 bool isAngled, 225 const DirectoryLookup *FromDir, 226 const DirectoryLookup *&CurDir, 227 const FileEntry *CurFileEnt, 228 llvm::SmallVectorImpl<char> *RawPath) { 229 // If 'Filename' is absolute, check to see if it exists and no searching. 230 if (llvm::sys::path::is_absolute(Filename)) { 231 CurDir = 0; 232 233 // If this was an #include_next "/absolute/file", fail. 234 if (FromDir) return 0; 235 236 if (RawPath != NULL) 237 llvm::Twine(Filename).toVector(*RawPath); 238 // Otherwise, just return the file. 239 return FileMgr.getFile(Filename, /*openFile=*/true); 240 } 241 242 // Step #0, unless disabled, check to see if the file is in the #includer's 243 // directory. This has to be based on CurFileEnt, not CurDir, because 244 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and 245 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h". 246 // This search is not done for <> headers. 247 if (CurFileEnt && !isAngled && !NoCurDirSearch) { 248 llvm::SmallString<1024> TmpDir; 249 // Concatenate the requested file onto the directory. 250 // FIXME: Portability. Filename concatenation should be in sys::Path. 251 TmpDir += CurFileEnt->getDir()->getName(); 252 TmpDir.push_back('/'); 253 TmpDir.append(Filename.begin(), Filename.end()); 254 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) { 255 // Leave CurDir unset. 256 // This file is a system header or C++ unfriendly if the old file is. 257 // 258 // Note that the temporary 'DirInfo' is required here, as either call to 259 // getFileInfo could resize the vector and we don't want to rely on order 260 // of evaluation. 261 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo; 262 getFileInfo(FE).DirInfo = DirInfo; 263 if (RawPath != NULL) 264 *RawPath = TmpDir; 265 return FE; 266 } 267 } 268 269 CurDir = 0; 270 271 // If this is a system #include, ignore the user #include locs. 272 unsigned i = isAngled ? SystemDirIdx : 0; 273 274 // If this is a #include_next request, start searching after the directory the 275 // file was found in. 276 if (FromDir) 277 i = FromDir-&SearchDirs[0]; 278 279 // Cache all of the lookups performed by this method. Many headers are 280 // multiply included, and the "pragma once" optimization prevents them from 281 // being relex/pp'd, but they would still have to search through a 282 // (potentially huge) series of SearchDirs to find it. 283 std::pair<unsigned, unsigned> &CacheLookup = 284 LookupFileCache.GetOrCreateValue(Filename).getValue(); 285 286 // If the entry has been previously looked up, the first value will be 287 // non-zero. If the value is equal to i (the start point of our search), then 288 // this is a matching hit. 289 if (CacheLookup.first == i+1) { 290 // Skip querying potentially lots of directories for this lookup. 291 i = CacheLookup.second; 292 } else { 293 // Otherwise, this is the first query, or the previous query didn't match 294 // our search start. We will fill in our found location below, so prime the 295 // start point value. 296 CacheLookup.first = i+1; 297 } 298 299 // Check each directory in sequence to see if it contains this file. 300 for (; i != SearchDirs.size(); ++i) { 301 const FileEntry *FE = 302 SearchDirs[i].LookupFile(Filename, *this, RawPath); 303 if (!FE) continue; 304 305 CurDir = &SearchDirs[i]; 306 307 // This file is a system header or C++ unfriendly if the dir is. 308 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic(); 309 310 // Remember this location for the next lookup we do. 311 CacheLookup.second = i; 312 return FE; 313 } 314 315 // Otherwise, didn't find it. Remember we didn't find this. 316 CacheLookup.second = SearchDirs.size(); 317 return 0; 318 } 319 320 /// LookupSubframeworkHeader - Look up a subframework for the specified 321 /// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from 322 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox 323 /// is a subframework within Carbon.framework. If so, return the FileEntry 324 /// for the designated file, otherwise return null. 325 const FileEntry *HeaderSearch:: 326 LookupSubframeworkHeader(llvm::StringRef Filename, 327 const FileEntry *ContextFileEnt, 328 llvm::SmallVectorImpl<char> *RawPath) { 329 assert(ContextFileEnt && "No context file?"); 330 331 // Framework names must have a '/' in the filename. Find it. 332 size_t SlashPos = Filename.find('/'); 333 if (SlashPos == llvm::StringRef::npos) return 0; 334 335 // Look up the base framework name of the ContextFileEnt. 336 const char *ContextName = ContextFileEnt->getName(); 337 338 // If the context info wasn't a framework, couldn't be a subframework. 339 const char *FrameworkPos = strstr(ContextName, ".framework/"); 340 if (FrameworkPos == 0) 341 return 0; 342 343 llvm::SmallString<1024> FrameworkName(ContextName, 344 FrameworkPos+strlen(".framework/")); 345 346 // Append Frameworks/HIToolbox.framework/ 347 FrameworkName += "Frameworks/"; 348 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos); 349 FrameworkName += ".framework/"; 350 351 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup = 352 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos)); 353 354 // Some other location? 355 if (CacheLookup.getValue() && 356 CacheLookup.getKeyLength() == FrameworkName.size() && 357 memcmp(CacheLookup.getKeyData(), &FrameworkName[0], 358 CacheLookup.getKeyLength()) != 0) 359 return 0; 360 361 // Cache subframework. 362 if (CacheLookup.getValue() == 0) { 363 ++NumSubFrameworkLookups; 364 365 // If the framework dir doesn't exist, we fail. 366 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str()); 367 if (Dir == 0) return 0; 368 369 // Otherwise, if it does, remember that this is the right direntry for this 370 // framework. 371 CacheLookup.setValue(Dir); 372 } 373 374 const FileEntry *FE = 0; 375 376 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h" 377 llvm::SmallString<1024> HeadersFilename(FrameworkName); 378 HeadersFilename += "Headers/"; 379 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); 380 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) { 381 382 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h" 383 HeadersFilename = FrameworkName; 384 HeadersFilename += "PrivateHeaders/"; 385 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); 386 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) 387 return 0; 388 } 389 if (RawPath != NULL) 390 *RawPath = HeadersFilename; 391 392 // This file is a system header or C++ unfriendly if the old file is. 393 // 394 // Note that the temporary 'DirInfo' is required here, as either call to 395 // getFileInfo could resize the vector and we don't want to rely on order 396 // of evaluation. 397 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo; 398 getFileInfo(FE).DirInfo = DirInfo; 399 return FE; 400 } 401 402 //===----------------------------------------------------------------------===// 403 // File Info Management. 404 //===----------------------------------------------------------------------===// 405 406 407 /// getFileInfo - Return the HeaderFileInfo structure for the specified 408 /// FileEntry. 409 HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) { 410 if (FE->getUID() >= FileInfo.size()) 411 FileInfo.resize(FE->getUID()+1); 412 413 HeaderFileInfo &HFI = FileInfo[FE->getUID()]; 414 if (ExternalSource && !HFI.Resolved) { 415 HFI = ExternalSource->GetHeaderFileInfo(FE); 416 HFI.Resolved = true; 417 } 418 return HFI; 419 } 420 421 void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) { 422 if (UID >= FileInfo.size()) 423 FileInfo.resize(UID+1); 424 HFI.Resolved = true; 425 FileInfo[UID] = HFI; 426 } 427 428 /// ShouldEnterIncludeFile - Mark the specified file as a target of of a 429 /// #include, #include_next, or #import directive. Return false if #including 430 /// the file will have no effect or true if we should include it. 431 bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){ 432 ++NumIncluded; // Count # of attempted #includes. 433 434 // Get information about this file. 435 HeaderFileInfo &FileInfo = getFileInfo(File); 436 437 // If this is a #import directive, check that we have not already imported 438 // this header. 439 if (isImport) { 440 // If this has already been imported, don't import it again. 441 FileInfo.isImport = true; 442 443 // Has this already been #import'ed or #include'd? 444 if (FileInfo.NumIncludes) return false; 445 } else { 446 // Otherwise, if this is a #include of a file that was previously #import'd 447 // or if this is the second #include of a #pragma once file, ignore it. 448 if (FileInfo.isImport) 449 return false; 450 } 451 452 // Next, check to see if the file is wrapped with #ifndef guards. If so, and 453 // if the macro that guards it is defined, we know the #include has no effect. 454 if (const IdentifierInfo *ControllingMacro 455 = FileInfo.getControllingMacro(ExternalLookup)) 456 if (ControllingMacro->hasMacroDefinition()) { 457 ++NumMultiIncludeFileOptzn; 458 return false; 459 } 460 461 // Increment the number of times this file has been included. 462 ++FileInfo.NumIncludes; 463 464 return true; 465 } 466 467 468