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);
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())
644 for (
const auto &PD : PreprocessorDirs) {
645 int NextChar = *CurPtr;
648 for (;
I < strlen(
PD.Word); ++
I) {
649 if (NextChar !=
PD.Word[
I]) {
654 NextChar = peekNextChar(
I + 1);
664 if (NextChar ==
' ' || NextChar ==
'\t' || NextChar == EOF ||
684 if (NextChar ==
'/') {
685 NextChar = peekNextChar(
I + 1);
687 if (NextChar ==
'*' || NextChar ==
'/')
701 for (
const auto &PD : PreprocessorDirs)
702 if (
PD.Kind == Kind) {
704 CurPtr += strlen(
PD.Word);
709 "prepEatPreprocessorDirective()");
717 if (!prepEatPreprocessorDirective(Kind))
719 "preprocessor directive");
722 StringRef MacroName = prepLexMacroName();
724 if (MacroName.
empty())
725 return ReturnError(TokStart,
"Expected macro name after " + IfTokName);
727 bool MacroIsDefined = DefinedMacros.
count(MacroName) != 0;
731 MacroIsDefined = !MacroIsDefined;
736 PrepIncludeStack.back()->push_back(
739 if (!prepSkipDirectiveEnd())
740 return ReturnError(CurPtr,
"Only comments are supported after " +
741 IfTokName +
" NAME");
745 if (!ReturnNextLiveToken)
757 if (prepSkipRegion(ReturnNextLiveToken))
764 if (PrepIncludeStack.back()->empty())
765 return ReturnError(TokStart,
"#else without #ifdef or #ifndef");
767 PreprocessorControlDesc IfdefEntry = PrepIncludeStack.back()->back();
771 return ReturnError(IfdefEntry.SrcPos,
"Previous #else is here");
776 PrepIncludeStack.back()->pop_back();
777 PrepIncludeStack.back()->push_back(
780 if (!prepSkipDirectiveEnd())
781 return ReturnError(CurPtr,
"Only comments are supported after #else");
785 if (ReturnNextLiveToken) {
786 if (prepSkipRegion(ReturnNextLiveToken))
797 if (PrepIncludeStack.back()->empty())
798 return ReturnError(TokStart,
"#endif without #ifdef");
800 auto &IfdefOrElseEntry = PrepIncludeStack.back()->back();
808 if (!prepSkipDirectiveEnd())
809 return ReturnError(CurPtr,
"Only comments are supported after #endif");
811 PrepIncludeStack.back()->pop_back();
815 if (ReturnNextLiveToken) {
822 StringRef MacroName = prepLexMacroName();
823 if (MacroName.
empty())
824 return ReturnError(TokStart,
"Expected macro name after #define");
826 if (!DefinedMacros.
insert(MacroName).second)
828 "Duplicate definition of macro: " +
Twine(MacroName));
830 if (!prepSkipDirectiveEnd())
831 return ReturnError(CurPtr,
832 "Only comments are supported after #define NAME");
834 if (!ReturnNextLiveToken) {
846bool TGLexer::prepSkipRegion(
bool MustNeverBeFalse) {
847 if (!MustNeverBeFalse)
852 while (*CurPtr !=
'\n')
856 if (!prepSkipLineBegin())
884 if (Kind != ProcessedKind)
886 "returned different token kinds");
892 if (prepIsProcessingEnabled()) {
895 "preprocessing directive");
901 }
while (CurPtr != CurBuf.
end());
905 prepReportPreprocessorStackError();
911 while (*CurPtr ==
' ' || *CurPtr ==
'\t')
916 if (*CurPtr !=
'_' && !isalpha(*CurPtr))
920 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr ==
'_')
923 return StringRef(TokStart, CurPtr - TokStart);
926bool TGLexer::prepSkipLineBegin() {
927 while (CurPtr != CurBuf.
end()) {
936 int NextChar = peekNextChar(1);
937 if (NextChar ==
'*') {
975bool TGLexer::prepSkipDirectiveEnd() {
976 while (CurPtr != CurBuf.
end()) {
987 int NextChar = peekNextChar(1);
988 if (NextChar ==
'/') {
994 }
else if (NextChar ==
'*') {
1036bool TGLexer::prepIsProcessingEnabled() {
1037 for (
const PreprocessorControlDesc &
I :
1045void TGLexer::prepReportPreprocessorStackError() {
1046 if (PrepIncludeStack.back()->empty())
1048 "empty control stack");
1050 auto &PrepControl = PrepIncludeStack.back()->back();
1051 PrintError(CurBuf.
end(),
"Reached EOF without matching #endif");
1052 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.
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...
@ PD
PD - Prefix code for packed double precision vector floating point operations performed in the SSE re...
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)