diff options
Diffstat (limited to 'clang/lib/Lex')
-rw-r--r-- | clang/lib/Lex/HeaderSearch.cpp | 51 | ||||
-rw-r--r-- | clang/lib/Lex/ModuleMap.cpp | 19 | ||||
-rw-r--r-- | clang/lib/Lex/PPDirectives.cpp | 56 |
3 files changed, 114 insertions, 12 deletions
diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp index f663caeff81..4e88248607b 100644 --- a/clang/lib/Lex/HeaderSearch.cpp +++ b/clang/lib/Lex/HeaderSearch.cpp @@ -1419,3 +1419,54 @@ void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) { SearchDir.setSearchedAllModuleMaps(true); } + +std::string HeaderSearch::suggestPathToFileForDiagnostics(const FileEntry *File, + bool *IsSystem) { + // FIXME: We assume that the path name currently cached in the FileEntry is + // the most appropriate one for this analysis (and that it's spelled the same + // way as the corresponding header search path). + const char *Name = File->getName(); + + unsigned BestPrefixLength = 0; + unsigned BestSearchDir; + + for (unsigned I = 0; I != SearchDirs.size(); ++I) { + // FIXME: Support this search within frameworks and header maps. + if (!SearchDirs[I].isNormalDir()) + continue; + + const char *Dir = SearchDirs[I].getDir()->getName(); + for (auto NI = llvm::sys::path::begin(Name), + NE = llvm::sys::path::end(Name), + DI = llvm::sys::path::begin(Dir), + DE = llvm::sys::path::end(Dir); + /*termination condition in loop*/; ++NI, ++DI) { + // '.' components in Name are ignored. + while (NI != NE && *NI == ".") + ++NI; + if (NI == NE) + break; + + // '.' components in Dir are ignored. + while (DI != DE && *DI == ".") + ++DI; + if (DI == DE) { + // Dir is a prefix of Name, up to '.' components and choice of path + // separators. + unsigned PrefixLength = NI - llvm::sys::path::begin(Name); + if (PrefixLength > BestPrefixLength) { + BestPrefixLength = PrefixLength; + BestSearchDir = I; + } + break; + } + + if (*NI != *DI) + break; + } + } + + if (IsSystem) + *IsSystem = BestPrefixLength ? BestSearchDir >= SystemDirIdx : false; + return Name + BestPrefixLength; +} diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp index 4b782a3e8ec..467ae7ec518 100644 --- a/clang/lib/Lex/ModuleMap.cpp +++ b/clang/lib/Lex/ModuleMap.cpp @@ -209,29 +209,25 @@ ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File, static bool violatesPrivateInclude(Module *RequestingModule, const FileEntry *IncFileEnt, - ModuleMap::ModuleHeaderRole Role, - Module *RequestedModule) { - bool IsPrivateRole = Role & ModuleMap::PrivateHeader; + ModuleMap::KnownHeader Header) { #ifndef NDEBUG - if (IsPrivateRole) { + if (Header.getRole() & ModuleMap::PrivateHeader) { // Check for consistency between the module header role // as obtained from the lookup and as obtained from the module. // This check is not cheap, so enable it only for debugging. bool IsPrivate = false; SmallVectorImpl<Module::Header> *HeaderList[] = { - &RequestedModule->Headers[Module::HK_Private], - &RequestedModule->Headers[Module::HK_PrivateTextual]}; + &Header.getModule()->Headers[Module::HK_Private], + &Header.getModule()->Headers[Module::HK_PrivateTextual]}; for (auto *Hs : HeaderList) IsPrivate |= std::find_if(Hs->begin(), Hs->end(), [&](const Module::Header &H) { return H.Entry == IncFileEnt; }) != Hs->end(); - assert((!IsPrivateRole || IsPrivate) && "inconsistent headers and roles"); + assert(IsPrivate && "inconsistent headers and roles"); } #endif - return IsPrivateRole && (!RequestingModule || - RequestedModule->getTopLevelModule() != - RequestingModule->getTopLevelModule()); + return !Header.isAccessibleFrom(RequestingModule); } static Module *getTopLevelOrNull(Module *M) { @@ -259,8 +255,7 @@ void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule, if (Known != Headers.end()) { for (const KnownHeader &Header : Known->second) { // Remember private headers for later printing of a diagnostic. - if (violatesPrivateInclude(RequestingModule, File, Header.getRole(), - Header.getModule())) { + if (violatesPrivateInclude(RequestingModule, File, Header)) { Private = Header.getModule(); continue; } diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 59ea27c5868..c33f9aba267 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -597,6 +597,62 @@ Module *Preprocessor::getModuleContainingLocation(SourceLocation Loc) { FullSourceLoc(Loc, SourceMgr)); } +const FileEntry * +Preprocessor::getModuleHeaderToIncludeForDiagnostics(SourceLocation IncLoc, + SourceLocation Loc) { + // If we have a module import syntax, we shouldn't include a header to + // make a particular module visible. + if (getLangOpts().ObjC2) + return nullptr; + + // Figure out which module we'd want to import. + Module *M = getModuleContainingLocation(Loc); + if (!M) + return nullptr; + + Module *TopM = M->getTopLevelModule(); + Module *IncM = getModuleForLocation(IncLoc); + + // Walk up through the include stack, looking through textual headers of M + // until we hit a non-textual header that we can #include. (We assume textual + // headers of a module with non-textual headers aren't meant to be used to + // import entities from the module.) + auto &SM = getSourceManager(); + while (!Loc.isInvalid() && !SM.isInMainFile(Loc)) { + auto ID = SM.getFileID(SM.getExpansionLoc(Loc)); + auto *FE = SM.getFileEntryForID(ID); + + bool InTextualHeader = false; + for (auto Header : HeaderInfo.getModuleMap().findAllModulesForHeader(FE)) { + if (!Header.getModule()->isSubModuleOf(TopM)) + continue; + + if (!(Header.getRole() & ModuleMap::TextualHeader)) { + // If this is an accessible, non-textual header of M's top-level module + // that transitively includes the given location and makes the + // corresponding module visible, this is the thing to #include. + if (Header.isAccessibleFrom(IncM)) + return FE; + + // It's in a private header; we can't #include it. + // FIXME: If there's a public header in some module that re-exports it, + // then we could suggest including that, but it's not clear that's the + // expected way to make this entity visible. + continue; + } + + InTextualHeader = true; + } + + if (!InTextualHeader) + break; + + Loc = SM.getIncludeLoc(ID); + } + + return nullptr; +} + const FileEntry *Preprocessor::LookupFile( SourceLocation FilenameLoc, StringRef Filename, |