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), |
Ted Kremenek | 31aba42 | 2008-11-20 16:32:22 +0000 | [diff] [blame] | 23 | LastTokenIdx(NumTokens - 1), |
| 24 | CurTokenIdx(0) { |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 25 | |
Ted Kremenek | d6f53dc | 2008-11-20 07:58:05 +0000 | [diff] [blame] | 26 | assert(NumTokens >= 1); |
Ted Kremenek | 31aba42 | 2008-11-20 16:32:22 +0000 | [diff] [blame] | 27 | assert(Tokens[LastTokenIdx].is(tok::eof)); |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Ted Kremenek | 89d7ee9 | 2008-11-20 19:49:00 +0000 | [diff] [blame] | 30 | Token PTHLexer::GetToken() { |
| 31 | Token Tok = Tokens[CurTokenIdx]; |
| 32 | |
| 33 | // If we are in raw mode, zero out identifier pointers. This is |
| 34 | // needed for 'pragma poison'. Note that this requires that the Preprocessor |
| 35 | // can go back to the original source when it calls getSpelling(). |
| 36 | if (LexingRawMode && Tok.is(tok::identifier)) |
| 37 | Tok.setIdentifierInfo(0); |
| 38 | |
| 39 | return Tok; |
| 40 | } |
| 41 | |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 42 | void PTHLexer::Lex(Token& Tok) { |
Ted Kremenek | d6f53dc | 2008-11-20 07:58:05 +0000 | [diff] [blame] | 43 | LexNextToken: |
Ted Kremenek | 31aba42 | 2008-11-20 16:32:22 +0000 | [diff] [blame] | 44 | Tok = GetToken(); |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | cd4e2ae | 2008-11-21 00:58:35 +0000 | [diff] [blame] | 46 | if (AtLastToken()) { |
| 47 | Preprocessor *PPCache = PP; |
| 48 | |
| 49 | if (LexEndOfFile(Tok)) |
| 50 | return; |
| 51 | |
| 52 | assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); |
| 53 | return PPCache->Lex(Tok); |
| 54 | } |
| 55 | |
Ted Kremenek | d6f53dc | 2008-11-20 07:58:05 +0000 | [diff] [blame] | 56 | // Don't advance to the next token yet. Check if we are at the |
| 57 | // start of a new line and we're processing a directive. If so, we |
| 58 | // consume this token twice, once as an tok::eom. |
| 59 | if (Tok.isAtStartOfLine() && ParsingPreprocessorDirective) { |
| 60 | ParsingPreprocessorDirective = false; |
| 61 | Tok.setKind(tok::eom); |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 62 | MIOpt.ReadToken(); |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 63 | return; |
| 64 | } |
Ted Kremenek | d6f53dc | 2008-11-20 07:58:05 +0000 | [diff] [blame] | 65 | |
| 66 | // Advance to the next token. |
Ted Kremenek | 31aba42 | 2008-11-20 16:32:22 +0000 | [diff] [blame] | 67 | AdvanceToken(); |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | d6f53dc | 2008-11-20 07:58:05 +0000 | [diff] [blame] | 69 | if (Tok.is(tok::hash)) { |
| 70 | if (Tok.isAtStartOfLine() && !LexingRawMode) { |
| 71 | PP->HandleDirective(Tok); |
| 72 | |
| 73 | if (PP->isCurrentLexer(this)) |
| 74 | goto LexNextToken; |
| 75 | |
| 76 | return PP->Lex(Tok); |
| 77 | } |
| 78 | } |
| 79 | |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 80 | MIOpt.ReadToken(); |
Ted Kremenek | d6f53dc | 2008-11-20 07:58:05 +0000 | [diff] [blame] | 81 | |
| 82 | if (Tok.is(tok::identifier)) { |
| 83 | if (LexingRawMode) return; |
| 84 | return PP->HandleIdentifier(Tok); |
| 85 | } |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Ted Kremenek | cd4e2ae | 2008-11-21 00:58:35 +0000 | [diff] [blame] | 88 | bool PTHLexer::LexEndOfFile(Token &Tok) { |
| 89 | |
| 90 | if (ParsingPreprocessorDirective) { |
| 91 | ParsingPreprocessorDirective = false; |
| 92 | Tok.setKind(tok::eom); |
| 93 | MIOpt.ReadToken(); |
| 94 | return true; // Have a token. |
| 95 | } |
| 96 | |
| 97 | if (LexingRawMode) { |
| 98 | MIOpt.ReadToken(); |
| 99 | return true; // Have an eof token. |
| 100 | } |
| 101 | |
| 102 | // FIXME: Issue diagnostics similar to Lexer. |
| 103 | return PP->HandleEndOfFile(Tok, false); |
| 104 | } |
| 105 | |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 106 | void PTHLexer::setEOF(Token& Tok) { |
Ted Kremenek | 31aba42 | 2008-11-20 16:32:22 +0000 | [diff] [blame] | 107 | Tok = Tokens[LastTokenIdx]; |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 108 | } |
Ted Kremenek | 17ff58a | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 109 | |
| 110 | void PTHLexer::DiscardToEndOfLine() { |
| 111 | assert(ParsingPreprocessorDirective && ParsingFilename == false && |
| 112 | "Must be in a preprocessing directive!"); |
Ted Kremenek | 4d35da2 | 2008-11-20 01:16:50 +0000 | [diff] [blame] | 113 | |
| 114 | // Already at end-of-file? |
Ted Kremenek | 31aba42 | 2008-11-20 16:32:22 +0000 | [diff] [blame] | 115 | if (AtLastToken()) |
Ted Kremenek | 4d35da2 | 2008-11-20 01:16:50 +0000 | [diff] [blame] | 116 | return; |
| 117 | |
| 118 | // Find the first token that is not the start of the *current* line. |
Ted Kremenek | d2bdeed | 2008-11-21 23:28:56 +0000 | [diff] [blame] | 119 | Token T; |
| 120 | for (Lex(T); !AtLastToken(); Lex(T)) |
Ted Kremenek | 31aba42 | 2008-11-20 16:32:22 +0000 | [diff] [blame] | 121 | if (GetToken().isAtStartOfLine()) |
Ted Kremenek | 4d35da2 | 2008-11-20 01:16:50 +0000 | [diff] [blame] | 122 | return; |
Ted Kremenek | 17ff58a | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 123 | } |