1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "clang/Lex/TokenLexer.h" |
14 | #include "clang/Basic/Diagnostic.h" |
15 | #include "clang/Basic/IdentifierTable.h" |
16 | #include "clang/Basic/LangOptions.h" |
17 | #include "clang/Basic/SourceLocation.h" |
18 | #include "clang/Basic/SourceManager.h" |
19 | #include "clang/Basic/TokenKinds.h" |
20 | #include "clang/Lex/LexDiagnostic.h" |
21 | #include "clang/Lex/Lexer.h" |
22 | #include "clang/Lex/MacroArgs.h" |
23 | #include "clang/Lex/MacroInfo.h" |
24 | #include "clang/Lex/Preprocessor.h" |
25 | #include "clang/Lex/Token.h" |
26 | #include "clang/Lex/VariadicMacroSupport.h" |
27 | #include "llvm/ADT/ArrayRef.h" |
28 | #include "llvm/ADT/SmallString.h" |
29 | #include "llvm/ADT/SmallVector.h" |
30 | #include "llvm/ADT/iterator_range.h" |
31 | #include <cassert> |
32 | #include <cstring> |
33 | |
34 | using namespace clang; |
35 | |
36 | |
37 | |
38 | void TokenLexer::Init(Token &Tok, SourceLocation ELEnd, MacroInfo *MI, |
39 | MacroArgs *Actuals) { |
40 | |
41 | |
42 | destroy(); |
43 | |
44 | Macro = MI; |
45 | ActualArgs = Actuals; |
46 | CurTokenIdx = 0; |
47 | |
48 | ExpandLocStart = Tok.getLocation(); |
49 | ExpandLocEnd = ELEnd; |
50 | AtStartOfLine = Tok.isAtStartOfLine(); |
51 | HasLeadingSpace = Tok.hasLeadingSpace(); |
52 | NextTokGetsSpace = false; |
53 | Tokens = &*Macro->tokens_begin(); |
54 | OwnsTokens = false; |
55 | DisableMacroExpansion = false; |
56 | NumTokens = Macro->tokens_end()-Macro->tokens_begin(); |
57 | MacroExpansionStart = SourceLocation(); |
58 | |
59 | SourceManager &SM = PP.getSourceManager(); |
60 | MacroStartSLocOffset = SM.getNextLocalOffset(); |
61 | |
62 | if (NumTokens > 0) { |
63 | assert(Tokens[0].getLocation().isValid()); |
64 | (0) . __assert_fail ("(Tokens[0].getLocation().isFileID() || Tokens[0].is(tok..comment)) && \"Macro defined in macro?\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 65, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((Tokens[0].getLocation().isFileID() || Tokens[0].is(tok::comment)) && |
65 | (0) . __assert_fail ("(Tokens[0].getLocation().isFileID() || Tokens[0].is(tok..comment)) && \"Macro defined in macro?\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 65, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Macro defined in macro?"); |
66 | assert(ExpandLocStart.isValid()); |
67 | |
68 | |
69 | |
70 | |
71 | |
72 | MacroDefStart = SM.getExpansionLoc(Tokens[0].getLocation()); |
73 | MacroDefLength = Macro->getDefinitionLength(SM); |
74 | MacroExpansionStart = SM.createExpansionLoc(MacroDefStart, |
75 | ExpandLocStart, |
76 | ExpandLocEnd, |
77 | MacroDefLength); |
78 | } |
79 | |
80 | |
81 | |
82 | if (Macro->isFunctionLike() && Macro->getNumParams()) |
83 | ExpandFunctionArguments(); |
84 | |
85 | |
86 | |
87 | |
88 | Macro->DisableMacro(); |
89 | } |
90 | |
91 | |
92 | |
93 | void TokenLexer::Init(const Token *TokArray, unsigned NumToks, |
94 | bool disableMacroExpansion, bool ownsTokens) { |
95 | |
96 | |
97 | destroy(); |
98 | |
99 | Macro = nullptr; |
100 | ActualArgs = nullptr; |
101 | Tokens = TokArray; |
102 | OwnsTokens = ownsTokens; |
103 | DisableMacroExpansion = disableMacroExpansion; |
104 | NumTokens = NumToks; |
105 | CurTokenIdx = 0; |
106 | ExpandLocStart = ExpandLocEnd = SourceLocation(); |
107 | AtStartOfLine = false; |
108 | HasLeadingSpace = false; |
109 | NextTokGetsSpace = false; |
110 | MacroExpansionStart = SourceLocation(); |
111 | |
112 | |
113 | |
114 | if (NumToks != 0) { |
115 | AtStartOfLine = TokArray[0].isAtStartOfLine(); |
116 | HasLeadingSpace = TokArray[0].hasLeadingSpace(); |
117 | } |
118 | } |
119 | |
120 | void TokenLexer::destroy() { |
121 | |
122 | |
123 | if (OwnsTokens) { |
124 | delete [] Tokens; |
125 | Tokens = nullptr; |
126 | OwnsTokens = false; |
127 | } |
128 | |
129 | |
130 | if (ActualArgs) ActualArgs->destroy(PP); |
131 | } |
132 | |
133 | bool TokenLexer::MaybeRemoveCommaBeforeVaArgs( |
134 | SmallVectorImpl<Token> &ResultToks, bool HasPasteOperator, MacroInfo *Macro, |
135 | unsigned MacroArgNo, Preprocessor &PP) { |
136 | |
137 | if (!Macro->isVariadic() || MacroArgNo != Macro->getNumParams()-1) |
138 | return false; |
139 | |
140 | |
141 | |
142 | |
143 | if (!HasPasteOperator && !PP.getLangOpts().MSVCCompat) |
144 | return false; |
145 | |
146 | |
147 | |
148 | |
149 | |
150 | |
151 | if (PP.getLangOpts().C99 && !PP.getLangOpts().GNUMode |
152 | && Macro->getNumParams() < 2) |
153 | return false; |
154 | |
155 | |
156 | if (ResultToks.empty() || !ResultToks.back().is(tok::comma)) |
157 | return false; |
158 | |
159 | |
160 | if (HasPasteOperator) |
161 | PP.Diag(ResultToks.back().getLocation(), diag::ext_paste_comma); |
162 | |
163 | |
164 | ResultToks.pop_back(); |
165 | |
166 | if (!ResultToks.empty()) { |
167 | |
168 | |
169 | |
170 | |
171 | if (ResultToks.back().is(tok::hashhash)) |
172 | ResultToks.pop_back(); |
173 | |
174 | |
175 | ResultToks.back().setFlag(Token::CommaAfterElided); |
176 | } |
177 | |
178 | |
179 | NextTokGetsSpace = false; |
180 | return true; |
181 | } |
182 | |
183 | void TokenLexer::stringifyVAOPTContents( |
184 | SmallVectorImpl<Token> &ResultToks, const VAOptExpansionContext &VCtx, |
185 | const SourceLocation VAOPTClosingParenLoc) { |
186 | const int NumToksPriorToVAOpt = VCtx.getNumberOfTokensPriorToVAOpt(); |
187 | const unsigned int NumVAOptTokens = ResultToks.size() - NumToksPriorToVAOpt; |
188 | Token *const VAOPTTokens = |
189 | NumVAOptTokens ? &ResultToks[NumToksPriorToVAOpt] : nullptr; |
190 | |
191 | SmallVector<Token, 64> ConcatenatedVAOPTResultToks; |
192 | |
193 | |
194 | |
195 | |
196 | for (unsigned int CurTokenIdx = 0; CurTokenIdx != NumVAOptTokens; |
197 | ++CurTokenIdx) { |
198 | if (VAOPTTokens[CurTokenIdx].is(tok::hashhash)) { |
199 | (0) . __assert_fail ("CurTokenIdx != 0 && \"Can not have __VAOPT__ contents begin with a ##\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 200, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CurTokenIdx != 0 && |
200 | (0) . __assert_fail ("CurTokenIdx != 0 && \"Can not have __VAOPT__ contents begin with a ##\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 200, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Can not have __VAOPT__ contents begin with a ##"); |
201 | Token &LHS = VAOPTTokens[CurTokenIdx - 1]; |
202 | pasteTokens(LHS, llvm::makeArrayRef(VAOPTTokens, NumVAOptTokens), |
203 | CurTokenIdx); |
204 | |
205 | ConcatenatedVAOPTResultToks.back() = LHS; |
206 | if (CurTokenIdx == NumVAOptTokens) |
207 | break; |
208 | } |
209 | ConcatenatedVAOPTResultToks.push_back(VAOPTTokens[CurTokenIdx]); |
210 | } |
211 | |
212 | ConcatenatedVAOPTResultToks.push_back(VCtx.getEOFTok()); |
213 | |
214 | |
215 | |
216 | |
217 | |
218 | const SourceLocation ExpansionLocStartWithinMacro = |
219 | getExpansionLocForMacroDefLoc(VCtx.getVAOptLoc()); |
220 | const SourceLocation ExpansionLocEndWithinMacro = |
221 | getExpansionLocForMacroDefLoc(VAOPTClosingParenLoc); |
222 | |
223 | Token StringifiedVAOPT = MacroArgs::StringifyArgument( |
224 | &ConcatenatedVAOPTResultToks[0], PP, VCtx.hasCharifyBefore() , |
225 | ExpansionLocStartWithinMacro, ExpansionLocEndWithinMacro); |
226 | |
227 | if (VCtx.getLeadingSpaceForStringifiedToken()) |
228 | StringifiedVAOPT.setFlag(Token::LeadingSpace); |
229 | |
230 | StringifiedVAOPT.setFlag(Token::StringifiedInMacro); |
231 | |
232 | ResultToks.resize(NumToksPriorToVAOpt + 1); |
233 | ResultToks.back() = StringifiedVAOPT; |
234 | } |
235 | |
236 | |
237 | |
238 | void TokenLexer::ExpandFunctionArguments() { |
239 | SmallVector<Token, 128> ResultToks; |
240 | |
241 | |
242 | |
243 | |
244 | bool MadeChange = false; |
245 | |
246 | const bool CalledWithVariadicArguments = |
247 | ActualArgs->invokedWithVariadicArgument(Macro); |
248 | |
249 | VAOptExpansionContext VCtx(PP); |
250 | |
251 | for (unsigned I = 0, E = NumTokens; I != E; ++I) { |
252 | const Token &CurTok = Tokens[I]; |
253 | |
254 | |
255 | |
256 | |
257 | |
258 | |
259 | if (I != 0 && !Tokens[I-1].is(tok::hashhash) && CurTok.hasLeadingSpace()) |
260 | NextTokGetsSpace = true; |
261 | |
262 | if (VCtx.isVAOptToken(CurTok)) { |
263 | MadeChange = true; |
264 | (0) . __assert_fail ("Tokens[I + 1].is(tok..l_paren) && \"__VA_OPT__ must be followed by '('\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 265, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tokens[I + 1].is(tok::l_paren) && |
265 | (0) . __assert_fail ("Tokens[I + 1].is(tok..l_paren) && \"__VA_OPT__ must be followed by '('\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 265, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "__VA_OPT__ must be followed by '('"); |
266 | |
267 | ++I; |
268 | VCtx.sawVAOptFollowedByOpeningParens(CurTok.getLocation(), |
269 | ResultToks.size()); |
270 | |
271 | continue; |
272 | } |
273 | |
274 | |
275 | |
276 | if (VCtx.isInVAOpt()) { |
277 | |
278 | |
279 | |
280 | |
281 | |
282 | |
283 | |
284 | |
285 | |
286 | |
287 | if (Tokens[I].is(tok::l_paren)) |
288 | VCtx.sawOpeningParen(Tokens[I].getLocation()); |
289 | |
290 | |
291 | |
292 | |
293 | if (!Tokens[I].is(tok::r_paren) || !VCtx.sawClosingParen()) { |
294 | if (!CalledWithVariadicArguments) { |
295 | |
296 | continue; |
297 | } |
298 | |
299 | |
300 | } else { |
301 | |
302 | |
303 | |
304 | |
305 | |
306 | |
307 | |
308 | if (VCtx.hasStringifyOrCharifyBefore()) { |
309 | |
310 | |
311 | |
312 | |
313 | |
314 | stringifyVAOPTContents(ResultToks, VCtx, |
315 | Tokens[I].getLocation()); |
316 | |
317 | } else if ( !( |
318 | ResultToks.size() - VCtx.getNumberOfTokensPriorToVAOpt())) { |
319 | |
320 | |
321 | |
322 | |
323 | |
324 | if (ResultToks.size() && ResultToks.back().is(tok::hashhash)) { |
325 | ResultToks.pop_back(); |
326 | } else if ((I + 1 != E) && Tokens[I + 1].is(tok::hashhash)) { |
327 | ++I; |
328 | } |
329 | } |
330 | VCtx.reset(); |
331 | |
332 | |
333 | continue; |
334 | } |
335 | } |
336 | |
337 | |
338 | |
339 | |
340 | |
341 | if (CurTok.isOneOf(tok::hash, tok::hashat)) { |
342 | int ArgNo = Macro->getParameterNum(Tokens[I+1].getIdentifierInfo()); |
343 | (0) . __assert_fail ("(ArgNo != -1 || VCtx.isVAOptToken(Tokens[I + 1])) && \"Token following # is not an argument or __VA_OPT__!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 344, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((ArgNo != -1 || VCtx.isVAOptToken(Tokens[I + 1])) && |
344 | (0) . __assert_fail ("(ArgNo != -1 || VCtx.isVAOptToken(Tokens[I + 1])) && \"Token following # is not an argument or __VA_OPT__!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 344, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Token following # is not an argument or __VA_OPT__!"); |
345 | |
346 | if (ArgNo == -1) { |
347 | |
348 | VCtx.sawHashOrHashAtBefore(NextTokGetsSpace, |
349 | CurTok.is(tok::hashat)); |
350 | continue; |
351 | } |
352 | |
353 | SourceLocation ExpansionLocStart = |
354 | getExpansionLocForMacroDefLoc(CurTok.getLocation()); |
355 | SourceLocation ExpansionLocEnd = |
356 | getExpansionLocForMacroDefLoc(Tokens[I+1].getLocation()); |
357 | |
358 | Token Res; |
359 | if (CurTok.is(tok::hash)) |
360 | Res = ActualArgs->getStringifiedArgument(ArgNo, PP, |
361 | ExpansionLocStart, |
362 | ExpansionLocEnd); |
363 | else { |
364 | |
365 | Res = MacroArgs::StringifyArgument(ActualArgs->getUnexpArgument(ArgNo), |
366 | PP, true, |
367 | ExpansionLocStart, |
368 | ExpansionLocEnd); |
369 | } |
370 | Res.setFlag(Token::StringifiedInMacro); |
371 | |
372 | |
373 | |
374 | if (NextTokGetsSpace) |
375 | Res.setFlag(Token::LeadingSpace); |
376 | |
377 | ResultToks.push_back(Res); |
378 | MadeChange = true; |
379 | ++I; |
380 | NextTokGetsSpace = false; |
381 | continue; |
382 | } |
383 | |
384 | |
385 | bool NonEmptyPasteBefore = |
386 | !ResultToks.empty() && ResultToks.back().is(tok::hashhash); |
387 | bool PasteBefore = I != 0 && Tokens[I-1].is(tok::hashhash); |
388 | bool PasteAfter = I+1 != E && Tokens[I+1].is(tok::hashhash); |
389 | |
390 | (0) . __assert_fail ("(!NonEmptyPasteBefore || PasteBefore || VCtx.isInVAOpt()) && \"unexpected ## in ResultToks\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 391, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((!NonEmptyPasteBefore || PasteBefore || VCtx.isInVAOpt()) && |
391 | (0) . __assert_fail ("(!NonEmptyPasteBefore || PasteBefore || VCtx.isInVAOpt()) && \"unexpected ## in ResultToks\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 391, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "unexpected ## in ResultToks"); |
392 | |
393 | |
394 | |
395 | IdentifierInfo *II = CurTok.getIdentifierInfo(); |
396 | int ArgNo = II ? Macro->getParameterNum(II) : -1; |
397 | if (ArgNo == -1) { |
398 | |
399 | ResultToks.push_back(CurTok); |
400 | |
401 | if (NextTokGetsSpace) { |
402 | ResultToks.back().setFlag(Token::LeadingSpace); |
403 | NextTokGetsSpace = false; |
404 | } else if (PasteBefore && !NonEmptyPasteBefore) |
405 | ResultToks.back().clearFlag(Token::LeadingSpace); |
406 | |
407 | continue; |
408 | } |
409 | |
410 | |
411 | |
412 | MadeChange = true; |
413 | |
414 | |
415 | |
416 | |
417 | |
418 | if (!PasteBefore && ActualArgs->isVarargsElidedUse() && |
419 | MaybeRemoveCommaBeforeVaArgs(ResultToks, |
420 | , |
421 | Macro, ArgNo, PP)) |
422 | continue; |
423 | |
424 | |
425 | |
426 | |
427 | if (!PasteBefore && !PasteAfter) { |
428 | const Token *ResultArgToks; |
429 | |
430 | |
431 | |
432 | const Token *ArgTok = ActualArgs->getUnexpArgument(ArgNo); |
433 | if (ActualArgs->ArgNeedsPreexpansion(ArgTok, PP)) |
434 | ResultArgToks = &ActualArgs->getPreExpArgument(ArgNo, PP)[0]; |
435 | else |
436 | ResultArgToks = ArgTok; |
437 | |
438 | |
439 | if (ResultArgToks->isNot(tok::eof)) { |
440 | size_t FirstResult = ResultToks.size(); |
441 | unsigned NumToks = MacroArgs::getArgLength(ResultArgToks); |
442 | ResultToks.append(ResultArgToks, ResultArgToks+NumToks); |
443 | |
444 | |
445 | |
446 | |
447 | |
448 | if (PP.getLangOpts().MSVCCompat && NumToks == 1 && |
449 | ResultToks.back().is(tok::comma)) |
450 | ResultToks.back().setFlag(Token::IgnoredComma); |
451 | |
452 | |
453 | |
454 | for (Token &Tok : llvm::make_range(ResultToks.begin() + FirstResult, |
455 | ResultToks.end())) { |
456 | if (Tok.is(tok::hashhash)) |
457 | Tok.setKind(tok::unknown); |
458 | } |
459 | |
460 | if(ExpandLocStart.isValid()) { |
461 | updateLocForMacroArgTokens(CurTok.getLocation(), |
462 | ResultToks.begin()+FirstResult, |
463 | ResultToks.end()); |
464 | } |
465 | |
466 | |
467 | |
468 | |
469 | ResultToks[FirstResult].setFlagValue(Token::LeadingSpace, |
470 | NextTokGetsSpace); |
471 | ResultToks[FirstResult].setFlagValue(Token::StartOfLine, false); |
472 | NextTokGetsSpace = false; |
473 | } |
474 | continue; |
475 | } |
476 | |
477 | |
478 | |
479 | const Token *ArgToks = ActualArgs->getUnexpArgument(ArgNo); |
480 | unsigned NumToks = MacroArgs::getArgLength(ArgToks); |
481 | if (NumToks) { |
482 | bool VaArgsPseudoPaste = false; |
483 | |
484 | |
485 | |
486 | |
487 | if (NonEmptyPasteBefore && ResultToks.size() >= 2 && |
488 | ResultToks[ResultToks.size()-2].is(tok::comma) && |
489 | (unsigned)ArgNo == Macro->getNumParams()-1 && |
490 | Macro->isVariadic()) { |
491 | VaArgsPseudoPaste = true; |
492 | |
493 | PP.Diag(ResultToks.pop_back_val().getLocation(), diag::ext_paste_comma); |
494 | } |
495 | |
496 | ResultToks.append(ArgToks, ArgToks+NumToks); |
497 | |
498 | |
499 | |
500 | for (Token &Tok : llvm::make_range(ResultToks.end() - NumToks, |
501 | ResultToks.end())) { |
502 | if (Tok.is(tok::hashhash)) |
503 | Tok.setKind(tok::unknown); |
504 | } |
505 | |
506 | if (ExpandLocStart.isValid()) { |
507 | updateLocForMacroArgTokens(CurTok.getLocation(), |
508 | ResultToks.end()-NumToks, ResultToks.end()); |
509 | } |
510 | |
511 | |
512 | |
513 | |
514 | |
515 | if (!VaArgsPseudoPaste) { |
516 | ResultToks[ResultToks.size() - NumToks].setFlagValue(Token::StartOfLine, |
517 | false); |
518 | ResultToks[ResultToks.size() - NumToks].setFlagValue( |
519 | Token::LeadingSpace, NextTokGetsSpace); |
520 | } |
521 | |
522 | NextTokGetsSpace = false; |
523 | continue; |
524 | } |
525 | |
526 | |
527 | |
528 | |
529 | |
530 | if (PasteAfter) { |
531 | |
532 | |
533 | ++I; |
534 | continue; |
535 | } |
536 | |
537 | |
538 | |
539 | |
540 | assert(PasteBefore); |
541 | if (NonEmptyPasteBefore) { |
542 | assert(ResultToks.back().is(tok::hashhash)); |
543 | |
544 | |
545 | |
546 | |
547 | if (!VCtx.isInVAOpt() || |
548 | ResultToks.size() > VCtx.getNumberOfTokensPriorToVAOpt()) |
549 | ResultToks.pop_back(); |
550 | } |
551 | |
552 | |
553 | |
554 | |
555 | |
556 | if (ActualArgs->isVarargsElidedUse()) |
557 | MaybeRemoveCommaBeforeVaArgs(ResultToks, |
558 | , |
559 | Macro, ArgNo, PP); |
560 | } |
561 | |
562 | |
563 | if (MadeChange) { |
564 | (0) . __assert_fail ("!OwnsTokens && \"This would leak if we already own the token list\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 564, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!OwnsTokens && "This would leak if we already own the token list"); |
565 | |
566 | NumTokens = ResultToks.size(); |
567 | |
568 | |
569 | Tokens = PP.cacheMacroExpandedTokens(this, ResultToks); |
570 | |
571 | |
572 | OwnsTokens = false; |
573 | } |
574 | } |
575 | |
576 | |
577 | static bool isWideStringLiteralFromMacro(const Token &FirstTok, |
578 | const Token &SecondTok) { |
579 | return FirstTok.is(tok::identifier) && |
580 | FirstTok.getIdentifierInfo()->isStr("L") && SecondTok.isLiteral() && |
581 | SecondTok.stringifiedInMacro(); |
582 | } |
583 | |
584 | |
585 | bool TokenLexer::Lex(Token &Tok) { |
586 | |
587 | if (isAtEnd()) { |
588 | |
589 | |
590 | if (Macro) Macro->EnableMacro(); |
591 | |
592 | Tok.startToken(); |
593 | Tok.setFlagValue(Token::StartOfLine , AtStartOfLine); |
594 | Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace || NextTokGetsSpace); |
595 | if (CurTokenIdx == 0) |
596 | Tok.setFlag(Token::LeadingEmptyMacro); |
597 | return PP.HandleEndOfTokenLexer(Tok); |
598 | } |
599 | |
600 | SourceManager &SM = PP.getSourceManager(); |
601 | |
602 | |
603 | |
604 | bool isFirstToken = CurTokenIdx == 0; |
605 | |
606 | |
607 | Tok = Tokens[CurTokenIdx++]; |
608 | |
609 | bool TokenIsFromPaste = false; |
610 | |
611 | |
612 | |
613 | if (!isAtEnd() && Macro && |
614 | (Tokens[CurTokenIdx].is(tok::hashhash) || |
615 | |
616 | |
617 | |
618 | (PP.getLangOpts().MSVCCompat && |
619 | isWideStringLiteralFromMacro(Tok, Tokens[CurTokenIdx])))) { |
620 | |
621 | |
622 | if (pasteTokens(Tok)) |
623 | return true; |
624 | |
625 | TokenIsFromPaste = true; |
626 | } |
627 | |
628 | |
629 | |
630 | |
631 | |
632 | |
633 | if (ExpandLocStart.isValid() && |
634 | |
635 | SM.isBeforeInSLocAddrSpace(Tok.getLocation(), MacroStartSLocOffset)) { |
636 | SourceLocation instLoc; |
637 | if (Tok.is(tok::comment)) { |
638 | instLoc = SM.createExpansionLoc(Tok.getLocation(), |
639 | ExpandLocStart, |
640 | ExpandLocEnd, |
641 | Tok.getLength()); |
642 | } else { |
643 | instLoc = getExpansionLocForMacroDefLoc(Tok.getLocation()); |
644 | } |
645 | |
646 | Tok.setLocation(instLoc); |
647 | } |
648 | |
649 | |
650 | |
651 | if (isFirstToken) { |
652 | Tok.setFlagValue(Token::StartOfLine , AtStartOfLine); |
653 | Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace); |
654 | } else { |
655 | |
656 | |
657 | if (AtStartOfLine) Tok.setFlag(Token::StartOfLine); |
658 | if (HasLeadingSpace) Tok.setFlag(Token::LeadingSpace); |
659 | } |
660 | AtStartOfLine = false; |
661 | HasLeadingSpace = false; |
662 | |
663 | |
664 | if (!Tok.isAnnotation() && Tok.getIdentifierInfo() != nullptr) { |
665 | |
666 | |
667 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
668 | Tok.setKind(II->getTokenID()); |
669 | |
670 | |
671 | |
672 | |
673 | if (II->isPoisoned() && TokenIsFromPaste) { |
674 | PP.HandlePoisonedIdentifier(Tok); |
675 | } |
676 | |
677 | if (!DisableMacroExpansion && II->isHandleIdentifierCase()) |
678 | return PP.HandleIdentifier(Tok); |
679 | } |
680 | |
681 | |
682 | return true; |
683 | } |
684 | |
685 | bool TokenLexer::pasteTokens(Token &Tok) { |
686 | return pasteTokens(Tok, llvm::makeArrayRef(Tokens, NumTokens), CurTokenIdx); |
687 | } |
688 | |
689 | |
690 | |
691 | |
692 | |
693 | bool TokenLexer::pasteTokens(Token &LHSTok, ArrayRef<Token> TokenStream, |
694 | unsigned int &CurIdx) { |
695 | (0) . __assert_fail ("CurIdx > 0 && \"## can not be the first token within tokens\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 695, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CurIdx > 0 && "## can not be the first token within tokens"); |
696 | (0) . __assert_fail ("(TokenStream[CurIdx].is(tok..hashhash) || (PP.getLangOpts().MSVCCompat && isWideStringLiteralFromMacro(LHSTok, TokenStream[CurIdx]))) && \"Token at this Index must be ## or part of the MSVC 'L \" \"#macro-arg' pasting pair\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 700, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((TokenStream[CurIdx].is(tok::hashhash) || |
697 | (0) . __assert_fail ("(TokenStream[CurIdx].is(tok..hashhash) || (PP.getLangOpts().MSVCCompat && isWideStringLiteralFromMacro(LHSTok, TokenStream[CurIdx]))) && \"Token at this Index must be ## or part of the MSVC 'L \" \"#macro-arg' pasting pair\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 700, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> (PP.getLangOpts().MSVCCompat && |
698 | (0) . __assert_fail ("(TokenStream[CurIdx].is(tok..hashhash) || (PP.getLangOpts().MSVCCompat && isWideStringLiteralFromMacro(LHSTok, TokenStream[CurIdx]))) && \"Token at this Index must be ## or part of the MSVC 'L \" \"#macro-arg' pasting pair\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 700, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> isWideStringLiteralFromMacro(LHSTok, TokenStream[CurIdx]))) && |
699 | (0) . __assert_fail ("(TokenStream[CurIdx].is(tok..hashhash) || (PP.getLangOpts().MSVCCompat && isWideStringLiteralFromMacro(LHSTok, TokenStream[CurIdx]))) && \"Token at this Index must be ## or part of the MSVC 'L \" \"#macro-arg' pasting pair\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 700, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Token at this Index must be ## or part of the MSVC 'L " |
700 | (0) . __assert_fail ("(TokenStream[CurIdx].is(tok..hashhash) || (PP.getLangOpts().MSVCCompat && isWideStringLiteralFromMacro(LHSTok, TokenStream[CurIdx]))) && \"Token at this Index must be ## or part of the MSVC 'L \" \"#macro-arg' pasting pair\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 700, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "#macro-arg' pasting pair"); |
701 | |
702 | |
703 | |
704 | |
705 | if (PP.getLangOpts().MicrosoftExt && (CurIdx >= 2) && |
706 | TokenStream[CurIdx - 2].is(tok::hashhash)) |
707 | LHSTok.clearFlag(Token::LeadingSpace); |
708 | |
709 | SmallString<128> Buffer; |
710 | const char *ResultTokStrPtr = nullptr; |
711 | SourceLocation StartLoc = LHSTok.getLocation(); |
712 | SourceLocation PasteOpLoc; |
713 | |
714 | auto IsAtEnd = [&TokenStream, &CurIdx] { |
715 | return TokenStream.size() == CurIdx; |
716 | }; |
717 | |
718 | do { |
719 | |
720 | PasteOpLoc = TokenStream[CurIdx].getLocation(); |
721 | if (TokenStream[CurIdx].is(tok::hashhash)) |
722 | ++CurIdx; |
723 | (0) . __assert_fail ("!IsAtEnd() && \"No token on the RHS of a paste operator!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 723, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!IsAtEnd() && "No token on the RHS of a paste operator!"); |
724 | |
725 | |
726 | const Token &RHS = TokenStream[CurIdx]; |
727 | |
728 | |
729 | |
730 | Buffer.resize(LHSTok.getLength() + RHS.getLength()); |
731 | |
732 | |
733 | const char *BufPtr = &Buffer[0]; |
734 | bool Invalid = false; |
735 | unsigned LHSLen = PP.getSpelling(LHSTok, BufPtr, &Invalid); |
736 | if (BufPtr != &Buffer[0]) |
737 | memcpy(&Buffer[0], BufPtr, LHSLen); |
738 | if (Invalid) |
739 | return true; |
740 | |
741 | BufPtr = Buffer.data() + LHSLen; |
742 | unsigned RHSLen = PP.getSpelling(RHS, BufPtr, &Invalid); |
743 | if (Invalid) |
744 | return true; |
745 | if (RHSLen && BufPtr != &Buffer[LHSLen]) |
746 | |
747 | memcpy(&Buffer[LHSLen], BufPtr, RHSLen); |
748 | |
749 | |
750 | Buffer.resize(LHSLen+RHSLen); |
751 | |
752 | |
753 | |
754 | Token ResultTokTmp; |
755 | ResultTokTmp.startToken(); |
756 | |
757 | |
758 | |
759 | ResultTokTmp.setKind(tok::string_literal); |
760 | PP.CreateString(Buffer, ResultTokTmp); |
761 | SourceLocation ResultTokLoc = ResultTokTmp.getLocation(); |
762 | ResultTokStrPtr = ResultTokTmp.getLiteralData(); |
763 | |
764 | |
765 | Token Result; |
766 | |
767 | if (LHSTok.isAnyIdentifier() && RHS.isAnyIdentifier()) { |
768 | |
769 | |
770 | PP.IncrementPasteCounter(true); |
771 | Result.startToken(); |
772 | Result.setKind(tok::raw_identifier); |
773 | Result.setRawIdentifierData(ResultTokStrPtr); |
774 | Result.setLocation(ResultTokLoc); |
775 | Result.setLength(LHSLen+RHSLen); |
776 | } else { |
777 | PP.IncrementPasteCounter(false); |
778 | |
779 | (0) . __assert_fail ("ResultTokLoc.isFileID() && \"Should be a raw location into scratch buffer\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 780, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ResultTokLoc.isFileID() && |
780 | (0) . __assert_fail ("ResultTokLoc.isFileID() && \"Should be a raw location into scratch buffer\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 780, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Should be a raw location into scratch buffer"); |
781 | SourceManager &SourceMgr = PP.getSourceManager(); |
782 | FileID LocFileID = SourceMgr.getFileID(ResultTokLoc); |
783 | |
784 | bool Invalid = false; |
785 | const char *ScratchBufStart |
786 | = SourceMgr.getBufferData(LocFileID, &Invalid).data(); |
787 | if (Invalid) |
788 | return false; |
789 | |
790 | |
791 | |
792 | Lexer TL(SourceMgr.getLocForStartOfFile(LocFileID), |
793 | PP.getLangOpts(), ScratchBufStart, |
794 | ResultTokStrPtr, ResultTokStrPtr+LHSLen+RHSLen); |
795 | |
796 | |
797 | |
798 | |
799 | |
800 | bool isInvalid = !TL.LexFromRawLexer(Result); |
801 | |
802 | |
803 | |
804 | isInvalid |= Result.is(tok::eof); |
805 | |
806 | |
807 | |
808 | |
809 | if (isInvalid) { |
810 | |
811 | |
812 | SourceManager &SM = PP.getSourceManager(); |
813 | SourceLocation Loc = |
814 | SM.createExpansionLoc(PasteOpLoc, ExpandLocStart, ExpandLocEnd, 2); |
815 | |
816 | |
817 | |
818 | if (PP.getLangOpts().MicrosoftExt && LHSTok.is(tok::slash) && |
819 | RHS.is(tok::slash)) { |
820 | HandleMicrosoftCommentPaste(LHSTok, Loc); |
821 | return true; |
822 | } |
823 | |
824 | |
825 | if (!PP.getLangOpts().AsmPreprocessor) { |
826 | |
827 | |
828 | |
829 | PP.Diag(Loc, PP.getLangOpts().MicrosoftExt ? diag::ext_pp_bad_paste_ms |
830 | : diag::err_pp_bad_paste) |
831 | << Buffer; |
832 | } |
833 | |
834 | |
835 | break; |
836 | } |
837 | |
838 | |
839 | |
840 | if (Result.is(tok::hashhash)) |
841 | Result.setKind(tok::unknown); |
842 | } |
843 | |
844 | |
845 | Result.setFlagValue(Token::StartOfLine , LHSTok.isAtStartOfLine()); |
846 | Result.setFlagValue(Token::LeadingSpace, LHSTok.hasLeadingSpace()); |
847 | |
848 | |
849 | ++CurIdx; |
850 | LHSTok = Result; |
851 | } while (!IsAtEnd() && TokenStream[CurIdx].is(tok::hashhash)); |
852 | |
853 | SourceLocation EndLoc = TokenStream[CurIdx - 1].getLocation(); |
854 | |
855 | |
856 | |
857 | |
858 | |
859 | |
860 | SourceManager &SM = PP.getSourceManager(); |
861 | if (StartLoc.isFileID()) |
862 | StartLoc = getExpansionLocForMacroDefLoc(StartLoc); |
863 | if (EndLoc.isFileID()) |
864 | EndLoc = getExpansionLocForMacroDefLoc(EndLoc); |
865 | FileID MacroFID = SM.getFileID(MacroExpansionStart); |
866 | while (SM.getFileID(StartLoc) != MacroFID) |
867 | StartLoc = SM.getImmediateExpansionRange(StartLoc).getBegin(); |
868 | while (SM.getFileID(EndLoc) != MacroFID) |
869 | EndLoc = SM.getImmediateExpansionRange(EndLoc).getEnd(); |
870 | |
871 | LHSTok.setLocation(SM.createExpansionLoc(LHSTok.getLocation(), StartLoc, EndLoc, |
872 | LHSTok.getLength())); |
873 | |
874 | |
875 | |
876 | |
877 | if (LHSTok.is(tok::raw_identifier)) { |
878 | |
879 | |
880 | PP.LookUpIdentifierInfo(LHSTok); |
881 | } |
882 | return false; |
883 | } |
884 | |
885 | |
886 | |
887 | |
888 | unsigned TokenLexer::isNextTokenLParen() const { |
889 | |
890 | if (isAtEnd()) |
891 | return 2; |
892 | return Tokens[CurTokenIdx].is(tok::l_paren); |
893 | } |
894 | |
895 | |
896 | |
897 | bool TokenLexer::isParsingPreprocessorDirective() const { |
898 | return Tokens[NumTokens-1].is(tok::eod) && !isAtEnd(); |
899 | } |
900 | |
901 | |
902 | |
903 | |
904 | |
905 | |
906 | void TokenLexer::HandleMicrosoftCommentPaste(Token &Tok, SourceLocation OpLoc) { |
907 | PP.Diag(OpLoc, diag::ext_comment_paste_microsoft); |
908 | |
909 | |
910 | |
911 | |
912 | |
913 | |
914 | (0) . __assert_fail ("Macro && \"Token streams can't paste comments\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 914, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Macro && "Token streams can't paste comments"); |
915 | Macro->EnableMacro(); |
916 | |
917 | PP.HandleMicrosoftCommentPaste(Tok); |
918 | } |
919 | |
920 | |
921 | |
922 | |
923 | |
924 | SourceLocation |
925 | TokenLexer::getExpansionLocForMacroDefLoc(SourceLocation loc) const { |
926 | (0) . __assert_fail ("ExpandLocStart.isValid() && MacroExpansionStart.isValid() && \"Not appropriate for token streams\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 927, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ExpandLocStart.isValid() && MacroExpansionStart.isValid() && |
927 | (0) . __assert_fail ("ExpandLocStart.isValid() && MacroExpansionStart.isValid() && \"Not appropriate for token streams\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 927, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Not appropriate for token streams"); |
928 | assert(loc.isValid() && loc.isFileID()); |
929 | |
930 | SourceManager &SM = PP.getSourceManager(); |
931 | (0) . __assert_fail ("SM.isInSLocAddrSpace(loc, MacroDefStart, MacroDefLength) && \"Expected loc to come from the macro definition\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 932, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SM.isInSLocAddrSpace(loc, MacroDefStart, MacroDefLength) && |
932 | (0) . __assert_fail ("SM.isInSLocAddrSpace(loc, MacroDefStart, MacroDefLength) && \"Expected loc to come from the macro definition\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/TokenLexer.cpp", 932, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Expected loc to come from the macro definition"); |
933 | |
934 | unsigned relativeOffset = 0; |
935 | SM.isInSLocAddrSpace(loc, MacroDefStart, MacroDefLength, &relativeOffset); |
936 | return MacroExpansionStart.getLocWithOffset(relativeOffset); |
937 | } |
938 | |
939 | |
940 | |
941 | |
942 | |
943 | |
944 | |
945 | |
946 | |
947 | |
948 | static void updateConsecutiveMacroArgTokens(SourceManager &SM, |
949 | SourceLocation InstLoc, |
950 | Token *&begin_tokens, |
951 | Token * end_tokens) { |
952 | assert(begin_tokens < end_tokens); |
953 | |
954 | SourceLocation FirstLoc = begin_tokens->getLocation(); |
955 | SourceLocation CurLoc = FirstLoc; |
956 | |
957 | |
958 | |
959 | |
960 | |
961 | |
962 | |
963 | |
964 | |
965 | |
966 | |
967 | Token *NextTok = begin_tokens + 1; |
968 | for (; NextTok < end_tokens; ++NextTok) { |
969 | SourceLocation NextLoc = NextTok->getLocation(); |
970 | if (CurLoc.isFileID() != NextLoc.isFileID()) |
971 | break; |
972 | |
973 | int RelOffs; |
974 | if (!SM.isInSameSLocAddrSpace(CurLoc, NextLoc, &RelOffs)) |
975 | break; |
976 | |
977 | |
978 | if (RelOffs < 0 || RelOffs > 50) |
979 | break; |
980 | |
981 | if (CurLoc.isMacroID() && !SM.isWrittenInSameFile(CurLoc, NextLoc)) |
982 | break; |
983 | |
984 | CurLoc = NextLoc; |
985 | } |
986 | |
987 | |
988 | |
989 | Token &LastConsecutiveTok = *(NextTok-1); |
990 | int LastRelOffs = 0; |
991 | SM.isInSameSLocAddrSpace(FirstLoc, LastConsecutiveTok.getLocation(), |
992 | &LastRelOffs); |
993 | unsigned FullLength = LastRelOffs + LastConsecutiveTok.getLength(); |
994 | |
995 | |
996 | SourceLocation Expansion = |
997 | SM.createMacroArgExpansionLoc(FirstLoc, InstLoc,FullLength); |
998 | |
999 | |
1000 | |
1001 | for (; begin_tokens < NextTok; ++begin_tokens) { |
1002 | Token &Tok = *begin_tokens; |
1003 | int RelOffs = 0; |
1004 | SM.isInSameSLocAddrSpace(FirstLoc, Tok.getLocation(), &RelOffs); |
1005 | Tok.setLocation(Expansion.getLocWithOffset(RelOffs)); |
1006 | } |
1007 | } |
1008 | |
1009 | |
1010 | |
1011 | |
1012 | |
1013 | |
1014 | void TokenLexer::updateLocForMacroArgTokens(SourceLocation ArgIdSpellLoc, |
1015 | Token *begin_tokens, |
1016 | Token *end_tokens) { |
1017 | SourceManager &SM = PP.getSourceManager(); |
1018 | |
1019 | SourceLocation InstLoc = |
1020 | getExpansionLocForMacroDefLoc(ArgIdSpellLoc); |
1021 | |
1022 | while (begin_tokens < end_tokens) { |
1023 | |
1024 | if (end_tokens - begin_tokens == 1) { |
1025 | Token &Tok = *begin_tokens; |
1026 | Tok.setLocation(SM.createMacroArgExpansionLoc(Tok.getLocation(), |
1027 | InstLoc, |
1028 | Tok.getLength())); |
1029 | return; |
1030 | } |
1031 | |
1032 | updateConsecutiveMacroArgTokens(SM, InstLoc, begin_tokens, end_tokens); |
1033 | } |
1034 | } |
1035 | |
1036 | void TokenLexer::PropagateLineStartLeadingSpaceInfo(Token &Result) { |
1037 | AtStartOfLine = Result.isAtStartOfLine(); |
1038 | HasLeadingSpace = Result.hasLeadingSpace(); |
1039 | } |
1040 | |