PreprocessorLexer.h revision 0e977de1eacfbc143bdedad87c14b53814159023
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
64protected:
65
66  //===--------------------------------------------------------------------===//
67  // #if directive handling.
68
69  /// pushConditionalLevel - When we enter a #if directive, this keeps track of
70  /// what we are currently in for diagnostic emission (e.g. #if with missing
71  /// #endif).
72  void pushConditionalLevel(SourceLocation DirectiveStart, bool WasSkipping,
73                            bool FoundNonSkip, bool FoundElse) {
74    PPConditionalInfo CI;
75    CI.IfLoc = DirectiveStart;
76    CI.WasSkipping = WasSkipping;
77    CI.FoundNonSkip = FoundNonSkip;
78    CI.FoundElse = FoundElse;
79    ConditionalStack.push_back(CI);
80  }
81  void pushConditionalLevel(const PPConditionalInfo &CI) {
82    ConditionalStack.push_back(CI);
83  }
84
85  /// popConditionalLevel - Remove an entry off the top of the conditional
86  /// stack, returning information about it.  If the conditional stack is empty,
87  /// this returns true and does not fill in the arguments.
88  bool popConditionalLevel(PPConditionalInfo &CI) {
89    if (ConditionalStack.empty()) return true;
90    CI = ConditionalStack.back();
91    ConditionalStack.pop_back();
92    return false;
93  }
94
95  /// peekConditionalLevel - Return the top of the conditional stack.  This
96  /// requires that there be a conditional active.
97  PPConditionalInfo &peekConditionalLevel() {
98    assert(!ConditionalStack.empty() && "No conditionals active!");
99    return ConditionalStack.back();
100  }
101
102  unsigned getConditionalStackDepth() const { return ConditionalStack.size(); }
103};
104
105}  // end namespace clang
106
107#endif
108