Mercurial > hg > CbC > CbC_llvm
comparison clang/lib/Lex/TokenLexer.cpp @ 252:1f2b6ac9f198 llvm-original
LLVM16-1
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 18 Aug 2023 09:04:13 +0900 |
parents | c4bab56944e8 |
children |
comparison
equal
deleted
inserted
replaced
237:c80f45b162ad | 252:1f2b6ac9f198 |
---|---|
29 #include "llvm/ADT/SmallString.h" | 29 #include "llvm/ADT/SmallString.h" |
30 #include "llvm/ADT/SmallVector.h" | 30 #include "llvm/ADT/SmallVector.h" |
31 #include "llvm/ADT/iterator_range.h" | 31 #include "llvm/ADT/iterator_range.h" |
32 #include <cassert> | 32 #include <cassert> |
33 #include <cstring> | 33 #include <cstring> |
34 #include <optional> | |
34 | 35 |
35 using namespace clang; | 36 using namespace clang; |
36 | 37 |
37 /// Create a TokenLexer for the specified macro with the specified actual | 38 /// Create a TokenLexer for the specified macro with the specified actual |
38 /// arguments. Note that this ctor takes ownership of the ActualArgs pointer. | 39 /// arguments. Note that this ctor takes ownership of the ActualArgs pointer. |
202 ++CurTokenIdx) { | 203 ++CurTokenIdx) { |
203 if (VAOPTTokens[CurTokenIdx].is(tok::hashhash)) { | 204 if (VAOPTTokens[CurTokenIdx].is(tok::hashhash)) { |
204 assert(CurTokenIdx != 0 && | 205 assert(CurTokenIdx != 0 && |
205 "Can not have __VAOPT__ contents begin with a ##"); | 206 "Can not have __VAOPT__ contents begin with a ##"); |
206 Token &LHS = VAOPTTokens[CurTokenIdx - 1]; | 207 Token &LHS = VAOPTTokens[CurTokenIdx - 1]; |
207 pasteTokens(LHS, llvm::makeArrayRef(VAOPTTokens, NumVAOptTokens), | 208 pasteTokens(LHS, llvm::ArrayRef(VAOPTTokens, NumVAOptTokens), |
208 CurTokenIdx); | 209 CurTokenIdx); |
209 // Replace the token prior to the first ## in this iteration. | 210 // Replace the token prior to the first ## in this iteration. |
210 ConcatenatedVAOPTResultToks.back() = LHS; | 211 ConcatenatedVAOPTResultToks.back() = LHS; |
211 if (CurTokenIdx == NumVAOptTokens) | 212 if (CurTokenIdx == NumVAOptTokens) |
212 break; | 213 break; |
246 // Loop through 'Tokens', expanding them into ResultToks. Keep | 247 // Loop through 'Tokens', expanding them into ResultToks. Keep |
247 // track of whether we change anything. If not, no need to keep them. If so, | 248 // track of whether we change anything. If not, no need to keep them. If so, |
248 // we install the newly expanded sequence as the new 'Tokens' list. | 249 // we install the newly expanded sequence as the new 'Tokens' list. |
249 bool MadeChange = false; | 250 bool MadeChange = false; |
250 | 251 |
251 Optional<bool> CalledWithVariadicArguments; | 252 std::optional<bool> CalledWithVariadicArguments; |
252 | 253 |
253 VAOptExpansionContext VCtx(PP); | 254 VAOptExpansionContext VCtx(PP); |
254 | 255 |
255 for (unsigned I = 0, E = NumTokens; I != E; ++I) { | 256 for (unsigned I = 0, E = NumTokens; I != E; ++I) { |
256 const Token &CurTok = Tokens[I]; | 257 const Token &CurTok = Tokens[I]; |
497 if (NonEmptyPasteBefore) { | 498 if (NonEmptyPasteBefore) { |
498 // We're imagining a placeholder token is inserted here. If this is | 499 // We're imagining a placeholder token is inserted here. If this is |
499 // the first token in a __VA_OPT__ after a ##, delete the ##. | 500 // the first token in a __VA_OPT__ after a ##, delete the ##. |
500 assert(VCtx.isInVAOpt() && "should only happen inside a __VA_OPT__"); | 501 assert(VCtx.isInVAOpt() && "should only happen inside a __VA_OPT__"); |
501 VCtx.hasPlaceholderAfterHashhashAtStart(); | 502 VCtx.hasPlaceholderAfterHashhashAtStart(); |
502 } | 503 } else if (RParenAfter) |
503 if (RParenAfter) | |
504 VCtx.hasPlaceholderBeforeRParen(); | 504 VCtx.hasPlaceholderBeforeRParen(); |
505 } | 505 } |
506 continue; | 506 continue; |
507 } | 507 } |
508 | 508 |
564 // buffer) the paste operator after it. | 564 // buffer) the paste operator after it. |
565 ++I; | 565 ++I; |
566 continue; | 566 continue; |
567 } | 567 } |
568 | 568 |
569 if (RParenAfter) | 569 if (RParenAfter && !NonEmptyPasteBefore) |
570 VCtx.hasPlaceholderBeforeRParen(); | 570 VCtx.hasPlaceholderBeforeRParen(); |
571 | 571 |
572 // If this is on the RHS of a paste operator, we've already copied the | 572 // If this is on the RHS of a paste operator, we've already copied the |
573 // paste operator to the ResultToks list, unless the LHS was empty too. | 573 // paste operator to the ResultToks list, unless the LHS was empty too. |
574 // Remove it. | 574 // Remove it. |
720 // Otherwise, return a normal token. | 720 // Otherwise, return a normal token. |
721 return true; | 721 return true; |
722 } | 722 } |
723 | 723 |
724 bool TokenLexer::pasteTokens(Token &Tok) { | 724 bool TokenLexer::pasteTokens(Token &Tok) { |
725 return pasteTokens(Tok, llvm::makeArrayRef(Tokens, NumTokens), CurTokenIdx); | 725 return pasteTokens(Tok, llvm::ArrayRef(Tokens, NumTokens), CurTokenIdx); |
726 } | 726 } |
727 | 727 |
728 /// LHSTok is the LHS of a ## operator, and CurTokenIdx is the ## | 728 /// LHSTok is the LHS of a ## operator, and CurTokenIdx is the ## |
729 /// operator. Read the ## and RHS, and paste the LHS/RHS together. If there | 729 /// operator. Read the ## and RHS, and paste the LHS/RHS together. If there |
730 /// are more ## after it, chomp them iteratively. Return the result as LHSTok. | 730 /// are more ## after it, chomp them iteratively. Return the result as LHSTok. |
1017 // sourcelocation-against-bounds comparison. | 1017 // sourcelocation-against-bounds comparison. |
1018 FileID BeginFID = SM.getFileID(BeginLoc); | 1018 FileID BeginFID = SM.getFileID(BeginLoc); |
1019 SourceLocation Limit = | 1019 SourceLocation Limit = |
1020 SM.getComposedLoc(BeginFID, SM.getFileIDSize(BeginFID)); | 1020 SM.getComposedLoc(BeginFID, SM.getFileIDSize(BeginFID)); |
1021 Partition = All.take_while([&](const Token &T) { | 1021 Partition = All.take_while([&](const Token &T) { |
1022 return T.getLocation() >= BeginLoc && T.getLocation() < Limit && | 1022 // NOTE: the Limit is included! The lexer recovery only ever inserts a |
1023 NearLast(T.getLocation()); | 1023 // single token past the end of the FileID, specifically the ) when a |
1024 // macro-arg containing a comma should be guarded by parentheses. | |
1025 // | |
1026 // It is safe to include the Limit here because SourceManager allocates | |
1027 // FileSize + 1 for each SLocEntry. | |
1028 // | |
1029 // See https://github.com/llvm/llvm-project/issues/60722. | |
1030 return T.getLocation() >= BeginLoc && T.getLocation() <= Limit | |
1031 && NearLast(T.getLocation()); | |
1024 }); | 1032 }); |
1025 } | 1033 } |
1026 assert(!Partition.empty()); | 1034 assert(!Partition.empty()); |
1027 | 1035 |
1028 // For the consecutive tokens, find the length of the SLocEntry to contain | 1036 // For the consecutive tokens, find the length of the SLocEntry to contain |