16 #include "llvm/Config/config.h" 37 } PreprocessorDirs[] = {
49 CurPtr = CurBuf.
begin();
53 PrepIncludeStack.push_back(
54 std::make_unique<std::vector<PreprocessorControlDesc>>());
58 [
this](
const std::string &MacroName) {
59 DefinedMacros.
insert(MacroName);
78 bool TGLexer::processEOF() {
80 if (ParentIncludeLoc !=
SMLoc()) {
85 if (!prepExitInclude(
false))
101 prepExitInclude(
true);
105 int TGLexer::getNextChar() {
106 char CurChar = *CurPtr++;
109 return (
unsigned char)CurChar;
113 if (CurPtr-1 != CurBuf.
end())
125 if ((*CurPtr ==
'\n' || (*CurPtr ==
'\r')) &&
132 int TGLexer::peekNextChar(
int Index)
const {
133 return *(CurPtr +
Index);
139 int CurChar = getNextChar();
144 if (isalpha(CurChar) || CurChar ==
'_')
145 return LexIdentifier();
148 return ReturnError(TokStart,
"Unexpected character");
175 if (FileOrLineStart) {
178 return lexPreprocessor(Kind);
191 return LexToken(FileOrLineStart);
194 return LexToken(
true);
200 else if (*CurPtr ==
'*') {
204 return ReturnError(TokStart,
"Unexpected character");
205 return LexToken(FileOrLineStart);
207 case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
208 case '7':
case '8':
case '9': {
210 if (isdigit(CurChar)) {
216 NextChar = peekNextChar(i++);
217 }
while (isdigit(NextChar));
219 if (NextChar ==
'x' || NextChar ==
'b') {
222 int NextNextChar = peekNextChar(i);
223 switch (NextNextChar) {
230 case '2':
case '3':
case '4':
case '5':
231 case '6':
case '7':
case '8':
case '9':
232 case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
233 case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
241 if (isalpha(NextChar) || NextChar ==
'_')
242 return LexIdentifier();
246 case '"':
return LexString();
247 case '$':
return LexVarName();
248 case '[':
return LexBracket();
249 case '!':
return LexExclaim();
255 const char *StrStart = CurPtr;
259 while (*CurPtr !=
'"') {
261 if (*CurPtr == 0 && CurPtr == CurBuf.
end())
262 return ReturnError(StrStart,
"End of file in string literal");
264 if (*CurPtr ==
'\n' || *CurPtr ==
'\r')
265 return ReturnError(StrStart,
"End of line in string literal");
267 if (*CurPtr !=
'\\') {
268 CurStrVal += *CurPtr++;
275 case '\\':
case '\'':
case '"':
277 CurStrVal += *CurPtr++;
290 return ReturnError(CurPtr,
"escaped newlines not supported in tblgen");
294 if (CurPtr == CurBuf.
end())
295 return ReturnError(StrStart,
"End of file in string literal");
298 return ReturnError(CurPtr,
"invalid escape in string literal");
307 if (!isalpha(CurPtr[0]) && CurPtr[0] !=
'_')
308 return ReturnError(TokStart,
"Invalid variable name");
311 const char *VarNameStart = CurPtr++;
313 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr ==
'_')
316 CurStrVal.assign(VarNameStart, CurPtr);
322 const char *IdentStart = TokStart;
325 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr ==
'_')
329 StringRef Str(IdentStart, CurPtr-IdentStart);
331 if (Str ==
"include") {
360 CurStrVal.assign(Str.
begin(), Str.
end());
366 bool TGLexer::LexInclude() {
376 std::string Filename = CurStrVal;
377 std::string IncludedFile;
386 Dependencies.insert(IncludedFile);
389 CurPtr = CurBuf.
begin();
391 PrepIncludeStack.push_back(
392 std::make_unique<std::vector<PreprocessorControlDesc>>());
396 void TGLexer::SkipBCPLComment() {
405 if (CurPtr == CurBuf.
end())
416 bool TGLexer::SkipCComment() {
418 unsigned CommentDepth = 1;
421 int CurChar = getNextChar();
424 PrintError(TokStart,
"Unterminated comment!");
428 if (CurPtr[0] !=
'/')
break;
431 if (--CommentDepth == 0)
436 if (CurPtr[0] !=
'*')
break;
449 if (CurPtr[-1] ==
'0') {
450 if (CurPtr[0] ==
'x') {
452 const char *NumStart = CurPtr;
453 while (isxdigit(CurPtr[0]))
457 if (CurPtr == NumStart)
458 return ReturnError(TokStart,
"Invalid hexadecimal number");
461 CurIntVal = strtoll(NumStart,
nullptr, 16);
463 return ReturnError(TokStart,
"Invalid hexadecimal number");
464 if (errno == ERANGE) {
466 CurIntVal = (int64_t)strtoull(NumStart,
nullptr, 16);
468 return ReturnError(TokStart,
"Invalid hexadecimal number");
470 return ReturnError(TokStart,
"Hexadecimal number out of range");
473 }
else if (CurPtr[0] ==
'b') {
475 const char *NumStart = CurPtr;
476 while (CurPtr[0] ==
'0' || CurPtr[0] ==
'1')
480 if (CurPtr == NumStart)
481 return ReturnError(CurPtr-2,
"Invalid binary number");
482 CurIntVal = strtoll(NumStart,
nullptr, 2);
488 if (!isdigit(CurPtr[0])) {
489 if (CurPtr[-1] ==
'-')
491 else if (CurPtr[-1] ==
'+')
495 while (isdigit(CurPtr[0]))
497 CurIntVal = strtoll(TokStart,
nullptr, 10);
504 if (CurPtr[0] !=
'{')
507 const char *CodeStart = CurPtr;
509 int Char = getNextChar();
510 if (Char == EOF)
break;
512 if (Char !=
'}')
continue;
514 Char = getNextChar();
515 if (Char == EOF)
break;
517 CurStrVal.assign(CodeStart, CurPtr-2);
522 return ReturnError(CodeStart-2,
"Unterminated Code Block");
527 if (!isalpha(*CurPtr))
528 return ReturnError(CurPtr - 1,
"Invalid \"!operator\"");
530 const char *Start = CurPtr++;
531 while (isalpha(*CurPtr))
570 return Kind !=
tgtok::Error ?
Kind : ReturnError(Start-1,
"Unknown operator");
573 bool TGLexer::prepExitInclude(
bool IncludeStackMustBeEmpty) {
576 if (!PrepIncludeStack.back()->empty()) {
577 prepReportPreprocessorStackError();
583 if (PrepIncludeStack.empty()) {
587 PrepIncludeStack.pop_back();
589 if (IncludeStackMustBeEmpty) {
590 if (!PrepIncludeStack.empty())
593 if (PrepIncludeStack.empty())
602 int NextChar = *CurPtr;
605 for (; I < strlen(PreprocessorDirs[
ID].
Word); ++
I) {
606 if (NextChar != PreprocessorDirs[
ID].Word[I]) {
611 NextChar = peekNextChar(I + 1);
621 if (NextChar ==
' ' || NextChar ==
'\t' || NextChar == EOF ||
641 if (NextChar ==
'/') {
642 NextChar = peekNextChar(I + 1);
644 if (NextChar ==
'*' || NextChar ==
'/')
659 if (PreprocessorDirs[
ID].Kind == Kind) {
661 CurPtr += strlen(PreprocessorDirs[
ID].
Word);
666 "prepEatPreprocessorDirective()");
674 if (!prepEatPreprocessorDirective(
Kind))
676 "preprocessor directive");
679 StringRef MacroName = prepLexMacroName();
681 if (MacroName.
empty())
682 return ReturnError(TokStart,
"Expected macro name after " + IfTokName);
684 bool MacroIsDefined = DefinedMacros.
count(MacroName) != 0;
688 MacroIsDefined = !MacroIsDefined;
694 PrepIncludeStack.back()->push_back(
697 if (!prepSkipDirectiveEnd())
698 return ReturnError(CurPtr,
"Only comments are supported after " +
699 IfTokName +
" NAME");
703 if (!ReturnNextLiveToken)
715 if (prepSkipRegion(ReturnNextLiveToken))
722 if (PrepIncludeStack.back()->empty())
723 return ReturnError(TokStart,
"#else without #ifdef or #ifndef");
725 PreprocessorControlDesc IfdefEntry = PrepIncludeStack.back()->back();
729 return ReturnError(IfdefEntry.SrcPos,
"Previous #else is here");
734 PrepIncludeStack.back()->pop_back();
735 PrepIncludeStack.back()->push_back(
738 if (!prepSkipDirectiveEnd())
739 return ReturnError(CurPtr,
"Only comments are supported after #else");
743 if (ReturnNextLiveToken) {
744 if (prepSkipRegion(ReturnNextLiveToken))
755 if (PrepIncludeStack.back()->empty())
756 return ReturnError(TokStart,
"#endif without #ifdef");
758 auto &IfdefOrElseEntry = PrepIncludeStack.back()->back();
766 if (!prepSkipDirectiveEnd())
767 return ReturnError(CurPtr,
"Only comments are supported after #endif");
769 PrepIncludeStack.back()->pop_back();
773 if (ReturnNextLiveToken) {
780 StringRef MacroName = prepLexMacroName();
781 if (MacroName.
empty())
782 return ReturnError(TokStart,
"Expected macro name after #define");
784 if (!DefinedMacros.
insert(MacroName).second)
786 "Duplicate definition of macro: " +
Twine(MacroName));
788 if (!prepSkipDirectiveEnd())
789 return ReturnError(CurPtr,
790 "Only comments are supported after #define NAME");
792 if (!ReturnNextLiveToken) {
804 bool TGLexer::prepSkipRegion(
bool MustNeverBeFalse) {
805 if (!MustNeverBeFalse)
813 if (!prepSkipLineBegin())
841 if (Kind != ProcessedKind)
843 "returned different token kinds");
849 if (prepIsProcessingEnabled()) {
852 "preprocessing directive");
858 }
while (CurPtr != CurBuf.
end());
862 prepReportPreprocessorStackError();
868 while (*CurPtr ==
' ' || *CurPtr ==
'\t')
873 if (*CurPtr !=
'_' && !isalpha(*CurPtr))
877 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr ==
'_')
880 return StringRef(TokStart, CurPtr - TokStart);
883 bool TGLexer::prepSkipLineBegin() {
884 while (CurPtr != CurBuf.
end()) {
893 int NextChar = peekNextChar(1);
894 if (NextChar ==
'*') {
932 bool TGLexer::prepSkipDirectiveEnd() {
933 while (CurPtr != CurBuf.
end()) {
944 int NextChar = peekNextChar(1);
945 if (NextChar ==
'/') {
951 }
else if (NextChar ==
'*') {
993 void TGLexer::prepSkipToLineEnd() {
994 while (*CurPtr !=
'\n' && *CurPtr !=
'\r' && CurPtr != CurBuf.
end())
998 bool TGLexer::prepIsProcessingEnabled() {
999 for (
auto I = PrepIncludeStack.back()->rbegin(),
1000 E = PrepIncludeStack.back()->rend();
1009 void TGLexer::prepReportPreprocessorStackError() {
1010 if (PrepIncludeStack.back()->empty())
1012 "empty control stack");
1014 auto &PrepControl = PrepIncludeStack.back()->back();
1015 PrintError(CurBuf.
end(),
"Reached EOF without matching #endif");
1016 PrintError(PrepControl.SrcPos,
"The latest preprocessor control is here");
unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
StringRef getBuffer() const
This class represents lattice values for constants.
StringSwitch & Case(StringLiteral S, T Value)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
support::ulittle32_t Word
LLVM_NODISCARD R Default(T Value)
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
const MemoryBuffer * getMemoryBuffer(unsigned i) const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
unsigned getMainFileID() const
const char * getPointer() const
A switch()-like statement whose cases are string literals.
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
TGLexer(SourceMgr &SrcMgr, ArrayRef< std::string > Macros)
void PrintWarning(ArrayRef< SMLoc > WarningLoc, const Twine &Msg)
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
std::pair< typename base::iterator, bool > insert(StringRef Key)
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
LLVM_ATTRIBUTE_NORETURN void PrintFatalError(const Twine &Msg)
static SMLoc getFromPointer(const char *Ptr)
unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc, std::string &IncludedFile)
Search for a file with the specified name in the current directory or in one of the IncludeDirs...
SMLoc getParentIncludeLoc(unsigned i) const
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
StringRef - Represent a constant reference to a string, i.e.
UnaryPredicate for_each(R &&Range, UnaryPredicate P)
Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly...
Represents a location in source code.
void PrintError(ArrayRef< SMLoc > ErrorLoc, const Twine &Msg)