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