Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 1 | //===--- PTHLexer.cpp - Lex from a token stream ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the PTHLexer interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Lex/PTHLexer.h" |
| 15 | #include "clang/Lex/Preprocessor.h" |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TokenKinds.h" |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | |
| 19 | PTHLexer::PTHLexer(Preprocessor& pp, SourceLocation fileloc, |
Ted Kremenek | 452e378 | 2008-11-20 01:29:45 +0000 | [diff] [blame^] | 20 | const Token *TokArray, unsigned NumTokens) |
| 21 | : PreprocessorLexer(&pp, fileloc), |
| 22 | Tokens(TokArray), |
| 23 | LastToken(NumTokens - 1), |
| 24 | CurToken(0) { |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 25 | |
Ted Kremenek | 452e378 | 2008-11-20 01:29:45 +0000 | [diff] [blame^] | 26 | assert (NumTokens >= 1); |
| 27 | assert (Tokens[LastToken].is(tok::eof)); |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void PTHLexer::Lex(Token& Tok) { |
| 31 | |
Ted Kremenek | 452e378 | 2008-11-20 01:29:45 +0000 | [diff] [blame^] | 32 | if (CurToken == LastToken) { |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 33 | // If we hit the end of the file while parsing a preprocessor directive, |
| 34 | // end the preprocessor directive first. The next token returned will |
| 35 | // then be the end of file. |
| 36 | // OR |
| 37 | // If we are in raw mode, return this event as an EOF token. Let the caller |
| 38 | // that put us in raw mode handle the event. |
| 39 | if (ParsingPreprocessorDirective || LexingRawMode) { |
| 40 | // Done parsing the "line". |
| 41 | ParsingPreprocessorDirective = false; |
| 42 | Tok = Tokens[CurToken]; // not an out-of-bound access |
| 43 | // FIXME: eom handling? |
| 44 | } |
| 45 | else |
Ted Kremenek | d6a2e7d | 2008-11-12 23:13:54 +0000 | [diff] [blame] | 46 | PP->HandleEndOfFile(Tok, false); |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 47 | |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | Tok = Tokens[CurToken]; |
| 52 | |
| 53 | if (ParsingPreprocessorDirective && Tok.isAtStartOfLine()) { |
| 54 | ParsingPreprocessorDirective = false; // Done parsing the "line". |
| 55 | MIOpt.ReadToken(); |
| 56 | // FIXME: Need to replicate: |
| 57 | // FormTokenWithChars(Tok, CurPtr, tok::eom); |
| 58 | Tok.setKind(tok::eom); |
| 59 | return; |
| 60 | } |
| 61 | else // Otherwise, advance to the next token. |
| 62 | ++CurToken; |
| 63 | |
| 64 | if (Tok.isAtStartOfLine() && Tok.is(tok::hash) && !LexingRawMode) { |
Ted Kremenek | d6a2e7d | 2008-11-12 23:13:54 +0000 | [diff] [blame] | 65 | PP->HandleDirective(Tok); |
| 66 | PP->Lex(Tok); |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 67 | return; |
| 68 | } |
| 69 | |
| 70 | MIOpt.ReadToken(); |
| 71 | } |
| 72 | |
| 73 | void PTHLexer::setEOF(Token& Tok) { |
Ted Kremenek | 452e378 | 2008-11-20 01:29:45 +0000 | [diff] [blame^] | 74 | Tok = Tokens[LastToken]; |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 75 | } |
Ted Kremenek | 17ff58a | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 76 | |
| 77 | void PTHLexer::DiscardToEndOfLine() { |
| 78 | assert(ParsingPreprocessorDirective && ParsingFilename == false && |
| 79 | "Must be in a preprocessing directive!"); |
Ted Kremenek | 4d35da2 | 2008-11-20 01:16:50 +0000 | [diff] [blame] | 80 | |
| 81 | // Already at end-of-file? |
Ted Kremenek | 452e378 | 2008-11-20 01:29:45 +0000 | [diff] [blame^] | 82 | if (CurToken == LastToken) |
Ted Kremenek | 4d35da2 | 2008-11-20 01:16:50 +0000 | [diff] [blame] | 83 | return; |
| 84 | |
| 85 | // Find the first token that is not the start of the *current* line. |
Ted Kremenek | 452e378 | 2008-11-20 01:29:45 +0000 | [diff] [blame^] | 86 | for ( ++CurToken; CurToken != LastToken ; ++CurToken ) |
Ted Kremenek | 4d35da2 | 2008-11-20 01:16:50 +0000 | [diff] [blame] | 87 | if (Tokens[CurToken].isAtStartOfLine()) |
| 88 | return; |
Ted Kremenek | 17ff58a | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 89 | } |
Ted Kremenek | 2f1c024 | 2008-11-19 22:42:26 +0000 | [diff] [blame] | 90 | |
Ted Kremenek | 4d35da2 | 2008-11-20 01:16:50 +0000 | [diff] [blame] | 91 | unsigned PTHLexer::isNextPPTokenLParen() { |
Ted Kremenek | 452e378 | 2008-11-20 01:29:45 +0000 | [diff] [blame^] | 92 | if (CurToken == LastToken) |
Ted Kremenek | 4d35da2 | 2008-11-20 01:16:50 +0000 | [diff] [blame] | 93 | return 2; |
| 94 | |
| 95 | return Tokens[CurToken].is(tok::l_paren); |
Ted Kremenek | 2f1c024 | 2008-11-19 22:42:26 +0000 | [diff] [blame] | 96 | } |
| 97 | |