blob: a88470bbad69eeed5da14e4990537f67bf56bc53 [file] [log] [blame]
Ted Kremenek274b2082008-11-12 21:37:15 +00001//===--- 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 Kremenek274b2082008-11-12 21:37:15 +000016#include "clang/Basic/TokenKinds.h"
Ted Kremenek274b2082008-11-12 21:37:15 +000017using namespace clang;
18
19PTHLexer::PTHLexer(Preprocessor& pp, SourceLocation fileloc,
Ted Kremenek452e3782008-11-20 01:29:45 +000020 const Token *TokArray, unsigned NumTokens)
21 : PreprocessorLexer(&pp, fileloc),
22 Tokens(TokArray),
Ted Kremenek31aba422008-11-20 16:32:22 +000023 LastTokenIdx(NumTokens - 1),
24 CurTokenIdx(0) {
Ted Kremenek274b2082008-11-12 21:37:15 +000025
Ted Kremenekd6f53dc2008-11-20 07:58:05 +000026 assert(NumTokens >= 1);
Ted Kremenek31aba422008-11-20 16:32:22 +000027 assert(Tokens[LastTokenIdx].is(tok::eof));
Ted Kremenek274b2082008-11-12 21:37:15 +000028}
29
Ted Kremenek89d7ee92008-11-20 19:49:00 +000030Token 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 Kremenek274b2082008-11-12 21:37:15 +000042void PTHLexer::Lex(Token& Tok) {
Ted Kremenekd6f53dc2008-11-20 07:58:05 +000043LexNextToken:
Ted Kremenek31aba422008-11-20 16:32:22 +000044 if (AtLastToken()) {
Ted Kremenekd6f53dc2008-11-20 07:58:05 +000045 if (ParsingPreprocessorDirective) {
Ted Kremenek274b2082008-11-12 21:37:15 +000046 ParsingPreprocessorDirective = false;
Ted Kremenek31aba422008-11-20 16:32:22 +000047 Tok = GetToken();
Ted Kremenekd6f53dc2008-11-20 07:58:05 +000048 Tok.setKind(tok::eom);
49 MIOpt.ReadToken();
50 return;
Ted Kremenek274b2082008-11-12 21:37:15 +000051 }
Ted Kremenek274b2082008-11-12 21:37:15 +000052
Ted Kremenekd6f53dc2008-11-20 07:58:05 +000053 assert(!LexingRawMode && "PTHLexer cannot lex in raw mode.");
54
55 // FIXME: Issue diagnostics similar to Lexer.
56 PP->HandleEndOfFile(Tok, false);
Ted Kremenek274b2082008-11-12 21:37:15 +000057 return;
58 }
59
Ted Kremenek31aba422008-11-20 16:32:22 +000060 Tok = GetToken();
Ted Kremenek274b2082008-11-12 21:37:15 +000061
Ted Kremenekd6f53dc2008-11-20 07:58:05 +000062 // Don't advance to the next token yet. Check if we are at the
63 // start of a new line and we're processing a directive. If so, we
64 // consume this token twice, once as an tok::eom.
65 if (Tok.isAtStartOfLine() && ParsingPreprocessorDirective) {
66 ParsingPreprocessorDirective = false;
67 Tok.setKind(tok::eom);
Ted Kremenek274b2082008-11-12 21:37:15 +000068 MIOpt.ReadToken();
Ted Kremenek274b2082008-11-12 21:37:15 +000069 return;
70 }
Ted Kremenekd6f53dc2008-11-20 07:58:05 +000071
72 // Advance to the next token.
Ted Kremenek31aba422008-11-20 16:32:22 +000073 AdvanceToken();
Ted Kremenek274b2082008-11-12 21:37:15 +000074
Ted Kremenekd6f53dc2008-11-20 07:58:05 +000075 if (Tok.is(tok::hash)) {
76 if (Tok.isAtStartOfLine() && !LexingRawMode) {
77 PP->HandleDirective(Tok);
78
79 if (PP->isCurrentLexer(this))
80 goto LexNextToken;
81
82 return PP->Lex(Tok);
83 }
84 }
85
Ted Kremenek274b2082008-11-12 21:37:15 +000086 MIOpt.ReadToken();
Ted Kremenekd6f53dc2008-11-20 07:58:05 +000087
88 if (Tok.is(tok::identifier)) {
89 if (LexingRawMode) return;
90 return PP->HandleIdentifier(Tok);
91 }
Ted Kremenek274b2082008-11-12 21:37:15 +000092}
93
94void PTHLexer::setEOF(Token& Tok) {
Ted Kremenek31aba422008-11-20 16:32:22 +000095 Tok = Tokens[LastTokenIdx];
Ted Kremenek274b2082008-11-12 21:37:15 +000096}
Ted Kremenek17ff58a2008-11-19 22:21:33 +000097
98void PTHLexer::DiscardToEndOfLine() {
99 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
100 "Must be in a preprocessing directive!");
Ted Kremenek4d35da22008-11-20 01:16:50 +0000101
102 // Already at end-of-file?
Ted Kremenek31aba422008-11-20 16:32:22 +0000103 if (AtLastToken())
Ted Kremenek4d35da22008-11-20 01:16:50 +0000104 return;
105
106 // Find the first token that is not the start of the *current* line.
Ted Kremenek31aba422008-11-20 16:32:22 +0000107 for (AdvanceToken(); !AtLastToken(); AdvanceToken())
108 if (GetToken().isAtStartOfLine())
Ted Kremenek4d35da22008-11-20 01:16:50 +0000109 return;
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000110}