18#include "llvm/Config/config.h"
35struct PreprocessorDir {
47 if (
C ==
'_' || isAlpha(
C))
64 const char *Next = Str.begin();
71 const char *
End = Str.end();
80 CurPtr = CurBuf.
begin();
84 PrepIncludeStack.emplace_back();
90 if (
End != MacroName.end())
92 "` specified on command line");
94 DefinedMacros.
insert(MacroName);
117bool TGLexer::processEOF() {
119 if (ParentIncludeLoc !=
SMLoc()) {
124 if (!prepExitInclude(
false))
140 prepExitInclude(
true);
144int TGLexer::getNextChar() {
145 char CurChar = *CurPtr++;
148 return (
unsigned char)CurChar;
153 if (CurPtr - 1 == CurBuf.
end()) {
158 "NUL character is invalid in source; treated as space");
167 if ((*CurPtr ==
'\n' || (*CurPtr ==
'\r')) &&
174int TGLexer::peekNextChar(
int Index)
const {
175 return *(CurPtr +
Index);
181 int CurChar = getNextChar();
187 return LexIdentifier();
190 return ReturnError(TokStart,
"unexpected character");
216 if (FileOrLineStart) {
219 return lexPreprocessor(Kind);
227 if (peekNextChar(0) ==
'.') {
229 if (peekNextChar(0) ==
'.') {
233 return ReturnError(TokStart,
"invalid '..' punctuation");
244 return LexToken(FileOrLineStart);
247 return LexToken(
true);
253 else if (*CurPtr ==
'*') {
257 return ReturnError(TokStart,
"unexpected character");
258 return LexToken(FileOrLineStart);
260 case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
261 case '7':
case '8':
case '9': {
269 NextChar = peekNextChar(i++);
272 if (NextChar ==
'x' || NextChar ==
'b') {
275 int NextNextChar = peekNextChar(i);
276 switch (NextNextChar) {
283 case '2':
case '3':
case '4':
case '5':
284 case '6':
case '7':
case '8':
case '9':
285 case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
286 case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
295 return LexIdentifier();
299 case '"':
return LexString();
300 case '$':
return LexVarName();
301 case '[':
return LexBracket();
302 case '!':
return LexExclaim();
308 const char *StrStart = CurPtr;
312 while (*CurPtr !=
'"') {
314 if (*CurPtr == 0 && CurPtr == CurBuf.
end())
315 return ReturnError(StrStart,
"end of file in string literal");
317 if (*CurPtr ==
'\n' || *CurPtr ==
'\r')
318 return ReturnError(StrStart,
"end of line in string literal");
320 if (*CurPtr !=
'\\') {
321 CurStrVal += *CurPtr++;
328 case '\\':
case '\'':
case '"':
330 CurStrVal += *CurPtr++;
343 return ReturnError(CurPtr,
"escaped newlines not supported in tblgen");
347 if (CurPtr == CurBuf.
end())
348 return ReturnError(StrStart,
"end of file in string literal");
351 return ReturnError(CurPtr,
"invalid escape in string literal");
361 return ReturnError(TokStart,
"invalid variable name");
364 const char *VarNameStart = CurPtr++;
369 CurStrVal.assign(VarNameStart, CurPtr);
375 const char *IdentStart = TokStart;
382 StringRef Str(IdentStart, CurPtr-IdentStart);
419 CurStrVal.assign(Str.begin(), Str.end());
430bool TGLexer::LexInclude() {
441 std::string IncludedFile;
450 Dependencies.insert(IncludedFile);
453 CurPtr = CurBuf.
begin();
455 PrepIncludeStack.emplace_back();
461void TGLexer::SkipBCPLComment() {
469bool TGLexer::SkipCComment() {
471 unsigned CommentDepth = 1;
474 int CurChar = getNextChar();
481 if (CurPtr[0] !=
'/')
break;
484 if (--CommentDepth == 0)
489 if (CurPtr[0] !=
'*')
break;
503 const char *NumStart;
506 if (CurPtr[-1] ==
'0') {
507 NumStart = CurPtr + 1;
508 if (CurPtr[0] ==
'x') {
513 }
else if (CurPtr[0] ==
'b') {
517 while (CurPtr[0] ==
'0' || CurPtr[0] ==
'1');
522 bool IsMinus =
false;
528 if (CurPtr[-1] ==
'-')
530 else if (CurPtr[-1] ==
'+')
536 IsMinus = CurPtr[-1] ==
'-';
543 if (CurPtr == NumStart)
544 return ReturnError(TokStart,
"invalid number");
548 CurIntVal = strtoll(NumStart,
nullptr,
Base);
550 CurIntVal = strtoull(NumStart,
nullptr,
Base);
553 return ReturnError(TokStart,
"invalid number");
555 return ReturnError(TokStart,
"number out of range");
563 if (CurPtr[0] !=
'{')
566 const char *CodeStart = CurPtr;
568 int Char = getNextChar();
569 if (Char == EOF)
break;
571 if (Char !=
'}')
continue;
573 Char = getNextChar();
574 if (Char == EOF)
break;
576 CurStrVal.assign(CodeStart, CurPtr-2);
581 return ReturnError(CodeStart - 2,
"unterminated code block");
586 if (!isAlpha(*CurPtr))
587 return ReturnError(CurPtr - 1,
"invalid \"!operator\"");
589 const char *Start = CurPtr++;
590 while (isAlpha(*CurPtr))
651 : ReturnError(Start - 1,
"unknown operator");
654bool TGLexer::prepExitInclude(
bool IncludeStackMustBeEmpty) {
657 if (!PrepIncludeStack.back().empty()) {
658 prepReportPreprocessorStackError();
664 PrepIncludeStack.pop_back();
666 if (IncludeStackMustBeEmpty) {
667 if (!PrepIncludeStack.empty())
670 if (PrepIncludeStack.empty())
681 int NextChar = peekNextChar(
Word.size());
688 if (NextChar ==
' ' || NextChar ==
'\t' || NextChar == EOF ||
708 if (NextChar ==
'/') {
709 NextChar = peekNextChar(
Word.size() + 1);
711 if (NextChar ==
'*' || NextChar ==
'/')
727 CurPtr += PWord.size();
732 "prepEatPreprocessorDirective()");
737 bool ReturnNextLiveToken) {
739 if (!prepEatPreprocessorDirective(Kind))
741 "preprocessor directive");
744 StringRef MacroName = prepLexMacroName();
746 if (MacroName.
empty())
747 return ReturnError(TokStart,
"expected macro name after " + IfTokName);
749 bool MacroIsDefined = DefinedMacros.
count(MacroName) != 0;
753 MacroIsDefined = !MacroIsDefined;
758 PrepIncludeStack.back().push_back(
761 if (!prepSkipDirectiveEnd())
762 return ReturnError(CurPtr,
"only comments are supported after " +
763 IfTokName +
" NAME");
767 if (!ReturnNextLiveToken)
779 if (prepSkipRegion(ReturnNextLiveToken))
786 if (PrepIncludeStack.back().empty())
787 return ReturnError(TokStart,
"#else without #ifdef or #ifndef");
789 PreprocessorControlDesc IfdefEntry = PrepIncludeStack.back().back();
793 return ReturnError(IfdefEntry.SrcPos,
"previous #else is here");
798 PrepIncludeStack.back().back() = {
Kind, !IfdefEntry.IsDefined,
801 if (!prepSkipDirectiveEnd())
802 return ReturnError(CurPtr,
"only comments are supported after #else");
806 if (ReturnNextLiveToken) {
807 if (prepSkipRegion(ReturnNextLiveToken))
818 if (PrepIncludeStack.back().empty())
819 return ReturnError(TokStart,
"#endif without #ifdef");
821 auto &IfdefOrElseEntry = PrepIncludeStack.back().back();
829 if (!prepSkipDirectiveEnd())
830 return ReturnError(CurPtr,
"only comments are supported after #endif");
832 PrepIncludeStack.back().pop_back();
836 if (ReturnNextLiveToken) {
843 StringRef MacroName = prepLexMacroName();
844 if (MacroName.
empty())
845 return ReturnError(TokStart,
"expected macro name after #define");
847 if (!DefinedMacros.
insert(MacroName).second)
849 "duplicate definition of macro: " +
Twine(MacroName));
851 if (!prepSkipDirectiveEnd())
852 return ReturnError(CurPtr,
853 "only comments are supported after #define NAME");
855 if (!ReturnNextLiveToken) {
867bool TGLexer::prepSkipRegion(
bool MustNeverBeFalse) {
868 if (!MustNeverBeFalse)
873 while (*CurPtr !=
'\n')
877 if (!prepSkipLineBegin())
905 if (Kind != ProcessedKind)
907 "returned different token kinds");
913 if (prepIsProcessingEnabled()) {
916 "preprocessing directive");
922 }
while (CurPtr != CurBuf.
end());
926 prepReportPreprocessorStackError();
932 while (*CurPtr ==
' ' || *CurPtr ==
'\t')
937 return StringRef(TokStart, CurPtr - TokStart);
940bool TGLexer::prepSkipLineBegin() {
941 while (CurPtr != CurBuf.
end()) {
950 int NextChar = peekNextChar(1);
951 if (NextChar ==
'*') {
989bool TGLexer::prepSkipDirectiveEnd() {
990 while (CurPtr != CurBuf.
end()) {
1001 int NextChar = peekNextChar(1);
1002 if (NextChar ==
'/') {
1008 }
else if (NextChar ==
'*') {
1050bool TGLexer::prepIsProcessingEnabled() {
1051 return all_of(PrepIncludeStack.back(),
1052 [](
const PreprocessorControlDesc &
I) { return I.IsDefined; });
1055void TGLexer::prepReportPreprocessorStackError() {
1056 if (PrepIncludeStack.back().empty())
1058 "empty control stack");
1060 auto &PrepControl = PrepIncludeStack.back().back();
1061 PrintError(CurBuf.
end(),
"reached EOF without matching #endif");
1062 PrintError(PrepControl.SrcPos,
"the latest preprocessor control is here");
static bool isDigit(const char C)
static bool isHexDigit(const char C)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
constexpr PreprocessorDir PreprocessorDirs[]
static bool isValidIDChar(char C, bool First)
Returns true if C is a valid character in an identifier.
static const char * lexMacroName(StringRef 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...
@ C
The default llvm calling convention, compatible with C.
support::ulittle32_t Word
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
void PrintFatalError(const Twine &Msg)
void PrintError(const Twine &Msg)
void PrintWarning(const Twine &Msg)
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.