blob: 936a03ac0f0aa151a25f57a7e61ea44bc06a059b [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),
23 LastToken(NumTokens - 1),
24 CurToken(0) {
Ted Kremenek274b2082008-11-12 21:37:15 +000025
Ted Kremenek452e3782008-11-20 01:29:45 +000026 assert (NumTokens >= 1);
27 assert (Tokens[LastToken].is(tok::eof));
Ted Kremenek274b2082008-11-12 21:37:15 +000028}
29
30void PTHLexer::Lex(Token& Tok) {
31
Ted Kremenek452e3782008-11-20 01:29:45 +000032 if (CurToken == LastToken) {
Ted Kremenek274b2082008-11-12 21:37:15 +000033 // 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 Kremenekd6a2e7d2008-11-12 23:13:54 +000046 PP->HandleEndOfFile(Tok, false);
Ted Kremenek274b2082008-11-12 21:37:15 +000047
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 Kremenekd6a2e7d2008-11-12 23:13:54 +000065 PP->HandleDirective(Tok);
66 PP->Lex(Tok);
Ted Kremenek274b2082008-11-12 21:37:15 +000067 return;
68 }
69
70 MIOpt.ReadToken();
71}
72
73void PTHLexer::setEOF(Token& Tok) {
Ted Kremenek452e3782008-11-20 01:29:45 +000074 Tok = Tokens[LastToken];
Ted Kremenek274b2082008-11-12 21:37:15 +000075}
Ted Kremenek17ff58a2008-11-19 22:21:33 +000076
77void PTHLexer::DiscardToEndOfLine() {
78 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
79 "Must be in a preprocessing directive!");
Ted Kremenek4d35da22008-11-20 01:16:50 +000080
81 // Already at end-of-file?
Ted Kremenek452e3782008-11-20 01:29:45 +000082 if (CurToken == LastToken)
Ted Kremenek4d35da22008-11-20 01:16:50 +000083 return;
84
85 // Find the first token that is not the start of the *current* line.
Ted Kremenek452e3782008-11-20 01:29:45 +000086 for ( ++CurToken; CurToken != LastToken ; ++CurToken )
Ted Kremenek4d35da22008-11-20 01:16:50 +000087 if (Tokens[CurToken].isAtStartOfLine())
88 return;
Ted Kremenek17ff58a2008-11-19 22:21:33 +000089}
Ted Kremenek2f1c0242008-11-19 22:42:26 +000090
Ted Kremenek4d35da22008-11-20 01:16:50 +000091unsigned PTHLexer::isNextPPTokenLParen() {
Ted Kremenek452e3782008-11-20 01:29:45 +000092 if (CurToken == LastToken)
Ted Kremenek4d35da22008-11-20 01:16:50 +000093 return 2;
94
95 return Tokens[CurToken].is(tok::l_paren);
Ted Kremenek2f1c0242008-11-19 22:42:26 +000096}
97