aboutsummaryrefslogtreecommitdiff
path: root/lib/Lex/PTHLexer.cpp
blob: 5adcd9f6a1427a8c0ab18d44b7ab07c3fac7ce85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//===--- PTHLexer.cpp - Lex from a token stream ---------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the PTHLexer interface.
//
//===----------------------------------------------------------------------===//

#include "clang/Lex/PTHLexer.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Basic/TokenKinds.h"
using namespace clang;

PTHLexer::PTHLexer(Preprocessor& pp, SourceLocation fileloc,
                   const Token *TokArray, unsigned NumToks)
  : PreprocessorLexer(&pp, fileloc), FileLoc(fileloc),
    Tokens(TokArray), NumTokens(NumToks), CurToken(0) {

  assert (Tokens[NumTokens-1].is(tok::eof));
  --NumTokens;
    
  LexingRawMode = false;
  ParsingPreprocessorDirective = false;
  ParsingFilename = false;
}

void PTHLexer::Lex(Token& Tok) {

  if (CurToken == NumTokens) {    
    // If we hit the end of the file while parsing a preprocessor directive,
    // end the preprocessor directive first.  The next token returned will
    // then be the end of file.
    //   OR
    // If we are in raw mode, return this event as an EOF token.  Let the caller
    // that put us in raw mode handle the event.
    if (ParsingPreprocessorDirective || LexingRawMode) {
      // Done parsing the "line".
      ParsingPreprocessorDirective = false;
      Tok = Tokens[CurToken]; // not an out-of-bound access
      // FIXME: eom handling?
    }
    else
      PP->HandleEndOfFile(Tok, false);
    
    return;
  }

  Tok = Tokens[CurToken];
  
  if (ParsingPreprocessorDirective && Tok.isAtStartOfLine()) {
    ParsingPreprocessorDirective = false; // Done parsing the "line".
    MIOpt.ReadToken();
    // FIXME:  Need to replicate:
    // FormTokenWithChars(Tok, CurPtr, tok::eom);
    Tok.setKind(tok::eom);    
    return;
  }
  else // Otherwise, advance to the next token.
    ++CurToken;

  if (Tok.isAtStartOfLine() && Tok.is(tok::hash) && !LexingRawMode) {
    PP->HandleDirective(Tok);
    PP->Lex(Tok);
    return;
  }
    
  MIOpt.ReadToken();
}

void PTHLexer::setEOF(Token& Tok) {
  Tok = Tokens[NumTokens]; // NumTokens is already adjusted, so this isn't
                           // an overflow.
}

void PTHLexer::DiscardToEndOfLine() {
  assert(ParsingPreprocessorDirective && ParsingFilename == false &&
         "Must be in a preprocessing directive!");

  // Already at end-of-file?
  if (CurToken == NumTokens)
    return;

  // Find the first token that is not the start of the *current* line.
  for ( ++CurToken; CurToken != NumTokens ; ++CurToken )
    if (Tokens[CurToken].isAtStartOfLine())
      return;
}

unsigned PTHLexer::isNextPPTokenLParen() {  
  if (CurToken == NumTokens)
    return 2;
  
  return Tokens[CurToken].is(tok::l_paren);
}