17 #include "llvm/Config/config.h"
38 } PreprocessorDirs[] = {
50 CurPtr = CurBuf.
begin();
54 PrepIncludeStack.push_back(
55 std::make_unique<std::vector<PreprocessorControlDesc>>());
58 for (
const std::string &MacroName : Macros)
59 DefinedMacros.
insert(MacroName);
77 bool TGLexer::processEOF() {
79 if (ParentIncludeLoc !=
SMLoc()) {
84 if (!prepExitInclude(
false))
100 prepExitInclude(
true);
104 int TGLexer::getNextChar() {
105 char CurChar = *CurPtr++;
108 return (
unsigned char)CurChar;
113 if (CurPtr - 1 == CurBuf.
end()) {
118 "NUL character is invalid in source; treated as space");
127 if ((*CurPtr ==
'\n' || (*CurPtr ==
'\r')) &&
134 int TGLexer::peekNextChar(
int Index)
const {
135 return *(CurPtr +
Index);
141 int CurChar = getNextChar();
146 if (isalpha(CurChar) || CurChar ==
'_')
147 return LexIdentifier();
150 return ReturnError(TokStart,
"Unexpected character");
176 if (FileOrLineStart) {
179 return lexPreprocessor(
Kind);
187 if (peekNextChar(0) ==
'.') {
189 if (peekNextChar(0) ==
'.') {
193 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>>());
420 void TGLexer::SkipBCPLComment() {
428 bool TGLexer::SkipCComment() {
430 unsigned CommentDepth = 1;
433 int CurChar = getNextChar();
436 PrintError(TokStart,
"Unterminated comment!");
440 if (CurPtr[0] !=
'/')
break;
443 if (--CommentDepth == 0)
448 if (CurPtr[0] !=
'*')
break;
461 if (CurPtr[-1] ==
'0') {
462 if (CurPtr[0] ==
'x') {
464 const char *NumStart = CurPtr;
465 while (isxdigit(CurPtr[0]))
469 if (CurPtr == NumStart)
470 return ReturnError(TokStart,
"Invalid hexadecimal number");
473 CurIntVal = strtoll(NumStart,
nullptr, 16);
475 return ReturnError(TokStart,
"Invalid hexadecimal number");
476 if (errno == ERANGE) {
478 CurIntVal = (int64_t)strtoull(NumStart,
nullptr, 16);
480 return ReturnError(TokStart,
"Invalid hexadecimal number");
482 return ReturnError(TokStart,
"Hexadecimal number out of range");
485 }
else if (CurPtr[0] ==
'b') {
487 const char *NumStart = CurPtr;
488 while (CurPtr[0] ==
'0' || CurPtr[0] ==
'1')
492 if (CurPtr == NumStart)
493 return ReturnError(CurPtr-2,
"Invalid binary number");
494 CurIntVal = strtoll(NumStart,
nullptr, 2);
500 if (!isdigit(CurPtr[0])) {
501 if (CurPtr[-1] ==
'-')
503 else if (CurPtr[-1] ==
'+')
507 while (isdigit(CurPtr[0]))
509 CurIntVal = strtoll(TokStart,
nullptr, 10);
516 if (CurPtr[0] !=
'{')
519 const char *CodeStart = CurPtr;
521 int Char = getNextChar();
522 if (Char == EOF)
break;
524 if (Char !=
'}')
continue;
526 Char = getNextChar();
527 if (Char == EOF)
break;
529 CurStrVal.assign(CodeStart, CurPtr-2);
534 return ReturnError(CodeStart - 2,
"Unterminated code block");
539 if (!isalpha(*CurPtr))
540 return ReturnError(CurPtr - 1,
"Invalid \"!operator\"");
542 const char *Start = CurPtr++;
543 while (isalpha(*CurPtr))
593 bool TGLexer::prepExitInclude(
bool IncludeStackMustBeEmpty) {
596 if (!PrepIncludeStack.back()->empty()) {
597 prepReportPreprocessorStackError();
603 if (PrepIncludeStack.empty()) {
607 PrepIncludeStack.pop_back();
609 if (IncludeStackMustBeEmpty) {
610 if (!PrepIncludeStack.empty())
613 if (PrepIncludeStack.empty())
621 for (
const auto &
PD : PreprocessorDirs) {
622 int NextChar = *CurPtr;
625 for (;
I < strlen(
PD.Word); ++
I) {
626 if (NextChar !=
PD.Word[
I]) {
631 NextChar = peekNextChar(
I + 1);
641 if (NextChar ==
' ' || NextChar ==
'\t' || NextChar == EOF ||
661 if (NextChar ==
'/') {
662 NextChar = peekNextChar(
I + 1);
664 if (NextChar ==
'*' || NextChar ==
'/')
678 for (
const auto &
PD : PreprocessorDirs)
681 CurPtr += strlen(
PD.Word);
686 "prepEatPreprocessorDirective()");
694 if (!prepEatPreprocessorDirective(
Kind))
696 "preprocessor directive");
699 StringRef MacroName = prepLexMacroName();
701 if (MacroName.
empty())
702 return ReturnError(TokStart,
"Expected macro name after " + IfTokName);
704 bool MacroIsDefined = DefinedMacros.
count(MacroName) != 0;
708 MacroIsDefined = !MacroIsDefined;
714 PrepIncludeStack.back()->push_back(
717 if (!prepSkipDirectiveEnd())
718 return ReturnError(CurPtr,
"Only comments are supported after " +
719 IfTokName +
" NAME");
723 if (!ReturnNextLiveToken)
735 if (prepSkipRegion(ReturnNextLiveToken))
742 if (PrepIncludeStack.back()->empty())
743 return ReturnError(TokStart,
"#else without #ifdef or #ifndef");
745 PreprocessorControlDesc IfdefEntry = PrepIncludeStack.back()->back();
749 return ReturnError(IfdefEntry.SrcPos,
"Previous #else is here");
754 PrepIncludeStack.back()->pop_back();
755 PrepIncludeStack.back()->push_back(
758 if (!prepSkipDirectiveEnd())
759 return ReturnError(CurPtr,
"Only comments are supported after #else");
763 if (ReturnNextLiveToken) {
764 if (prepSkipRegion(ReturnNextLiveToken))
775 if (PrepIncludeStack.back()->empty())
776 return ReturnError(TokStart,
"#endif without #ifdef");
778 auto &IfdefOrElseEntry = PrepIncludeStack.back()->back();
786 if (!prepSkipDirectiveEnd())
787 return ReturnError(CurPtr,
"Only comments are supported after #endif");
789 PrepIncludeStack.back()->pop_back();
793 if (ReturnNextLiveToken) {
800 StringRef MacroName = prepLexMacroName();
801 if (MacroName.
empty())
802 return ReturnError(TokStart,
"Expected macro name after #define");
804 if (!DefinedMacros.
insert(MacroName).second)
806 "Duplicate definition of macro: " +
Twine(MacroName));
808 if (!prepSkipDirectiveEnd())
809 return ReturnError(CurPtr,
810 "Only comments are supported after #define NAME");
812 if (!ReturnNextLiveToken) {
824 bool TGLexer::prepSkipRegion(
bool MustNeverBeFalse) {
825 if (!MustNeverBeFalse)
833 if (!prepSkipLineBegin())
861 if (
Kind != ProcessedKind)
863 "returned different token kinds");
869 if (prepIsProcessingEnabled()) {
872 "preprocessing directive");
878 }
while (CurPtr != CurBuf.
end());
882 prepReportPreprocessorStackError();
888 while (*CurPtr ==
' ' || *CurPtr ==
'\t')
893 if (*CurPtr !=
'_' && !isalpha(*CurPtr))
897 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr ==
'_')
900 return StringRef(TokStart, CurPtr - TokStart);
903 bool TGLexer::prepSkipLineBegin() {
904 while (CurPtr != CurBuf.
end()) {
913 int NextChar = peekNextChar(1);
914 if (NextChar ==
'*') {
952 bool TGLexer::prepSkipDirectiveEnd() {
953 while (CurPtr != CurBuf.
end()) {
964 int NextChar = peekNextChar(1);
965 if (NextChar ==
'/') {
971 }
else if (NextChar ==
'*') {
1013 void TGLexer::prepSkipToLineEnd() {
1014 while (*CurPtr !=
'\n' && *CurPtr !=
'\r' && CurPtr != CurBuf.
end())
1018 bool TGLexer::prepIsProcessingEnabled() {
1019 for (
const PreprocessorControlDesc &
I :
1027 void TGLexer::prepReportPreprocessorStackError() {
1028 if (PrepIncludeStack.back()->empty())
1030 "empty control stack");
1032 auto &PrepControl = PrepIncludeStack.back()->back();
1033 PrintError(CurBuf.
end(),
"Reached EOF without matching #endif");
1034 PrintError(PrepControl.SrcPos,
"The latest preprocessor control is here");