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