88
99#include " BitcodeReader.h"
1010#include " llvm/Support/Error.h"
11+ #include " llvm/Support/ErrorHandling.h"
1112#include " llvm/Support/TimeProfiler.h"
1213#include " llvm/Support/raw_ostream.h"
1314#include < optional>
1415
1516namespace clang {
1617namespace doc {
1718
19+ static llvm::ExitOnError ExitOnErr (" clang-doc error: " );
20+
1821using Record = llvm::SmallVector<uint64_t , 1024 >;
1922
2023// This implements decode for SmallString.
@@ -716,8 +719,8 @@ llvm::Error addReference(FriendInfo *Friend, Reference &&R, FieldId F) {
716719
717720template <typename T, typename ChildInfoType>
718721static void addChild (T I, ChildInfoType &&R) {
719- llvm::errs () << " invalid child type for info " ;
720- exit ( 1 );
722+ ExitOnErr ( llvm::createStringError ( llvm::inconvertibleErrorCode (),
723+ " invalid child type for info " ) );
721724}
722725
723726// Namespace children:
@@ -766,8 +769,9 @@ template <> void addChild(BaseRecordInfo *I, FunctionInfo &&R) {
766769// parameters) or TemplateSpecializationInfo (for the specialization's
767770// parameters).
768771template <typename T> static void addTemplateParam (T I, TemplateParamInfo &&P) {
769- llvm::errs () << " invalid container for template parameter" ;
770- exit (1 );
772+ ExitOnErr (
773+ llvm::createStringError (llvm::inconvertibleErrorCode (),
774+ " invalid container for template parameter" ));
771775}
772776template <> void addTemplateParam (TemplateInfo *I, TemplateParamInfo &&P) {
773777 I->Params .emplace_back (std::move (P));
@@ -779,8 +783,8 @@ void addTemplateParam(TemplateSpecializationInfo *I, TemplateParamInfo &&P) {
779783
780784// Template info. These apply to either records or functions.
781785template <typename T> static void addTemplate (T I, TemplateInfo &&P) {
782- llvm::errs () << " invalid container for template info " ;
783- exit ( 1 );
786+ ExitOnErr ( llvm::createStringError ( llvm::inconvertibleErrorCode (),
787+ " invalid container for template info " ) );
784788}
785789template <> void addTemplate (RecordInfo *I, TemplateInfo &&P) {
786790 I->Template .emplace (std::move (P));
@@ -798,8 +802,9 @@ template <> void addTemplate(FriendInfo *I, TemplateInfo &&P) {
798802// Template specializations go only into template records.
799803template <typename T>
800804static void addTemplateSpecialization (T I, TemplateSpecializationInfo &&TSI) {
801- llvm::errs () << " invalid container for template specialization info" ;
802- exit (1 );
805+ ExitOnErr (llvm::createStringError (
806+ llvm::inconvertibleErrorCode (),
807+ " invalid container for template specialization info" ));
803808}
804809template <>
805810void addTemplateSpecialization (TemplateInfo *I,
@@ -808,8 +813,8 @@ void addTemplateSpecialization(TemplateInfo *I,
808813}
809814
810815template <typename T> static void addConstraint (T I, ConstraintInfo &&C) {
811- llvm::errs () << " invalid container for constraint info " ;
812- exit ( 1 );
816+ ExitOnErr ( llvm::createStringError ( llvm::inconvertibleErrorCode (),
817+ " invalid container for constraint info " ) );
813818}
814819template <> void addConstraint (TemplateInfo *I, ConstraintInfo &&C) {
815820 I->Constraints .emplace_back (std::move (C));
@@ -846,9 +851,11 @@ llvm::Error ClangDocBitcodeReader::readBlock(unsigned ID, T I) {
846851
847852 while (true ) {
848853 unsigned BlockOrCode = 0 ;
849- Cursor Res = skipUntilRecordOrBlock (BlockOrCode);
854+ llvm::Expected<Cursor> C = skipUntilRecordOrBlock (BlockOrCode);
855+ if (!C)
856+ return C.takeError ();
850857
851- switch (Res ) {
858+ switch (*C ) {
852859 case Cursor::BadBlock:
853860 return llvm::createStringError (llvm::inconvertibleErrorCode (),
854861 " bad block found" );
@@ -984,45 +991,39 @@ llvm::Error ClangDocBitcodeReader::readSubBlock(unsigned ID, T I) {
984991 }
985992}
986993
987- ClangDocBitcodeReader::Cursor
994+ llvm::Expected< ClangDocBitcodeReader::Cursor>
988995ClangDocBitcodeReader::skipUntilRecordOrBlock (unsigned &BlockOrRecordID) {
989996 llvm::TimeTraceScope (" Reducing infos" , " skipUntilRecordOrBlock" );
990997 BlockOrRecordID = 0 ;
991998
992999 while (!Stream.AtEndOfStream ()) {
993- Expected<unsigned > MaybeCode = Stream.ReadCode ();
994- if (!MaybeCode) {
995- // FIXME this drops the error on the floor.
996- consumeError (MaybeCode.takeError ());
997- return Cursor::BadBlock;
998- }
1000+ Expected<unsigned > Code = Stream.ReadCode ();
1001+ if (!Code)
1002+ return Code.takeError ();
9991003
1000- unsigned Code = MaybeCode.get ();
1001- if (Code >= static_cast <unsigned >(llvm::bitc::FIRST_APPLICATION_ABBREV)) {
1002- BlockOrRecordID = Code;
1004+ if (*Code >= static_cast <unsigned >(llvm::bitc::FIRST_APPLICATION_ABBREV)) {
1005+ BlockOrRecordID = *Code;
10031006 return Cursor::Record;
10041007 }
1005- switch (static_cast <llvm::bitc::FixedAbbrevIDs>(Code)) {
1008+ switch (static_cast <llvm::bitc::FixedAbbrevIDs>(* Code)) {
10061009 case llvm::bitc::ENTER_SUBBLOCK:
10071010 if (Expected<unsigned > MaybeID = Stream.ReadSubBlockID ())
10081011 BlockOrRecordID = MaybeID.get ();
1009- else {
1010- // FIXME this drops the error on the floor.
1011- consumeError (MaybeID.takeError ());
1012- }
1012+ else
1013+ return MaybeID.takeError ();
10131014 return Cursor::BlockBegin;
10141015 case llvm::bitc::END_BLOCK:
10151016 if (Stream.ReadBlockEnd ())
1016- return Cursor::BadBlock;
1017+ return llvm::createStringError (llvm::inconvertibleErrorCode (),
1018+ " error at end of block" );
10171019 return Cursor::BlockEnd;
10181020 case llvm::bitc::DEFINE_ABBREV:
1019- if (llvm::Error Err = Stream.ReadAbbrevRecord ()) {
1020- // FIXME this drops the error on the floor.
1021- consumeError (std::move (Err));
1022- }
1021+ if (llvm::Error Err = Stream.ReadAbbrevRecord ())
1022+ return std::move (Err);
10231023 continue ;
10241024 case llvm::bitc::UNABBREV_RECORD:
1025- return Cursor::BadBlock;
1025+ return llvm::createStringError (llvm::inconvertibleErrorCode (),
1026+ " found unabbreviated record" );
10261027 case llvm::bitc::FIRST_APPLICATION_ABBREV:
10271028 llvm_unreachable (" Unexpected abbrev id." );
10281029 }
@@ -1148,10 +1149,8 @@ ClangDocBitcodeReader::readBitcode() {
11481149 return std::move (Err);
11491150 continue ;
11501151 default :
1151- if (llvm::Error Err = Stream.SkipBlock ()) {
1152- // FIXME this drops the error on the floor.
1153- consumeError (std::move (Err));
1154- }
1152+ if (llvm::Error Err = Stream.SkipBlock ())
1153+ return std::move (Err);
11551154 continue ;
11561155 }
11571156 }
0 commit comments