freebsd-nq/include/clang/Lex/PreprocessorLexer.h

184 lines
6.2 KiB
C
Raw Normal View History

2009-06-02 17:58:47 +00:00
//===--- PreprocessorLexer.h - C Language Family Lexer ----------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief Defines the PreprocessorLexer interface.
///
2009-06-02 17:58:47 +00:00
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_PreprocessorLexer_H
#define LLVM_CLANG_PreprocessorLexer_H
#include "clang/Lex/MultipleIncludeOpt.h"
#include "clang/Lex/Token.h"
#include "llvm/ADT/SmallVector.h"
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
namespace clang {
2010-04-02 08:55:10 +00:00
class FileEntry;
2009-06-02 17:58:47 +00:00
class Preprocessor;
class PreprocessorLexer {
virtual void anchor();
2009-06-02 17:58:47 +00:00
protected:
Preprocessor *PP; // Preprocessor object controlling lexing.
/// The SourceManager FileID corresponding to the file being lexed.
const FileID FID;
2009-10-14 18:03:49 +00:00
/// \brief Number of SLocEntries before lexing the file.
unsigned InitialNumSLocEntries;
2009-06-02 17:58:47 +00:00
//===--------------------------------------------------------------------===//
// Context-specific lexing flags set by the preprocessor.
//===--------------------------------------------------------------------===//
2009-10-14 18:03:49 +00:00
/// \brief True when parsing \#XXX; turns '\\n' into a tok::eod token.
2009-06-02 17:58:47 +00:00
bool ParsingPreprocessorDirective;
2009-10-14 18:03:49 +00:00
/// \brief True after \#include; turns \<xx> into a tok::angle_string_literal
/// token.
2009-06-02 17:58:47 +00:00
bool ParsingFilename;
2009-10-14 18:03:49 +00:00
/// \brief True if in raw mode.
///
/// Raw mode disables interpretation of tokens and is a far faster mode to
/// lex in than non-raw-mode. This flag:
2009-06-02 17:58:47 +00:00
/// 1. If EOF of the current lexer is found, the include stack isn't popped.
/// 2. Identifier information is not looked up for identifier tokens. As an
/// effect of this, implicit macro expansion is naturally disabled.
/// 3. "#" tokens at the start of a line are treated as normal tokens, not
/// implicitly transformed by the lexer.
/// 4. All diagnostic messages are disabled.
/// 5. No callbacks are made into the preprocessor.
///
/// Note that in raw mode that the PP pointer may be null.
bool LexingRawMode;
2009-10-14 18:03:49 +00:00
/// \brief A state machine that detects the \#ifndef-wrapping a file
2009-06-02 17:58:47 +00:00
/// idiom for the multiple-include optimization.
MultipleIncludeOpt MIOpt;
2009-10-14 18:03:49 +00:00
/// \brief Information about the set of \#if/\#ifdef/\#ifndef blocks
2009-06-02 17:58:47 +00:00
/// we are currently in.
SmallVector<PPConditionalInfo, 4> ConditionalStack;
2009-10-14 18:03:49 +00:00
PreprocessorLexer(const PreprocessorLexer &) LLVM_DELETED_FUNCTION;
void operator=(const PreprocessorLexer &) LLVM_DELETED_FUNCTION;
2009-06-02 17:58:47 +00:00
friend class Preprocessor;
2009-10-14 18:03:49 +00:00
PreprocessorLexer(Preprocessor *pp, FileID fid);
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
PreprocessorLexer()
: PP(0), InitialNumSLocEntries(0),
2009-06-02 17:58:47 +00:00
ParsingPreprocessorDirective(false),
ParsingFilename(false),
LexingRawMode(false) {}
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
virtual ~PreprocessorLexer() {}
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
virtual void IndirectLex(Token& Result) = 0;
2009-10-14 18:03:49 +00:00
/// \brief Return the source location for the next observable location.
2009-06-02 17:58:47 +00:00
virtual SourceLocation getSourceLocation() = 0;
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
//===--------------------------------------------------------------------===//
// #if directive handling.
2009-10-14 18:03:49 +00:00
/// pushConditionalLevel - When we enter a \#if directive, this keeps track of
/// what we are currently in for diagnostic emission (e.g. \#if with missing
/// \#endif).
2009-06-02 17:58:47 +00:00
void pushConditionalLevel(SourceLocation DirectiveStart, bool WasSkipping,
bool FoundNonSkip, bool FoundElse) {
PPConditionalInfo CI;
CI.IfLoc = DirectiveStart;
CI.WasSkipping = WasSkipping;
CI.FoundNonSkip = FoundNonSkip;
CI.FoundElse = FoundElse;
ConditionalStack.push_back(CI);
}
void pushConditionalLevel(const PPConditionalInfo &CI) {
ConditionalStack.push_back(CI);
2009-10-14 18:03:49 +00:00
}
2009-06-02 17:58:47 +00:00
/// popConditionalLevel - Remove an entry off the top of the conditional
/// stack, returning information about it. If the conditional stack is empty,
/// this returns true and does not fill in the arguments.
bool popConditionalLevel(PPConditionalInfo &CI) {
if (ConditionalStack.empty())
return true;
CI = ConditionalStack.pop_back_val();
2009-06-02 17:58:47 +00:00
return false;
}
2009-10-14 18:03:49 +00:00
/// \brief Return the top of the conditional stack.
/// \pre This requires that there be a conditional active.
2009-06-02 17:58:47 +00:00
PPConditionalInfo &peekConditionalLevel() {
assert(!ConditionalStack.empty() && "No conditionals active!");
return ConditionalStack.back();
}
2009-10-14 18:03:49 +00:00
unsigned getConditionalStackDepth() const { return ConditionalStack.size(); }
2009-06-02 17:58:47 +00:00
public:
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
//===--------------------------------------------------------------------===//
// Misc. lexing methods.
2009-10-14 18:03:49 +00:00
/// \brief After the preprocessor has parsed a \#include, lex and
/// (potentially) macro expand the filename.
///
/// If the sequence parsed is not lexically legal, emit a diagnostic and
/// return a result EOD token.
2009-06-02 17:58:47 +00:00
void LexIncludeFilename(Token &Result);
2009-10-14 18:03:49 +00:00
/// \brief Inform the lexer whether or not we are currently lexing a
/// preprocessor directive.
2009-06-02 17:58:47 +00:00
void setParsingPreprocessorDirective(bool f) {
ParsingPreprocessorDirective = f;
}
2009-10-14 18:03:49 +00:00
/// \brief Return true if this lexer is in raw mode or not.
2009-06-02 17:58:47 +00:00
bool isLexingRawMode() const { return LexingRawMode; }
/// \brief Return the preprocessor object for this lexer.
2009-06-02 17:58:47 +00:00
Preprocessor *getPP() const { return PP; }
2009-10-14 18:03:49 +00:00
FileID getFileID() const {
2009-06-02 17:58:47 +00:00
assert(PP &&
"PreprocessorLexer::getFileID() should only be used with a Preprocessor");
return FID;
}
2009-10-14 18:03:49 +00:00
/// \brief Number of SLocEntries before lexing the file.
unsigned getInitialNumSLocEntries() const {
return InitialNumSLocEntries;
}
2009-06-02 17:58:47 +00:00
/// getFileEntry - Return the FileEntry corresponding to this FileID. Like
/// getFileID(), this only works for lexers with attached preprocessors.
const FileEntry *getFileEntry() const;
/// \brief Iterator that traverses the current stack of preprocessor
/// conditional directives (\#if/\#ifdef/\#ifndef).
typedef SmallVectorImpl<PPConditionalInfo>::const_iterator
conditional_iterator;
conditional_iterator conditional_begin() const {
return ConditionalStack.begin();
}
conditional_iterator conditional_end() const {
return ConditionalStack.end();
}
2009-06-02 17:58:47 +00:00
};
} // end namespace clang
#endif