1 //===- TGLexer.cpp - Lexer for TableGen -----------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Implement the Lexer for TableGen.
11 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Config/config.h" // for strtoull()/strtoll() define
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "llvm/TableGen/Error.h"
33 // A list of supported preprocessing directives with their
34 // internal token kinds and names.
38 } PreprocessorDirs
[] = {
39 { tgtok::Ifdef
, "ifdef" },
40 { tgtok::Ifndef
, "ifndef" },
41 { tgtok::Else
, "else" },
42 { tgtok::Endif
, "endif" },
43 { tgtok::Define
, "define" }
45 } // end anonymous namespace
47 TGLexer::TGLexer(SourceMgr
&SM
, ArrayRef
<std::string
> Macros
) : SrcMgr(SM
) {
48 CurBuffer
= SrcMgr
.getMainFileID();
49 CurBuf
= SrcMgr
.getMemoryBuffer(CurBuffer
)->getBuffer();
50 CurPtr
= CurBuf
.begin();
53 // Pretend that we enter the "top-level" include file.
54 PrepIncludeStack
.push_back(
55 std::make_unique
<std::vector
<PreprocessorControlDesc
>>());
57 // Put all macros defined in the command line into the DefinedMacros set.
58 std::for_each(Macros
.begin(), Macros
.end(),
59 [this](const std::string
&MacroName
) {
60 DefinedMacros
.insert(MacroName
);
64 SMLoc
TGLexer::getLoc() const {
65 return SMLoc::getFromPointer(TokStart
);
68 /// ReturnError - Set the error to the specified string at the specified
69 /// location. This is defined to always return tgtok::Error.
70 tgtok::TokKind
TGLexer::ReturnError(SMLoc Loc
, const Twine
&Msg
) {
75 tgtok::TokKind
TGLexer::ReturnError(const char *Loc
, const Twine
&Msg
) {
76 return ReturnError(SMLoc::getFromPointer(Loc
), Msg
);
79 bool TGLexer::processEOF() {
80 SMLoc ParentIncludeLoc
= SrcMgr
.getParentIncludeLoc(CurBuffer
);
81 if (ParentIncludeLoc
!= SMLoc()) {
82 // If prepExitInclude() detects a problem with the preprocessing
83 // control stack, it will return false. Pretend that we reached
84 // the final EOF and stop lexing more tokens by returning false
86 if (!prepExitInclude(false))
89 CurBuffer
= SrcMgr
.FindBufferContainingLoc(ParentIncludeLoc
);
90 CurBuf
= SrcMgr
.getMemoryBuffer(CurBuffer
)->getBuffer();
91 CurPtr
= ParentIncludeLoc
.getPointer();
92 // Make sure TokStart points into the parent file's buffer.
93 // LexToken() assigns to it before calling getNextChar(),
94 // so it is pointing into the included file now.
99 // Pretend that we exit the "top-level" include file.
100 // Note that in case of an error (e.g. control stack imbalance)
101 // the routine will issue a fatal error.
102 prepExitInclude(true);
106 int TGLexer::getNextChar() {
107 char CurChar
= *CurPtr
++;
110 return (unsigned char)CurChar
;
113 // A NUL character in the stream is either the end of the current buffer or
114 // a spurious NUL in the file. Disambiguate that here.
115 if (CurPtr
- 1 == CurBuf
.end()) {
116 --CurPtr
; // Arrange for another call to return EOF again.
120 "NUL character is invalid in source; treated as space");
126 // Handle the newline character by ignoring it and incrementing the line
127 // count. However, be careful about 'dos style' files with \n\r in them.
128 // Only treat a \n\r or \r\n as a single line.
129 if ((*CurPtr
== '\n' || (*CurPtr
== '\r')) &&
131 ++CurPtr
; // Eat the two char newline sequence.
136 int TGLexer::peekNextChar(int Index
) const {
137 return *(CurPtr
+ Index
);
140 tgtok::TokKind
TGLexer::LexToken(bool FileOrLineStart
) {
142 // This always consumes at least one character.
143 int CurChar
= getNextChar();
147 // Handle letters: [a-zA-Z_]
148 if (isalpha(CurChar
) || CurChar
== '_')
149 return LexIdentifier();
151 // Unknown character, emit an error.
152 return ReturnError(TokStart
, "Unexpected character");
154 // Lex next token, if we just left an include file.
155 // Note that leaving an include file means that the next
156 // symbol is located at the end of the 'include "..."'
157 // construct, so LexToken() is called with default
162 // Return EOF denoting the end of lexing.
165 case ':': return tgtok::colon
;
166 case ';': return tgtok::semi
;
167 case ',': return tgtok::comma
;
168 case '<': return tgtok::less
;
169 case '>': return tgtok::greater
;
170 case ']': return tgtok::r_square
;
171 case '{': return tgtok::l_brace
;
172 case '}': return tgtok::r_brace
;
173 case '(': return tgtok::l_paren
;
174 case ')': return tgtok::r_paren
;
175 case '=': return tgtok::equal
;
176 case '?': return tgtok::question
;
178 if (FileOrLineStart
) {
179 tgtok::TokKind Kind
= prepIsDirective();
180 if (Kind
!= tgtok::Error
)
181 return lexPreprocessor(Kind
);
186 // The period is a separate case so we can recognize the "..."
189 if (peekNextChar(0) == '.') {
190 ++CurPtr
; // Eat second dot.
191 if (peekNextChar(0) == '.') {
192 ++CurPtr
; // Eat third dot.
193 return tgtok::dotdotdot
;
195 return ReturnError(TokStart
, "Invalid '..' punctuation");
200 PrintFatalError("getNextChar() must never return '\r'");
205 // Ignore whitespace.
206 return LexToken(FileOrLineStart
);
208 // Ignore whitespace, and identify the new line.
209 return LexToken(true);
211 // If this is the start of a // comment, skip until the end of the line or
212 // the end of the buffer.
215 else if (*CurPtr
== '*') {
218 } else // Otherwise, this is an error.
219 return ReturnError(TokStart
, "Unexpected character");
220 return LexToken(FileOrLineStart
);
222 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
223 case '7': case '8': case '9': {
225 if (isdigit(CurChar
)) {
226 // Allow identifiers to start with a number if it is followed by
227 // an identifier. This can happen with paste operations like
231 NextChar
= peekNextChar(i
++);
232 } while (isdigit(NextChar
));
234 if (NextChar
== 'x' || NextChar
== 'b') {
235 // If this is [0-9]b[01] or [0-9]x[0-9A-fa-f] this is most
237 int NextNextChar
= peekNextChar(i
);
238 switch (NextNextChar
) {
245 case '2': case '3': case '4': case '5':
246 case '6': case '7': case '8': case '9':
247 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
248 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
256 if (isalpha(NextChar
) || NextChar
== '_')
257 return LexIdentifier();
261 case '"': return LexString();
262 case '$': return LexVarName();
263 case '[': return LexBracket();
264 case '!': return LexExclaim();
268 /// LexString - Lex "[^"]*"
269 tgtok::TokKind
TGLexer::LexString() {
270 const char *StrStart
= CurPtr
;
274 while (*CurPtr
!= '"') {
275 // If we hit the end of the buffer, report an error.
276 if (*CurPtr
== 0 && CurPtr
== CurBuf
.end())
277 return ReturnError(StrStart
, "End of file in string literal");
279 if (*CurPtr
== '\n' || *CurPtr
== '\r')
280 return ReturnError(StrStart
, "End of line in string literal");
282 if (*CurPtr
!= '\\') {
283 CurStrVal
+= *CurPtr
++;
290 case '\\': case '\'': case '"':
291 // These turn into their literal character.
292 CurStrVal
+= *CurPtr
++;
305 return ReturnError(CurPtr
, "escaped newlines not supported in tblgen");
307 // If we hit the end of the buffer, report an error.
309 if (CurPtr
== CurBuf
.end())
310 return ReturnError(StrStart
, "End of file in string literal");
313 return ReturnError(CurPtr
, "invalid escape in string literal");
318 return tgtok::StrVal
;
321 tgtok::TokKind
TGLexer::LexVarName() {
322 if (!isalpha(CurPtr
[0]) && CurPtr
[0] != '_')
323 return ReturnError(TokStart
, "Invalid variable name");
325 // Otherwise, we're ok, consume the rest of the characters.
326 const char *VarNameStart
= CurPtr
++;
328 while (isalpha(*CurPtr
) || isdigit(*CurPtr
) || *CurPtr
== '_')
331 CurStrVal
.assign(VarNameStart
, CurPtr
);
332 return tgtok::VarName
;
335 tgtok::TokKind
TGLexer::LexIdentifier() {
336 // The first letter is [a-zA-Z_].
337 const char *IdentStart
= TokStart
;
339 // Match the rest of the identifier regex: [0-9a-zA-Z_]*
340 while (isalpha(*CurPtr
) || isdigit(*CurPtr
) || *CurPtr
== '_')
343 // Check to see if this identifier is a reserved keyword.
344 StringRef
Str(IdentStart
, CurPtr
-IdentStart
);
346 tgtok::TokKind Kind
= StringSwitch
<tgtok::TokKind
>(Str
)
347 .Case("int", tgtok::Int
)
348 .Case("bit", tgtok::Bit
)
349 .Case("bits", tgtok::Bits
)
350 .Case("string", tgtok::String
)
351 .Case("list", tgtok::List
)
352 .Case("code", tgtok::Code
)
353 .Case("dag", tgtok::Dag
)
354 .Case("class", tgtok::Class
)
355 .Case("def", tgtok::Def
)
356 .Case("true", tgtok::TrueVal
)
357 .Case("false", tgtok::FalseVal
)
358 .Case("foreach", tgtok::Foreach
)
359 .Case("defm", tgtok::Defm
)
360 .Case("defset", tgtok::Defset
)
361 .Case("multiclass", tgtok::MultiClass
)
362 .Case("field", tgtok::Field
)
363 .Case("let", tgtok::Let
)
364 .Case("in", tgtok::In
)
365 .Case("defvar", tgtok::Defvar
)
366 .Case("include", tgtok::Include
)
367 .Case("if", tgtok::If
)
368 .Case("then", tgtok::Then
)
369 .Case("else", tgtok::ElseKW
)
370 .Case("assert", tgtok::Assert
)
373 // A couple of tokens require special processing.
376 if (LexInclude()) return tgtok::Error
;
379 CurStrVal
.assign(Str
.begin(), Str
.end());
388 /// LexInclude - We just read the "include" token. Get the string token that
389 /// comes next and enter the include.
390 bool TGLexer::LexInclude() {
391 // The token after the include must be a string.
392 tgtok::TokKind Tok
= LexToken();
393 if (Tok
== tgtok::Error
) return true;
394 if (Tok
!= tgtok::StrVal
) {
395 PrintError(getLoc(), "Expected filename after include");
400 std::string Filename
= CurStrVal
;
401 std::string IncludedFile
;
403 CurBuffer
= SrcMgr
.AddIncludeFile(Filename
, SMLoc::getFromPointer(CurPtr
),
406 PrintError(getLoc(), "Could not find include file '" + Filename
+ "'");
410 Dependencies
.insert(IncludedFile
);
411 // Save the line number and lex buffer of the includer.
412 CurBuf
= SrcMgr
.getMemoryBuffer(CurBuffer
)->getBuffer();
413 CurPtr
= CurBuf
.begin();
415 PrepIncludeStack
.push_back(
416 std::make_unique
<std::vector
<PreprocessorControlDesc
>>());
420 /// SkipBCPLComment - Skip over the comment by finding the next CR or LF.
421 /// Or we may end up at the end of the buffer.
422 void TGLexer::SkipBCPLComment() {
423 ++CurPtr
; // skip the second slash.
424 auto EOLPos
= CurBuf
.find_first_of("\r\n", CurPtr
- CurBuf
.data());
425 CurPtr
= (EOLPos
== StringRef::npos
) ? CurBuf
.end() : CurBuf
.data() + EOLPos
;
428 /// SkipCComment - This skips C-style /**/ comments. The only difference from C
429 /// is that we allow nesting.
430 bool TGLexer::SkipCComment() {
431 ++CurPtr
; // skip the star.
432 unsigned CommentDepth
= 1;
435 int CurChar
= getNextChar();
438 PrintError(TokStart
, "Unterminated comment!");
441 // End of the comment?
442 if (CurPtr
[0] != '/') break;
444 ++CurPtr
; // End the */.
445 if (--CommentDepth
== 0)
449 // Start of a nested comment?
450 if (CurPtr
[0] != '*') break;
462 tgtok::TokKind
TGLexer::LexNumber() {
463 if (CurPtr
[-1] == '0') {
464 if (CurPtr
[0] == 'x') {
466 const char *NumStart
= CurPtr
;
467 while (isxdigit(CurPtr
[0]))
470 // Requires at least one hex digit.
471 if (CurPtr
== NumStart
)
472 return ReturnError(TokStart
, "Invalid hexadecimal number");
475 CurIntVal
= strtoll(NumStart
, nullptr, 16);
477 return ReturnError(TokStart
, "Invalid hexadecimal number");
478 if (errno
== ERANGE
) {
480 CurIntVal
= (int64_t)strtoull(NumStart
, nullptr, 16);
482 return ReturnError(TokStart
, "Invalid hexadecimal number");
484 return ReturnError(TokStart
, "Hexadecimal number out of range");
486 return tgtok::IntVal
;
487 } else if (CurPtr
[0] == 'b') {
489 const char *NumStart
= CurPtr
;
490 while (CurPtr
[0] == '0' || CurPtr
[0] == '1')
493 // Requires at least one binary digit.
494 if (CurPtr
== NumStart
)
495 return ReturnError(CurPtr
-2, "Invalid binary number");
496 CurIntVal
= strtoll(NumStart
, nullptr, 2);
497 return tgtok::BinaryIntVal
;
501 // Check for a sign without a digit.
502 if (!isdigit(CurPtr
[0])) {
503 if (CurPtr
[-1] == '-')
505 else if (CurPtr
[-1] == '+')
509 while (isdigit(CurPtr
[0]))
511 CurIntVal
= strtoll(TokStart
, nullptr, 10);
512 return tgtok::IntVal
;
515 /// LexBracket - We just read '['. If this is a code block, return it,
516 /// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
517 tgtok::TokKind
TGLexer::LexBracket() {
518 if (CurPtr
[0] != '{')
519 return tgtok::l_square
;
521 const char *CodeStart
= CurPtr
;
523 int Char
= getNextChar();
524 if (Char
== EOF
) break;
526 if (Char
!= '}') continue;
528 Char
= getNextChar();
529 if (Char
== EOF
) break;
531 CurStrVal
.assign(CodeStart
, CurPtr
-2);
532 return tgtok::CodeFragment
;
536 return ReturnError(CodeStart
- 2, "Unterminated code block");
539 /// LexExclaim - Lex '!' and '![a-zA-Z]+'.
540 tgtok::TokKind
TGLexer::LexExclaim() {
541 if (!isalpha(*CurPtr
))
542 return ReturnError(CurPtr
- 1, "Invalid \"!operator\"");
544 const char *Start
= CurPtr
++;
545 while (isalpha(*CurPtr
))
548 // Check to see which operator this is.
549 tgtok::TokKind Kind
=
550 StringSwitch
<tgtok::TokKind
>(StringRef(Start
, CurPtr
- Start
))
551 .Case("eq", tgtok::XEq
)
552 .Case("ne", tgtok::XNe
)
553 .Case("le", tgtok::XLe
)
554 .Case("lt", tgtok::XLt
)
555 .Case("ge", tgtok::XGe
)
556 .Case("gt", tgtok::XGt
)
557 .Case("if", tgtok::XIf
)
558 .Case("cond", tgtok::XCond
)
559 .Case("isa", tgtok::XIsA
)
560 .Case("head", tgtok::XHead
)
561 .Case("tail", tgtok::XTail
)
562 .Case("size", tgtok::XSize
)
563 .Case("con", tgtok::XConcat
)
564 .Case("dag", tgtok::XDag
)
565 .Case("add", tgtok::XADD
)
566 .Case("sub", tgtok::XSUB
)
567 .Case("mul", tgtok::XMUL
)
568 .Case("not", tgtok::XNOT
)
569 .Case("and", tgtok::XAND
)
570 .Case("or", tgtok::XOR
)
571 .Case("xor", tgtok::XXOR
)
572 .Case("shl", tgtok::XSHL
)
573 .Case("sra", tgtok::XSRA
)
574 .Case("srl", tgtok::XSRL
)
575 .Case("cast", tgtok::XCast
)
576 .Case("empty", tgtok::XEmpty
)
577 .Case("subst", tgtok::XSubst
)
578 .Case("foldl", tgtok::XFoldl
)
579 .Case("foreach", tgtok::XForEach
)
580 .Case("filter", tgtok::XFilter
)
581 .Case("listconcat", tgtok::XListConcat
)
582 .Case("listsplat", tgtok::XListSplat
)
583 .Case("strconcat", tgtok::XStrConcat
)
584 .Case("interleave", tgtok::XInterleave
)
585 .Case("substr", tgtok::XSubstr
)
586 .Case("find", tgtok::XFind
)
587 .Cases("setdagop", "setop", tgtok::XSetDagOp
) // !setop is deprecated.
588 .Cases("getdagop", "getop", tgtok::XGetDagOp
) // !getop is deprecated.
589 .Default(tgtok::Error
);
591 return Kind
!= tgtok::Error
? Kind
: ReturnError(Start
-1, "Unknown operator");
594 bool TGLexer::prepExitInclude(bool IncludeStackMustBeEmpty
) {
595 // Report an error, if preprocessor control stack for the current
596 // file is not empty.
597 if (!PrepIncludeStack
.back()->empty()) {
598 prepReportPreprocessorStackError();
603 // Pop the preprocessing controls from the include stack.
604 if (PrepIncludeStack
.empty()) {
605 PrintFatalError("Preprocessor include stack is empty");
608 PrepIncludeStack
.pop_back();
610 if (IncludeStackMustBeEmpty
) {
611 if (!PrepIncludeStack
.empty())
612 PrintFatalError("Preprocessor include stack is not empty");
614 if (PrepIncludeStack
.empty())
615 PrintFatalError("Preprocessor include stack is empty");
621 tgtok::TokKind
TGLexer::prepIsDirective() const {
622 for (const auto &PD
: PreprocessorDirs
) {
623 int NextChar
= *CurPtr
;
626 for (; I
< strlen(PD
.Word
); ++I
) {
627 if (NextChar
!= PD
.Word
[I
]) {
632 NextChar
= peekNextChar(I
+ 1);
635 // Check for whitespace after the directive. If there is no whitespace,
636 // then we do not recognize it as a preprocessing directive.
638 tgtok::TokKind Kind
= PD
.Kind
;
640 // New line and EOF may follow only #else/#endif. It will be reported
641 // as an error for #ifdef/#define after the call to prepLexMacroName().
642 if (NextChar
== ' ' || NextChar
== '\t' || NextChar
== EOF
||
644 // It looks like TableGen does not support '\r' as the actual
645 // carriage return, e.g. getNextChar() treats a single '\r'
646 // as '\n'. So we do the same here.
650 // Allow comments after some directives, e.g.:
651 // #else// OR #else/**/
652 // #endif// OR #endif/**/
654 // Note that we do allow comments after #ifdef/#define here, e.g.
655 // #ifdef/**/ AND #ifdef//
656 // #define/**/ AND #define//
658 // These cases will be reported as incorrect after calling
659 // prepLexMacroName(). We could have supported C-style comments
660 // after #ifdef/#define, but this would complicate the code
661 // for little benefit.
662 if (NextChar
== '/') {
663 NextChar
= peekNextChar(I
+ 1);
665 if (NextChar
== '*' || NextChar
== '/')
668 // Pretend that we do not recognize the directive.
676 bool TGLexer::prepEatPreprocessorDirective(tgtok::TokKind Kind
) {
679 for (const auto &PD
: PreprocessorDirs
)
680 if (PD
.Kind
== Kind
) {
681 // Advance CurPtr to the end of the preprocessing word.
682 CurPtr
+= strlen(PD
.Word
);
686 PrintFatalError("Unsupported preprocessing token in "
687 "prepEatPreprocessorDirective()");
691 tgtok::TokKind
TGLexer::lexPreprocessor(
692 tgtok::TokKind Kind
, bool ReturnNextLiveToken
) {
694 // We must be looking at a preprocessing directive. Eat it!
695 if (!prepEatPreprocessorDirective(Kind
))
696 PrintFatalError("lexPreprocessor() called for unknown "
697 "preprocessor directive");
699 if (Kind
== tgtok::Ifdef
|| Kind
== tgtok::Ifndef
) {
700 StringRef MacroName
= prepLexMacroName();
701 StringRef IfTokName
= Kind
== tgtok::Ifdef
? "#ifdef" : "#ifndef";
702 if (MacroName
.empty())
703 return ReturnError(TokStart
, "Expected macro name after " + IfTokName
);
705 bool MacroIsDefined
= DefinedMacros
.count(MacroName
) != 0;
707 // Canonicalize ifndef to ifdef equivalent
708 if (Kind
== tgtok::Ifndef
) {
709 MacroIsDefined
= !MacroIsDefined
;
713 // Regardless of whether we are processing tokens or not,
714 // we put the #ifdef control on stack.
715 PrepIncludeStack
.back()->push_back(
716 {Kind
, MacroIsDefined
, SMLoc::getFromPointer(TokStart
)});
718 if (!prepSkipDirectiveEnd())
719 return ReturnError(CurPtr
, "Only comments are supported after " +
720 IfTokName
+ " NAME");
722 // If we were not processing tokens before this #ifdef,
723 // then just return back to the lines skipping code.
724 if (!ReturnNextLiveToken
)
727 // If we were processing tokens before this #ifdef,
728 // and the macro is defined, then just return the next token.
732 // We were processing tokens before this #ifdef, and the macro
733 // is not defined, so we have to start skipping the lines.
734 // If the skipping is successful, it will return the token following
735 // either #else or #endif corresponding to this #ifdef.
736 if (prepSkipRegion(ReturnNextLiveToken
))
740 } else if (Kind
== tgtok::Else
) {
741 // Check if this #else is correct before calling prepSkipDirectiveEnd(),
742 // which will move CurPtr away from the beginning of #else.
743 if (PrepIncludeStack
.back()->empty())
744 return ReturnError(TokStart
, "#else without #ifdef or #ifndef");
746 PreprocessorControlDesc IfdefEntry
= PrepIncludeStack
.back()->back();
748 if (IfdefEntry
.Kind
!= tgtok::Ifdef
) {
749 PrintError(TokStart
, "double #else");
750 return ReturnError(IfdefEntry
.SrcPos
, "Previous #else is here");
753 // Replace the corresponding #ifdef's control with its negation
754 // on the control stack.
755 PrepIncludeStack
.back()->pop_back();
756 PrepIncludeStack
.back()->push_back(
757 {Kind
, !IfdefEntry
.IsDefined
, SMLoc::getFromPointer(TokStart
)});
759 if (!prepSkipDirectiveEnd())
760 return ReturnError(CurPtr
, "Only comments are supported after #else");
762 // If we were processing tokens before this #else,
763 // we have to start skipping lines until the matching #endif.
764 if (ReturnNextLiveToken
) {
765 if (prepSkipRegion(ReturnNextLiveToken
))
771 // Return to the lines skipping code.
773 } else if (Kind
== tgtok::Endif
) {
774 // Check if this #endif is correct before calling prepSkipDirectiveEnd(),
775 // which will move CurPtr away from the beginning of #endif.
776 if (PrepIncludeStack
.back()->empty())
777 return ReturnError(TokStart
, "#endif without #ifdef");
779 auto &IfdefOrElseEntry
= PrepIncludeStack
.back()->back();
781 if (IfdefOrElseEntry
.Kind
!= tgtok::Ifdef
&&
782 IfdefOrElseEntry
.Kind
!= tgtok::Else
) {
783 PrintFatalError("Invalid preprocessor control on the stack");
787 if (!prepSkipDirectiveEnd())
788 return ReturnError(CurPtr
, "Only comments are supported after #endif");
790 PrepIncludeStack
.back()->pop_back();
792 // If we were processing tokens before this #endif, then
793 // we should continue it.
794 if (ReturnNextLiveToken
) {
798 // Return to the lines skipping code.
800 } else if (Kind
== tgtok::Define
) {
801 StringRef MacroName
= prepLexMacroName();
802 if (MacroName
.empty())
803 return ReturnError(TokStart
, "Expected macro name after #define");
805 if (!DefinedMacros
.insert(MacroName
).second
)
806 PrintWarning(getLoc(),
807 "Duplicate definition of macro: " + Twine(MacroName
));
809 if (!prepSkipDirectiveEnd())
810 return ReturnError(CurPtr
,
811 "Only comments are supported after #define NAME");
813 if (!ReturnNextLiveToken
) {
814 PrintFatalError("#define must be ignored during the lines skipping");
821 PrintFatalError("Preprocessing directive is not supported");
825 bool TGLexer::prepSkipRegion(bool MustNeverBeFalse
) {
826 if (!MustNeverBeFalse
)
827 PrintFatalError("Invalid recursion.");
830 // Skip all symbols to the line end.
833 // Find the first non-whitespace symbol in the next line(s).
834 if (!prepSkipLineBegin())
837 // If the first non-blank/comment symbol on the line is '#',
838 // it may be a start of preprocessing directive.
840 // If it is not '#' just go to the next line.
846 tgtok::TokKind Kind
= prepIsDirective();
848 // If we did not find a preprocessing directive or it is #define,
849 // then just skip to the next line. We do not have to do anything
850 // for #define in the line-skipping mode.
851 if (Kind
== tgtok::Error
|| Kind
== tgtok::Define
)
854 tgtok::TokKind ProcessedKind
= lexPreprocessor(Kind
, false);
856 // If lexPreprocessor() encountered an error during lexing this
857 // preprocessor idiom, then return false to the calling lexPreprocessor().
858 // This will force tgtok::Error to be returned to the tokens processing.
859 if (ProcessedKind
== tgtok::Error
)
862 if (Kind
!= ProcessedKind
)
863 PrintFatalError("prepIsDirective() and lexPreprocessor() "
864 "returned different token kinds");
866 // If this preprocessing directive enables tokens processing,
867 // then return to the lexPreprocessor() and get to the next token.
868 // We can move from line-skipping mode to processing tokens only
869 // due to #else or #endif.
870 if (prepIsProcessingEnabled()) {
871 if (Kind
!= tgtok::Else
&& Kind
!= tgtok::Endif
) {
872 PrintFatalError("Tokens processing was enabled by an unexpected "
873 "preprocessing directive");
879 } while (CurPtr
!= CurBuf
.end());
881 // We have reached the end of the file, but never left the lines-skipping
882 // mode. This means there is no matching #endif.
883 prepReportPreprocessorStackError();
887 StringRef
TGLexer::prepLexMacroName() {
888 // Skip whitespaces between the preprocessing directive and the macro name.
889 while (*CurPtr
== ' ' || *CurPtr
== '\t')
893 // Macro names start with [a-zA-Z_].
894 if (*CurPtr
!= '_' && !isalpha(*CurPtr
))
897 // Match the rest of the identifier regex: [0-9a-zA-Z_]*
898 while (isalpha(*CurPtr
) || isdigit(*CurPtr
) || *CurPtr
== '_')
901 return StringRef(TokStart
, CurPtr
- TokStart
);
904 bool TGLexer::prepSkipLineBegin() {
905 while (CurPtr
!= CurBuf
.end()) {
914 int NextChar
= peekNextChar(1);
915 if (NextChar
== '*') {
916 // Skip C-style comment.
917 // Note that we do not care about skipping the C++-style comments.
918 // If the line contains "//", it may not contain any processable
919 // preprocessing directive. Just return CurPtr pointing to
920 // the first '/' in this case. We also do not care about
921 // incorrect symbols after the first '/' - we are in lines-skipping
922 // mode, so incorrect code is allowed to some extent.
924 // Set TokStart to the beginning of the comment to enable proper
925 // diagnostic printing in case of error in SkipCComment().
928 // CurPtr must point to '*' before call to SkipCComment().
933 // CurPtr points to the non-whitespace '/'.
937 // We must not increment CurPtr after the comment was lexed.
948 // We have reached the end of the file. Return to the lines skipping
949 // code, and allow it to handle the EOF as needed.
953 bool TGLexer::prepSkipDirectiveEnd() {
954 while (CurPtr
!= CurBuf
.end()) {
965 int NextChar
= peekNextChar(1);
966 if (NextChar
== '/') {
967 // Skip C++-style comment.
968 // We may just return true now, but let's skip to the line/buffer end
969 // to simplify the method specification.
972 } else if (NextChar
== '*') {
973 // When we are skipping C-style comment at the end of a preprocessing
974 // directive, we can skip several lines. If any meaningful TD token
975 // follows the end of the C-style comment on the same line, it will
976 // be considered as an invalid usage of TD token.
977 // For example, we want to forbid usages like this one:
978 // #define MACRO class Class {}
979 // But with C-style comments we also disallow the following:
980 // #define MACRO /* This macro is used
981 // to ... */ class Class {}
982 // One can argue that this should be allowed, but it does not seem
983 // to be worth of the complication. Moreover, this matches
984 // the C preprocessor behavior.
986 // Set TokStart to the beginning of the comment to enable proper
987 // diagnostic printer in case of error in SkipCComment().
994 PrintError(CurPtr
, "Unexpected character");
998 // We must not increment CurPtr after the comment was lexed.
1003 // Do not allow any non-whitespaces after the directive.
1014 void TGLexer::prepSkipToLineEnd() {
1015 while (*CurPtr
!= '\n' && *CurPtr
!= '\r' && CurPtr
!= CurBuf
.end())
1019 bool TGLexer::prepIsProcessingEnabled() {
1020 for (auto I
= PrepIncludeStack
.back()->rbegin(),
1021 E
= PrepIncludeStack
.back()->rend();
1030 void TGLexer::prepReportPreprocessorStackError() {
1031 if (PrepIncludeStack
.back()->empty())
1032 PrintFatalError("prepReportPreprocessorStackError() called with "
1033 "empty control stack");
1035 auto &PrepControl
= PrepIncludeStack
.back()->back();
1036 PrintError(CurBuf
.end(), "Reached EOF without matching #endif");
1037 PrintError(PrepControl
.SrcPos
, "The latest preprocessor control is here");