17 #include "llvm/Config/config.h"
38 } PreprocessorDirs[] = {
50 CurPtr = CurBuf.
begin();
54 PrepIncludeStack.push_back(
55 std::make_unique<std::vector<PreprocessorControlDesc>>());
59 [
this](
const std::string &MacroName) {
60 DefinedMacros.insert(MacroName);
79 bool TGLexer::processEOF() {
81 if (ParentIncludeLoc !=
SMLoc()) {
86 if (!prepExitInclude(
false))
102 prepExitInclude(
true);
106 int TGLexer::getNextChar() {
107 char CurChar = *CurPtr++;
110 return (
unsigned char)CurChar;
114 if (CurPtr-1 != CurBuf.
end())
126 if ((*CurPtr ==
'\n' || (*CurPtr ==
'\r')) &&
133 int TGLexer::peekNextChar(
int Index)
const {
134 return *(CurPtr +
Index);
140 int CurChar = getNextChar();
145 if (isalpha(CurChar) || CurChar ==
'_')
146 return LexIdentifier();
149 return ReturnError(TokStart,
"Unexpected character");
175 if (FileOrLineStart) {
178 return lexPreprocessor(
Kind);
186 if (peekNextChar(0) ==
'.') {
188 if (peekNextChar(0) ==
'.') {
192 return ReturnError(TokStart,
"Invalid '..' punctuation");
204 return LexToken(FileOrLineStart);
207 return LexToken(
true);
213 else if (*CurPtr ==
'*') {
217 return ReturnError(TokStart,
"Unexpected character");
218 return LexToken(FileOrLineStart);
220 case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
221 case '7':
case '8':
case '9': {
223 if (isdigit(CurChar)) {
229 NextChar = peekNextChar(
i++);
230 }
while (isdigit(NextChar));
232 if (NextChar ==
'x' || NextChar ==
'b') {
235 int NextNextChar = peekNextChar(
i);
236 switch (NextNextChar) {
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':
254 if (isalpha(NextChar) || NextChar ==
'_')
255 return LexIdentifier();
259 case '"':
return LexString();
260 case '$':
return LexVarName();
261 case '[':
return LexBracket();
262 case '!':
return LexExclaim();
268 const char *StrStart = CurPtr;
272 while (*CurPtr !=
'"') {
274 if (*CurPtr == 0 && CurPtr == CurBuf.
end())
275 return ReturnError(StrStart,
"End of file in string literal");
277 if (*CurPtr ==
'\n' || *CurPtr ==
'\r')
278 return ReturnError(StrStart,
"End of line in string literal");
280 if (*CurPtr !=
'\\') {
281 CurStrVal += *CurPtr++;
288 case '\\':
case '\'':
case '"':
290 CurStrVal += *CurPtr++;
303 return ReturnError(CurPtr,
"escaped newlines not supported in tblgen");
307 if (CurPtr == CurBuf.
end())
308 return ReturnError(StrStart,
"End of file in string literal");
311 return ReturnError(CurPtr,
"invalid escape in string literal");
320 if (!isalpha(CurPtr[0]) && CurPtr[0] !=
'_')
321 return ReturnError(TokStart,
"Invalid variable name");
324 const char *VarNameStart = CurPtr++;
326 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr ==
'_')
329 CurStrVal.assign(VarNameStart, CurPtr);
335 const char *IdentStart = TokStart;
338 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr ==
'_')
342 StringRef Str(IdentStart, CurPtr-IdentStart);
377 CurStrVal.assign(Str.begin(), Str.end());
388 bool TGLexer::LexInclude() {
398 std::string Filename = CurStrVal;
399 std::string IncludedFile;
408 Dependencies.insert(IncludedFile);
411 CurPtr = CurBuf.
begin();
413 PrepIncludeStack.push_back(
414 std::make_unique<std::vector<PreprocessorControlDesc>>());
418 void TGLexer::SkipBCPLComment() {
427 if (CurPtr == CurBuf.
end())
438 bool TGLexer::SkipCComment() {
440 unsigned CommentDepth = 1;
443 int CurChar = getNextChar();
446 PrintError(TokStart,
"Unterminated comment!");
450 if (CurPtr[0] !=
'/')
break;
453 if (--CommentDepth == 0)
458 if (CurPtr[0] !=
'*')
break;
471 if (CurPtr[-1] ==
'0') {
472 if (CurPtr[0] ==
'x') {
474 const char *NumStart = CurPtr;
475 while (isxdigit(CurPtr[0]))
479 if (CurPtr == NumStart)
480 return ReturnError(TokStart,
"Invalid hexadecimal number");
483 CurIntVal = strtoll(NumStart,
nullptr, 16);
485 return ReturnError(TokStart,
"Invalid hexadecimal number");
486 if (errno == ERANGE) {
488 CurIntVal = (int64_t)strtoull(NumStart,
nullptr, 16);
490 return ReturnError(TokStart,
"Invalid hexadecimal number");
492 return ReturnError(TokStart,
"Hexadecimal number out of range");
495 }
else if (CurPtr[0] ==
'b') {
497 const char *NumStart = CurPtr;
498 while (CurPtr[0] ==
'0' || CurPtr[0] ==
'1')
502 if (CurPtr == NumStart)
503 return ReturnError(CurPtr-2,
"Invalid binary number");
504 CurIntVal = strtoll(NumStart,
nullptr, 2);
510 if (!isdigit(CurPtr[0])) {
511 if (CurPtr[-1] ==
'-')
513 else if (CurPtr[-1] ==
'+')
517 while (isdigit(CurPtr[0]))
519 CurIntVal = strtoll(TokStart,
nullptr, 10);
526 if (CurPtr[0] !=
'{')
529 const char *CodeStart = CurPtr;
531 int Char = getNextChar();
532 if (Char == EOF)
break;
534 if (Char !=
'}')
continue;
536 Char = getNextChar();
537 if (Char == EOF)
break;
539 CurStrVal.assign(CodeStart, CurPtr-2);
544 return ReturnError(CodeStart - 2,
"Unterminated code block");
549 if (!isalpha(*CurPtr))
550 return ReturnError(CurPtr - 1,
"Invalid \"!operator\"");
552 const char *Start = CurPtr++;
553 while (isalpha(*CurPtr))
601 bool TGLexer::prepExitInclude(
bool IncludeStackMustBeEmpty) {
604 if (!PrepIncludeStack.back()->empty()) {
605 prepReportPreprocessorStackError();
611 if (PrepIncludeStack.empty()) {
615 PrepIncludeStack.pop_back();
617 if (IncludeStackMustBeEmpty) {
618 if (!PrepIncludeStack.empty())
621 if (PrepIncludeStack.empty())
630 int NextChar = *CurPtr;
633 for (;
I < strlen(PreprocessorDirs[
ID].
Word); ++
I) {
634 if (NextChar != PreprocessorDirs[
ID].
Word[
I]) {
639 NextChar = peekNextChar(
I + 1);
649 if (NextChar ==
' ' || NextChar ==
'\t' || NextChar == EOF ||
669 if (NextChar ==
'/') {
670 NextChar = peekNextChar(
I + 1);
672 if (NextChar ==
'*' || NextChar ==
'/')
689 CurPtr += strlen(PreprocessorDirs[
ID].
Word);
694 "prepEatPreprocessorDirective()");
702 if (!prepEatPreprocessorDirective(
Kind))
704 "preprocessor directive");
707 StringRef MacroName = prepLexMacroName();
709 if (MacroName.
empty())
710 return ReturnError(TokStart,
"Expected macro name after " + IfTokName);
712 bool MacroIsDefined = DefinedMacros.
count(MacroName) != 0;
716 MacroIsDefined = !MacroIsDefined;
722 PrepIncludeStack.back()->push_back(
725 if (!prepSkipDirectiveEnd())
726 return ReturnError(CurPtr,
"Only comments are supported after " +
727 IfTokName +
" NAME");
731 if (!ReturnNextLiveToken)
743 if (prepSkipRegion(ReturnNextLiveToken))
750 if (PrepIncludeStack.back()->empty())
751 return ReturnError(TokStart,
"#else without #ifdef or #ifndef");
753 PreprocessorControlDesc IfdefEntry = PrepIncludeStack.back()->back();
757 return ReturnError(IfdefEntry.SrcPos,
"Previous #else is here");
762 PrepIncludeStack.back()->pop_back();
763 PrepIncludeStack.back()->push_back(
766 if (!prepSkipDirectiveEnd())
767 return ReturnError(CurPtr,
"Only comments are supported after #else");
771 if (ReturnNextLiveToken) {
772 if (prepSkipRegion(ReturnNextLiveToken))
783 if (PrepIncludeStack.back()->empty())
784 return ReturnError(TokStart,
"#endif without #ifdef");
786 auto &IfdefOrElseEntry = PrepIncludeStack.back()->back();
794 if (!prepSkipDirectiveEnd())
795 return ReturnError(CurPtr,
"Only comments are supported after #endif");
797 PrepIncludeStack.back()->pop_back();
801 if (ReturnNextLiveToken) {
808 StringRef MacroName = prepLexMacroName();
809 if (MacroName.
empty())
810 return ReturnError(TokStart,
"Expected macro name after #define");
812 if (!DefinedMacros.
insert(MacroName).second)
814 "Duplicate definition of macro: " +
Twine(MacroName));
816 if (!prepSkipDirectiveEnd())
817 return ReturnError(CurPtr,
818 "Only comments are supported after #define NAME");
820 if (!ReturnNextLiveToken) {
832 bool TGLexer::prepSkipRegion(
bool MustNeverBeFalse) {
833 if (!MustNeverBeFalse)
841 if (!prepSkipLineBegin())
869 if (
Kind != ProcessedKind)
871 "returned different token kinds");
877 if (prepIsProcessingEnabled()) {
880 "preprocessing directive");
886 }
while (CurPtr != CurBuf.
end());
890 prepReportPreprocessorStackError();
896 while (*CurPtr ==
' ' || *CurPtr ==
'\t')
901 if (*CurPtr !=
'_' && !isalpha(*CurPtr))
905 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr ==
'_')
908 return StringRef(TokStart, CurPtr - TokStart);
911 bool TGLexer::prepSkipLineBegin() {
912 while (CurPtr != CurBuf.
end()) {
921 int NextChar = peekNextChar(1);
922 if (NextChar ==
'*') {
960 bool TGLexer::prepSkipDirectiveEnd() {
961 while (CurPtr != CurBuf.
end()) {
972 int NextChar = peekNextChar(1);
973 if (NextChar ==
'/') {
979 }
else if (NextChar ==
'*') {
1021 void TGLexer::prepSkipToLineEnd() {
1022 while (*CurPtr !=
'\n' && *CurPtr !=
'\r' && CurPtr != CurBuf.
end())
1026 bool TGLexer::prepIsProcessingEnabled() {
1027 for (
auto I = PrepIncludeStack.back()->rbegin(),
1028 E = PrepIncludeStack.back()->rend();
1037 void TGLexer::prepReportPreprocessorStackError() {
1038 if (PrepIncludeStack.back()->empty())
1040 "empty control stack");
1042 auto &PrepControl = PrepIncludeStack.back()->back();
1043 PrintError(CurBuf.
end(),
"Reached EOF without matching #endif");
1044 PrintError(PrepControl.SrcPos,
"The latest preprocessor control is here");