2525
2626using namespace llvm ;
2727
28- bool LLLexer::Error (LocTy ErrorLoc, const Twine &Msg) const {
29- ErrorInfo = SM.GetMessage (ErrorLoc, SourceMgr::DK_Error, Msg);
30- return true ;
28+ // Both the lexer and parser can issue error messages. If the lexer issues a
29+ // lexer error, since we do not terminate execution immediately, usually that
30+ // is followed by the parser issuing a parser error. However, the error issued
31+ // by the lexer is more relevant in that case as opposed to potentially more
32+ // generic parser error. So instead of always recording the last error message
33+ // use the `Priority` to establish a priority, with Lexer > Parser > None. We
34+ // record the issued message only if the message has same or higher priority
35+ // than the existing one. This prevents lexer errors from being overwritten by
36+ // parser errors.
37+ void LLLexer::Error (LocTy ErrorLoc, const Twine &Msg,
38+ LLLexer::ErrorPriority Priority) {
39+ if (Priority < ErrorInfo.Priority )
40+ return ;
41+ ErrorInfo.Error = SM.GetMessage (ErrorLoc, SourceMgr::DK_Error, Msg);
42+ ErrorInfo.Priority = Priority;
3143}
3244
3345void LLLexer::Warning (LocTy WarningLoc, const Twine &Msg) const {
@@ -49,7 +61,7 @@ uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
4961 Result *= 10 ;
5062 Result += *Buffer-' 0' ;
5163 if (Result < OldRes) { // Uh, oh, overflow detected!!!
52- Error (" constant bigger than 64 bits detected!" );
64+ LexError (" constant bigger than 64 bits detected!" );
5365 return 0 ;
5466 }
5567 }
@@ -64,7 +76,7 @@ uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
6476 Result += hexDigitValue (*Buffer);
6577
6678 if (Result < OldRes) { // Uh, oh, overflow detected!!!
67- Error (" constant bigger than 64 bits detected!" );
79+ LexError (" constant bigger than 64 bits detected!" );
6880 return 0 ;
6981 }
7082 }
@@ -87,7 +99,7 @@ void LLLexer::HexToIntPair(const char *Buffer, const char *End,
8799 Pair[1 ] += hexDigitValue (*Buffer);
88100 }
89101 if (Buffer != End)
90- Error (" constant bigger than 128 bits detected!" );
102+ LexError (" constant bigger than 128 bits detected!" );
91103}
92104
93105// / FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
@@ -106,7 +118,7 @@ void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
106118 Pair[0 ] += hexDigitValue (*Buffer);
107119 }
108120 if (Buffer != End)
109- Error (" constant bigger than 128 bits detected!" );
121+ LexError (" constant bigger than 128 bits detected!" );
110122}
111123
112124// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
@@ -273,14 +285,14 @@ lltok::Kind LLLexer::LexDollar() {
273285 int CurChar = getNextChar ();
274286
275287 if (CurChar == EOF) {
276- Error (" end of file in COMDAT variable name" );
288+ LexError (" end of file in COMDAT variable name" );
277289 return lltok::Error;
278290 }
279291 if (CurChar == ' "' ) {
280292 StrVal.assign (TokStart + 2 , CurPtr - 1 );
281293 UnEscapeLexed (StrVal);
282294 if (StringRef (StrVal).contains (0 )) {
283- Error (" Null bytes are not allowed in names" );
295+ LexError (" Null bytes are not allowed in names" );
284296 return lltok::Error;
285297 }
286298 return lltok::ComdatVar;
@@ -302,7 +314,7 @@ lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
302314 int CurChar = getNextChar ();
303315
304316 if (CurChar == EOF) {
305- Error (" end of file in string constant" );
317+ LexError (" end of file in string constant" );
306318 return lltok::Error;
307319 }
308320 if (CurChar == ' "' ) {
@@ -342,7 +354,7 @@ lltok::Kind LLLexer::LexUIntID(lltok::Kind Token) {
342354
343355 uint64_t Val = atoull (TokStart + 1 , CurPtr);
344356 if ((unsigned )Val != Val)
345- Error (" invalid value number (too large)!" );
357+ LexError (" invalid value number (too large)!" );
346358 UIntVal = unsigned (Val);
347359 return Token;
348360}
@@ -356,14 +368,14 @@ lltok::Kind LLLexer::LexVar(lltok::Kind Var, lltok::Kind VarID) {
356368 int CurChar = getNextChar ();
357369
358370 if (CurChar == EOF) {
359- Error (" end of file in global variable name" );
371+ LexError (" end of file in global variable name" );
360372 return lltok::Error;
361373 }
362374 if (CurChar == ' "' ) {
363375 StrVal.assign (TokStart+2 , CurPtr-1 );
364376 UnEscapeLexed (StrVal);
365377 if (StringRef (StrVal).contains (0 )) {
366- Error (" Null bytes are not allowed in names" );
378+ LexError (" Null bytes are not allowed in names" );
367379 return lltok::Error;
368380 }
369381 return Var;
@@ -398,7 +410,7 @@ lltok::Kind LLLexer::LexQuote() {
398410 if (CurPtr[0 ] == ' :' ) {
399411 ++CurPtr;
400412 if (StringRef (StrVal).contains (0 )) {
401- Error (" Null bytes are not allowed in names" );
413+ LexError (" Null bytes are not allowed in names" );
402414 kind = lltok::Error;
403415 } else {
404416 kind = lltok::LabelStr;
@@ -480,7 +492,7 @@ lltok::Kind LLLexer::LexIdentifier() {
480492 uint64_t NumBits = atoull (StartChar, CurPtr);
481493 if (NumBits < IntegerType::MIN_INT_BITS ||
482494 NumBits > IntegerType::MAX_INT_BITS) {
483- Error (" bitwidth for integer type out of range!" );
495+ LexError (" bitwidth for integer type out of range!" );
484496 return lltok::Error;
485497 }
486498 TyVal = IntegerType::get (Context, NumBits);
@@ -1109,7 +1121,7 @@ lltok::Kind LLLexer::LexDigitOrNegative() {
11091121 uint64_t Val = atoull (TokStart, CurPtr);
11101122 ++CurPtr; // Skip the colon.
11111123 if ((unsigned )Val != Val)
1112- Error (" invalid value number (too large)!" );
1124+ LexError (" invalid value number (too large)!" );
11131125 UIntVal = unsigned (Val);
11141126 return lltok::LabelID;
11151127 }
0 commit comments