PreprocessorLexer.h revision 2c71d0f00e40b7b0a85561d430f52247b9c6e7f2
1//===--- PreprocessorLexer.h - C Language Family Lexer ----------*- C++ -*-===//
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 defines the PreprocessorLexer interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_PreprocessorLexer_H
15#define LLVM_CLANG_PreprocessorLexer_H
16
17#include "clang/Lex/MultipleIncludeOpt.h"
18#include "clang/Lex/Token.h"
19#include <vector>
20
21namespace clang {
22
23class Preprocessor;
24
25class PreprocessorLexer {
26protected:
27  // Context-specific lexing flags set by the preprocessor.
28
29  /// ParsingPreprocessorDirective - This is true when parsing #XXX.  This turns
30  /// '\n' into a tok::eom token.
31  bool ParsingPreprocessorDirective;
32
33  /// ParsingFilename - True after #include: this turns <xx> into a
34  /// tok::angle_string_literal token.
35  bool ParsingFilename;
36
37  /// LexingRawMode - True if in raw mode:  This flag disables interpretation of
38  /// tokens and is a far faster mode to lex in than non-raw-mode.  This flag:
39  ///  1. If EOF of the current lexer is found, the include stack isn't popped.
40  ///  2. Identifier information is not looked up for identifier tokens.  As an
41  ///     effect of this, implicit macro expansion is naturally disabled.
42  ///  3. "#" tokens at the start of a line are treated as normal tokens, not
43  ///     implicitly transformed by the lexer.
44  ///  4. All diagnostic messages are disabled.
45  ///  5. No callbacks are made into the preprocessor.
46  ///
47  /// Note that in raw mode that the PP pointer may be null.
48  bool LexingRawMode;
49
50  /// MIOpt - This is a state machine that detects the #ifndef-wrapping a file
51  /// idiom for the multiple-include optimization.
52  MultipleIncludeOpt MIOpt;
53
54  /// ConditionalStack - Information about the set of #if/#ifdef/#ifndef blocks
55  /// we are currently in.
56  std::vector<PPConditionalInfo> ConditionalStack;
57
58  PreprocessorLexer(const PreprocessorLexer&);          // DO NOT IMPLEMENT
59  void operator=(const PreprocessorLexer&); // DO NOT IMPLEMENT
60  friend class Preprocessor;
61
62  PreprocessorLexer() {}
63  virtual ~PreprocessorLexer();
64
65  virtual void IndirectLex(Token& Result) = 0;
66
67protected:
68
69  //===--------------------------------------------------------------------===//
70  // #if directive handling.
71
72  /// pushConditionalLevel - When we enter a #if directive, this keeps track of
73  /// what we are currently in for diagnostic emission (e.g. #if with missing
74  /// #endif).
75  void pushConditionalLevel(SourceLocation DirectiveStart, bool WasSkipping,
76                            bool FoundNonSkip, bool FoundElse) {
77    PPConditionalInfo CI;
78    CI.IfLoc = DirectiveStart;
79    CI.WasSkipping = WasSkipping;
80    CI.FoundNonSkip = FoundNonSkip;
81    CI.FoundElse = FoundElse;
82    ConditionalStack.push_back(CI);
83  }
84  void pushConditionalLevel(const PPConditionalInfo &CI) {
85    ConditionalStack.push_back(CI);
86  }
87
88  /// popConditionalLevel - Remove an entry off the top of the conditional
89  /// stack, returning information about it.  If the conditional stack is empty,
90  /// this returns true and does not fill in the arguments.
91  bool popConditionalLevel(PPConditionalInfo &CI) {
92    if (ConditionalStack.empty()) return true;
93    CI = ConditionalStack.back();
94    ConditionalStack.pop_back();
95    return false;
96  }
97
98  /// peekConditionalLevel - Return the top of the conditional stack.  This
99  /// requires that there be a conditional active.
100  PPConditionalInfo &peekConditionalLevel() {
101    assert(!ConditionalStack.empty() && "No conditionals active!");
102    return ConditionalStack.back();
103  }
104
105  unsigned getConditionalStackDepth() const { return ConditionalStack.size(); }
106
107  //===--------------------------------------------------------------------===//
108  // Misc. lexing methods.
109
110  /// LexIncludeFilename - After the preprocessor has parsed a #include, lex and
111  /// (potentially) macro expand the filename.  If the sequence parsed is not
112  /// lexically legal, emit a diagnostic and return a result EOM token.
113  void LexIncludeFilename(Token &Result);
114};
115
116}  // end namespace clang
117
118#endif
119