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