summaryrefslogtreecommitdiffstats
path: root/clang/lib/Serialization
diff options
context:
space:
mode:
authorAdrian Prantl <aprantl@apple.com>2015-02-20 19:44:52 +0000
committerAdrian Prantl <aprantl@apple.com>2015-02-20 19:44:52 +0000
commitc4091aa74e030ca5a447b7d119a4d46d6be17de2 (patch)
tree84c43c5614a3f0f5747b7cc8f80cbd7341e1e9c5 /clang/lib/Serialization
parent7035178aebc91d0ed99759919865d6745ac052e2 (diff)
downloadbcm5719-llvm-c4091aa74e030ca5a447b7d119a4d46d6be17de2.tar.gz
bcm5719-llvm-c4091aa74e030ca5a447b7d119a4d46d6be17de2.zip
Wrap clang module files in a Mach-O, ELF, or COFF container.
This is a necessary prerequisite for debugging with modules. The .pcm files become containers that hold the serialized AST which allows us to store debug information in the module file that can be shared by all object files that were built importing the module. rdar://problem/19104245 llvm-svn: 230044
Diffstat (limited to 'clang/lib/Serialization')
-rw-r--r--clang/lib/Serialization/ASTReader.cpp34
-rw-r--r--clang/lib/Serialization/CMakeLists.txt1
-rw-r--r--clang/lib/Serialization/GeneratePCH.cpp18
-rw-r--r--clang/lib/Serialization/GlobalModuleIndex.cpp5
-rw-r--r--clang/lib/Serialization/ModuleManager.cpp9
5 files changed, 43 insertions, 24 deletions
diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp
index bb6a450222d..69d8f117cb9 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -46,6 +46,8 @@
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Bitcode/BitstreamReader.h"
+#include "llvm/Object/COFF.h"
+#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
@@ -632,6 +634,27 @@ void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
// AST reader implementation
//===----------------------------------------------------------------------===//
+void ASTReader::InitStreamFileWithModule(llvm::MemoryBufferRef Buffer,
+ llvm::BitstreamReader &StreamFile) {
+ if (auto OF = llvm::object::ObjectFile::createObjectFile(Buffer)) {
+ bool IsCOFF = isa<llvm::object::COFFObjectFile>(OF.get().get());
+ // Find the clang AST section in the container.
+ for (auto &Section : OF->get()->sections()) {
+ StringRef Name;
+ Section.getName(Name);
+ if ((!IsCOFF && Name == "__clangast") ||
+ ( IsCOFF && Name == "clangast")) {
+ StringRef Buf;
+ Section.getContents(Buf);
+ return StreamFile.init((const unsigned char*)Buf.begin(),
+ (const unsigned char*)Buf.end());
+ }
+ }
+ }
+ StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
+ (const unsigned char *)Buffer.getBufferEnd());
+}
+
void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
bool TakeOwnership) {
DeserializationListener = Listener;
@@ -3883,9 +3906,10 @@ ASTReader::ReadASTCore(StringRef FileName,
ModuleFile &F = *M;
BitstreamCursor &Stream = F.Stream;
+ InitStreamFileWithModule(F.Buffer->getMemBufferRef(), F.StreamFile);
Stream.init(&F.StreamFile);
- F.SizeInBits = F.Buffer->getBufferSize() * 8;
-
+ F.SizeInBits = F.StreamFile.getBitcodeBytes().getExtent() * 8;
+
// Sniff for the signature.
if (Stream.Read(8) != 'C' ||
Stream.Read(8) != 'P' ||
@@ -4174,8 +4198,7 @@ std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
// Initialize the stream
llvm::BitstreamReader StreamFile;
- StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(),
- (const unsigned char *)(*Buffer)->getBufferEnd());
+ InitStreamFileWithModule((*Buffer)->getMemBufferRef(), StreamFile);
BitstreamCursor Stream(StreamFile);
// Sniff for the signature.
@@ -4270,8 +4293,7 @@ bool ASTReader::readASTFileControlBlock(StringRef Filename,
// Initialize the stream
llvm::BitstreamReader StreamFile;
- StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(),
- (const unsigned char *)(*Buffer)->getBufferEnd());
+ InitStreamFileWithModule((*Buffer)->getMemBufferRef(), StreamFile);
BitstreamCursor Stream(StreamFile);
// Sniff for the signature.
diff --git a/clang/lib/Serialization/CMakeLists.txt b/clang/lib/Serialization/CMakeLists.txt
index d885db22975..a1a5ad4abe7 100644
--- a/clang/lib/Serialization/CMakeLists.txt
+++ b/clang/lib/Serialization/CMakeLists.txt
@@ -1,5 +1,6 @@
set(LLVM_LINK_COMPONENTS
BitReader
+ Object
Support
)
diff --git a/clang/lib/Serialization/GeneratePCH.cpp b/clang/lib/Serialization/GeneratePCH.cpp
index b5031fdf92a..0cd01dc6f3e 100644
--- a/clang/lib/Serialization/GeneratePCH.cpp
+++ b/clang/lib/Serialization/GeneratePCH.cpp
@@ -19,7 +19,6 @@
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/SemaConsumer.h"
#include "llvm/Bitcode/BitstreamWriter.h"
-#include "llvm/Support/raw_ostream.h"
#include <string>
using namespace clang;
@@ -28,10 +27,11 @@ PCHGenerator::PCHGenerator(const Preprocessor &PP,
StringRef OutputFile,
clang::Module *Module,
StringRef isysroot,
- raw_ostream *OS, bool AllowASTWithErrors)
+ bool AllowASTWithErrors)
: PP(PP), OutputFile(OutputFile), Module(Module),
- isysroot(isysroot.str()), Out(OS),
- SemaPtr(nullptr), Stream(Buffer), Writer(Stream),
+ isysroot(isysroot.str()),
+ SemaPtr(nullptr), Stream(Buffer),
+ Writer(Stream),
AllowASTWithErrors(AllowASTWithErrors),
HasEmittedPCH(false) {
}
@@ -52,14 +52,8 @@ void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
assert(SemaPtr && "No Sema?");
Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, hasErrors);
- // Write the generated bitstream to "Out".
- Out->write((char *)&Buffer.front(), Buffer.size());
-
- // Make sure it hits disk now.
- Out->flush();
-
- // Free up some memory, in case the process is kept alive.
- Buffer.clear();
+ if (SerializationFinishedCallback)
+ SerializationFinishedCallback(&Buffer);
HasEmittedPCH = true;
}
diff --git a/clang/lib/Serialization/GlobalModuleIndex.cpp b/clang/lib/Serialization/GlobalModuleIndex.cpp
index 47913880454..68a23ea870d 100644
--- a/clang/lib/Serialization/GlobalModuleIndex.cpp
+++ b/clang/lib/Serialization/GlobalModuleIndex.cpp
@@ -15,6 +15,7 @@
#include "clang/Basic/FileManager.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Serialization/ASTBitCodes.h"
+#include "clang/Serialization/ASTReader.h"
#include "clang/Serialization/GlobalModuleIndex.h"
#include "clang/Serialization/Module.h"
#include "llvm/ADT/DenseMap.h"
@@ -501,8 +502,8 @@ bool GlobalModuleIndexBuilder::loadModuleFile(const FileEntry *File) {
// Initialize the input stream
llvm::BitstreamReader InStreamFile;
- InStreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(),
- (const unsigned char *)(*Buffer)->getBufferEnd());
+ ASTReader::InitStreamFileWithModule((*Buffer)->getMemBufferRef(),
+ InStreamFile);
llvm::BitstreamCursor InStream(InStreamFile);
// Sniff for the signature.
diff --git a/clang/lib/Serialization/ModuleManager.cpp b/clang/lib/Serialization/ModuleManager.cpp
index ac98ca0b872..8f1473f3a3a 100644
--- a/clang/lib/Serialization/ModuleManager.cpp
+++ b/clang/lib/Serialization/ModuleManager.cpp
@@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/ModuleMap.h"
+#include "clang/Serialization/ASTReader.h"
#include "clang/Serialization/GlobalModuleIndex.h"
#include "clang/Serialization/ModuleManager.h"
#include "llvm/Support/MemoryBuffer.h"
@@ -135,10 +136,10 @@ ModuleManager::addModule(StringRef FileName, ModuleKind Type,
New->Buffer = std::move(*Buf);
}
-
- // Initialize the stream
- New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
- (const unsigned char *)New->Buffer->getBufferEnd());
+
+ // Initialize the stream.
+ ASTReader::InitStreamFileWithModule(New->Buffer->getMemBufferRef(),
+ New->StreamFile);
}
if (ExpectedSignature) {
OpenPOWER on IntegriCloud