[Modules] Resolve top-headers of modules lazily.

This allows resolving top-header filenames of modules to FileEntries when
we need them, not eagerly.

Note that that this breaks ABI for libclang functions
clang_Module_getTopLevelHeader / clang_Module_getNumTopLevelHeaders
but this is fine because they are experimental and not widely used yet.

llvm-svn: 176975
This commit is contained in:
Argyrios Kyrtzidis 2013-03-13 21:13:43 +00:00
parent 6b72269b3d
commit 3c5305c15e
9 changed files with 65 additions and 25 deletions

View File

@ -32,7 +32,7 @@
* compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
*/
#define CINDEX_VERSION_MAJOR 0
#define CINDEX_VERSION_MINOR 14
#define CINDEX_VERSION_MINOR 15
#define CINDEX_VERSION_ENCODE(major, minor) ( \
((major) * 10000) \
@ -3347,7 +3347,8 @@ CINDEX_LINKAGE CXString clang_Module_getFullName(CXModule Module);
*
* \returns the number of top level headers associated with this module.
*/
CINDEX_LINKAGE unsigned clang_Module_getNumTopLevelHeaders(CXModule Module);
CINDEX_LINKAGE unsigned clang_Module_getNumTopLevelHeaders(CXTranslationUnit,
CXModule Module);
/**
* \param Module a module object.
@ -3357,7 +3358,8 @@ CINDEX_LINKAGE unsigned clang_Module_getNumTopLevelHeaders(CXModule Module);
* \returns the specified top level header associated with the module.
*/
CINDEX_LINKAGE
CXFile clang_Module_getTopLevelHeader(CXModule Module, unsigned Index);
CXFile clang_Module_getTopLevelHeader(CXTranslationUnit,
CXModule Module, unsigned Index);
/**
* @}

View File

@ -34,6 +34,7 @@ namespace clang {
class DirectoryEntry;
class FileEntry;
class FileManager;
class LangOptions;
class TargetInfo;
@ -67,7 +68,13 @@ private:
/// \brief The AST file if this is a top-level module which has a
/// corresponding serialized AST file, or null otherwise.
const FileEntry *ASTFile;
/// \brief The top-level headers associated with this module.
llvm::SmallSetVector<const FileEntry *, 2> TopHeaders;
/// \brief top-level header filenames that aren't resolved to FileEntries yet.
std::vector<std::string> TopHeaderNames;
public:
/// \brief The headers that are part of this module.
SmallVector<const FileEntry *, 2> Headers;
@ -75,9 +82,6 @@ public:
/// \brief The headers that are explicitly excluded from this module.
SmallVector<const FileEntry *, 2> ExcludedHeaders;
/// \brief The top-level headers associated with this module.
llvm::SmallSetVector<const FileEntry *, 2> TopHeaders;
/// \brief The set of language features required to use this module.
///
/// If any of these features is not present, the \c IsAvailable bit
@ -292,6 +296,20 @@ public:
return Umbrella && Umbrella.is<const DirectoryEntry *>();
}
/// \brief Add a top-level header associated with this module.
void addTopHeader(const FileEntry *File) {
assert(File);
TopHeaders.insert(File);
}
/// \brief Add a top-level header filename associated with this module.
void addTopHeaderFilename(StringRef Filename) {
TopHeaderNames.push_back(Filename);
}
/// \brief The top-level headers associated with this module.
ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr);
/// \brief Add the given feature requirement to the list of features
/// required by this module.
///

View File

@ -15,6 +15,7 @@
#include "clang/Basic/FileManager.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/ErrorHandling.h"
@ -129,6 +130,19 @@ const DirectoryEntry *Module::getUmbrellaDir() const {
return Umbrella.dyn_cast<const DirectoryEntry *>();
}
ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
if (!TopHeaderNames.empty()) {
for (std::vector<std::string>::iterator
I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
if (const FileEntry *FE = FileMgr.getFile(*I))
TopHeaders.insert(FE);
}
TopHeaderNames.clear();
}
return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
}
void Module::addRequirement(StringRef Feature, const LangOptions &LangOpts,
const TargetInfo &Target) {
Requires.push_back(Feature);

View File

@ -173,12 +173,12 @@ static void collectModuleHeaderIncludes(const LangOptions &LangOpts,
// Add includes for each of these headers.
for (unsigned I = 0, N = Module->Headers.size(); I != N; ++I) {
const FileEntry *Header = Module->Headers[I];
Module->TopHeaders.insert(Header);
Module->addTopHeader(Header);
addHeaderInclude(Header, Includes, LangOpts);
}
if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) {
Module->TopHeaders.insert(UmbrellaHeader);
Module->addTopHeader(UmbrellaHeader);
if (Module->Parent) {
// Include the umbrella header for submodules.
addHeaderInclude(UmbrellaHeader, Includes, LangOpts);
@ -203,7 +203,7 @@ static void collectModuleHeaderIncludes(const LangOptions &LangOpts,
if (const FileEntry *Header = FileMgr.getFile(Dir->path())) {
if (ModMap.isHeaderInUnavailableModule(Header))
continue;
Module->TopHeaders.insert(Header);
Module->addTopHeader(Header);
}
// Include this header umbrella header for submodules.

View File

@ -202,7 +202,7 @@ Module *ModuleMap::findModuleForHeader(const FileEntry *File) {
llvm::sys::path::stem(File->getName()), NameBuf);
Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
Explicit).first;
Result->TopHeaders.insert(File);
Result->addTopHeader(File);
// If inferred submodules export everything they import, add a
// wildcard to the set of exports.

View File

@ -3546,9 +3546,7 @@ bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
if (!CurrentModule)
break;
// FIXME: Be more lazy about this!
if (const FileEntry *File = PP.getFileManager().getFile(Blob))
CurrentModule->TopHeaders.insert(File);
CurrentModule->addTopHeaderFilename(Blob);
break;
}

View File

@ -2170,11 +2170,13 @@ void ASTWriter::WriteSubmodules(Module *WritingModule) {
Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
Mod->ExcludedHeaders[I]->getName());
}
for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
ArrayRef<const FileEntry *>
TopHeaders = Mod->getTopHeaders(PP->getFileManager());
for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Record.clear();
Record.push_back(SUBMODULE_TOPHEADER);
Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Mod->TopHeaders[I]->getName());
TopHeaders[I]->getName());
}
// Emit the imports.

View File

@ -1996,12 +1996,12 @@ static int inspect_cursor_at(int argc, const char **argv) {
unsigned i, numHeaders;
if (mod) {
name = clang_Module_getFullName(mod);
numHeaders = clang_Module_getNumTopLevelHeaders(mod);
numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod);
printf(" ModuleName=%s Headers(%d):",
clang_getCString(name), numHeaders);
clang_disposeString(name);
for (i = 0; i < numHeaders; ++i) {
CXFile file = clang_Module_getTopLevelHeader(mod, i);
CXFile file = clang_Module_getTopLevelHeader(TU, mod, i);
CXString filename = clang_getFileName(file);
printf("\n%s", clang_getCString(filename));
clang_disposeString(filename);

View File

@ -5984,20 +5984,26 @@ CXString clang_Module_getFullName(CXModule CXMod) {
return cxstring::createDup(Mod->getFullModuleName());
}
unsigned clang_Module_getNumTopLevelHeaders(CXModule CXMod) {
if (!CXMod)
unsigned clang_Module_getNumTopLevelHeaders(CXTranslationUnit TU,
CXModule CXMod) {
if (!TU || !CXMod)
return 0;
Module *Mod = static_cast<Module*>(CXMod);
return Mod->TopHeaders.size();
FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager();
ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr);
return TopHeaders.size();
}
CXFile clang_Module_getTopLevelHeader(CXModule CXMod, unsigned Index) {
if (!CXMod)
CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU,
CXModule CXMod, unsigned Index) {
if (!TU || !CXMod)
return 0;
Module *Mod = static_cast<Module*>(CXMod);
FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager();
if (Index < Mod->TopHeaders.size())
return const_cast<FileEntry *>(Mod->TopHeaders[Index]);
ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr);
if (Index < TopHeaders.size())
return const_cast<FileEntry *>(TopHeaders[Index]);
return 0;
}