PTHLexer.h revision 2b2453a7d8fe732561795431f39ceb2b2a832d84
1274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek//===--- PTHLexer.h - Lexer based on Pre-tokenized input --------*- C++ -*-===// 2274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek// 3274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek// The LLVM Compiler Infrastructure 4274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek// 5274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek// This file is distributed under the University of Illinois Open Source 6274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek// License. See LICENSE.TXT for details. 7274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek// 8274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek//===----------------------------------------------------------------------===// 9274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek// 10274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek// This file defines the PTHLexer interface. 11274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek// 12274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek//===----------------------------------------------------------------------===// 13274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek 14452e37837a48b2f0ced144784277fd4d28cbede9Ted Kremenek#ifndef LLVM_CLANG_PTHLEXER_H 15452e37837a48b2f0ced144784277fd4d28cbede9Ted Kremenek#define LLVM_CLANG_PTHLEXER_H 16274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek 17274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek#include "clang/Lex/PreprocessorLexer.h" 1882a500b141b9a8001dac69f047478a43e2aebdffTed Kremenek#include <vector> 19274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek 20274b20863a728cc6a31ee75c670e3733600c1531Ted Kremeneknamespace clang { 21274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek 220c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenekclass PTHManager; 235f074266cc59563036c40516c814d63825723e20Ted Kremenekclass PTHSpellingSearch; 240c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek 25274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenekclass PTHLexer : public PreprocessorLexer { 262b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner SourceLocation FileStartLoc; 275f074266cc59563036c40516c814d63825723e20Ted Kremenek 280c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek /// TokBuf - Buffer from PTH file containing raw token data. 290c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek const char* TokBuf; 30cd223444d1680290efe11da657faafc9a1ac14baTed Kremenek 31cd223444d1680290efe11da657faafc9a1ac14baTed Kremenek /// CurPtr - Pointer into current offset of the token buffer where 32cd223444d1680290efe11da657faafc9a1ac14baTed Kremenek /// the next token will be read. 33cd223444d1680290efe11da657faafc9a1ac14baTed Kremenek const char* CurPtr; 34cd223444d1680290efe11da657faafc9a1ac14baTed Kremenek 35cd223444d1680290efe11da657faafc9a1ac14baTed Kremenek /// LastHashTokPtr - Pointer into TokBuf of the last processed '#' 36cd223444d1680290efe11da657faafc9a1ac14baTed Kremenek /// token that appears at the start of a line. 37cd223444d1680290efe11da657faafc9a1ac14baTed Kremenek const char* LastHashTokPtr; 38268ee7016a2811803989487c0ad3799486092c63Ted Kremenek 39268ee7016a2811803989487c0ad3799486092c63Ted Kremenek /// PPCond - Pointer to a side table in the PTH file that provides a 40268ee7016a2811803989487c0ad3799486092c63Ted Kremenek /// a consise summary of the preproccessor conditional block structure. 41268ee7016a2811803989487c0ad3799486092c63Ted Kremenek /// This is used to perform quick skipping of conditional blocks. 42268ee7016a2811803989487c0ad3799486092c63Ted Kremenek const char* PPCond; 43268ee7016a2811803989487c0ad3799486092c63Ted Kremenek 44268ee7016a2811803989487c0ad3799486092c63Ted Kremenek /// CurPPCondPtr - Pointer inside PPCond that refers to the next entry 45268ee7016a2811803989487c0ad3799486092c63Ted Kremenek /// to process when doing quick skipping of preprocessor blocks. 46268ee7016a2811803989487c0ad3799486092c63Ted Kremenek const char* CurPPCondPtr; 475f074266cc59563036c40516c814d63825723e20Ted Kremenek 485f074266cc59563036c40516c814d63825723e20Ted Kremenek /// MySpellingMgr - Reference to the spelling manager used to get spellings 495f074266cc59563036c40516c814d63825723e20Ted Kremenek /// for the source file indicated by \c FileID. 505f074266cc59563036c40516c814d63825723e20Ted Kremenek PTHSpellingSearch& MySpellingSrch; 5182a500b141b9a8001dac69f047478a43e2aebdffTed Kremenek 52274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek PTHLexer(const PTHLexer&); // DO NOT IMPLEMENT 53274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek void operator=(const PTHLexer&); // DO NOT IMPLEMENT 540c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek 550c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek /// ReadToken - Used by PTHLexer to read tokens TokBuf. 560c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek void ReadToken(Token& T); 57274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek 580c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek /// PTHMgr - The PTHManager object that created this PTHLexer. 590c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek PTHManager& PTHMgr; 600c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek 610c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek Token EofToken; 625f074266cc59563036c40516c814d63825723e20Ted Kremenek 635f074266cc59563036c40516c814d63825723e20Ted Kremenekprotected: 645f074266cc59563036c40516c814d63825723e20Ted Kremenek friend class PTHManager; 65274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek 66274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek /// Create a PTHLexer for the specified token stream. 672b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner PTHLexer(Preprocessor& pp, FileID FID, const char *D, const char* ppcond, 682b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner PTHSpellingSearch& mySpellingSrch, PTHManager &PM); 695f074266cc59563036c40516c814d63825723e20Ted Kremenekpublic: 705f074266cc59563036c40516c814d63825723e20Ted Kremenek 71274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek ~PTHLexer() {} 72274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek 73274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek /// Lex - Return the next token. 74274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek void Lex(Token &Tok); 75274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek 7659d08cb672136322375e5400578ee1fbd0947de2Ted Kremenek void getEOF(Token &Tok); 77274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek 789f1384f7db76e24068f6c9d7d881714addb6c029Ted Kremenek /// DiscardToEndOfLine - Read the rest of the current preprocessor line as an 7917ff58a63197b398ae52697b088dc0fb8b255519Ted Kremenek /// uninterpreted string. This switches the lexer out of directive mode. 8017ff58a63197b398ae52697b088dc0fb8b255519Ted Kremenek void DiscardToEndOfLine(); 812f1c0243f7fc5d12446efcb006c071854c345923Ted Kremenek 822f1c0243f7fc5d12446efcb006c071854c345923Ted Kremenek /// isNextPPTokenLParen - Return 1 if the next unexpanded token will return a 832f1c0243f7fc5d12446efcb006c071854c345923Ted Kremenek /// tok::l_paren token, 0 if it is something else and 2 if there are no more 842f1c0243f7fc5d12446efcb006c071854c345923Ted Kremenek /// tokens controlled by this lexer. 8531aba425a01c8c957e662ccfaa71f923d0f0932aTed Kremenek unsigned isNextPPTokenLParen() { 86daeee815169f37f2a49f80af7bd104722672ad97Ted Kremenek // isNextPPTokenLParen is not on the hot path, and all we care about is 87daeee815169f37f2a49f80af7bd104722672ad97Ted Kremenek // whether or not we are at a token with kind tok::eof or tok::l_paren. 88daeee815169f37f2a49f80af7bd104722672ad97Ted Kremenek // Just read the first byte from the current token pointer to determine 89daeee815169f37f2a49f80af7bd104722672ad97Ted Kremenek // its kind. 90e36dabb2d9567c98734a1063ac7748dc37a999ceChris Lattner tok::TokenKind x = (tok::TokenKind) (unsigned char) *CurPtr; 91daeee815169f37f2a49f80af7bd104722672ad97Ted Kremenek return x == tok::eof ? 2 : x == tok::l_paren; 92daeee815169f37f2a49f80af7bd104722672ad97Ted Kremenek } 9331aba425a01c8c957e662ccfaa71f923d0f0932aTed Kremenek 94ac2dda65d2d39c0a83d95bdf6237edbf4685bc58Ted Kremenek /// IndirectLex - An indirect call to 'Lex' that can be invoked via 95ac2dda65d2d39c0a83d95bdf6237edbf4685bc58Ted Kremenek /// the PreprocessorLexer interface. 96ac2dda65d2d39c0a83d95bdf6237edbf4685bc58Ted Kremenek void IndirectLex(Token &Result) { Lex(Result); } 97b70e3dafb9618f34017061400dc19ac5e3539a6dTed Kremenek 98b70e3dafb9618f34017061400dc19ac5e3539a6dTed Kremenek /// Returns the cached spelling of a token. 99b70e3dafb9618f34017061400dc19ac5e3539a6dTed Kremenek /// \param[in] sloc The SourceLocation of the token. 100b70e3dafb9618f34017061400dc19ac5e3539a6dTed Kremenek /// \param[out] Buffer If a token's spelling is found in the PTH file then 101b70e3dafb9618f34017061400dc19ac5e3539a6dTed Kremenek /// upon exit from this method \c Buffer will be set to the address of 102b70e3dafb9618f34017061400dc19ac5e3539a6dTed Kremenek /// the character array representing that spelling. No characters 103b70e3dafb9618f34017061400dc19ac5e3539a6dTed Kremenek /// are copied. 104b70e3dafb9618f34017061400dc19ac5e3539a6dTed Kremenek /// \returns The number of characters for the spelling of the token. This 105b70e3dafb9618f34017061400dc19ac5e3539a6dTed Kremenek /// value is 0 if the spelling could not be found in the PTH file. 106b70e3dafb9618f34017061400dc19ac5e3539a6dTed Kremenek unsigned getSpelling(SourceLocation sloc, const char *&Buffer); 10731aba425a01c8c957e662ccfaa71f923d0f0932aTed Kremenek 108bc0f6bc0391ecdff331885cdc769c20b2cb628a6Ted Kremenek /// getSourceLocation - Return a source location for the token in 109bc0f6bc0391ecdff331885cdc769c20b2cb628a6Ted Kremenek /// the current file. 11030a12ec2a7f331d9e08acabe7cda853aaa7ba54bTed Kremenek SourceLocation getSourceLocation(); 11130a12ec2a7f331d9e08acabe7cda853aaa7ba54bTed Kremenek 112268ee7016a2811803989487c0ad3799486092c63Ted Kremenek /// SkipBlock - Used by Preprocessor to skip the current conditional block. 113268ee7016a2811803989487c0ad3799486092c63Ted Kremenek bool SkipBlock(); 114274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek}; 115274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek 116274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek} // end namespace clang 117274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek 118274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek#endif 119