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" |
| 16 | #include "clang/Lex/Token.h" |
| 17 | #include "clang/Basic/TokenKinds.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
| 21 | PTHLexer::PTHLexer(Preprocessor& pp, SourceLocation fileloc, |
| 22 | const Token *TokArray, unsigned NumToks) |
Ted Kremenek | d6a2e7d | 2008-11-12 23:13:54 +0000 | [diff] [blame^] | 23 | : PreprocessorLexer(&pp), FileLoc(fileloc), Tokens(TokArray), |
| 24 | NumTokens(NumToks), CurToken(0) { |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 25 | |
| 26 | assert (Tokens[NumTokens-1].is(tok::eof)); |
| 27 | --NumTokens; |
| 28 | |
| 29 | LexingRawMode = false; |
| 30 | ParsingPreprocessorDirective = false; |
| 31 | ParsingFilename = false; |
| 32 | } |
| 33 | |
| 34 | void PTHLexer::Lex(Token& Tok) { |
| 35 | |
| 36 | if (CurToken == NumTokens) { |
| 37 | // If we hit the end of the file while parsing a preprocessor directive, |
| 38 | // end the preprocessor directive first. The next token returned will |
| 39 | // then be the end of file. |
| 40 | // OR |
| 41 | // If we are in raw mode, return this event as an EOF token. Let the caller |
| 42 | // that put us in raw mode handle the event. |
| 43 | if (ParsingPreprocessorDirective || LexingRawMode) { |
| 44 | // Done parsing the "line". |
| 45 | ParsingPreprocessorDirective = false; |
| 46 | Tok = Tokens[CurToken]; // not an out-of-bound access |
| 47 | // FIXME: eom handling? |
| 48 | } |
| 49 | else |
Ted Kremenek | d6a2e7d | 2008-11-12 23:13:54 +0000 | [diff] [blame^] | 50 | PP->HandleEndOfFile(Tok, false); |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 51 | |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | Tok = Tokens[CurToken]; |
| 56 | |
| 57 | if (ParsingPreprocessorDirective && Tok.isAtStartOfLine()) { |
| 58 | ParsingPreprocessorDirective = false; // Done parsing the "line". |
| 59 | MIOpt.ReadToken(); |
| 60 | // FIXME: Need to replicate: |
| 61 | // FormTokenWithChars(Tok, CurPtr, tok::eom); |
| 62 | Tok.setKind(tok::eom); |
| 63 | return; |
| 64 | } |
| 65 | else // Otherwise, advance to the next token. |
| 66 | ++CurToken; |
| 67 | |
| 68 | if (Tok.isAtStartOfLine() && Tok.is(tok::hash) && !LexingRawMode) { |
Ted Kremenek | d6a2e7d | 2008-11-12 23:13:54 +0000 | [diff] [blame^] | 69 | PP->HandleDirective(Tok); |
| 70 | PP->Lex(Tok); |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 71 | return; |
| 72 | } |
| 73 | |
| 74 | MIOpt.ReadToken(); |
| 75 | } |
| 76 | |
| 77 | void PTHLexer::setEOF(Token& Tok) { |
| 78 | Tok = Tokens[NumTokens]; // NumTokens is already adjusted, so this isn't |
| 79 | // an overflow. |
| 80 | } |