1 //===- TGLexer.cpp - Lexer for TableGen -----------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implement the Lexer for TableGen.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "TGLexer.h"
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"
22 #include <algorithm>
23 #include <cctype>
24 #include <cerrno>
25 #include <cstdint>
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cstring>
29 
30 using namespace llvm;
31 
32 namespace {
33 // A list of supported preprocessing directives with their
34 // internal token kinds and names.
35 struct {
36   tgtok::TokKind Kind;
37   const char *Word;
38 } PreprocessorDirs[] = {
39   { tgtok::Ifdef, "ifdef" },
40   { tgtok::Ifndef, "ifndef" },
41   { tgtok::Else, "else" },
42   { tgtok::Endif, "endif" },
43   { tgtok::Define, "define" }
44 };
45 } // end anonymous namespace
46 
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();
51   TokStart = nullptr;
52 
53   // Pretend that we enter the "top-level" include file.
54   PrepIncludeStack.push_back(
55       std::make_unique<std::vector<PreprocessorControlDesc>>());
56 
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);
61                 });
62 }
63 
64 SMLoc TGLexer::getLoc() const {
65   return SMLoc::getFromPointer(TokStart);
66 }
67 
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) {
71   PrintError(Loc, Msg);
72   return tgtok::Error;
73 }
74 
75 tgtok::TokKind TGLexer::ReturnError(const char *Loc, const Twine &Msg) {
76   return ReturnError(SMLoc::getFromPointer(Loc), Msg);
77 }
78 
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
85     // to LexToken().
86     if (!prepExitInclude(false))
87       return false;
88 
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.
95     TokStart = CurPtr;
96     return true;
97   }
98 
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);
103   return false;
104 }
105 
106 int TGLexer::getNextChar() {
107   char CurChar = *CurPtr++;
108   switch (CurChar) {
109   default:
110     return (unsigned char)CurChar;
111   case 0: {
112     // A nul character in the stream is either the end of the current buffer or
113     // a random nul in the file.  Disambiguate that here.
114     if (CurPtr-1 != CurBuf.end())
115       return 0;  // Just whitespace.
116 
117     // Otherwise, return end of file.
118     --CurPtr;  // Another call to lex will return EOF again.
119     return EOF;
120   }
121   case '\n':
122   case '\r':
123     // Handle the newline character by ignoring it and incrementing the line
124     // count.  However, be careful about 'dos style' files with \n\r in them.
125     // Only treat a \n\r or \r\n as a single line.
126     if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
127         *CurPtr != CurChar)
128       ++CurPtr;  // Eat the two char newline sequence.
129     return '\n';
130   }
131 }
132 
133 int TGLexer::peekNextChar(int Index) const {
134   return *(CurPtr + Index);
135 }
136 
137 tgtok::TokKind TGLexer::LexToken(bool FileOrLineStart) {
138   TokStart = CurPtr;
139   // This always consumes at least one character.
140   int CurChar = getNextChar();
141 
142   switch (CurChar) {
143   default:
144     // Handle letters: [a-zA-Z_]
145     if (isalpha(CurChar) || CurChar == '_')
146       return LexIdentifier();
147 
148     // Unknown character, emit an error.
149     return ReturnError(TokStart, "Unexpected character");
150   case EOF:
151     // Lex next token, if we just left an include file.
152     // Note that leaving an include file means that the next
153     // symbol is located at the end of 'include "..."'
154     // construct, so LexToken() is called with default
155     // false parameter.
156     if (processEOF())
157       return LexToken();
158 
159     // Return EOF denoting the end of lexing.
160     return tgtok::Eof;
161 
162   case ':': return tgtok::colon;
163   case ';': return tgtok::semi;
164   case ',': return tgtok::comma;
165   case '<': return tgtok::less;
166   case '>': return tgtok::greater;
167   case ']': return tgtok::r_square;
168   case '{': return tgtok::l_brace;
169   case '}': return tgtok::r_brace;
170   case '(': return tgtok::l_paren;
171   case ')': return tgtok::r_paren;
172   case '=': return tgtok::equal;
173   case '?': return tgtok::question;
174   case '#':
175     if (FileOrLineStart) {
176       tgtok::TokKind Kind = prepIsDirective();
177       if (Kind != tgtok::Error)
178         return lexPreprocessor(Kind);
179     }
180 
181     return tgtok::paste;
182 
183   // The period is a separate case so we can recognize the "..."
184   // range punctuator.
185   case '.':
186     if (peekNextChar(0) == '.') {
187       ++CurPtr; // Eat second dot.
188       if (peekNextChar(0) == '.') {
189         ++CurPtr; // Eat third dot.
190         return tgtok::dotdotdot;
191       }
192       return ReturnError(TokStart, "Invalid '..' punctuation");
193     }
194     return tgtok::dot;
195 
196   case '\r':
197     PrintFatalError("getNextChar() must never return '\r'");
198     return tgtok::Error;
199 
200   case 0:
201   case ' ':
202   case '\t':
203     // Ignore whitespace.
204     return LexToken(FileOrLineStart);
205   case '\n':
206     // Ignore whitespace, and identify the new line.
207     return LexToken(true);
208   case '/':
209     // If this is the start of a // comment, skip until the end of the line or
210     // the end of the buffer.
211     if (*CurPtr == '/')
212       SkipBCPLComment();
213     else if (*CurPtr == '*') {
214       if (SkipCComment())
215         return tgtok::Error;
216     } else // Otherwise, this is an error.
217       return ReturnError(TokStart, "Unexpected character");
218     return LexToken(FileOrLineStart);
219   case '-': case '+':
220   case '0': case '1': case '2': case '3': case '4': case '5': case '6':
221   case '7': case '8': case '9': {
222     int NextChar = 0;
223     if (isdigit(CurChar)) {
224       // Allow identifiers to start with a number if it is followed by
225       // an identifier.  This can happen with paste operations like
226       // foo#8i.
227       int i = 0;
228       do {
229         NextChar = peekNextChar(i++);
230       } while (isdigit(NextChar));
231 
232       if (NextChar == 'x' || NextChar == 'b') {
233         // If this is [0-9]b[01] or [0-9]x[0-9A-fa-f] this is most
234         // likely a number.
235         int NextNextChar = peekNextChar(i);
236         switch (NextNextChar) {
237         default:
238           break;
239         case '0': case '1':
240           if (NextChar == 'b')
241             return LexNumber();
242           LLVM_FALLTHROUGH;
243         case '2': case '3': case '4': case '5':
244         case '6': case '7': case '8': case '9':
245         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
246         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
247           if (NextChar == 'x')
248             return LexNumber();
249           break;
250         }
251       }
252     }
253 
254     if (isalpha(NextChar) || NextChar == '_')
255       return LexIdentifier();
256 
257     return LexNumber();
258   }
259   case '"': return LexString();
260   case '$': return LexVarName();
261   case '[': return LexBracket();
262   case '!': return LexExclaim();
263   }
264 }
265 
266 /// LexString - Lex "[^"]*"
267 tgtok::TokKind TGLexer::LexString() {
268   const char *StrStart = CurPtr;
269 
270   CurStrVal = "";
271 
272   while (*CurPtr != '"') {
273     // If we hit the end of the buffer, report an error.
274     if (*CurPtr == 0 && CurPtr == CurBuf.end())
275       return ReturnError(StrStart, "End of file in string literal");
276 
277     if (*CurPtr == '\n' || *CurPtr == '\r')
278       return ReturnError(StrStart, "End of line in string literal");
279 
280     if (*CurPtr != '\\') {
281       CurStrVal += *CurPtr++;
282       continue;
283     }
284 
285     ++CurPtr;
286 
287     switch (*CurPtr) {
288     case '\\': case '\'': case '"':
289       // These turn into their literal character.
290       CurStrVal += *CurPtr++;
291       break;
292     case 't':
293       CurStrVal += '\t';
294       ++CurPtr;
295       break;
296     case 'n':
297       CurStrVal += '\n';
298       ++CurPtr;
299       break;
300 
301     case '\n':
302     case '\r':
303       return ReturnError(CurPtr, "escaped newlines not supported in tblgen");
304 
305     // If we hit the end of the buffer, report an error.
306     case '\0':
307       if (CurPtr == CurBuf.end())
308         return ReturnError(StrStart, "End of file in string literal");
309       LLVM_FALLTHROUGH;
310     default:
311       return ReturnError(CurPtr, "invalid escape in string literal");
312     }
313   }
314 
315   ++CurPtr;
316   return tgtok::StrVal;
317 }
318 
319 tgtok::TokKind TGLexer::LexVarName() {
320   if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
321     return ReturnError(TokStart, "Invalid variable name");
322 
323   // Otherwise, we're ok, consume the rest of the characters.
324   const char *VarNameStart = CurPtr++;
325 
326   while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
327     ++CurPtr;
328 
329   CurStrVal.assign(VarNameStart, CurPtr);
330   return tgtok::VarName;
331 }
332 
333 tgtok::TokKind TGLexer::LexIdentifier() {
334   // The first letter is [a-zA-Z_].
335   const char *IdentStart = TokStart;
336 
337   // Match the rest of the identifier regex: [0-9a-zA-Z_]*
338   while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
339     ++CurPtr;
340 
341   // Check to see if this identifier is a keyword.
342   StringRef Str(IdentStart, CurPtr-IdentStart);
343 
344   if (Str == "include") {
345     if (LexInclude()) return tgtok::Error;
346     return Lex();
347   }
348 
349   tgtok::TokKind Kind = StringSwitch<tgtok::TokKind>(Str)
350     .Case("int", tgtok::Int)
351     .Case("bit", tgtok::Bit)
352     .Case("bits", tgtok::Bits)
353     .Case("string", tgtok::String)
354     .Case("list", tgtok::List)
355     .Case("code", tgtok::Code)
356     .Case("dag", tgtok::Dag)
357     .Case("class", tgtok::Class)
358     .Case("def", tgtok::Def)
359     .Case("foreach", tgtok::Foreach)
360     .Case("defm", tgtok::Defm)
361     .Case("defset", tgtok::Defset)
362     .Case("multiclass", tgtok::MultiClass)
363     .Case("field", tgtok::Field)
364     .Case("let", tgtok::Let)
365     .Case("in", tgtok::In)
366     .Case("defvar", tgtok::Defvar)
367     .Case("if", tgtok::If)
368     .Case("then", tgtok::Then)
369     .Case("else", tgtok::ElseKW)
370     .Default(tgtok::Id);
371 
372   if (Kind == tgtok::Id)
373     CurStrVal.assign(Str.begin(), Str.end());
374   return Kind;
375 }
376 
377 /// LexInclude - We just read the "include" token.  Get the string token that
378 /// comes next and enter the include.
379 bool TGLexer::LexInclude() {
380   // The token after the include must be a string.
381   tgtok::TokKind Tok = LexToken();
382   if (Tok == tgtok::Error) return true;
383   if (Tok != tgtok::StrVal) {
384     PrintError(getLoc(), "Expected filename after include");
385     return true;
386   }
387 
388   // Get the string.
389   std::string Filename = CurStrVal;
390   std::string IncludedFile;
391 
392   CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr),
393                                     IncludedFile);
394   if (!CurBuffer) {
395     PrintError(getLoc(), "Could not find include file '" + Filename + "'");
396     return true;
397   }
398 
399   Dependencies.insert(IncludedFile);
400   // Save the line number and lex buffer of the includer.
401   CurBuf = SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer();
402   CurPtr = CurBuf.begin();
403 
404   PrepIncludeStack.push_back(
405       std::make_unique<std::vector<PreprocessorControlDesc>>());
406   return false;
407 }
408 
409 void TGLexer::SkipBCPLComment() {
410   ++CurPtr;  // skip the second slash.
411   while (true) {
412     switch (*CurPtr) {
413     case '\n':
414     case '\r':
415       return;  // Newline is end of comment.
416     case 0:
417       // If this is the end of the buffer, end the comment.
418       if (CurPtr == CurBuf.end())
419         return;
420       break;
421     }
422     // Otherwise, skip the character.
423     ++CurPtr;
424   }
425 }
426 
427 /// SkipCComment - This skips C-style /**/ comments.  The only difference from C
428 /// is that we allow nesting.
429 bool TGLexer::SkipCComment() {
430   ++CurPtr;  // skip the star.
431   unsigned CommentDepth = 1;
432 
433   while (true) {
434     int CurChar = getNextChar();
435     switch (CurChar) {
436     case EOF:
437       PrintError(TokStart, "Unterminated comment!");
438       return true;
439     case '*':
440       // End of the comment?
441       if (CurPtr[0] != '/') break;
442 
443       ++CurPtr;   // End the */.
444       if (--CommentDepth == 0)
445         return false;
446       break;
447     case '/':
448       // Start of a nested comment?
449       if (CurPtr[0] != '*') break;
450       ++CurPtr;
451       ++CommentDepth;
452       break;
453     }
454   }
455 }
456 
457 /// LexNumber - Lex:
458 ///    [-+]?[0-9]+
459 ///    0x[0-9a-fA-F]+
460 ///    0b[01]+
461 tgtok::TokKind TGLexer::LexNumber() {
462   if (CurPtr[-1] == '0') {
463     if (CurPtr[0] == 'x') {
464       ++CurPtr;
465       const char *NumStart = CurPtr;
466       while (isxdigit(CurPtr[0]))
467         ++CurPtr;
468 
469       // Requires at least one hex digit.
470       if (CurPtr == NumStart)
471         return ReturnError(TokStart, "Invalid hexadecimal number");
472 
473       errno = 0;
474       CurIntVal = strtoll(NumStart, nullptr, 16);
475       if (errno == EINVAL)
476         return ReturnError(TokStart, "Invalid hexadecimal number");
477       if (errno == ERANGE) {
478         errno = 0;
479         CurIntVal = (int64_t)strtoull(NumStart, nullptr, 16);
480         if (errno == EINVAL)
481           return ReturnError(TokStart, "Invalid hexadecimal number");
482         if (errno == ERANGE)
483           return ReturnError(TokStart, "Hexadecimal number out of range");
484       }
485       return tgtok::IntVal;
486     } else if (CurPtr[0] == 'b') {
487       ++CurPtr;
488       const char *NumStart = CurPtr;
489       while (CurPtr[0] == '0' || CurPtr[0] == '1')
490         ++CurPtr;
491 
492       // Requires at least one binary digit.
493       if (CurPtr == NumStart)
494         return ReturnError(CurPtr-2, "Invalid binary number");
495       CurIntVal = strtoll(NumStart, nullptr, 2);
496       return tgtok::BinaryIntVal;
497     }
498   }
499 
500   // Check for a sign without a digit.
501   if (!isdigit(CurPtr[0])) {
502     if (CurPtr[-1] == '-')
503       return tgtok::minus;
504     else if (CurPtr[-1] == '+')
505       return tgtok::plus;
506   }
507 
508   while (isdigit(CurPtr[0]))
509     ++CurPtr;
510   CurIntVal = strtoll(TokStart, nullptr, 10);
511   return tgtok::IntVal;
512 }
513 
514 /// LexBracket - We just read '['.  If this is a code block, return it,
515 /// otherwise return the bracket.  Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
516 tgtok::TokKind TGLexer::LexBracket() {
517   if (CurPtr[0] != '{')
518     return tgtok::l_square;
519   ++CurPtr;
520   const char *CodeStart = CurPtr;
521   while (true) {
522     int Char = getNextChar();
523     if (Char == EOF) break;
524 
525     if (Char != '}') continue;
526 
527     Char = getNextChar();
528     if (Char == EOF) break;
529     if (Char == ']') {
530       CurStrVal.assign(CodeStart, CurPtr-2);
531       return tgtok::CodeFragment;
532     }
533   }
534 
535   return ReturnError(CodeStart-2, "Unterminated Code Block");
536 }
537 
538 /// LexExclaim - Lex '!' and '![a-zA-Z]+'.
539 tgtok::TokKind TGLexer::LexExclaim() {
540   if (!isalpha(*CurPtr))
541     return ReturnError(CurPtr - 1, "Invalid \"!operator\"");
542 
543   const char *Start = CurPtr++;
544   while (isalpha(*CurPtr))
545     ++CurPtr;
546 
547   // Check to see which operator this is.
548   tgtok::TokKind Kind =
549     StringSwitch<tgtok::TokKind>(StringRef(Start, CurPtr - Start))
550     .Case("eq", tgtok::XEq)
551     .Case("ne", tgtok::XNe)
552     .Case("le", tgtok::XLe)
553     .Case("lt", tgtok::XLt)
554     .Case("ge", tgtok::XGe)
555     .Case("gt", tgtok::XGt)
556     .Case("if", tgtok::XIf)
557     .Case("cond", tgtok::XCond)
558     .Case("isa", tgtok::XIsA)
559     .Case("head", tgtok::XHead)
560     .Case("tail", tgtok::XTail)
561     .Case("size", tgtok::XSize)
562     .Case("con", tgtok::XConcat)
563     .Case("dag", tgtok::XDag)
564     .Case("add", tgtok::XADD)
565     .Case("sub", tgtok::XSUB)
566     .Case("mul", tgtok::XMUL)
567     .Case("not", tgtok::XNOT)
568     .Case("and", tgtok::XAND)
569     .Case("or", tgtok::XOR)
570     .Case("xor", tgtok::XXOR)
571     .Case("shl", tgtok::XSHL)
572     .Case("sra", tgtok::XSRA)
573     .Case("srl", tgtok::XSRL)
574     .Case("cast", tgtok::XCast)
575     .Case("empty", tgtok::XEmpty)
576     .Case("subst", tgtok::XSubst)
577     .Case("foldl", tgtok::XFoldl)
578     .Case("foreach", tgtok::XForEach)
579     .Case("listconcat", tgtok::XListConcat)
580     .Case("listsplat", tgtok::XListSplat)
581     .Case("strconcat", tgtok::XStrConcat)
582     .Cases("setdagop", "setop", tgtok::XSetDagOp) // !setop is deprecated.
583     .Cases("getdagop", "getop", tgtok::XGetDagOp) // !getop is deprecated.
584     .Default(tgtok::Error);
585 
586   return Kind != tgtok::Error ? Kind : ReturnError(Start-1, "Unknown operator");
587 }
588 
589 bool TGLexer::prepExitInclude(bool IncludeStackMustBeEmpty) {
590   // Report an error, if preprocessor control stack for the current
591   // file is not empty.
592   if (!PrepIncludeStack.back()->empty()) {
593     prepReportPreprocessorStackError();
594 
595     return false;
596   }
597 
598   // Pop the preprocessing controls from the include stack.
599   if (PrepIncludeStack.empty()) {
600     PrintFatalError("Preprocessor include stack is empty");
601   }
602 
603   PrepIncludeStack.pop_back();
604 
605   if (IncludeStackMustBeEmpty) {
606     if (!PrepIncludeStack.empty())
607       PrintFatalError("Preprocessor include stack is not empty");
608   } else {
609     if (PrepIncludeStack.empty())
610       PrintFatalError("Preprocessor include stack is empty");
611   }
612 
613   return true;
614 }
615 
616 tgtok::TokKind TGLexer::prepIsDirective() const {
617   for (unsigned ID = 0; ID < llvm::array_lengthof(PreprocessorDirs); ++ID) {
618     int NextChar = *CurPtr;
619     bool Match = true;
620     unsigned I = 0;
621     for (; I < strlen(PreprocessorDirs[ID].Word); ++I) {
622       if (NextChar != PreprocessorDirs[ID].Word[I]) {
623         Match = false;
624         break;
625       }
626 
627       NextChar = peekNextChar(I + 1);
628     }
629 
630     // Check for whitespace after the directive.  If there is no whitespace,
631     // then we do not recognize it as a preprocessing directive.
632     if (Match) {
633       tgtok::TokKind Kind = PreprocessorDirs[ID].Kind;
634 
635       // New line and EOF may follow only #else/#endif.  It will be reported
636       // as an error for #ifdef/#define after the call to prepLexMacroName().
637       if (NextChar == ' ' || NextChar == '\t' || NextChar == EOF ||
638           NextChar == '\n' ||
639           // It looks like TableGen does not support '\r' as the actual
640           // carriage return, e.g. getNextChar() treats a single '\r'
641           // as '\n'.  So we do the same here.
642           NextChar == '\r')
643         return Kind;
644 
645       // Allow comments after some directives, e.g.:
646       //     #else// OR #else/**/
647       //     #endif// OR #endif/**/
648       //
649       // Note that we do allow comments after #ifdef/#define here, e.g.
650       //     #ifdef/**/ AND #ifdef//
651       //     #define/**/ AND #define//
652       //
653       // These cases will be reported as incorrect after calling
654       // prepLexMacroName().  We could have supported C-style comments
655       // after #ifdef/#define, but this would complicate the code
656       // for little benefit.
657       if (NextChar == '/') {
658         NextChar = peekNextChar(I + 1);
659 
660         if (NextChar == '*' || NextChar == '/')
661           return Kind;
662 
663         // Pretend that we do not recognize the directive.
664       }
665     }
666   }
667 
668   return tgtok::Error;
669 }
670 
671 bool TGLexer::prepEatPreprocessorDirective(tgtok::TokKind Kind) {
672   TokStart = CurPtr;
673 
674   for (unsigned ID = 0; ID < llvm::array_lengthof(PreprocessorDirs); ++ID)
675     if (PreprocessorDirs[ID].Kind == Kind) {
676       // Advance CurPtr to the end of the preprocessing word.
677       CurPtr += strlen(PreprocessorDirs[ID].Word);
678       return true;
679     }
680 
681   PrintFatalError("Unsupported preprocessing token in "
682                   "prepEatPreprocessorDirective()");
683   return false;
684 }
685 
686 tgtok::TokKind TGLexer::lexPreprocessor(
687     tgtok::TokKind Kind, bool ReturnNextLiveToken) {
688 
689   // We must be looking at a preprocessing directive.  Eat it!
690   if (!prepEatPreprocessorDirective(Kind))
691     PrintFatalError("lexPreprocessor() called for unknown "
692                     "preprocessor directive");
693 
694   if (Kind == tgtok::Ifdef || Kind == tgtok::Ifndef) {
695     StringRef MacroName = prepLexMacroName();
696     StringRef IfTokName = Kind == tgtok::Ifdef ? "#ifdef" : "#ifndef";
697     if (MacroName.empty())
698       return ReturnError(TokStart, "Expected macro name after " + IfTokName);
699 
700     bool MacroIsDefined = DefinedMacros.count(MacroName) != 0;
701 
702     // Canonicalize ifndef to ifdef equivalent
703     if (Kind == tgtok::Ifndef) {
704       MacroIsDefined = !MacroIsDefined;
705       Kind = tgtok::Ifdef;
706     }
707 
708     // Regardless of whether we are processing tokens or not,
709     // we put the #ifdef control on stack.
710     PrepIncludeStack.back()->push_back(
711         {Kind, MacroIsDefined, SMLoc::getFromPointer(TokStart)});
712 
713     if (!prepSkipDirectiveEnd())
714       return ReturnError(CurPtr, "Only comments are supported after " +
715                                      IfTokName + " NAME");
716 
717     // If we were not processing tokens before this #ifdef,
718     // then just return back to the lines skipping code.
719     if (!ReturnNextLiveToken)
720       return Kind;
721 
722     // If we were processing tokens before this #ifdef,
723     // and the macro is defined, then just return the next token.
724     if (MacroIsDefined)
725       return LexToken();
726 
727     // We were processing tokens before this #ifdef, and the macro
728     // is not defined, so we have to start skipping the lines.
729     // If the skipping is successful, it will return the token following
730     // either #else or #endif corresponding to this #ifdef.
731     if (prepSkipRegion(ReturnNextLiveToken))
732       return LexToken();
733 
734     return tgtok::Error;
735   } else if (Kind == tgtok::Else) {
736     // Check if this #else is correct before calling prepSkipDirectiveEnd(),
737     // which will move CurPtr away from the beginning of #else.
738     if (PrepIncludeStack.back()->empty())
739       return ReturnError(TokStart, "#else without #ifdef or #ifndef");
740 
741     PreprocessorControlDesc IfdefEntry = PrepIncludeStack.back()->back();
742 
743     if (IfdefEntry.Kind != tgtok::Ifdef) {
744       PrintError(TokStart, "double #else");
745       return ReturnError(IfdefEntry.SrcPos, "Previous #else is here");
746     }
747 
748     // Replace the corresponding #ifdef's control with its negation
749     // on the control stack.
750     PrepIncludeStack.back()->pop_back();
751     PrepIncludeStack.back()->push_back(
752         {Kind, !IfdefEntry.IsDefined, SMLoc::getFromPointer(TokStart)});
753 
754     if (!prepSkipDirectiveEnd())
755       return ReturnError(CurPtr, "Only comments are supported after #else");
756 
757     // If we were processing tokens before this #else,
758     // we have to start skipping lines until the matching #endif.
759     if (ReturnNextLiveToken) {
760       if (prepSkipRegion(ReturnNextLiveToken))
761         return LexToken();
762 
763       return tgtok::Error;
764     }
765 
766     // Return to the lines skipping code.
767     return Kind;
768   } else if (Kind == tgtok::Endif) {
769     // Check if this #endif is correct before calling prepSkipDirectiveEnd(),
770     // which will move CurPtr away from the beginning of #endif.
771     if (PrepIncludeStack.back()->empty())
772       return ReturnError(TokStart, "#endif without #ifdef");
773 
774     auto &IfdefOrElseEntry = PrepIncludeStack.back()->back();
775 
776     if (IfdefOrElseEntry.Kind != tgtok::Ifdef &&
777         IfdefOrElseEntry.Kind != tgtok::Else) {
778       PrintFatalError("Invalid preprocessor control on the stack");
779       return tgtok::Error;
780     }
781 
782     if (!prepSkipDirectiveEnd())
783       return ReturnError(CurPtr, "Only comments are supported after #endif");
784 
785     PrepIncludeStack.back()->pop_back();
786 
787     // If we were processing tokens before this #endif, then
788     // we should continue it.
789     if (ReturnNextLiveToken) {
790       return LexToken();
791     }
792 
793     // Return to the lines skipping code.
794     return Kind;
795   } else if (Kind == tgtok::Define) {
796     StringRef MacroName = prepLexMacroName();
797     if (MacroName.empty())
798       return ReturnError(TokStart, "Expected macro name after #define");
799 
800     if (!DefinedMacros.insert(MacroName).second)
801       PrintWarning(getLoc(),
802                    "Duplicate definition of macro: " + Twine(MacroName));
803 
804     if (!prepSkipDirectiveEnd())
805       return ReturnError(CurPtr,
806                          "Only comments are supported after #define NAME");
807 
808     if (!ReturnNextLiveToken) {
809       PrintFatalError("#define must be ignored during the lines skipping");
810       return tgtok::Error;
811     }
812 
813     return LexToken();
814   }
815 
816   PrintFatalError("Preprocessing directive is not supported");
817   return tgtok::Error;
818 }
819 
820 bool TGLexer::prepSkipRegion(bool MustNeverBeFalse) {
821   if (!MustNeverBeFalse)
822     PrintFatalError("Invalid recursion.");
823 
824   do {
825     // Skip all symbols to the line end.
826     prepSkipToLineEnd();
827 
828     // Find the first non-whitespace symbol in the next line(s).
829     if (!prepSkipLineBegin())
830       return false;
831 
832     // If the first non-blank/comment symbol on the line is '#',
833     // it may be a start of preprocessing directive.
834     //
835     // If it is not '#' just go to the next line.
836     if (*CurPtr == '#')
837       ++CurPtr;
838     else
839       continue;
840 
841     tgtok::TokKind Kind = prepIsDirective();
842 
843     // If we did not find a preprocessing directive or it is #define,
844     // then just skip to the next line.  We do not have to do anything
845     // for #define in the line-skipping mode.
846     if (Kind == tgtok::Error || Kind == tgtok::Define)
847       continue;
848 
849     tgtok::TokKind ProcessedKind = lexPreprocessor(Kind, false);
850 
851     // If lexPreprocessor() encountered an error during lexing this
852     // preprocessor idiom, then return false to the calling lexPreprocessor().
853     // This will force tgtok::Error to be returned to the tokens processing.
854     if (ProcessedKind == tgtok::Error)
855       return false;
856 
857     if (Kind != ProcessedKind)
858       PrintFatalError("prepIsDirective() and lexPreprocessor() "
859                       "returned different token kinds");
860 
861     // If this preprocessing directive enables tokens processing,
862     // then return to the lexPreprocessor() and get to the next token.
863     // We can move from line-skipping mode to processing tokens only
864     // due to #else or #endif.
865     if (prepIsProcessingEnabled()) {
866       if (Kind != tgtok::Else && Kind != tgtok::Endif) {
867         PrintFatalError("Tokens processing was enabled by an unexpected "
868                         "preprocessing directive");
869         return false;
870       }
871 
872       return true;
873     }
874   } while (CurPtr != CurBuf.end());
875 
876   // We have reached the end of the file, but never left the lines-skipping
877   // mode.  This means there is no matching #endif.
878   prepReportPreprocessorStackError();
879   return false;
880 }
881 
882 StringRef TGLexer::prepLexMacroName() {
883   // Skip whitespaces between the preprocessing directive and the macro name.
884   while (*CurPtr == ' ' || *CurPtr == '\t')
885     ++CurPtr;
886 
887   TokStart = CurPtr;
888   // Macro names start with [a-zA-Z_].
889   if (*CurPtr != '_' && !isalpha(*CurPtr))
890     return "";
891 
892   // Match the rest of the identifier regex: [0-9a-zA-Z_]*
893   while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
894     ++CurPtr;
895 
896   return StringRef(TokStart, CurPtr - TokStart);
897 }
898 
899 bool TGLexer::prepSkipLineBegin() {
900   while (CurPtr != CurBuf.end()) {
901     switch (*CurPtr) {
902     case ' ':
903     case '\t':
904     case '\n':
905     case '\r':
906       break;
907 
908     case '/': {
909       int NextChar = peekNextChar(1);
910       if (NextChar == '*') {
911         // Skip C-style comment.
912         // Note that we do not care about skipping the C++-style comments.
913         // If the line contains "//", it may not contain any processable
914         // preprocessing directive.  Just return CurPtr pointing to
915         // the first '/' in this case.  We also do not care about
916         // incorrect symbols after the first '/' - we are in lines-skipping
917         // mode, so incorrect code is allowed to some extent.
918 
919         // Set TokStart to the beginning of the comment to enable proper
920         // diagnostic printing in case of error in SkipCComment().
921         TokStart = CurPtr;
922 
923         // CurPtr must point to '*' before call to SkipCComment().
924         ++CurPtr;
925         if (SkipCComment())
926           return false;
927       } else {
928         // CurPtr points to the non-whitespace '/'.
929         return true;
930       }
931 
932       // We must not increment CurPtr after the comment was lexed.
933       continue;
934     }
935 
936     default:
937       return true;
938     }
939 
940     ++CurPtr;
941   }
942 
943   // We have reached the end of the file.  Return to the lines skipping
944   // code, and allow it to handle the EOF as needed.
945   return true;
946 }
947 
948 bool TGLexer::prepSkipDirectiveEnd() {
949   while (CurPtr != CurBuf.end()) {
950     switch (*CurPtr) {
951     case ' ':
952     case '\t':
953       break;
954 
955     case '\n':
956     case '\r':
957       return true;
958 
959     case '/': {
960       int NextChar = peekNextChar(1);
961       if (NextChar == '/') {
962         // Skip C++-style comment.
963         // We may just return true now, but let's skip to the line/buffer end
964         // to simplify the method specification.
965         ++CurPtr;
966         SkipBCPLComment();
967       } else if (NextChar == '*') {
968         // When we are skipping C-style comment at the end of a preprocessing
969         // directive, we can skip several lines.  If any meaningful TD token
970         // follows the end of the C-style comment on the same line, it will
971         // be considered as an invalid usage of TD token.
972         // For example, we want to forbid usages like this one:
973         //     #define MACRO class Class {}
974         // But with C-style comments we also disallow the following:
975         //     #define MACRO /* This macro is used
976         //                      to ... */ class Class {}
977         // One can argue that this should be allowed, but it does not seem
978         // to be worth of the complication.  Moreover, this matches
979         // the C preprocessor behavior.
980 
981         // Set TokStart to the beginning of the comment to enable proper
982         // diagnostic printer in case of error in SkipCComment().
983         TokStart = CurPtr;
984         ++CurPtr;
985         if (SkipCComment())
986           return false;
987       } else {
988         TokStart = CurPtr;
989         PrintError(CurPtr, "Unexpected character");
990         return false;
991       }
992 
993       // We must not increment CurPtr after the comment was lexed.
994       continue;
995     }
996 
997     default:
998       // Do not allow any non-whitespaces after the directive.
999       TokStart = CurPtr;
1000       return false;
1001     }
1002 
1003     ++CurPtr;
1004   }
1005 
1006   return true;
1007 }
1008 
1009 void TGLexer::prepSkipToLineEnd() {
1010   while (*CurPtr != '\n' && *CurPtr != '\r' && CurPtr != CurBuf.end())
1011     ++CurPtr;
1012 }
1013 
1014 bool TGLexer::prepIsProcessingEnabled() {
1015   for (auto I = PrepIncludeStack.back()->rbegin(),
1016             E = PrepIncludeStack.back()->rend();
1017        I != E; ++I) {
1018     if (!I->IsDefined)
1019       return false;
1020   }
1021 
1022   return true;
1023 }
1024 
1025 void TGLexer::prepReportPreprocessorStackError() {
1026   if (PrepIncludeStack.back()->empty())
1027     PrintFatalError("prepReportPreprocessorStackError() called with "
1028                     "empty control stack");
1029 
1030   auto &PrepControl = PrepIncludeStack.back()->back();
1031   PrintError(CurBuf.end(), "Reached EOF without matching #endif");
1032   PrintError(PrepControl.SrcPos, "The latest preprocessor control is here");
1033 
1034   TokStart = CurPtr;
1035 }
1036