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;
115 if (CurPtr - 1 == CurBuf.
end()) {
120 "NUL character is invalid in source; treated as space");
129 if ((*CurPtr ==
'\n' || (*CurPtr ==
'\r')) &&
136 int TGLexer::peekNextChar(
int Index)
const {
137 return *(CurPtr +
Index);
143 int CurChar = getNextChar();
148 if (isalpha(CurChar) || CurChar ==
'_')
149 return LexIdentifier();
152 return ReturnError(TokStart,
"Unexpected character");
178 if (FileOrLineStart) {
181 return lexPreprocessor(
Kind);
189 if (peekNextChar(0) ==
'.') {
191 if (peekNextChar(0) ==
'.') {
195 return ReturnError(TokStart,
"Invalid '..' punctuation");
206 return LexToken(FileOrLineStart);
209 return LexToken(
true);
215 else if (*CurPtr ==
'*') {
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)) {
231 NextChar = peekNextChar(
i++);
232 }
while (isdigit(NextChar));
234 if (NextChar ==
'x' || NextChar ==
'b') {
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();
270 const char *StrStart = CurPtr;
274 while (*CurPtr !=
'"') {
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 '"':
292 CurStrVal += *CurPtr++;
305 return ReturnError(CurPtr,
"escaped newlines not supported in tblgen");
309 if (CurPtr == CurBuf.
end())
310 return ReturnError(StrStart,
"End of file in string literal");
313 return ReturnError(CurPtr,
"invalid escape in string literal");
322 if (!isalpha(CurPtr[0]) && CurPtr[0] !=
'_')
323 return ReturnError(TokStart,
"Invalid variable name");
326 const char *VarNameStart = CurPtr++;
328 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr ==
'_')
331 CurStrVal.assign(VarNameStart, CurPtr);
337 const char *IdentStart = TokStart;
340 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr ==
'_')
344 StringRef Str(IdentStart, CurPtr-IdentStart);
379 CurStrVal.assign(Str.begin(), Str.end());
390 bool TGLexer::LexInclude() {
400 std::string Filename = CurStrVal;
401 std::string IncludedFile;
410 Dependencies.insert(IncludedFile);
413 CurPtr = CurBuf.
begin();
415 PrepIncludeStack.push_back(
416 std::make_unique<std::vector<PreprocessorControlDesc>>());
422 void TGLexer::SkipBCPLComment() {
430 bool TGLexer::SkipCComment() {
432 unsigned CommentDepth = 1;
435 int CurChar = getNextChar();
438 PrintError(TokStart,
"Unterminated comment!");
442 if (CurPtr[0] !=
'/')
break;
445 if (--CommentDepth == 0)
450 if (CurPtr[0] !=
'*')
break;
463 if (CurPtr[-1] ==
'0') {
464 if (CurPtr[0] ==
'x') {
466 const char *NumStart = CurPtr;
467 while (isxdigit(CurPtr[0]))
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");
487 }
else if (CurPtr[0] ==
'b') {
489 const char *NumStart = CurPtr;
490 while (CurPtr[0] ==
'0' || CurPtr[0] ==
'1')
494 if (CurPtr == NumStart)
495 return ReturnError(CurPtr-2,
"Invalid binary number");
496 CurIntVal = strtoll(NumStart,
nullptr, 2);
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);
518 if (CurPtr[0] !=
'{')
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);
536 return ReturnError(CodeStart - 2,
"Unterminated code block");
541 if (!isalpha(*CurPtr))
542 return ReturnError(CurPtr - 1,
"Invalid \"!operator\"");
544 const char *Start = CurPtr++;
545 while (isalpha(*CurPtr))
594 bool TGLexer::prepExitInclude(
bool IncludeStackMustBeEmpty) {
597 if (!PrepIncludeStack.back()->empty()) {
598 prepReportPreprocessorStackError();
604 if (PrepIncludeStack.empty()) {
608 PrepIncludeStack.pop_back();
610 if (IncludeStackMustBeEmpty) {
611 if (!PrepIncludeStack.empty())
614 if (PrepIncludeStack.empty())
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);
642 if (NextChar ==
' ' || NextChar ==
'\t' || NextChar == EOF ||
662 if (NextChar ==
'/') {
663 NextChar = peekNextChar(
I + 1);
665 if (NextChar ==
'*' || NextChar ==
'/')
679 for (
const auto &
PD : PreprocessorDirs)
682 CurPtr += strlen(
PD.Word);
687 "prepEatPreprocessorDirective()");
695 if (!prepEatPreprocessorDirective(
Kind))
697 "preprocessor directive");
700 StringRef MacroName = prepLexMacroName();
702 if (MacroName.
empty())
703 return ReturnError(TokStart,
"Expected macro name after " + IfTokName);
705 bool MacroIsDefined = DefinedMacros.
count(MacroName) != 0;
709 MacroIsDefined = !MacroIsDefined;
715 PrepIncludeStack.back()->push_back(
718 if (!prepSkipDirectiveEnd())
719 return ReturnError(CurPtr,
"Only comments are supported after " +
720 IfTokName +
" NAME");
724 if (!ReturnNextLiveToken)
736 if (prepSkipRegion(ReturnNextLiveToken))
743 if (PrepIncludeStack.back()->empty())
744 return ReturnError(TokStart,
"#else without #ifdef or #ifndef");
746 PreprocessorControlDesc IfdefEntry = PrepIncludeStack.back()->back();
750 return ReturnError(IfdefEntry.SrcPos,
"Previous #else is here");
755 PrepIncludeStack.back()->pop_back();
756 PrepIncludeStack.back()->push_back(
759 if (!prepSkipDirectiveEnd())
760 return ReturnError(CurPtr,
"Only comments are supported after #else");
764 if (ReturnNextLiveToken) {
765 if (prepSkipRegion(ReturnNextLiveToken))
776 if (PrepIncludeStack.back()->empty())
777 return ReturnError(TokStart,
"#endif without #ifdef");
779 auto &IfdefOrElseEntry = PrepIncludeStack.back()->back();
787 if (!prepSkipDirectiveEnd())
788 return ReturnError(CurPtr,
"Only comments are supported after #endif");
790 PrepIncludeStack.back()->pop_back();
794 if (ReturnNextLiveToken) {
801 StringRef MacroName = prepLexMacroName();
802 if (MacroName.
empty())
803 return ReturnError(TokStart,
"Expected macro name after #define");
805 if (!DefinedMacros.
insert(MacroName).second)
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) {
825 bool TGLexer::prepSkipRegion(
bool MustNeverBeFalse) {
826 if (!MustNeverBeFalse)
834 if (!prepSkipLineBegin())
862 if (
Kind != ProcessedKind)
864 "returned different token kinds");
870 if (prepIsProcessingEnabled()) {
873 "preprocessing directive");
879 }
while (CurPtr != CurBuf.
end());
883 prepReportPreprocessorStackError();
889 while (*CurPtr ==
' ' || *CurPtr ==
'\t')
894 if (*CurPtr !=
'_' && !isalpha(*CurPtr))
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 ==
'*') {
953 bool TGLexer::prepSkipDirectiveEnd() {
954 while (CurPtr != CurBuf.
end()) {
965 int NextChar = peekNextChar(1);
966 if (NextChar ==
'/') {
972 }
else if (NextChar ==
'*') {
1014 void TGLexer::prepSkipToLineEnd() {
1015 while (*CurPtr !=
'\n' && *CurPtr !=
'\r' && CurPtr != CurBuf.
end())
1019 bool TGLexer::prepIsProcessingEnabled() {
1020 for (
const PreprocessorControlDesc &
I :
1028 void TGLexer::prepReportPreprocessorStackError() {
1029 if (PrepIncludeStack.back()->empty())
1031 "empty control stack");
1033 auto &PrepControl = PrepIncludeStack.back()->back();
1034 PrintError(CurBuf.
end(),
"Reached EOF without matching #endif");
1035 PrintError(PrepControl.SrcPos,
"The latest preprocessor control is here");