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);
81bool TGLexer::processEOF() {
83 if (ParentIncludeLoc !=
SMLoc()) {
88 if (!prepExitInclude(
false))
104 prepExitInclude(
true);
108int TGLexer::getNextChar() {
109 char CurChar = *CurPtr++;
112 return (
unsigned char)CurChar;
117 if (CurPtr - 1 == CurBuf.
end()) {
122 "NUL character is invalid in source; treated as space");
131 if ((*CurPtr ==
'\n' || (*CurPtr ==
'\r')) &&
138int TGLexer::peekNextChar(
int Index)
const {
139 return *(CurPtr +
Index);
145 int CurChar = getNextChar();
150 if (isalpha(CurChar) || CurChar ==
'_')
151 return LexIdentifier();
154 return ReturnError(TokStart,
"Unexpected character");
180 if (FileOrLineStart) {
183 return lexPreprocessor(Kind);
191 if (peekNextChar(0) ==
'.') {
193 if (peekNextChar(0) ==
'.') {
197 return ReturnError(TokStart,
"Invalid '..' punctuation");
208 return LexToken(FileOrLineStart);
211 return LexToken(
true);
217 else if (*CurPtr ==
'*') {
221 return ReturnError(TokStart,
"Unexpected character");
222 return LexToken(FileOrLineStart);
224 case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
225 case '7':
case '8':
case '9': {
227 if (isdigit(CurChar)) {
233 NextChar = peekNextChar(i++);
234 }
while (isdigit(NextChar));
236 if (NextChar ==
'x' || NextChar ==
'b') {
239 int NextNextChar = peekNextChar(i);
240 switch (NextNextChar) {
247 case '2':
case '3':
case '4':
case '5':
248 case '6':
case '7':
case '8':
case '9':
249 case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
250 case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
258 if (isalpha(NextChar) || NextChar ==
'_')
259 return LexIdentifier();
263 case '"':
return LexString();
264 case '$':
return LexVarName();
265 case '[':
return LexBracket();
266 case '!':
return LexExclaim();
272 const char *StrStart = CurPtr;
276 while (*CurPtr !=
'"') {
278 if (*CurPtr == 0 && CurPtr == CurBuf.
end())
279 return ReturnError(StrStart,
"End of file in string literal");
281 if (*CurPtr ==
'\n' || *CurPtr ==
'\r')
282 return ReturnError(StrStart,
"End of line in string literal");
284 if (*CurPtr !=
'\\') {
285 CurStrVal += *CurPtr++;
292 case '\\':
case '\'':
case '"':
294 CurStrVal += *CurPtr++;
307 return ReturnError(CurPtr,
"escaped newlines not supported in tblgen");
311 if (CurPtr == CurBuf.
end())
312 return ReturnError(StrStart,
"End of file in string literal");
315 return ReturnError(CurPtr,
"invalid escape in string literal");
324 if (!isalpha(CurPtr[0]) && CurPtr[0] !=
'_')
325 return ReturnError(TokStart,
"Invalid variable name");
328 const char *VarNameStart = CurPtr++;
330 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr ==
'_')
333 CurStrVal.assign(VarNameStart, CurPtr);
339 const char *IdentStart = TokStart;
342 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr ==
'_')
346 StringRef Str(IdentStart, CurPtr-IdentStart);
381 CurStrVal.assign(Str.begin(), Str.end());
392bool TGLexer::LexInclude() {
403 std::string IncludedFile;
412 Dependencies.insert(IncludedFile);
415 CurPtr = CurBuf.
begin();
417 PrepIncludeStack.push_back(
418 std::make_unique<std::vector<PreprocessorControlDesc>>());
424void TGLexer::SkipBCPLComment() {
432bool TGLexer::SkipCComment() {
434 unsigned CommentDepth = 1;
437 int CurChar = getNextChar();
440 PrintError(TokStart,
"Unterminated comment!");
444 if (CurPtr[0] !=
'/')
break;
447 if (--CommentDepth == 0)
452 if (CurPtr[0] !=
'*')
break;
466 const char *NumStart;
469 if (CurPtr[-1] ==
'0') {
470 NumStart = CurPtr + 1;
471 if (CurPtr[0] ==
'x') {
475 while (isxdigit(CurPtr[0]));
476 }
else if (CurPtr[0] ==
'b') {
480 while (CurPtr[0] ==
'0' || CurPtr[0] ==
'1');
485 bool IsMinus =
false;
490 if (!isdigit(CurPtr[0])) {
491 if (CurPtr[-1] ==
'-')
493 else if (CurPtr[-1] ==
'+')
499 IsMinus = CurPtr[-1] ==
'-';
501 while (isdigit(CurPtr[0]))
506 if (CurPtr == NumStart)
507 return ReturnError(TokStart,
"Invalid number");
511 CurIntVal = strtoll(NumStart,
nullptr,
Base);
513 CurIntVal = strtoull(NumStart,
nullptr,
Base);
516 return ReturnError(TokStart,
"Invalid number");
518 return ReturnError(TokStart,
"Number out of range");
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))
613bool TGLexer::prepExitInclude(
bool IncludeStackMustBeEmpty) {
616 if (!PrepIncludeStack.back()->empty()) {
617 prepReportPreprocessorStackError();
623 if (PrepIncludeStack.empty()) {
627 PrepIncludeStack.pop_back();
629 if (IncludeStackMustBeEmpty) {
630 if (!PrepIncludeStack.empty())
633 if (PrepIncludeStack.empty())
641 for (
const auto &PD : PreprocessorDirs) {
642 int NextChar = *CurPtr;
645 for (;
I < strlen(
PD.Word); ++
I) {
646 if (NextChar !=
PD.Word[
I]) {
651 NextChar = peekNextChar(
I + 1);
661 if (NextChar ==
' ' || NextChar ==
'\t' || NextChar == EOF ||
681 if (NextChar ==
'/') {
682 NextChar = peekNextChar(
I + 1);
684 if (NextChar ==
'*' || NextChar ==
'/')
698 for (
const auto &PD : PreprocessorDirs)
699 if (
PD.Kind == Kind) {
701 CurPtr += strlen(
PD.Word);
706 "prepEatPreprocessorDirective()");
714 if (!prepEatPreprocessorDirective(Kind))
716 "preprocessor directive");
719 StringRef MacroName = prepLexMacroName();
721 if (MacroName.
empty())
722 return ReturnError(TokStart,
"Expected macro name after " + IfTokName);
724 bool MacroIsDefined = DefinedMacros.
count(MacroName) != 0;
728 MacroIsDefined = !MacroIsDefined;
733 PrepIncludeStack.back()->push_back(
736 if (!prepSkipDirectiveEnd())
737 return ReturnError(CurPtr,
"Only comments are supported after " +
738 IfTokName +
" NAME");
742 if (!ReturnNextLiveToken)
754 if (prepSkipRegion(ReturnNextLiveToken))
761 if (PrepIncludeStack.back()->empty())
762 return ReturnError(TokStart,
"#else without #ifdef or #ifndef");
764 PreprocessorControlDesc IfdefEntry = PrepIncludeStack.back()->back();
768 return ReturnError(IfdefEntry.SrcPos,
"Previous #else is here");
773 PrepIncludeStack.back()->pop_back();
774 PrepIncludeStack.back()->push_back(
777 if (!prepSkipDirectiveEnd())
778 return ReturnError(CurPtr,
"Only comments are supported after #else");
782 if (ReturnNextLiveToken) {
783 if (prepSkipRegion(ReturnNextLiveToken))
794 if (PrepIncludeStack.back()->empty())
795 return ReturnError(TokStart,
"#endif without #ifdef");
797 auto &IfdefOrElseEntry = PrepIncludeStack.back()->back();
805 if (!prepSkipDirectiveEnd())
806 return ReturnError(CurPtr,
"Only comments are supported after #endif");
808 PrepIncludeStack.back()->pop_back();
812 if (ReturnNextLiveToken) {
819 StringRef MacroName = prepLexMacroName();
820 if (MacroName.
empty())
821 return ReturnError(TokStart,
"Expected macro name after #define");
823 if (!DefinedMacros.
insert(MacroName).second)
825 "Duplicate definition of macro: " +
Twine(MacroName));
827 if (!prepSkipDirectiveEnd())
828 return ReturnError(CurPtr,
829 "Only comments are supported after #define NAME");
831 if (!ReturnNextLiveToken) {
843bool TGLexer::prepSkipRegion(
bool MustNeverBeFalse) {
844 if (!MustNeverBeFalse)
852 if (!prepSkipLineBegin())
880 if (Kind != ProcessedKind)
882 "returned different token kinds");
888 if (prepIsProcessingEnabled()) {
891 "preprocessing directive");
897 }
while (CurPtr != CurBuf.
end());
901 prepReportPreprocessorStackError();
907 while (*CurPtr ==
' ' || *CurPtr ==
'\t')
912 if (*CurPtr !=
'_' && !isalpha(*CurPtr))
916 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr ==
'_')
919 return StringRef(TokStart, CurPtr - TokStart);
922bool TGLexer::prepSkipLineBegin() {
923 while (CurPtr != CurBuf.
end()) {
932 int NextChar = peekNextChar(1);
933 if (NextChar ==
'*') {
971bool TGLexer::prepSkipDirectiveEnd() {
972 while (CurPtr != CurBuf.
end()) {
983 int NextChar = peekNextChar(1);
984 if (NextChar ==
'/') {
990 }
else if (NextChar ==
'*') {
1032void TGLexer::prepSkipToLineEnd() {
1033 while (*CurPtr !=
'\n' && *CurPtr !=
'\r' && CurPtr != CurBuf.
end())
1037bool TGLexer::prepIsProcessingEnabled() {
1038 for (
const PreprocessorControlDesc &
I :
1046void TGLexer::prepReportPreprocessorStackError() {
1047 if (PrepIncludeStack.back()->empty())
1049 "empty control stack");
1051 auto &PrepControl = PrepIncludeStack.back()->back();
1052 PrintError(CurBuf.
end(),
"Reached EOF without matching #endif");
1053 PrintError(PrepControl.SrcPos,
"The latest preprocessor control is here");
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
StringRef getBuffer() const
Represents a location in source code.
static SMLoc getFromPointer(const char *Ptr)
constexpr const char * getPointer() const
Represents a range in source code.
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
unsigned getMainFileID() const
const MemoryBuffer * getMemoryBuffer(unsigned i) const
SMLoc getParentIncludeLoc(unsigned i) const
unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
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.
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
StringRef - Represent a constant reference to a string, i.e.
constexpr bool empty() const
empty - Check if the string is empty.
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
static constexpr size_t npos
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
std::pair< typename Base::iterator, bool > insert(StringRef key)
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
SMRange getLocRange() const
TGLexer(SourceMgr &SrcMgr, ArrayRef< std::string > Macros)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
support::ulittle32_t Word
This is an optimization pass for GlobalISel generic memory operations.
void PrintFatalError(const Twine &Msg)
void PrintError(const Twine &Msg)
void PrintWarning(const Twine &Msg)
auto reverse(ContainerTy &&C)