From ade5000c8763f4bec41f452d7efa3a9b2a6d4712 Mon Sep 17 00:00:00 2001 From: Sebastian Redl Date: Fri, 30 Jul 2010 17:03:48 +0000 Subject: [PATCH] Safely get a data pointer for vectors that might be empty. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@109867 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Frontend/PCHWriter.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/Frontend/PCHWriter.cpp b/lib/Frontend/PCHWriter.cpp index b574656bb6..a72f7cfea8 100644 --- a/lib/Frontend/PCHWriter.cpp +++ b/lib/Frontend/PCHWriter.cpp @@ -40,6 +40,15 @@ #include using namespace clang; +template +T *data(std::vector &v) { + return v.empty() ? 0 : &v.front(); +} +template +const T *data(const std::vector &v) { + return v.empty() ? 0 : &v.front(); +} + //===----------------------------------------------------------------------===// // Type serialization //===----------------------------------------------------------------------===// @@ -1218,7 +1227,7 @@ void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr, Record.push_back(SLocEntryOffsets.size()); Record.push_back(SourceMgr.getNextOffset()); Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, - (const char *)&SLocEntryOffsets.front(), + (const char *)data(SLocEntryOffsets), SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0])); // Write the source location entry preloads array, telling the PCH @@ -1376,7 +1385,7 @@ void PCHWriter::WritePreprocessor(const Preprocessor &PP) { Record.push_back(NumPreprocessingRecords); Record.push_back(MacroDefinitionOffsets.size()); Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record, - (const char *)&MacroDefinitionOffsets.front(), + (const char *)data(MacroDefinitionOffsets), MacroDefinitionOffsets.size() * sizeof(uint32_t)); } } @@ -1523,7 +1532,7 @@ void PCHWriter::WriteTypeDeclOffsets() { Record.push_back(pch::TYPE_OFFSET); Record.push_back(TypeOffsets.size()); Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, - (const char *)&TypeOffsets.front(), + (const char *)data(TypeOffsets), TypeOffsets.size() * sizeof(TypeOffsets[0])); // Write the declaration offsets array @@ -1536,7 +1545,7 @@ void PCHWriter::WriteTypeDeclOffsets() { Record.push_back(pch::DECL_OFFSET); Record.push_back(DeclOffsets.size()); Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, - (const char *)&DeclOffsets.front(), + (const char *)data(DeclOffsets), DeclOffsets.size() * sizeof(DeclOffsets[0])); } @@ -1738,7 +1747,7 @@ void PCHWriter::WriteMethodPool(Sema &SemaRef) { Record.push_back(pch::SELECTOR_OFFSETS); Record.push_back(SelectorOffsets.size()); Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record, - (const char *)&SelectorOffsets.front(), + (const char *)data(SelectorOffsets), SelectorOffsets.size() * 4); } } @@ -1939,7 +1948,7 @@ void PCHWriter::WriteIdentifierTable(Preprocessor &PP) { Record.push_back(pch::IDENTIFIER_OFFSET); Record.push_back(IdentifierOffsets.size()); Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record, - (const char *)&IdentifierOffsets.front(), + (const char *)data(IdentifierOffsets), IdentifierOffsets.size() * sizeof(uint32_t)); } -- 2.40.0