17#include "llvm/Config/config.h"
35struct PreprocessorDir {
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);
383 CurStrVal.assign(Str.begin(), Str.end());
394bool TGLexer::LexInclude() {
405 std::string IncludedFile;
414 Dependencies.insert(IncludedFile);
417 CurPtr = CurBuf.
begin();
419 PrepIncludeStack.push_back(
420 std::make_unique<std::vector<PreprocessorControlDesc>>());
426void TGLexer::SkipBCPLComment() {
434bool TGLexer::SkipCComment() {
436 unsigned CommentDepth = 1;
439 int CurChar = getNextChar();
442 PrintError(TokStart,
"Unterminated comment!");
446 if (CurPtr[0] !=
'/')
break;
449 if (--CommentDepth == 0)
454 if (CurPtr[0] !=
'*')
break;
468 const char *NumStart;
471 if (CurPtr[-1] ==
'0') {
472 NumStart = CurPtr + 1;
473 if (CurPtr[0] ==
'x') {
477 while (isxdigit(CurPtr[0]));
478 }
else if (CurPtr[0] ==
'b') {
482 while (CurPtr[0] ==
'0' || CurPtr[0] ==
'1');
487 bool IsMinus =
false;
492 if (!isdigit(CurPtr[0])) {
493 if (CurPtr[-1] ==
'-')
495 else if (CurPtr[-1] ==
'+')
501 IsMinus = CurPtr[-1] ==
'-';
503 while (isdigit(CurPtr[0]))
508 if (CurPtr == NumStart)
509 return ReturnError(TokStart,
"Invalid number");
513 CurIntVal = strtoll(NumStart,
nullptr,
Base);
515 CurIntVal = strtoull(NumStart,
nullptr,
Base);
518 return ReturnError(TokStart,
"Invalid number");
520 return ReturnError(TokStart,
"Number out of range");
528 if (CurPtr[0] !=
'{')
531 const char *CodeStart = CurPtr;
533 int Char = getNextChar();
534 if (Char == EOF)
break;
536 if (Char !=
'}')
continue;
538 Char = getNextChar();
539 if (Char == EOF)
break;
541 CurStrVal.assign(CodeStart, CurPtr-2);
546 return ReturnError(CodeStart - 2,
"Unterminated code block");
551 if (!isalpha(*CurPtr))
552 return ReturnError(CurPtr - 1,
"Invalid \"!operator\"");
554 const char *Start = CurPtr++;
555 while (isalpha(*CurPtr))
616bool TGLexer::prepExitInclude(
bool IncludeStackMustBeEmpty) {
619 if (!PrepIncludeStack.back()->empty()) {
620 prepReportPreprocessorStackError();
626 if (PrepIncludeStack.empty()) {
630 PrepIncludeStack.pop_back();
632 if (IncludeStackMustBeEmpty) {
633 if (!PrepIncludeStack.empty())
636 if (PrepIncludeStack.empty())
647 int NextChar = peekNextChar(
Word.size());
654 if (NextChar ==
' ' || NextChar ==
'\t' || NextChar == EOF ||
674 if (NextChar ==
'/') {
675 NextChar = peekNextChar(
Word.size() + 1);
677 if (NextChar ==
'*' || NextChar ==
'/')
693 CurPtr += PWord.size();
698 "prepEatPreprocessorDirective()");
706 if (!prepEatPreprocessorDirective(Kind))
708 "preprocessor directive");
711 StringRef MacroName = prepLexMacroName();
713 if (MacroName.
empty())
714 return ReturnError(TokStart,
"Expected macro name after " + IfTokName);
716 bool MacroIsDefined = DefinedMacros.
count(MacroName) != 0;
720 MacroIsDefined = !MacroIsDefined;
725 PrepIncludeStack.back()->push_back(
728 if (!prepSkipDirectiveEnd())
729 return ReturnError(CurPtr,
"Only comments are supported after " +
730 IfTokName +
" NAME");
734 if (!ReturnNextLiveToken)
746 if (prepSkipRegion(ReturnNextLiveToken))
753 if (PrepIncludeStack.back()->empty())
754 return ReturnError(TokStart,
"#else without #ifdef or #ifndef");
756 PreprocessorControlDesc IfdefEntry = PrepIncludeStack.back()->back();
760 return ReturnError(IfdefEntry.SrcPos,
"Previous #else is here");
765 PrepIncludeStack.back()->pop_back();
766 PrepIncludeStack.back()->push_back(
769 if (!prepSkipDirectiveEnd())
770 return ReturnError(CurPtr,
"Only comments are supported after #else");
774 if (ReturnNextLiveToken) {
775 if (prepSkipRegion(ReturnNextLiveToken))
786 if (PrepIncludeStack.back()->empty())
787 return ReturnError(TokStart,
"#endif without #ifdef");
789 auto &IfdefOrElseEntry = PrepIncludeStack.back()->back();
797 if (!prepSkipDirectiveEnd())
798 return ReturnError(CurPtr,
"Only comments are supported after #endif");
800 PrepIncludeStack.back()->pop_back();
804 if (ReturnNextLiveToken) {
811 StringRef MacroName = prepLexMacroName();
812 if (MacroName.
empty())
813 return ReturnError(TokStart,
"Expected macro name after #define");
815 if (!DefinedMacros.
insert(MacroName).second)
817 "Duplicate definition of macro: " +
Twine(MacroName));
819 if (!prepSkipDirectiveEnd())
820 return ReturnError(CurPtr,
821 "Only comments are supported after #define NAME");
823 if (!ReturnNextLiveToken) {
835bool TGLexer::prepSkipRegion(
bool MustNeverBeFalse) {
836 if (!MustNeverBeFalse)
841 while (*CurPtr !=
'\n')
845 if (!prepSkipLineBegin())
873 if (Kind != ProcessedKind)
875 "returned different token kinds");
881 if (prepIsProcessingEnabled()) {
884 "preprocessing directive");
890 }
while (CurPtr != CurBuf.
end());
894 prepReportPreprocessorStackError();
900 while (*CurPtr ==
' ' || *CurPtr ==
'\t')
905 if (*CurPtr !=
'_' && !isalpha(*CurPtr))
909 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr ==
'_')
912 return StringRef(TokStart, CurPtr - TokStart);
915bool TGLexer::prepSkipLineBegin() {
916 while (CurPtr != CurBuf.
end()) {
925 int NextChar = peekNextChar(1);
926 if (NextChar ==
'*') {
964bool TGLexer::prepSkipDirectiveEnd() {
965 while (CurPtr != CurBuf.
end()) {
976 int NextChar = peekNextChar(1);
977 if (NextChar ==
'/') {
983 }
else if (NextChar ==
'*') {
1025bool TGLexer::prepIsProcessingEnabled() {
1026 for (
const PreprocessorControlDesc &
I :
1034void TGLexer::prepReportPreprocessorStackError() {
1035 if (PrepIncludeStack.back()->empty())
1037 "empty control stack");
1039 auto &PrepControl = PrepIncludeStack.back()->back();
1040 PrintError(CurBuf.
end(),
"Reached EOF without matching #endif");
1041 PrintError(PrepControl.SrcPos,
"The latest preprocessor control is here");
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
constexpr PreprocessorDir PreprocessorDirs[]
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.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
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
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)