blob: 5dca134f8e8852a2635b54741d47ddd309983508 [file] [log] [blame]
Chris Lattnerda4ab672007-11-18 02:57:27 +00001//===- TGLexer.cpp - Lexer for TableGen -----------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattnerda4ab672007-11-18 02:57:27 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Implement the Lexer for TableGen.
10//
11//===----------------------------------------------------------------------===//
12
Chris Lattner98c39512007-11-18 05:25:45 +000013#include "TGLexer.h"
Bill Wendling55bc7182010-12-08 20:02:49 +000014#include "llvm/ADT/StringSwitch.h"
15#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Config/config.h" // for strtoull()/strtoll() define
Eugene Zelenko33d7b762016-08-23 17:14:32 +000017#include "llvm/Support/Compiler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Support/MemoryBuffer.h"
19#include "llvm/Support/SourceMgr.h"
20#include "llvm/TableGen/Error.h"
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +000021#include <algorithm>
Chris Lattnerda4ab672007-11-18 02:57:27 +000022#include <cctype>
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include <cerrno>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000024#include <cstdint>
Duncan Sands26ff6f92008-10-08 07:23:46 +000025#include <cstdio>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000026#include <cstdlib>
27#include <cstring>
Dylan Noblesmith345b7432011-12-22 23:08:39 +000028
Chris Lattnerda4ab672007-11-18 02:57:27 +000029using namespace llvm;
30
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +000031namespace {
32// A list of supported preprocessing directives with their
33// internal token kinds and names.
34struct {
35 tgtok::TokKind Kind;
36 const char *Word;
37} PreprocessorDirs[] = {
38 { tgtok::Ifdef, "ifdef" },
39 { tgtok::Else, "else" },
40 { tgtok::Endif, "endif" },
41 { tgtok::Define, "define" }
42};
43} // end anonymous namespace
44
45TGLexer::TGLexer(SourceMgr &SM, ArrayRef<std::string> Macros) : SrcMgr(SM) {
Alp Tokera55b95b2014-07-06 10:33:31 +000046 CurBuffer = SrcMgr.getMainFileID();
Rafael Espindolaa3c65092014-07-06 14:24:03 +000047 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer();
48 CurPtr = CurBuf.begin();
Craig Topper011817a2014-04-09 04:50:04 +000049 TokStart = nullptr;
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +000050
51 // Pretend that we enter the "top-level" include file.
52 PrepIncludeStack.push_back(
53 make_unique<std::vector<PreprocessorControlDesc>>());
54
55 // Put all macros defined in the command line into the DefinedMacros set.
56 std::for_each(Macros.begin(), Macros.end(),
57 [this](const std::string &MacroName) {
58 DefinedMacros.insert(MacroName);
59 });
Chris Lattnerda4ab672007-11-18 02:57:27 +000060}
61
Chris Lattner526c8cb2009-06-21 03:39:35 +000062SMLoc TGLexer::getLoc() const {
63 return SMLoc::getFromPointer(TokStart);
Chris Lattner87710ca2009-03-13 16:01:53 +000064}
65
Chris Lattner1a262962007-11-19 07:38:58 +000066/// ReturnError - Set the error to the specified string at the specified
Chris Lattnerf4127dd2007-11-22 20:49:04 +000067/// location. This is defined to always return tgtok::Error.
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +000068tgtok::TokKind TGLexer::ReturnError(SMLoc Loc, const Twine &Msg) {
Chris Lattner1a262962007-11-19 07:38:58 +000069 PrintError(Loc, Msg);
Chris Lattnerf4127dd2007-11-22 20:49:04 +000070 return tgtok::Error;
Chris Lattner1a262962007-11-19 07:38:58 +000071}
Chris Lattnerda4ab672007-11-18 02:57:27 +000072
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +000073tgtok::TokKind TGLexer::ReturnError(const char *Loc, const Twine &Msg) {
74 return ReturnError(SMLoc::getFromPointer(Loc), Msg);
75}
76
77bool TGLexer::processEOF() {
78 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
79 if (ParentIncludeLoc != SMLoc()) {
80 // If prepExitInclude() detects a problem with the preprocessing
81 // control stack, it will return false. Pretend that we reached
82 // the final EOF and stop lexing more tokens by returning false
83 // to LexToken().
84 if (!prepExitInclude(false))
85 return false;
86
87 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
88 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer();
89 CurPtr = ParentIncludeLoc.getPointer();
90 // Make sure TokStart points into the parent file's buffer.
91 // LexToken() assigns to it before calling getNextChar(),
92 // so it is pointing into the included file now.
93 TokStart = CurPtr;
94 return true;
95 }
96
97 // Pretend that we exit the "top-level" include file.
98 // Note that in case of an error (e.g. control stack imbalance)
99 // the routine will issue a fatal error.
100 prepExitInclude(true);
101 return false;
102}
103
Chris Lattnerda4ab672007-11-18 02:57:27 +0000104int TGLexer::getNextChar() {
105 char CurChar = *CurPtr++;
106 switch (CurChar) {
107 default:
Chris Lattner60700282007-11-18 05:48:46 +0000108 return (unsigned char)CurChar;
Chris Lattner8db9bc72009-03-13 07:05:43 +0000109 case 0: {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000110 // A nul character in the stream is either the end of the current buffer or
111 // a random nul in the file. Disambiguate that here.
Rafael Espindolaa3c65092014-07-06 14:24:03 +0000112 if (CurPtr-1 != CurBuf.end())
Chris Lattnerda4ab672007-11-18 02:57:27 +0000113 return 0; // Just whitespace.
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000114
Chris Lattnerda4ab672007-11-18 02:57:27 +0000115 // Otherwise, return end of file.
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000116 --CurPtr; // Another call to lex will return EOF again.
Chris Lattnerda4ab672007-11-18 02:57:27 +0000117 return EOF;
Chris Lattner8db9bc72009-03-13 07:05:43 +0000118 }
Chris Lattnerda4ab672007-11-18 02:57:27 +0000119 case '\n':
120 case '\r':
121 // Handle the newline character by ignoring it and incrementing the line
122 // count. However, be careful about 'dos style' files with \n\r in them.
123 // Only treat a \n\r or \r\n as a single line.
124 if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
125 *CurPtr != CurChar)
Chris Lattner60700282007-11-18 05:48:46 +0000126 ++CurPtr; // Eat the two char newline sequence.
Chris Lattnerda4ab672007-11-18 02:57:27 +0000127 return '\n';
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000128 }
Chris Lattnerda4ab672007-11-18 02:57:27 +0000129}
130
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +0000131int TGLexer::peekNextChar(int Index) const {
David Greene9ba42082011-10-19 13:03:35 +0000132 return *(CurPtr + Index);
133}
134
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +0000135tgtok::TokKind TGLexer::LexToken(bool FileOrLineStart) {
Chris Lattner4205d252007-11-19 07:43:52 +0000136 TokStart = CurPtr;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000137 // This always consumes at least one character.
138 int CurChar = getNextChar();
139
140 switch (CurChar) {
141 default:
David Greene8e85b482011-10-19 13:04:43 +0000142 // Handle letters: [a-zA-Z_]
143 if (isalpha(CurChar) || CurChar == '_')
Chris Lattnerda4ab672007-11-18 02:57:27 +0000144 return LexIdentifier();
David Greene8e85b482011-10-19 13:04:43 +0000145
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000146 // Unknown character, emit an error.
147 return ReturnError(TokStart, "Unexpected character");
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +0000148 case EOF:
149 // Lex next token, if we just left an include file.
150 // Note that leaving an include file means that the next
151 // symbol is located at the end of 'include "..."'
152 // construct, so LexToken() is called with default
153 // false parameter.
154 if (processEOF())
155 return LexToken();
156
157 // Return EOF denoting the end of lexing.
158 return tgtok::Eof;
159
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000160 case ':': return tgtok::colon;
161 case ';': return tgtok::semi;
162 case '.': return tgtok::period;
163 case ',': return tgtok::comma;
164 case '<': return tgtok::less;
165 case '>': return tgtok::greater;
166 case ']': return tgtok::r_square;
167 case '{': return tgtok::l_brace;
168 case '}': return tgtok::r_brace;
169 case '(': return tgtok::l_paren;
170 case ')': return tgtok::r_paren;
171 case '=': return tgtok::equal;
172 case '?': return tgtok::question;
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +0000173 case '#':
174 if (FileOrLineStart) {
175 tgtok::TokKind Kind = prepIsDirective();
176 if (Kind != tgtok::Error)
177 return lexPreprocessor(Kind);
178 }
179
180 return tgtok::paste;
181
182 case '\r':
183 PrintFatalError("getNextChar() must never return '\r'");
184 return tgtok::Error;
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000185
Chris Lattnerda4ab672007-11-18 02:57:27 +0000186 case 0:
187 case ' ':
188 case '\t':
Vyacheslav Zakharin6a5d5ac2018-11-17 02:26:34 +0000189 // Ignore whitespace.
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +0000190 return LexToken(FileOrLineStart);
191 case '\n':
192 // Ignore whitespace, and identify the new line.
193 return LexToken(true);
Chris Lattnerda4ab672007-11-18 02:57:27 +0000194 case '/':
195 // If this is the start of a // comment, skip until the end of the line or
196 // the end of the buffer.
197 if (*CurPtr == '/')
198 SkipBCPLComment();
199 else if (*CurPtr == '*') {
200 if (SkipCComment())
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000201 return tgtok::Error;
202 } else // Otherwise, this is an error.
203 return ReturnError(TokStart, "Unexpected character");
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +0000204 return LexToken(FileOrLineStart);
Chris Lattnerda4ab672007-11-18 02:57:27 +0000205 case '-': case '+':
206 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
David Greene5c9fa022011-10-19 13:03:39 +0000207 case '7': case '8': case '9': {
208 int NextChar = 0;
209 if (isdigit(CurChar)) {
210 // Allow identifiers to start with a number if it is followed by
211 // an identifier. This can happen with paste operations like
212 // foo#8i.
213 int i = 0;
214 do {
215 NextChar = peekNextChar(i++);
216 } while (isdigit(NextChar));
217
218 if (NextChar == 'x' || NextChar == 'b') {
219 // If this is [0-9]b[01] or [0-9]x[0-9A-fa-f] this is most
220 // likely a number.
221 int NextNextChar = peekNextChar(i);
222 switch (NextNextChar) {
223 default:
224 break;
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000225 case '0': case '1':
David Greene5c9fa022011-10-19 13:03:39 +0000226 if (NextChar == 'b')
227 return LexNumber();
Justin Bognerb03fd122016-08-17 05:10:15 +0000228 LLVM_FALLTHROUGH;
David Greene5c9fa022011-10-19 13:03:39 +0000229 case '2': case '3': case '4': case '5':
230 case '6': case '7': case '8': case '9':
231 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
232 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
233 if (NextChar == 'x')
234 return LexNumber();
235 break;
236 }
237 }
238 }
239
240 if (isalpha(NextChar) || NextChar == '_')
241 return LexIdentifier();
242
Chris Lattnerda4ab672007-11-18 02:57:27 +0000243 return LexNumber();
David Greene5c9fa022011-10-19 13:03:39 +0000244 }
Chris Lattnerda4ab672007-11-18 02:57:27 +0000245 case '"': return LexString();
246 case '$': return LexVarName();
247 case '[': return LexBracket();
248 case '!': return LexExclaim();
249 }
250}
251
252/// LexString - Lex "[^"]*"
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000253tgtok::TokKind TGLexer::LexString() {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000254 const char *StrStart = CurPtr;
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000255
Chris Lattner1bd36742009-03-13 21:03:27 +0000256 CurStrVal = "";
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000257
Chris Lattnerda4ab672007-11-18 02:57:27 +0000258 while (*CurPtr != '"') {
259 // If we hit the end of the buffer, report an error.
Rafael Espindolaa3c65092014-07-06 14:24:03 +0000260 if (*CurPtr == 0 && CurPtr == CurBuf.end())
Chris Lattner1a262962007-11-19 07:38:58 +0000261 return ReturnError(StrStart, "End of file in string literal");
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000262
Chris Lattner1a262962007-11-19 07:38:58 +0000263 if (*CurPtr == '\n' || *CurPtr == '\r')
264 return ReturnError(StrStart, "End of line in string literal");
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000265
Chris Lattner1bd36742009-03-13 21:03:27 +0000266 if (*CurPtr != '\\') {
267 CurStrVal += *CurPtr++;
268 continue;
269 }
270
Chris Lattnerda4ab672007-11-18 02:57:27 +0000271 ++CurPtr;
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000272
Chris Lattner1bd36742009-03-13 21:03:27 +0000273 switch (*CurPtr) {
274 case '\\': case '\'': case '"':
275 // These turn into their literal character.
276 CurStrVal += *CurPtr++;
277 break;
Chris Lattnera614ef22009-03-13 21:23:43 +0000278 case 't':
Chris Lattner8bd06d82009-03-13 21:33:17 +0000279 CurStrVal += '\t';
Chris Lattnera614ef22009-03-13 21:23:43 +0000280 ++CurPtr;
281 break;
282 case 'n':
Chris Lattner8bd06d82009-03-13 21:33:17 +0000283 CurStrVal += '\n';
Chris Lattnera614ef22009-03-13 21:23:43 +0000284 ++CurPtr;
285 break;
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000286
Chris Lattner1bd36742009-03-13 21:03:27 +0000287 case '\n':
288 case '\r':
289 return ReturnError(CurPtr, "escaped newlines not supported in tblgen");
290
291 // If we hit the end of the buffer, report an error.
292 case '\0':
Rafael Espindolaa3c65092014-07-06 14:24:03 +0000293 if (CurPtr == CurBuf.end())
Chris Lattner1bd36742009-03-13 21:03:27 +0000294 return ReturnError(StrStart, "End of file in string literal");
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000295 LLVM_FALLTHROUGH;
Chris Lattner1bd36742009-03-13 21:03:27 +0000296 default:
297 return ReturnError(CurPtr, "invalid escape in string literal");
298 }
Chris Lattnerda4ab672007-11-18 02:57:27 +0000299 }
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000300
Chris Lattnerda4ab672007-11-18 02:57:27 +0000301 ++CurPtr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000302 return tgtok::StrVal;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000303}
304
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000305tgtok::TokKind TGLexer::LexVarName() {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000306 if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000307 return ReturnError(TokStart, "Invalid variable name");
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000308
Chris Lattnerda4ab672007-11-18 02:57:27 +0000309 // Otherwise, we're ok, consume the rest of the characters.
310 const char *VarNameStart = CurPtr++;
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000311
Chris Lattnerda4ab672007-11-18 02:57:27 +0000312 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
313 ++CurPtr;
314
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000315 CurStrVal.assign(VarNameStart, CurPtr);
316 return tgtok::VarName;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000317}
318
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000319tgtok::TokKind TGLexer::LexIdentifier() {
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +0000320 // The first letter is [a-zA-Z_].
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000321 const char *IdentStart = TokStart;
Benjamin Kramera54985e2011-10-06 18:23:56 +0000322
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +0000323 // Match the rest of the identifier regex: [0-9a-zA-Z_]*
David Greene8e85b482011-10-19 13:04:43 +0000324 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
Chris Lattnerb8ff8f02010-10-05 22:59:29 +0000325 ++CurPtr;
Benjamin Kramera54985e2011-10-06 18:23:56 +0000326
Chris Lattnerda4ab672007-11-18 02:57:27 +0000327 // Check to see if this identifier is a keyword.
Benjamin Kramera54985e2011-10-06 18:23:56 +0000328 StringRef Str(IdentStart, CurPtr-IdentStart);
329
Benjamin Kramera54985e2011-10-06 18:23:56 +0000330 if (Str == "include") {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000331 if (LexInclude()) return tgtok::Error;
332 return Lex();
Chris Lattnerda4ab672007-11-18 02:57:27 +0000333 }
Benjamin Kramera54985e2011-10-06 18:23:56 +0000334
Benjamin Kramerf9389a32011-10-06 18:53:43 +0000335 tgtok::TokKind Kind = StringSwitch<tgtok::TokKind>(Str)
336 .Case("int", tgtok::Int)
337 .Case("bit", tgtok::Bit)
338 .Case("bits", tgtok::Bits)
339 .Case("string", tgtok::String)
340 .Case("list", tgtok::List)
341 .Case("code", tgtok::Code)
342 .Case("dag", tgtok::Dag)
343 .Case("class", tgtok::Class)
344 .Case("def", tgtok::Def)
David Greenefb927af2012-02-22 16:09:41 +0000345 .Case("foreach", tgtok::Foreach)
Benjamin Kramerf9389a32011-10-06 18:53:43 +0000346 .Case("defm", tgtok::Defm)
Nicolai Haehnlefcd65252018-03-09 12:24:42 +0000347 .Case("defset", tgtok::Defset)
Benjamin Kramerf9389a32011-10-06 18:53:43 +0000348 .Case("multiclass", tgtok::MultiClass)
349 .Case("field", tgtok::Field)
350 .Case("let", tgtok::Let)
351 .Case("in", tgtok::In)
352 .Default(tgtok::Id);
353
354 if (Kind == tgtok::Id)
355 CurStrVal.assign(Str.begin(), Str.end());
356 return Kind;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000357}
358
359/// LexInclude - We just read the "include" token. Get the string token that
360/// comes next and enter the include.
361bool TGLexer::LexInclude() {
362 // The token after the include must be a string.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000363 tgtok::TokKind Tok = LexToken();
364 if (Tok == tgtok::Error) return true;
365 if (Tok != tgtok::StrVal) {
366 PrintError(getLoc(), "Expected filename after include");
Chris Lattnerda4ab672007-11-18 02:57:27 +0000367 return true;
368 }
369
370 // Get the string.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000371 std::string Filename = CurStrVal;
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000372 std::string IncludedFile;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000373
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000374 CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr),
375 IncludedFile);
Alp Tokera55b95b2014-07-06 10:33:31 +0000376 if (!CurBuffer) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000377 PrintError(getLoc(), "Could not find include file '" + Filename + "'");
Chris Lattnerda4ab672007-11-18 02:57:27 +0000378 return true;
379 }
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000380
Sean Silva3b964242013-02-07 04:30:39 +0000381 DependenciesMapTy::const_iterator Found = Dependencies.find(IncludedFile);
382 if (Found != Dependencies.end()) {
383 PrintError(getLoc(),
384 "File '" + IncludedFile + "' has already been included.");
385 SrcMgr.PrintMessage(Found->second, SourceMgr::DK_Note,
386 "previously included here");
387 return true;
388 }
389 Dependencies.insert(std::make_pair(IncludedFile, getLoc()));
Chris Lattnerda4ab672007-11-18 02:57:27 +0000390 // Save the line number and lex buffer of the includer.
Rafael Espindolaa3c65092014-07-06 14:24:03 +0000391 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer();
392 CurPtr = CurBuf.begin();
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +0000393
394 PrepIncludeStack.push_back(
395 make_unique<std::vector<PreprocessorControlDesc>>());
Chris Lattnerda4ab672007-11-18 02:57:27 +0000396 return false;
397}
398
399void TGLexer::SkipBCPLComment() {
400 ++CurPtr; // skip the second slash.
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000401 while (true) {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000402 switch (*CurPtr) {
403 case '\n':
404 case '\r':
405 return; // Newline is end of comment.
406 case 0:
407 // If this is the end of the buffer, end the comment.
Rafael Espindolaa3c65092014-07-06 14:24:03 +0000408 if (CurPtr == CurBuf.end())
Chris Lattnerda4ab672007-11-18 02:57:27 +0000409 return;
410 break;
411 }
412 // Otherwise, skip the character.
413 ++CurPtr;
414 }
415}
416
417/// SkipCComment - This skips C-style /**/ comments. The only difference from C
418/// is that we allow nesting.
419bool TGLexer::SkipCComment() {
420 ++CurPtr; // skip the star.
421 unsigned CommentDepth = 1;
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000422
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000423 while (true) {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000424 int CurChar = getNextChar();
425 switch (CurChar) {
426 case EOF:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000427 PrintError(TokStart, "Unterminated comment!");
Chris Lattnerda4ab672007-11-18 02:57:27 +0000428 return true;
429 case '*':
430 // End of the comment?
431 if (CurPtr[0] != '/') break;
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000432
Chris Lattnerda4ab672007-11-18 02:57:27 +0000433 ++CurPtr; // End the */.
434 if (--CommentDepth == 0)
435 return false;
436 break;
437 case '/':
438 // Start of a nested comment?
439 if (CurPtr[0] != '*') break;
440 ++CurPtr;
441 ++CommentDepth;
442 break;
443 }
444 }
445}
446
447/// LexNumber - Lex:
448/// [-+]?[0-9]+
449/// 0x[0-9a-fA-F]+
450/// 0b[01]+
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000451tgtok::TokKind TGLexer::LexNumber() {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000452 if (CurPtr[-1] == '0') {
453 if (CurPtr[0] == 'x') {
454 ++CurPtr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000455 const char *NumStart = CurPtr;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000456 while (isxdigit(CurPtr[0]))
457 ++CurPtr;
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000458
Chris Lattner1a262962007-11-19 07:38:58 +0000459 // Requires at least one hex digit.
460 if (CurPtr == NumStart)
Chris Lattner7d4c0d5f2009-06-21 19:22:49 +0000461 return ReturnError(TokStart, "Invalid hexadecimal number");
Chris Lattner1a262962007-11-19 07:38:58 +0000462
Dan Gohmanca0546f2008-10-17 01:33:43 +0000463 errno = 0;
Craig Topper011817a2014-04-09 04:50:04 +0000464 CurIntVal = strtoll(NumStart, nullptr, 16);
Dan Gohmanca0546f2008-10-17 01:33:43 +0000465 if (errno == EINVAL)
Chris Lattner7d4c0d5f2009-06-21 19:22:49 +0000466 return ReturnError(TokStart, "Invalid hexadecimal number");
Dan Gohmanca0546f2008-10-17 01:33:43 +0000467 if (errno == ERANGE) {
468 errno = 0;
Craig Topper011817a2014-04-09 04:50:04 +0000469 CurIntVal = (int64_t)strtoull(NumStart, nullptr, 16);
Dan Gohmanca0546f2008-10-17 01:33:43 +0000470 if (errno == EINVAL)
Chris Lattner7d4c0d5f2009-06-21 19:22:49 +0000471 return ReturnError(TokStart, "Invalid hexadecimal number");
Dan Gohmanca0546f2008-10-17 01:33:43 +0000472 if (errno == ERANGE)
Chris Lattner7d4c0d5f2009-06-21 19:22:49 +0000473 return ReturnError(TokStart, "Hexadecimal number out of range");
Dan Gohmanca0546f2008-10-17 01:33:43 +0000474 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000475 return tgtok::IntVal;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000476 } else if (CurPtr[0] == 'b') {
477 ++CurPtr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000478 const char *NumStart = CurPtr;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000479 while (CurPtr[0] == '0' || CurPtr[0] == '1')
480 ++CurPtr;
Chris Lattner1a262962007-11-19 07:38:58 +0000481
482 // Requires at least one binary digit.
483 if (CurPtr == NumStart)
484 return ReturnError(CurPtr-2, "Invalid binary number");
Craig Topper011817a2014-04-09 04:50:04 +0000485 CurIntVal = strtoll(NumStart, nullptr, 2);
Pete Cooper25977642014-08-07 05:47:00 +0000486 return tgtok::BinaryIntVal;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000487 }
488 }
489
490 // Check for a sign without a digit.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000491 if (!isdigit(CurPtr[0])) {
492 if (CurPtr[-1] == '-')
493 return tgtok::minus;
494 else if (CurPtr[-1] == '+')
495 return tgtok::plus;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000496 }
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000497
Chris Lattnerda4ab672007-11-18 02:57:27 +0000498 while (isdigit(CurPtr[0]))
499 ++CurPtr;
Craig Topper011817a2014-04-09 04:50:04 +0000500 CurIntVal = strtoll(TokStart, nullptr, 10);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000501 return tgtok::IntVal;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000502}
503
504/// LexBracket - We just read '['. If this is a code block, return it,
505/// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000506tgtok::TokKind TGLexer::LexBracket() {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000507 if (CurPtr[0] != '{')
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000508 return tgtok::l_square;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000509 ++CurPtr;
510 const char *CodeStart = CurPtr;
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000511 while (true) {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000512 int Char = getNextChar();
513 if (Char == EOF) break;
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000514
Chris Lattnerda4ab672007-11-18 02:57:27 +0000515 if (Char != '}') continue;
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000516
Chris Lattnerda4ab672007-11-18 02:57:27 +0000517 Char = getNextChar();
518 if (Char == EOF) break;
519 if (Char == ']') {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000520 CurStrVal.assign(CodeStart, CurPtr-2);
521 return tgtok::CodeFragment;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000522 }
523 }
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000524
Chris Lattner1a262962007-11-19 07:38:58 +0000525 return ReturnError(CodeStart-2, "Unterminated Code Block");
Chris Lattnerda4ab672007-11-18 02:57:27 +0000526}
527
528/// LexExclaim - Lex '!' and '![a-zA-Z]+'.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000529tgtok::TokKind TGLexer::LexExclaim() {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000530 if (!isalpha(*CurPtr))
Bill Wendling4182a162010-12-08 13:03:15 +0000531 return ReturnError(CurPtr - 1, "Invalid \"!operator\"");
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000532
Chris Lattnerda4ab672007-11-18 02:57:27 +0000533 const char *Start = CurPtr++;
534 while (isalpha(*CurPtr))
535 ++CurPtr;
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000536
Chris Lattnerda4ab672007-11-18 02:57:27 +0000537 // Check to see which operator this is.
Bill Wendling55bc7182010-12-08 20:02:49 +0000538 tgtok::TokKind Kind =
539 StringSwitch<tgtok::TokKind>(StringRef(Start, CurPtr - Start))
540 .Case("eq", tgtok::XEq)
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +0000541 .Case("ne", tgtok::XNe)
542 .Case("le", tgtok::XLe)
543 .Case("lt", tgtok::XLt)
544 .Case("ge", tgtok::XGe)
545 .Case("gt", tgtok::XGt)
Bill Wendling55bc7182010-12-08 20:02:49 +0000546 .Case("if", tgtok::XIf)
Nicolai Haehnleb5376052018-03-09 12:24:06 +0000547 .Case("isa", tgtok::XIsA)
David Greene2f7cf7f2011-01-07 17:05:37 +0000548 .Case("head", tgtok::XHead)
549 .Case("tail", tgtok::XTail)
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000550 .Case("size", tgtok::XSize)
Bill Wendling55bc7182010-12-08 20:02:49 +0000551 .Case("con", tgtok::XConcat)
Nicolai Haehnle6c118652018-03-14 11:00:26 +0000552 .Case("dag", tgtok::XDag)
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000553 .Case("add", tgtok::XADD)
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000554 .Case("and", tgtok::XAND)
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000555 .Case("or", tgtok::XOR)
Bill Wendling55bc7182010-12-08 20:02:49 +0000556 .Case("shl", tgtok::XSHL)
557 .Case("sra", tgtok::XSRA)
558 .Case("srl", tgtok::XSRL)
559 .Case("cast", tgtok::XCast)
David Greene2f7cf7f2011-01-07 17:05:37 +0000560 .Case("empty", tgtok::XEmpty)
Bill Wendling55bc7182010-12-08 20:02:49 +0000561 .Case("subst", tgtok::XSubst)
Nicolai Haehnled34f6842018-03-06 13:49:16 +0000562 .Case("foldl", tgtok::XFoldl)
Bill Wendling55bc7182010-12-08 20:02:49 +0000563 .Case("foreach", tgtok::XForEach)
Daniel Sanders314e80e2014-05-07 10:13:19 +0000564 .Case("listconcat", tgtok::XListConcat)
Bill Wendling55bc7182010-12-08 20:02:49 +0000565 .Case("strconcat", tgtok::XStrConcat)
566 .Default(tgtok::Error);
David Greene5d0c0512009-05-14 20:54:48 +0000567
Bill Wendling55bc7182010-12-08 20:02:49 +0000568 return Kind != tgtok::Error ? Kind : ReturnError(Start-1, "Unknown operator");
Chris Lattnerda4ab672007-11-18 02:57:27 +0000569}
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +0000570
571bool TGLexer::prepExitInclude(bool IncludeStackMustBeEmpty) {
572 // Report an error, if preprocessor control stack for the current
573 // file is not empty.
574 if (!PrepIncludeStack.back()->empty()) {
575 prepReportPreprocessorStackError();
576
577 return false;
578 }
579
580 // Pop the preprocessing controls from the include stack.
581 if (PrepIncludeStack.empty()) {
582 PrintFatalError("Preprocessor include stack is empty");
583 }
584
585 PrepIncludeStack.pop_back();
586
587 if (IncludeStackMustBeEmpty) {
588 if (!PrepIncludeStack.empty())
589 PrintFatalError("Preprocessor include stack is not empty");
590 } else {
591 if (PrepIncludeStack.empty())
592 PrintFatalError("Preprocessor include stack is empty");
593 }
594
595 return true;
596}
597
598tgtok::TokKind TGLexer::prepIsDirective() const {
599 for (unsigned ID = 0; ID < llvm::array_lengthof(PreprocessorDirs); ++ID) {
600 int NextChar = *CurPtr;
601 bool Match = true;
602 unsigned I = 0;
603 for (; I < strlen(PreprocessorDirs[ID].Word); ++I) {
604 if (NextChar != PreprocessorDirs[ID].Word[I]) {
605 Match = false;
606 break;
607 }
608
609 NextChar = peekNextChar(I + 1);
610 }
611
612 // Check for whitespace after the directive. If there is no whitespace,
613 // then we do not recognize it as a preprocessing directive.
614 if (Match) {
615 tgtok::TokKind Kind = PreprocessorDirs[ID].Kind;
616
617 // New line and EOF may follow only #else/#endif. It will be reported
618 // as an error for #ifdef/#define after the call to prepLexMacroName().
619 if (NextChar == ' ' || NextChar == '\t' || NextChar == EOF ||
620 NextChar == '\n' ||
621 // It looks like TableGen does not support '\r' as the actual
622 // carriage return, e.g. getNextChar() treats a single '\r'
623 // as '\n'. So we do the same here.
624 NextChar == '\r')
625 return Kind;
626
627 // Allow comments after some directives, e.g.:
628 // #else// OR #else/**/
629 // #endif// OR #endif/**/
630 //
631 // Note that we do allow comments after #ifdef/#define here, e.g.
632 // #ifdef/**/ AND #ifdef//
633 // #define/**/ AND #define//
634 //
635 // These cases will be reported as incorrect after calling
636 // prepLexMacroName(). We could have supported C-style comments
637 // after #ifdef/#define, but this would complicate the code
638 // for little benefit.
639 if (NextChar == '/') {
640 NextChar = peekNextChar(I + 1);
641
642 if (NextChar == '*' || NextChar == '/')
643 return Kind;
644
645 // Pretend that we do not recognize the directive.
646 }
647 }
648 }
649
650 return tgtok::Error;
651}
652
653bool TGLexer::prepEatPreprocessorDirective(tgtok::TokKind Kind) {
654 TokStart = CurPtr;
655
656 for (unsigned ID = 0; ID < llvm::array_lengthof(PreprocessorDirs); ++ID)
657 if (PreprocessorDirs[ID].Kind == Kind) {
658 // Advance CurPtr to the end of the preprocessing word.
659 CurPtr += strlen(PreprocessorDirs[ID].Word);
660 return true;
661 }
662
663 PrintFatalError("Unsupported preprocessing token in "
664 "prepEatPreprocessorDirective()");
665 return false;
666}
667
668tgtok::TokKind TGLexer::lexPreprocessor(
669 tgtok::TokKind Kind, bool ReturnNextLiveToken) {
670
671 // We must be looking at a preprocessing directive. Eat it!
672 if (!prepEatPreprocessorDirective(Kind))
673 PrintFatalError("lexPreprocessor() called for unknown "
674 "preprocessor directive");
675
676 if (Kind == tgtok::Ifdef) {
677 StringRef MacroName = prepLexMacroName();
678 if (MacroName.empty())
679 return ReturnError(TokStart, "Expected macro name after #ifdef");
680
681 bool MacroIsDefined = DefinedMacros.count(MacroName) != 0;
682
683 // Regardless of whether we are processing tokens or not,
684 // we put the #ifdef control on stack.
685 PrepIncludeStack.back()->push_back(
686 {Kind, MacroIsDefined, SMLoc::getFromPointer(TokStart)});
687
688 if (!prepSkipDirectiveEnd())
689 return ReturnError(CurPtr,
690 "Only comments are supported after #ifdef NAME");
691
692 // If we were not processing tokens before this #ifdef,
693 // then just return back to the lines skipping code.
694 if (!ReturnNextLiveToken)
695 return Kind;
696
697 // If we were processing tokens before this #ifdef,
698 // and the macro is defined, then just return the next token.
699 if (MacroIsDefined)
700 return LexToken();
701
702 // We were processing tokens before this #ifdef, and the macro
703 // is not defined, so we have to start skipping the lines.
704 // If the skipping is successful, it will return the token following
705 // either #else or #endif corresponding to this #ifdef.
706 if (prepSkipRegion(ReturnNextLiveToken))
707 return LexToken();
708
709 return tgtok::Error;
710 } else if (Kind == tgtok::Else) {
711 // Check if this #else is correct before calling prepSkipDirectiveEnd(),
712 // which will move CurPtr away from the beginning of #else.
713 if (PrepIncludeStack.back()->empty())
714 return ReturnError(TokStart, "#else without #ifdef");
715
716 PreprocessorControlDesc IfdefEntry = PrepIncludeStack.back()->back();
717
718 if (IfdefEntry.Kind != tgtok::Ifdef) {
719 PrintError(TokStart, "double #else");
720 return ReturnError(IfdefEntry.SrcPos, "Previous #else is here");
721 }
722
723 // Replace the corresponding #ifdef's control with its negation
724 // on the control stack.
725 PrepIncludeStack.back()->pop_back();
726 PrepIncludeStack.back()->push_back(
727 {Kind, !IfdefEntry.IsDefined, SMLoc::getFromPointer(TokStart)});
728
729 if (!prepSkipDirectiveEnd())
730 return ReturnError(CurPtr, "Only comments are supported after #else");
731
732 // If we were processing tokens before this #else,
733 // we have to start skipping lines until the matching #endif.
734 if (ReturnNextLiveToken) {
735 if (prepSkipRegion(ReturnNextLiveToken))
736 return LexToken();
737
738 return tgtok::Error;
739 }
740
741 // Return to the lines skipping code.
742 return Kind;
743 } else if (Kind == tgtok::Endif) {
744 // Check if this #endif is correct before calling prepSkipDirectiveEnd(),
745 // which will move CurPtr away from the beginning of #endif.
746 if (PrepIncludeStack.back()->empty())
747 return ReturnError(TokStart, "#endif without #ifdef");
748
749 auto &IfdefOrElseEntry = PrepIncludeStack.back()->back();
750
751 if (IfdefOrElseEntry.Kind != tgtok::Ifdef &&
752 IfdefOrElseEntry.Kind != tgtok::Else) {
753 PrintFatalError("Invalid preprocessor control on the stack");
754 return tgtok::Error;
755 }
756
757 if (!prepSkipDirectiveEnd())
758 return ReturnError(CurPtr, "Only comments are supported after #endif");
759
760 PrepIncludeStack.back()->pop_back();
761
762 // If we were processing tokens before this #endif, then
763 // we should continue it.
764 if (ReturnNextLiveToken) {
765 return LexToken();
766 }
767
768 // Return to the lines skipping code.
769 return Kind;
770 } else if (Kind == tgtok::Define) {
771 StringRef MacroName = prepLexMacroName();
772 if (MacroName.empty())
773 return ReturnError(TokStart, "Expected macro name after #define");
774
775 if (!DefinedMacros.insert(MacroName).second)
776 PrintWarning(getLoc(),
777 "Duplicate definition of macro: " + Twine(MacroName));
778
779 if (!prepSkipDirectiveEnd())
780 return ReturnError(CurPtr,
781 "Only comments are supported after #define NAME");
782
783 if (!ReturnNextLiveToken) {
784 PrintFatalError("#define must be ignored during the lines skipping");
785 return tgtok::Error;
786 }
787
788 return LexToken();
789 }
790
791 PrintFatalError("Preprocessing directive is not supported");
792 return tgtok::Error;
793}
794
795bool TGLexer::prepSkipRegion(bool MustNeverBeFalse) {
796 if (!MustNeverBeFalse)
797 PrintFatalError("Invalid recursion.");
798
799 do {
800 // Skip all symbols to the line end.
801 prepSkipToLineEnd();
802
803 // Find the first non-whitespace symbol in the next line(s).
804 if (!prepSkipLineBegin())
805 return false;
806
807 // If the first non-blank/comment symbol on the line is '#',
808 // it may be a start of preprocessing directive.
809 //
810 // If it is not '#' just go to the next line.
811 if (*CurPtr == '#')
812 ++CurPtr;
813 else
814 continue;
815
816 tgtok::TokKind Kind = prepIsDirective();
817
818 // If we did not find a preprocessing directive or it is #define,
819 // then just skip to the next line. We do not have to do anything
820 // for #define in the line-skipping mode.
821 if (Kind == tgtok::Error || Kind == tgtok::Define)
822 continue;
823
824 tgtok::TokKind ProcessedKind = lexPreprocessor(Kind, false);
825
826 // If lexPreprocessor() encountered an error during lexing this
827 // preprocessor idiom, then return false to the calling lexPreprocessor().
828 // This will force tgtok::Error to be returned to the tokens processing.
829 if (ProcessedKind == tgtok::Error)
830 return false;
831
832 if (Kind != ProcessedKind)
833 PrintFatalError("prepIsDirective() and lexPreprocessor() "
834 "returned different token kinds");
835
836 // If this preprocessing directive enables tokens processing,
837 // then return to the lexPreprocessor() and get to the next token.
838 // We can move from line-skipping mode to processing tokens only
839 // due to #else or #endif.
840 if (prepIsProcessingEnabled()) {
841 if (Kind != tgtok::Else && Kind != tgtok::Endif) {
842 PrintFatalError("Tokens processing was enabled by an unexpected "
843 "preprocessing directive");
844 return false;
845 }
846
847 return true;
848 }
849 } while (CurPtr != CurBuf.end());
850
851 // We have reached the end of the file, but never left the lines-skipping
852 // mode. This means there is no matching #endif.
853 prepReportPreprocessorStackError();
854 return false;
855}
856
857StringRef TGLexer::prepLexMacroName() {
858 // Skip whitespaces between the preprocessing directive and the macro name.
859 while (*CurPtr == ' ' || *CurPtr == '\t')
860 ++CurPtr;
861
862 TokStart = CurPtr;
863 // Macro names start with [a-zA-Z_].
864 if (*CurPtr != '_' && !isalpha(*CurPtr))
865 return "";
866
867 // Match the rest of the identifier regex: [0-9a-zA-Z_]*
868 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
869 ++CurPtr;
870
871 return StringRef(TokStart, CurPtr - TokStart);
872}
873
874bool TGLexer::prepSkipLineBegin() {
875 while (CurPtr != CurBuf.end()) {
876 switch (*CurPtr) {
877 case ' ':
878 case '\t':
879 case '\n':
880 case '\r':
881 break;
882
883 case '/': {
884 int NextChar = peekNextChar(1);
885 if (NextChar == '*') {
886 // Skip C-style comment.
887 // Note that we do not care about skipping the C++-style comments.
888 // If the line contains "//", it may not contain any processable
889 // preprocessing directive. Just return CurPtr pointing to
890 // the first '/' in this case. We also do not care about
891 // incorrect symbols after the first '/' - we are in lines-skipping
892 // mode, so incorrect code is allowed to some extent.
893
894 // Set TokStart to the beginning of the comment to enable proper
895 // diagnostic printing in case of error in SkipCComment().
896 TokStart = CurPtr;
897
898 // CurPtr must point to '*' before call to SkipCComment().
899 ++CurPtr;
900 if (SkipCComment())
901 return false;
902 } else {
903 // CurPtr points to the non-whitespace '/'.
904 return true;
905 }
906
907 // We must not increment CurPtr after the comment was lexed.
908 continue;
909 }
910
911 default:
912 return true;
913 }
914
915 ++CurPtr;
916 }
917
918 // We have reached the end of the file. Return to the lines skipping
919 // code, and allow it to handle the EOF as needed.
920 return true;
921}
922
923bool TGLexer::prepSkipDirectiveEnd() {
924 while (CurPtr != CurBuf.end()) {
925 switch (*CurPtr) {
926 case ' ':
927 case '\t':
928 break;
929
930 case '\n':
931 case '\r':
932 return true;
933
934 case '/': {
935 int NextChar = peekNextChar(1);
936 if (NextChar == '/') {
937 // Skip C++-style comment.
938 // We may just return true now, but let's skip to the line/buffer end
939 // to simplify the method specification.
940 ++CurPtr;
941 SkipBCPLComment();
942 } else if (NextChar == '*') {
943 // When we are skipping C-style comment at the end of a preprocessing
944 // directive, we can skip several lines. If any meaningful TD token
945 // follows the end of the C-style comment on the same line, it will
946 // be considered as an invalid usage of TD token.
947 // For example, we want to forbid usages like this one:
948 // #define MACRO class Class {}
949 // But with C-style comments we also disallow the following:
950 // #define MACRO /* This macro is used
951 // to ... */ class Class {}
952 // One can argue that this should be allowed, but it does not seem
953 // to be worth of the complication. Moreover, this matches
954 // the C preprocessor behavior.
955
956 // Set TokStart to the beginning of the comment to enable proper
957 // diagnostic printer in case of error in SkipCComment().
958 TokStart = CurPtr;
959 ++CurPtr;
960 if (SkipCComment())
961 return false;
962 } else {
963 TokStart = CurPtr;
964 PrintError(CurPtr, "Unexpected character");
965 return false;
966 }
967
968 // We must not increment CurPtr after the comment was lexed.
969 continue;
970 }
971
972 default:
973 // Do not allow any non-whitespaces after the directive.
974 TokStart = CurPtr;
975 return false;
976 }
977
978 ++CurPtr;
979 }
980
981 return true;
982}
983
984void TGLexer::prepSkipToLineEnd() {
985 while (*CurPtr != '\n' && *CurPtr != '\r' && CurPtr != CurBuf.end())
986 ++CurPtr;
987}
988
989bool TGLexer::prepIsProcessingEnabled() {
990 for (auto I = PrepIncludeStack.back()->rbegin(),
991 E = PrepIncludeStack.back()->rend();
992 I != E; ++I) {
993 if (!I->IsDefined)
994 return false;
995 }
996
997 return true;
998}
999
1000void TGLexer::prepReportPreprocessorStackError() {
1001 if (PrepIncludeStack.back()->empty())
1002 PrintFatalError("prepReportPreprocessorStackError() called with "
1003 "empty control stack");
1004
1005 auto &PrepControl = PrepIncludeStack.back()->back();
1006 PrintError(CurBuf.end(), "Reached EOF without matching #endif");
1007 PrintError(PrepControl.SrcPos, "The latest preprocessor control is here");
1008
1009 TokStart = CurPtr;
1010}