diff options
author | Douglas Gregor <dgregor@apple.com> | 2010-01-23 00:14:00 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2010-01-23 00:14:00 +0000 |
commit | aa98ed9a07668bce8a63ec65521008b085bfc0d8 (patch) | |
tree | 3fd94f7e47144ddaf6341b37ba63535ec74ffac4 /clang/lib | |
parent | 73b0f99f171923cc09ed57396edb2f69fd102743 (diff) | |
download | bcm5719-llvm-aa98ed9a07668bce8a63ec65521008b085bfc0d8.tar.gz bcm5719-llvm-aa98ed9a07668bce8a63ec65521008b085bfc0d8.zip |
Extend clang_createTranslationUnitFromSourceFile() to support creating
translation units that include unsaved files.
llvm-svn: 94258
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Frontend/ASTUnit.cpp | 32 | ||||
-rw-r--r-- | clang/lib/Frontend/InitPreprocessor.cpp | 41 |
2 files changed, 61 insertions, 12 deletions
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp index 48296c7289e..2fb47cbd8a8 100644 --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -30,6 +30,7 @@ #include "clang/Basic/TargetOptions.h" #include "clang/Basic/TargetInfo.h" #include "clang/Basic/Diagnostic.h" +#include "llvm/Support/MemoryBuffer.h" #include "llvm/System/Host.h" #include "llvm/System/Path.h" using namespace clang; @@ -103,11 +104,31 @@ const std::string &ASTUnit::getPCHFileName() { ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, Diagnostic &Diags, bool OnlyLocalDecls, - bool UseBumpAllocator) { + bool UseBumpAllocator, + RemappedFile *RemappedFiles, + unsigned NumRemappedFiles) { llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true)); AST->OnlyLocalDecls = OnlyLocalDecls; AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager())); + for (unsigned I = 0; I != NumRemappedFiles; ++I) { + // Create the file entry for the file that we're mapping from. + const FileEntry *FromFile + = AST->getFileManager().getVirtualFile(RemappedFiles[I].first, + RemappedFiles[I].second->getBufferSize(), + 0); + if (!FromFile) { + Diags.Report(diag::err_fe_remap_missing_from_file) + << RemappedFiles[I].first; + continue; + } + + // Override the contents of the "from" file with the contents of + // the "to" file. + AST->getSourceManager().overrideFileContents(FromFile, + RemappedFiles[I].second); + } + // Gather Info for preprocessor construction later on. LangOptions LangInfo; @@ -289,7 +310,9 @@ ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin, Diagnostic &Diags, llvm::StringRef ResourceFilesPath, bool OnlyLocalDecls, - bool UseBumpAllocator) { + bool UseBumpAllocator, + RemappedFile *RemappedFiles, + unsigned NumRemappedFiles) { llvm::SmallVector<const char *, 16> Args; Args.push_back("<clang>"); // FIXME: Remove dummy argument. Args.insert(Args.end(), ArgBegin, ArgEnd); @@ -327,6 +350,11 @@ ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin, (const char**) CCArgs.data()+CCArgs.size(), Diags); + // Override any files that need remapping + for (unsigned I = 0; I != NumRemappedFiles; ++I) + CI.getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, + RemappedFiles[I].second); + // Override the resources path. CI.getHeaderSearchOpts().ResourceDir = ResourceFilesPath; diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index 32363eed20a..9aaf1320346 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -424,40 +424,61 @@ static void InitializeFileRemapping(Diagnostic &Diags, SourceManager &SourceMgr, FileManager &FileMgr, const PreprocessorOptions &InitOpts) { - // Remap files in the source manager. + // Remap files in the source manager (with buffers). + for (PreprocessorOptions::remapped_file_buffer_iterator + Remap = InitOpts.remapped_file_buffer_begin(), + RemapEnd = InitOpts.remapped_file_buffer_end(); + Remap != RemapEnd; + ++Remap) { + // Create the file entry for the file that we're mapping from. + const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, + Remap->second->getBufferSize(), + 0); + if (!FromFile) { + Diags.Report(diag::err_fe_remap_missing_from_file) + << Remap->first; + continue; + } + + // Override the contents of the "from" file with the contents of + // the "to" file. + SourceMgr.overrideFileContents(FromFile, Remap->second); + } + + // Remap files in the source manager (with other files). for (PreprocessorOptions::remapped_file_iterator - Remap = InitOpts.remapped_file_begin(), - RemapEnd = InitOpts.remapped_file_end(); + Remap = InitOpts.remapped_file_begin(), + RemapEnd = InitOpts.remapped_file_end(); Remap != RemapEnd; ++Remap) { // Find the file that we're mapping to. const FileEntry *ToFile = FileMgr.getFile(Remap->second); if (!ToFile) { Diags.Report(diag::err_fe_remap_missing_to_file) - << Remap->first << Remap->second; + << Remap->first << Remap->second; continue; } - + // Create the file entry for the file that we're mapping from. const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first, ToFile->getSize(), 0); if (!FromFile) { Diags.Report(diag::err_fe_remap_missing_from_file) - << Remap->first; + << Remap->first; continue; } - + // Load the contents of the file we're mapping to. std::string ErrorStr; const llvm::MemoryBuffer *Buffer - = llvm::MemoryBuffer::getFile(ToFile->getName(), &ErrorStr); + = llvm::MemoryBuffer::getFile(ToFile->getName(), &ErrorStr); if (!Buffer) { Diags.Report(diag::err_fe_error_opening) - << Remap->second << ErrorStr; + << Remap->second << ErrorStr; continue; } - + // Override the contents of the "from" file with the contents of // the "to" file. SourceMgr.overrideFileContents(FromFile, Buffer); |