2006-06-18 13:43:12 +08:00
|
|
|
//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-07-21 00:59:19 +08:00
|
|
|
// This file implements the Lexer and Token interfaces.
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// TODO: GCC Diagnostics emitted by the lexer:
|
|
|
|
// PEDWARN: (form feed|vertical tab) in preprocessing directive
|
|
|
|
//
|
|
|
|
// Universal characters, unicode, char mapping:
|
|
|
|
// WARNING: `%.*s' is not in NFKC
|
|
|
|
// WARNING: `%.*s' is not in NFC
|
|
|
|
//
|
|
|
|
// Other:
|
|
|
|
// TODO: Options to support:
|
|
|
|
// -fexec-charset,-fwide-exec-charset
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Lex/LexDiagnostic.h"
|
2007-07-21 00:37:10 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2007-07-23 02:38:25 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2007-04-29 15:12:06 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
#include <cctype>
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-12-17 13:29:40 +08:00
|
|
|
static void InitCharacterInfo();
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2007-10-07 16:47:24 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Token Class Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
|
2007-10-07 16:47:24 +08:00
|
|
|
bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
|
2008-12-02 05:46:47 +08:00
|
|
|
if (IdentifierInfo *II = getIdentifierInfo())
|
|
|
|
return II->getObjCKeywordID() == objcKey;
|
|
|
|
return false;
|
2007-10-07 16:47:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getObjCKeywordID - Return the ObjC keyword kind.
|
|
|
|
tok::ObjCKeywordKind Token::getObjCKeywordID() const {
|
|
|
|
IdentifierInfo *specId = getIdentifierInfo();
|
|
|
|
return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
|
|
|
|
}
|
|
|
|
|
2007-12-13 09:59:49 +08:00
|
|
|
|
2007-10-07 16:47:24 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Lexer Class Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void Lexer::InitLexer(const char *BufStart, const char *BufPtr,
|
2009-01-17 14:55:17 +08:00
|
|
|
const char *BufEnd) {
|
2009-12-17 13:29:40 +08:00
|
|
|
InitCharacterInfo();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 14:55:17 +08:00
|
|
|
BufferStart = BufStart;
|
|
|
|
BufferPtr = BufPtr;
|
|
|
|
BufferEnd = BufEnd;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 14:55:17 +08:00
|
|
|
assert(BufEnd[0] == 0 &&
|
|
|
|
"We assume that the input buffer has a null character at the end"
|
|
|
|
" to simplify lexing!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 14:55:17 +08:00
|
|
|
Is_PragmaLexer = false;
|
2009-12-14 14:16:57 +08:00
|
|
|
IsInConflictMarker = false;
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
llvm-svn: 82166
2009-09-18 05:32:03 +08:00
|
|
|
|
2009-01-17 14:55:17 +08:00
|
|
|
// Start of the file is a start of line.
|
|
|
|
IsAtStartOfLine = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 14:55:17 +08:00
|
|
|
// We are not after parsing a #.
|
|
|
|
ParsingPreprocessorDirective = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 14:55:17 +08:00
|
|
|
// We are not after parsing #include.
|
|
|
|
ParsingFilename = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 14:55:17 +08:00
|
|
|
// We are not in raw mode. Raw mode disables diagnostics and interpretation
|
|
|
|
// of tokens (e.g. identifiers, thus disabling macro expansion). It is used
|
|
|
|
// to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
|
|
|
|
// or otherwise skipping over tokens.
|
|
|
|
LexingRawMode = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 14:55:17 +08:00
|
|
|
// Default to not keeping comments.
|
|
|
|
ExtendedTokenMode = 0;
|
|
|
|
}
|
|
|
|
|
2009-01-17 15:56:59 +08:00
|
|
|
/// Lexer constructor - Create a new lexer object for the specified buffer
|
|
|
|
/// with the specified preprocessor managing the lexing process. This lexer
|
|
|
|
/// assumes that the associated file buffer and Preprocessor objects will
|
|
|
|
/// outlive it, so it doesn't take ownership of either of them.
|
2009-11-30 12:18:44 +08:00
|
|
|
Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP)
|
2009-01-17 16:03:42 +08:00
|
|
|
: PreprocessorLexer(&PP, FID),
|
|
|
|
FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
|
|
|
|
Features(PP.getLangOptions()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 15:56:59 +08:00
|
|
|
InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
|
|
|
|
InputFile->getBufferEnd());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 15:56:59 +08:00
|
|
|
// Default to keeping comments if the preprocessor wants them.
|
|
|
|
SetCommentRetentionState(PP.getCommentRetentionState());
|
|
|
|
}
|
2007-10-07 16:47:24 +08:00
|
|
|
|
2007-10-18 04:41:00 +08:00
|
|
|
/// Lexer constructor - Create a new raw lexer object. This object is only
|
2008-10-12 09:15:46 +08:00
|
|
|
/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
|
|
|
|
/// range will outlive it, so it doesn't take ownership of it.
|
2007-10-18 04:41:00 +08:00
|
|
|
Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
|
2009-01-17 15:42:27 +08:00
|
|
|
const char *BufStart, const char *BufPtr, const char *BufEnd)
|
2009-01-17 11:48:08 +08:00
|
|
|
: FileLoc(fileloc), Features(features) {
|
2009-01-17 14:55:17 +08:00
|
|
|
|
|
|
|
InitLexer(BufStart, BufPtr, BufEnd);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-18 04:41:00 +08:00
|
|
|
// We *are* in raw mode.
|
|
|
|
LexingRawMode = true;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2009-01-17 15:35:14 +08:00
|
|
|
/// Lexer constructor - Create a new raw lexer object. This object is only
|
|
|
|
/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
|
|
|
|
/// range will outlive it, so it doesn't take ownership of it.
|
2009-11-30 12:18:44 +08:00
|
|
|
Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile,
|
|
|
|
const SourceManager &SM, const LangOptions &features)
|
2009-01-17 15:35:14 +08:00
|
|
|
: FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
|
2009-01-17 15:35:14 +08:00
|
|
|
FromFile->getBufferEnd());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 15:35:14 +08:00
|
|
|
// We *are* in raw mode.
|
|
|
|
LexingRawMode = true;
|
|
|
|
}
|
|
|
|
|
2009-01-17 16:27:52 +08:00
|
|
|
/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
|
|
|
|
/// _Pragma expansion. This has a variety of magic semantics that this method
|
|
|
|
/// sets up. It returns a new'd Lexer that must be delete'd when done.
|
|
|
|
///
|
|
|
|
/// On entrance to this routine, TokStartLoc is a macro location which has a
|
|
|
|
/// spelling loc that indicates the bytes to be lexed for the token and an
|
|
|
|
/// instantiation location that indicates where all lexed tokens should be
|
|
|
|
/// "expanded from".
|
|
|
|
///
|
|
|
|
/// FIXME: It would really be nice to make _Pragma just be a wrapper around a
|
|
|
|
/// normal lexer that remaps tokens as they fly by. This would require making
|
|
|
|
/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer
|
|
|
|
/// interface that could handle this stuff. This would pull GetMappedTokenLoc
|
|
|
|
/// out of the critical path of the lexer!
|
|
|
|
///
|
2009-09-09 23:08:12 +08:00
|
|
|
Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
|
2009-02-16 04:52:18 +08:00
|
|
|
SourceLocation InstantiationLocStart,
|
|
|
|
SourceLocation InstantiationLocEnd,
|
2009-01-19 14:46:35 +08:00
|
|
|
unsigned TokLen, Preprocessor &PP) {
|
2009-01-17 16:27:52 +08:00
|
|
|
SourceManager &SM = PP.getSourceManager();
|
|
|
|
|
|
|
|
// Create the lexer as if we were going to lex the file normally.
|
2009-01-19 15:46:45 +08:00
|
|
|
FileID SpellingFID = SM.getFileID(SpellingLoc);
|
2009-11-30 12:18:44 +08:00
|
|
|
const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID);
|
|
|
|
Lexer *L = new Lexer(SpellingFID, InputFile, PP);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 16:27:52 +08:00
|
|
|
// Now that the lexer is created, change the start/end locations so that we
|
|
|
|
// just lex the subsection of the file that we want. This is lexing from a
|
|
|
|
// scratch buffer.
|
|
|
|
const char *StrData = SM.getCharacterData(SpellingLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 16:27:52 +08:00
|
|
|
L->BufferPtr = StrData;
|
|
|
|
L->BufferEnd = StrData+TokLen;
|
2009-03-08 16:08:45 +08:00
|
|
|
assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
|
2009-01-17 16:27:52 +08:00
|
|
|
|
|
|
|
// Set the SourceLocation with the remapping information. This ensures that
|
|
|
|
// GetMappedTokenLoc will remap the tokens as they are lexed.
|
2009-01-26 08:43:02 +08:00
|
|
|
L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
|
2009-02-16 04:52:18 +08:00
|
|
|
InstantiationLocStart,
|
|
|
|
InstantiationLocEnd, TokLen);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 16:27:52 +08:00
|
|
|
// Ensure that the lexer thinks it is inside a directive, so that end \n will
|
|
|
|
// return an EOM token.
|
|
|
|
L->ParsingPreprocessorDirective = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 16:27:52 +08:00
|
|
|
// This lexer really is for _Pragma.
|
|
|
|
L->Is_PragmaLexer = true;
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
2007-10-18 04:41:00 +08:00
|
|
|
|
2006-07-03 09:13:26 +08:00
|
|
|
/// Stringify - Convert the specified string into a C string, with surrounding
|
|
|
|
/// ""'s, and with escaped \ and " characters.
|
2006-07-15 13:23:31 +08:00
|
|
|
std::string Lexer::Stringify(const std::string &Str, bool Charify) {
|
2006-07-03 09:13:26 +08:00
|
|
|
std::string Result = Str;
|
2006-07-15 13:23:31 +08:00
|
|
|
char Quote = Charify ? '\'' : '"';
|
2006-07-03 09:13:26 +08:00
|
|
|
for (unsigned i = 0, e = Result.size(); i != e; ++i) {
|
2006-07-15 13:23:31 +08:00
|
|
|
if (Result[i] == '\\' || Result[i] == Quote) {
|
2006-07-03 09:13:26 +08:00
|
|
|
Result.insert(Result.begin()+i, '\\');
|
|
|
|
++i; ++e;
|
|
|
|
}
|
|
|
|
}
|
2006-07-15 13:23:31 +08:00
|
|
|
return Result;
|
2006-07-03 09:13:26 +08:00
|
|
|
}
|
|
|
|
|
2007-07-24 14:57:14 +08:00
|
|
|
/// Stringify - Convert the specified string into a C string by escaping '\'
|
|
|
|
/// and " characters. This does not add surrounding ""'s to the string.
|
|
|
|
void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
|
|
|
|
for (unsigned i = 0, e = Str.size(); i != e; ++i) {
|
|
|
|
if (Str[i] == '\\' || Str[i] == '"') {
|
|
|
|
Str.insert(Str.begin()+i, '\\');
|
|
|
|
++i; ++e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-23 03:49:59 +08:00
|
|
|
static bool isWhitespace(unsigned char c);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2007-10-18 05:18:47 +08:00
|
|
|
/// MeasureTokenLength - Relex the token at the specified location and return
|
|
|
|
/// its length in bytes in the input file. If the token needs cleaning (e.g.
|
|
|
|
/// includes a trigraph or an escaped newline) then this count includes bytes
|
|
|
|
/// that are part of that.
|
|
|
|
unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
|
2009-04-15 07:22:57 +08:00
|
|
|
const SourceManager &SM,
|
|
|
|
const LangOptions &LangOpts) {
|
2007-10-18 05:18:47 +08:00
|
|
|
// TODO: this could be special cased for common tokens like identifiers, ')',
|
|
|
|
// etc to make this faster, if it mattered. Just look at StrData[0] to handle
|
2009-09-09 23:08:12 +08:00
|
|
|
// all obviously single-char tokens. This could use
|
2007-10-18 05:18:47 +08:00
|
|
|
// Lexer::isObviouslySimpleCharacter for example to handle identifiers or
|
|
|
|
// something.
|
2009-01-26 08:43:02 +08:00
|
|
|
|
|
|
|
// If this comes from a macro expansion, we really do want the macro name, not
|
|
|
|
// the token this macro expanded to.
|
2009-01-27 06:24:27 +08:00
|
|
|
Loc = SM.getInstantiationLoc(Loc);
|
|
|
|
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
|
2009-01-17 16:30:10 +08:00
|
|
|
std::pair<const char *,const char *> Buffer = SM.getBufferData(LocInfo.first);
|
|
|
|
const char *StrData = Buffer.first+LocInfo.second;
|
|
|
|
|
2010-01-23 03:49:59 +08:00
|
|
|
if (isWhitespace(StrData[0]))
|
|
|
|
return 0;
|
|
|
|
|
2007-10-18 05:18:47 +08:00
|
|
|
// Create a lexer starting at the beginning of this token.
|
2009-01-17 15:42:27 +08:00
|
|
|
Lexer TheLexer(Loc, LangOpts, Buffer.first, StrData, Buffer.second);
|
2009-10-14 23:04:18 +08:00
|
|
|
TheLexer.SetCommentRetentionState(true);
|
2007-10-18 05:18:47 +08:00
|
|
|
Token TheTok;
|
2008-10-12 09:15:46 +08:00
|
|
|
TheLexer.LexFromRawLexer(TheTok);
|
2007-10-18 05:18:47 +08:00
|
|
|
return TheTok.getLength();
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Character information.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
enum {
|
|
|
|
CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
|
|
|
|
CHAR_VERT_WS = 0x02, // '\r', '\n'
|
|
|
|
CHAR_LETTER = 0x04, // a-z,A-Z
|
|
|
|
CHAR_NUMBER = 0x08, // 0-9
|
|
|
|
CHAR_UNDER = 0x10, // _
|
|
|
|
CHAR_PERIOD = 0x20 // .
|
|
|
|
};
|
|
|
|
|
2009-07-08 01:09:54 +08:00
|
|
|
// Statically initialize CharInfo table based on ASCII character set
|
|
|
|
// Reference: FreeBSD 7.2 /usr/share/misc/ascii
|
2009-12-17 13:29:40 +08:00
|
|
|
static const unsigned char CharInfo[256] =
|
2009-07-08 01:09:54 +08:00
|
|
|
{
|
|
|
|
// 0 NUL 1 SOH 2 STX 3 ETX
|
|
|
|
// 4 EOT 5 ENQ 6 ACK 7 BEL
|
|
|
|
0 , 0 , 0 , 0 ,
|
|
|
|
0 , 0 , 0 , 0 ,
|
|
|
|
// 8 BS 9 HT 10 NL 11 VT
|
|
|
|
//12 NP 13 CR 14 SO 15 SI
|
|
|
|
0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS,
|
|
|
|
CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 ,
|
|
|
|
//16 DLE 17 DC1 18 DC2 19 DC3
|
|
|
|
//20 DC4 21 NAK 22 SYN 23 ETB
|
|
|
|
0 , 0 , 0 , 0 ,
|
|
|
|
0 , 0 , 0 , 0 ,
|
|
|
|
//24 CAN 25 EM 26 SUB 27 ESC
|
|
|
|
//28 FS 29 GS 30 RS 31 US
|
|
|
|
0 , 0 , 0 , 0 ,
|
|
|
|
0 , 0 , 0 , 0 ,
|
|
|
|
//32 SP 33 ! 34 " 35 #
|
|
|
|
//36 $ 37 % 38 & 39 '
|
|
|
|
CHAR_HORZ_WS, 0 , 0 , 0 ,
|
|
|
|
0 , 0 , 0 , 0 ,
|
|
|
|
//40 ( 41 ) 42 * 43 +
|
|
|
|
//44 , 45 - 46 . 47 /
|
|
|
|
0 , 0 , 0 , 0 ,
|
|
|
|
0 , 0 , CHAR_PERIOD , 0 ,
|
|
|
|
//48 0 49 1 50 2 51 3
|
|
|
|
//52 4 53 5 54 6 55 7
|
|
|
|
CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
|
|
|
|
CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
|
|
|
|
//56 8 57 9 58 : 59 ;
|
|
|
|
//60 < 61 = 62 > 63 ?
|
|
|
|
CHAR_NUMBER , CHAR_NUMBER , 0 , 0 ,
|
|
|
|
0 , 0 , 0 , 0 ,
|
|
|
|
//64 @ 65 A 66 B 67 C
|
|
|
|
//68 D 69 E 70 F 71 G
|
|
|
|
0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
|
|
|
|
CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
|
|
|
|
//72 H 73 I 74 J 75 K
|
|
|
|
//76 L 77 M 78 N 79 O
|
|
|
|
CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
|
|
|
|
CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
|
|
|
|
//80 P 81 Q 82 R 83 S
|
|
|
|
//84 T 85 U 86 V 87 W
|
|
|
|
CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
|
|
|
|
CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
|
|
|
|
//88 X 89 Y 90 Z 91 [
|
|
|
|
//92 \ 93 ] 94 ^ 95 _
|
|
|
|
CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
|
|
|
|
0 , 0 , 0 , CHAR_UNDER ,
|
|
|
|
//96 ` 97 a 98 b 99 c
|
|
|
|
//100 d 101 e 102 f 103 g
|
|
|
|
0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
|
|
|
|
CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
|
|
|
|
//104 h 105 i 106 j 107 k
|
|
|
|
//108 l 109 m 110 n 111 o
|
|
|
|
CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
|
|
|
|
CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
|
|
|
|
//112 p 113 q 114 r 115 s
|
|
|
|
//116 t 117 u 118 v 119 w
|
|
|
|
CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
|
|
|
|
CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
|
|
|
|
//120 x 121 y 122 z 123 {
|
|
|
|
//124 | 125 } 126 ~ 127 DEL
|
|
|
|
CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
|
|
|
|
0 , 0 , 0 , 0
|
|
|
|
};
|
|
|
|
|
2009-12-17 13:29:40 +08:00
|
|
|
static void InitCharacterInfo() {
|
2006-06-18 13:43:12 +08:00
|
|
|
static bool isInited = false;
|
|
|
|
if (isInited) return;
|
2009-07-08 01:09:54 +08:00
|
|
|
// check the statically-initialized CharInfo table
|
|
|
|
assert(CHAR_HORZ_WS == CharInfo[(int)' ']);
|
|
|
|
assert(CHAR_HORZ_WS == CharInfo[(int)'\t']);
|
|
|
|
assert(CHAR_HORZ_WS == CharInfo[(int)'\f']);
|
|
|
|
assert(CHAR_HORZ_WS == CharInfo[(int)'\v']);
|
|
|
|
assert(CHAR_VERT_WS == CharInfo[(int)'\n']);
|
|
|
|
assert(CHAR_VERT_WS == CharInfo[(int)'\r']);
|
|
|
|
assert(CHAR_UNDER == CharInfo[(int)'_']);
|
|
|
|
assert(CHAR_PERIOD == CharInfo[(int)'.']);
|
|
|
|
for (unsigned i = 'a'; i <= 'z'; ++i) {
|
|
|
|
assert(CHAR_LETTER == CharInfo[i]);
|
|
|
|
assert(CHAR_LETTER == CharInfo[i+'A'-'a']);
|
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
for (unsigned i = '0'; i <= '9'; ++i)
|
2009-07-08 01:09:54 +08:00
|
|
|
assert(CHAR_NUMBER == CharInfo[i]);
|
2009-12-09 00:38:12 +08:00
|
|
|
|
2009-07-08 01:09:54 +08:00
|
|
|
isInited = true;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2009-07-08 01:09:54 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// isIdentifierBody - Return true if this is the body character of an
|
|
|
|
/// identifier, which is [a-zA-Z0-9_].
|
|
|
|
static inline bool isIdentifierBody(unsigned char c) {
|
2007-10-18 20:47:01 +08:00
|
|
|
return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isHorizontalWhitespace - Return true if this character is horizontal
|
|
|
|
/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
|
|
|
|
static inline bool isHorizontalWhitespace(unsigned char c) {
|
2007-10-18 20:47:01 +08:00
|
|
|
return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isWhitespace - Return true if this character is horizontal or vertical
|
|
|
|
/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
|
|
|
|
/// for '\0'.
|
|
|
|
static inline bool isWhitespace(unsigned char c) {
|
2007-10-18 20:47:01 +08:00
|
|
|
return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isNumberBody - Return true if this is the body character of an
|
|
|
|
/// preprocessing number, which is [a-zA-Z0-9_.].
|
|
|
|
static inline bool isNumberBody(unsigned char c) {
|
2009-09-09 23:08:12 +08:00
|
|
|
return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
|
2007-10-18 20:47:01 +08:00
|
|
|
true : false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Diagnostics forwarding code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-07-23 02:38:25 +08:00
|
|
|
/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
|
|
|
|
/// lexer buffer was all instantiated at a single point, perform the mapping.
|
|
|
|
/// This is currently only used for _Pragma implementation, so it is the slow
|
|
|
|
/// path of the hot getSourceLocation method. Do not allow it to be inlined.
|
2009-11-15 00:36:57 +08:00
|
|
|
static DISABLE_INLINE SourceLocation GetMappedTokenLoc(Preprocessor &PP,
|
|
|
|
SourceLocation FileLoc,
|
|
|
|
unsigned CharNo,
|
|
|
|
unsigned TokLen);
|
2007-07-23 02:38:25 +08:00
|
|
|
static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
|
|
|
|
SourceLocation FileLoc,
|
2009-01-26 08:43:02 +08:00
|
|
|
unsigned CharNo, unsigned TokLen) {
|
2009-02-16 04:52:18 +08:00
|
|
|
assert(FileLoc.isMacroID() && "Must be an instantiation");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-07-23 02:38:25 +08:00
|
|
|
// Otherwise, we're lexing "mapped tokens". This is used for things like
|
|
|
|
// _Pragma handling. Combine the instantiation location of FileLoc with the
|
2009-01-16 15:00:02 +08:00
|
|
|
// spelling location.
|
2009-02-16 04:52:18 +08:00
|
|
|
SourceManager &SM = PP.getSourceManager();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-16 15:36:28 +08:00
|
|
|
// Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
|
2009-01-16 15:00:02 +08:00
|
|
|
// characters come from spelling(FileLoc)+Offset.
|
2009-02-16 04:52:18 +08:00
|
|
|
SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
|
2009-01-19 14:46:35 +08:00
|
|
|
SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-16 04:52:18 +08:00
|
|
|
// Figure out the expansion loc range, which is the range covered by the
|
|
|
|
// original _Pragma(...) sequence.
|
|
|
|
std::pair<SourceLocation,SourceLocation> II =
|
|
|
|
SM.getImmediateInstantiationRange(FileLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-16 04:52:18 +08:00
|
|
|
return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
|
2007-07-23 02:38:25 +08:00
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// getSourceLocation - Return a source location identifier for the specified
|
|
|
|
/// offset in the current file.
|
2009-01-26 08:43:02 +08:00
|
|
|
SourceLocation Lexer::getSourceLocation(const char *Loc,
|
|
|
|
unsigned TokLen) const {
|
2007-07-23 02:44:36 +08:00
|
|
|
assert(Loc >= BufferStart && Loc <= BufferEnd &&
|
2006-07-03 04:05:54 +08:00
|
|
|
"Location out of range for this buffer!");
|
2007-07-21 00:37:10 +08:00
|
|
|
|
|
|
|
// In the normal case, we're just lexing from a simple file buffer, return
|
|
|
|
// the file id from FileLoc with the offset specified.
|
2007-07-23 02:44:36 +08:00
|
|
|
unsigned CharNo = Loc-BufferStart;
|
2007-07-21 00:37:10 +08:00
|
|
|
if (FileLoc.isFileID())
|
2009-01-19 14:46:35 +08:00
|
|
|
return FileLoc.getFileLocWithOffset(CharNo);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
// Otherwise, this is the _Pragma lexer case, which pretends that all of the
|
|
|
|
// tokens are lexed from where the _Pragma was defined.
|
2007-10-18 04:41:00 +08:00
|
|
|
assert(PP && "This doesn't work on raw lexers");
|
2009-01-26 08:43:02 +08:00
|
|
|
return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Diag - Forwarding function for diagnostics. This translate a source
|
|
|
|
/// position in the current buffer into a SourceLocation object for rendering.
|
2008-11-22 08:59:29 +08:00
|
|
|
DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
|
2008-11-18 15:59:24 +08:00
|
|
|
return PP->Diag(getSourceLocation(Loc), DiagID);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Trigraph and Escaped Newline Handling Code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
|
|
|
|
/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
|
|
|
|
static char GetTrigraphCharForLetter(char Letter) {
|
|
|
|
switch (Letter) {
|
|
|
|
default: return 0;
|
|
|
|
case '=': return '#';
|
|
|
|
case ')': return ']';
|
|
|
|
case '(': return '[';
|
|
|
|
case '!': return '|';
|
|
|
|
case '\'': return '^';
|
|
|
|
case '>': return '}';
|
|
|
|
case '/': return '\\';
|
|
|
|
case '<': return '{';
|
|
|
|
case '-': return '~';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// DecodeTrigraphChar - If the specified character is a legal trigraph when
|
|
|
|
/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
|
|
|
|
/// return the result character. Finally, emit a warning about trigraph use
|
|
|
|
/// whether trigraphs are enabled or not.
|
|
|
|
static char DecodeTrigraphChar(const char *CP, Lexer *L) {
|
|
|
|
char Res = GetTrigraphCharForLetter(*CP);
|
2008-11-18 15:59:24 +08:00
|
|
|
if (!Res || !L) return Res;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-18 15:59:24 +08:00
|
|
|
if (!L->getFeatures().Trigraphs) {
|
2008-11-22 10:02:22 +08:00
|
|
|
if (!L->isLexingRawMode())
|
|
|
|
L->Diag(CP-2, diag::trigraph_ignored);
|
2008-11-18 15:59:24 +08:00
|
|
|
return 0;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-22 10:02:22 +08:00
|
|
|
if (!L->isLexingRawMode())
|
|
|
|
L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
|
2006-06-18 13:43:12 +08:00
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2009-04-19 06:05:41 +08:00
|
|
|
/// getEscapedNewLineSize - Return the size of the specified escaped newline,
|
|
|
|
/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a
|
2009-09-09 23:08:12 +08:00
|
|
|
/// trigraph equivalent on entry to this function.
|
2009-04-19 06:05:41 +08:00
|
|
|
unsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
|
|
|
|
unsigned Size = 0;
|
|
|
|
while (isWhitespace(Ptr[Size])) {
|
|
|
|
++Size;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-19 06:05:41 +08:00
|
|
|
if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If this is a \r\n or \n\r, skip the other half.
|
|
|
|
if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
|
|
|
|
Ptr[Size-1] != Ptr[Size])
|
|
|
|
++Size;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-19 06:05:41 +08:00
|
|
|
return Size;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
|
2009-04-19 06:05:41 +08:00
|
|
|
// Not an escaped newline, must be a \t or something else.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-19 06:27:02 +08:00
|
|
|
/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
|
|
|
|
/// them), skip over them and return the first non-escaped-newline found,
|
|
|
|
/// otherwise return P.
|
|
|
|
const char *Lexer::SkipEscapedNewLines(const char *P) {
|
|
|
|
while (1) {
|
|
|
|
const char *AfterEscape;
|
|
|
|
if (*P == '\\') {
|
|
|
|
AfterEscape = P+1;
|
|
|
|
} else if (*P == '?') {
|
|
|
|
// If not a trigraph for escape, bail out.
|
|
|
|
if (P[1] != '?' || P[2] != '/')
|
|
|
|
return P;
|
|
|
|
AfterEscape = P+3;
|
|
|
|
} else {
|
|
|
|
return P;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-19 06:27:02 +08:00
|
|
|
unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
|
|
|
|
if (NewLineSize == 0) return P;
|
|
|
|
P = AfterEscape+NewLineSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-19 06:05:41 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
|
|
|
|
/// get its size, and return it. This is tricky in several cases:
|
|
|
|
/// 1. If currently at the start of a trigraph, we warn about the trigraph,
|
|
|
|
/// then either return the trigraph (skipping 3 chars) or the '?',
|
|
|
|
/// depending on whether trigraphs are enabled or not.
|
|
|
|
/// 2. If this is an escaped newline (potentially with whitespace between
|
|
|
|
/// the backslash and newline), implicitly skip the newline and return
|
|
|
|
/// the char after it.
|
2006-07-03 08:55:48 +08:00
|
|
|
/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
|
2006-06-18 13:43:12 +08:00
|
|
|
///
|
|
|
|
/// This handles the slow/uncommon case of the getCharAndSize method. Here we
|
|
|
|
/// know that we can accumulate into Size, and that we have already incremented
|
|
|
|
/// Ptr by Size bytes.
|
|
|
|
///
|
2006-06-19 00:22:51 +08:00
|
|
|
/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
|
|
|
|
/// be updated to match.
|
2006-06-18 13:43:12 +08:00
|
|
|
///
|
|
|
|
char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
|
2007-07-21 00:59:19 +08:00
|
|
|
Token *Tok) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// If we have a slash, look for an escaped newline.
|
|
|
|
if (Ptr[0] == '\\') {
|
|
|
|
++Size;
|
|
|
|
++Ptr;
|
|
|
|
Slash:
|
|
|
|
// Common case, backslash-char where the char is not whitespace.
|
|
|
|
if (!isWhitespace(Ptr[0])) return '\\';
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-23 13:15:06 +08:00
|
|
|
// See if we have optional whitespace characters between the slash and
|
|
|
|
// newline.
|
2009-04-19 06:05:41 +08:00
|
|
|
if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
|
|
|
|
// Remember that this token needs to be cleaned.
|
|
|
|
if (Tok) Tok->setFlag(Token::NeedsCleaning);
|
|
|
|
|
|
|
|
// Warn if there was whitespace between the backslash and newline.
|
2009-06-23 13:15:06 +08:00
|
|
|
if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
|
2009-04-19 06:05:41 +08:00
|
|
|
Diag(Ptr, diag::backslash_newline_space);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-19 06:05:41 +08:00
|
|
|
// Found backslash<whitespace><newline>. Parse the char after it.
|
|
|
|
Size += EscapedNewLineSize;
|
|
|
|
Ptr += EscapedNewLineSize;
|
|
|
|
// Use slow version to accumulate a correct size field.
|
|
|
|
return getCharAndSizeSlow(Ptr, Size, Tok);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Otherwise, this is not an escaped newline, just return the slash.
|
|
|
|
return '\\';
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// If this is a trigraph, process it.
|
|
|
|
if (Ptr[0] == '?' && Ptr[1] == '?') {
|
|
|
|
// If this is actually a legal trigraph (not something like "??x"), emit
|
|
|
|
// a trigraph warning. If so, and if trigraphs are enabled, return it.
|
|
|
|
if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
|
|
|
|
// Remember that this token needs to be cleaned.
|
2007-07-21 00:59:19 +08:00
|
|
|
if (Tok) Tok->setFlag(Token::NeedsCleaning);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
Ptr += 3;
|
|
|
|
Size += 3;
|
|
|
|
if (C == '\\') goto Slash;
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// If this is neither, return a single character.
|
|
|
|
++Size;
|
|
|
|
return *Ptr;
|
|
|
|
}
|
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
|
|
|
|
/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
|
|
|
|
/// and that we have already incremented Ptr by Size bytes.
|
|
|
|
///
|
2006-06-19 00:22:51 +08:00
|
|
|
/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
|
|
|
|
/// be updated to match.
|
|
|
|
char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
|
2006-06-18 13:43:12 +08:00
|
|
|
const LangOptions &Features) {
|
|
|
|
// If we have a slash, look for an escaped newline.
|
|
|
|
if (Ptr[0] == '\\') {
|
|
|
|
++Size;
|
|
|
|
++Ptr;
|
|
|
|
Slash:
|
|
|
|
// Common case, backslash-char where the char is not whitespace.
|
|
|
|
if (!isWhitespace(Ptr[0])) return '\\';
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// See if we have optional whitespace characters followed by a newline.
|
2009-04-19 06:05:41 +08:00
|
|
|
if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
|
|
|
|
// Found backslash<whitespace><newline>. Parse the char after it.
|
|
|
|
Size += EscapedNewLineSize;
|
|
|
|
Ptr += EscapedNewLineSize;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-19 06:05:41 +08:00
|
|
|
// Use slow version to accumulate a correct size field.
|
|
|
|
return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Otherwise, this is not an escaped newline, just return the slash.
|
|
|
|
return '\\';
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// If this is a trigraph, process it.
|
|
|
|
if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
|
|
|
|
// If this is actually a legal trigraph (not something like "??x"), return
|
|
|
|
// it.
|
|
|
|
if (char C = GetTrigraphCharForLetter(Ptr[2])) {
|
|
|
|
Ptr += 3;
|
|
|
|
Size += 3;
|
|
|
|
if (C == '\\') goto Slash;
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// If this is neither, return a single character.
|
|
|
|
++Size;
|
|
|
|
return *Ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helper methods for lexing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-07-21 00:59:19 +08:00
|
|
|
void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
|
|
|
|
unsigned Size;
|
|
|
|
unsigned char C = *CurPtr++;
|
2010-01-11 10:38:50 +08:00
|
|
|
while (isIdentifierBody(C))
|
2006-06-18 13:43:12 +08:00
|
|
|
C = *CurPtr++;
|
2010-01-11 10:38:50 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
--CurPtr; // Back up over the skipped character.
|
|
|
|
|
|
|
|
// Fast path, no $,\,? in identifier found. '\' might be an escaped newline
|
|
|
|
// or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
|
2006-07-03 08:55:48 +08:00
|
|
|
// FIXME: UCNs.
|
2010-01-11 10:38:50 +08:00
|
|
|
//
|
|
|
|
// TODO: Could merge these checks into a CharInfo flag to make the comparison
|
|
|
|
// cheaper
|
2006-06-18 13:43:12 +08:00
|
|
|
if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
|
|
|
|
FinishIdentifier:
|
2006-07-08 16:28:12 +08:00
|
|
|
const char *IdStart = BufferPtr;
|
2008-10-12 12:51:35 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr, tok::identifier);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-20 12:16:23 +08:00
|
|
|
// If we are in raw mode, return this identifier raw. There is no need to
|
|
|
|
// look up identifier information or attempt to macro expand it.
|
|
|
|
if (LexingRawMode) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-08 16:28:12 +08:00
|
|
|
// Fill in Result.IdentifierInfo, looking up the identifier in the
|
|
|
|
// identifier table.
|
2009-01-21 15:45:14 +08:00
|
|
|
IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-24 02:35:48 +08:00
|
|
|
// Change the kind of this identifier to the appropriate token kind, e.g.
|
|
|
|
// turning "for" into a keyword.
|
|
|
|
Result.setKind(II->getTokenID());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-19 00:41:01 +08:00
|
|
|
// Finally, now that we know we have an identifier, pass this off to the
|
|
|
|
// preprocessor, which may macro expand it or something.
|
2009-01-21 15:45:14 +08:00
|
|
|
if (II->isHandleIdentifierCase())
|
2009-01-21 15:43:11 +08:00
|
|
|
PP->HandleIdentifier(Result);
|
|
|
|
return;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Otherwise, $,\,? in identifier found. Enter slower path.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
C = getCharAndSize(CurPtr, Size);
|
|
|
|
while (1) {
|
|
|
|
if (C == '$') {
|
|
|
|
// If we hit a $ and they are not supported in identifiers, we are done.
|
|
|
|
if (!Features.DollarIdents) goto FinishIdentifier;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Otherwise, emit a diagnostic and continue.
|
2008-11-22 10:02:22 +08:00
|
|
|
if (!isLexingRawMode())
|
|
|
|
Diag(CurPtr, diag::ext_dollar_in_identifier);
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, Size, Result);
|
|
|
|
C = getCharAndSize(CurPtr, Size);
|
|
|
|
continue;
|
2006-07-03 08:55:48 +08:00
|
|
|
} else if (!isIdentifierBody(C)) { // FIXME: UCNs.
|
2006-06-18 13:43:12 +08:00
|
|
|
// Found end of identifier.
|
|
|
|
goto FinishIdentifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, this character is good, consume it.
|
|
|
|
CurPtr = ConsumeChar(CurPtr, Size, Result);
|
|
|
|
|
|
|
|
C = getCharAndSize(CurPtr, Size);
|
2006-07-03 08:55:48 +08:00
|
|
|
while (isIdentifierBody(C)) { // FIXME: UCNs.
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, Size, Result);
|
|
|
|
C = getCharAndSize(CurPtr, Size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-14 10:26:39 +08:00
|
|
|
/// LexNumericConstant - Lex the remainder of a integer or floating point
|
2006-06-18 13:43:12 +08:00
|
|
|
/// constant. From[-1] is the first character lexed. Return the end of the
|
|
|
|
/// constant.
|
2007-07-21 00:59:19 +08:00
|
|
|
void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
|
2006-06-18 13:43:12 +08:00
|
|
|
unsigned Size;
|
|
|
|
char C = getCharAndSize(CurPtr, Size);
|
|
|
|
char PrevCh = 0;
|
2006-07-03 08:55:48 +08:00
|
|
|
while (isNumberBody(C)) { // FIXME: UCNs?
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, Size, Result);
|
|
|
|
PrevCh = C;
|
|
|
|
C = getCharAndSize(CurPtr, Size);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// If we fell out, check for a sign, due to 1e+12. If we have one, continue.
|
|
|
|
if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
|
|
|
|
return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
|
|
|
|
|
|
|
|
// If we have a hex FP constant, continue.
|
2010-01-11 07:37:56 +08:00
|
|
|
if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
|
|
|
|
(!PP || !PP->getLangOptions().CPlusPlus0x))
|
2006-06-18 13:43:12 +08:00
|
|
|
return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
// Update the location of token as well as BufferPtr.
|
2009-01-27 03:29:26 +08:00
|
|
|
const char *TokStart = BufferPtr;
|
2008-10-12 12:51:35 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
|
2009-01-27 03:29:26 +08:00
|
|
|
Result.setLiteralData(TokStart);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
|
|
|
|
/// either " or L".
|
2008-10-12 12:05:48 +08:00
|
|
|
void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
|
2006-06-18 13:43:12 +08:00
|
|
|
const char *NulCharacter = 0; // Does this string contain the \0 character?
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
char C = getAndAdvanceChar(CurPtr, Result);
|
|
|
|
while (C != '"') {
|
|
|
|
// Skip escaped characters.
|
|
|
|
if (C == '\\') {
|
|
|
|
// Skip the escaped character.
|
|
|
|
C = getAndAdvanceChar(CurPtr, Result);
|
|
|
|
} else if (C == '\n' || C == '\r' || // Newline.
|
|
|
|
(C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
|
2009-03-19 05:10:12 +08:00
|
|
|
if (!isLexingRawMode() && !Features.AsmPreprocessor)
|
2008-11-22 10:02:22 +08:00
|
|
|
Diag(BufferPtr, diag::err_unterminated_string);
|
2008-10-12 12:51:35 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr-1, tok::unknown);
|
2006-07-20 14:02:19 +08:00
|
|
|
return;
|
2006-06-18 13:43:12 +08:00
|
|
|
} else if (C == 0) {
|
|
|
|
NulCharacter = CurPtr-1;
|
|
|
|
}
|
|
|
|
C = getAndAdvanceChar(CurPtr, Result);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-20 14:02:19 +08:00
|
|
|
// If a nul character existed in the string, warn about it.
|
2008-11-22 10:02:22 +08:00
|
|
|
if (NulCharacter && !isLexingRawMode())
|
|
|
|
Diag(NulCharacter, diag::null_in_string);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
// Update the location of the token as well as the BufferPtr instance var.
|
2009-01-27 03:29:26 +08:00
|
|
|
const char *TokStart = BufferPtr;
|
2008-10-12 12:51:35 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr,
|
|
|
|
Wide ? tok::wide_string_literal : tok::string_literal);
|
2009-01-27 03:29:26 +08:00
|
|
|
Result.setLiteralData(TokStart);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
|
|
|
|
/// after having lexed the '<' character. This is used for #include filenames.
|
2007-07-21 00:59:19 +08:00
|
|
|
void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
|
2006-06-18 13:43:12 +08:00
|
|
|
const char *NulCharacter = 0; // Does this string contain the \0 character?
|
2009-04-18 07:56:52 +08:00
|
|
|
const char *AfterLessPos = CurPtr;
|
2006-06-18 13:43:12 +08:00
|
|
|
char C = getAndAdvanceChar(CurPtr, Result);
|
|
|
|
while (C != '>') {
|
|
|
|
// Skip escaped characters.
|
|
|
|
if (C == '\\') {
|
|
|
|
// Skip the escaped character.
|
|
|
|
C = getAndAdvanceChar(CurPtr, Result);
|
|
|
|
} else if (C == '\n' || C == '\r' || // Newline.
|
|
|
|
(C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
|
2009-04-18 07:56:52 +08:00
|
|
|
// If the filename is unterminated, then it must just be a lone <
|
|
|
|
// character. Return this as such.
|
|
|
|
FormTokenWithChars(Result, AfterLessPos, tok::less);
|
2006-07-20 14:02:19 +08:00
|
|
|
return;
|
2006-06-18 13:43:12 +08:00
|
|
|
} else if (C == 0) {
|
|
|
|
NulCharacter = CurPtr-1;
|
|
|
|
}
|
|
|
|
C = getAndAdvanceChar(CurPtr, Result);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-20 14:02:19 +08:00
|
|
|
// If a nul character existed in the string, warn about it.
|
2008-11-22 10:02:22 +08:00
|
|
|
if (NulCharacter && !isLexingRawMode())
|
|
|
|
Diag(NulCharacter, diag::null_in_string);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
// Update the location of token as well as BufferPtr.
|
2009-01-27 03:29:26 +08:00
|
|
|
const char *TokStart = BufferPtr;
|
2008-10-12 12:51:35 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
|
2009-01-27 03:29:26 +08:00
|
|
|
Result.setLiteralData(TokStart);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// LexCharConstant - Lex the remainder of a character constant, after having
|
|
|
|
/// lexed either ' or L'.
|
2007-07-21 00:59:19 +08:00
|
|
|
void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
|
2006-06-18 13:43:12 +08:00
|
|
|
const char *NulCharacter = 0; // Does this character contain the \0 character?
|
|
|
|
|
|
|
|
// Handle the common case of 'x' and '\y' efficiently.
|
|
|
|
char C = getAndAdvanceChar(CurPtr, Result);
|
|
|
|
if (C == '\'') {
|
2009-03-19 05:10:12 +08:00
|
|
|
if (!isLexingRawMode() && !Features.AsmPreprocessor)
|
2008-11-22 10:02:22 +08:00
|
|
|
Diag(BufferPtr, diag::err_empty_character);
|
2008-10-12 12:51:35 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr, tok::unknown);
|
2006-07-20 14:02:19 +08:00
|
|
|
return;
|
2006-06-18 13:43:12 +08:00
|
|
|
} else if (C == '\\') {
|
|
|
|
// Skip the escaped character.
|
|
|
|
// FIXME: UCN's.
|
|
|
|
C = getAndAdvanceChar(CurPtr, Result);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
|
|
|
|
++CurPtr;
|
|
|
|
} else {
|
|
|
|
// Fall back on generic code for embedded nulls, newlines, wide chars.
|
|
|
|
do {
|
|
|
|
// Skip escaped characters.
|
|
|
|
if (C == '\\') {
|
|
|
|
// Skip the escaped character.
|
|
|
|
C = getAndAdvanceChar(CurPtr, Result);
|
|
|
|
} else if (C == '\n' || C == '\r' || // Newline.
|
|
|
|
(C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
|
2009-03-19 05:10:12 +08:00
|
|
|
if (!isLexingRawMode() && !Features.AsmPreprocessor)
|
2008-11-22 10:02:22 +08:00
|
|
|
Diag(BufferPtr, diag::err_unterminated_char);
|
2008-10-12 12:51:35 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr-1, tok::unknown);
|
2006-07-20 14:02:19 +08:00
|
|
|
return;
|
2006-06-18 13:43:12 +08:00
|
|
|
} else if (C == 0) {
|
|
|
|
NulCharacter = CurPtr-1;
|
|
|
|
}
|
|
|
|
C = getAndAdvanceChar(CurPtr, Result);
|
|
|
|
} while (C != '\'');
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-22 10:02:22 +08:00
|
|
|
if (NulCharacter && !isLexingRawMode())
|
|
|
|
Diag(NulCharacter, diag::null_in_char);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
// Update the location of token as well as BufferPtr.
|
2009-01-27 03:29:26 +08:00
|
|
|
const char *TokStart = BufferPtr;
|
2008-10-12 12:51:35 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr, tok::char_constant);
|
2009-01-27 03:29:26 +08:00
|
|
|
Result.setLiteralData(TokStart);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
|
|
|
|
/// Update BufferPtr to point to the next non-whitespace character and return.
|
2008-10-12 12:05:48 +08:00
|
|
|
///
|
|
|
|
/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
|
|
|
|
///
|
|
|
|
bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// Whitespace - Skip it, then return the token after the whitespace.
|
|
|
|
unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
|
|
|
|
while (1) {
|
|
|
|
// Skip horizontal whitespace very aggressively.
|
|
|
|
while (isHorizontalWhitespace(Char))
|
|
|
|
Char = *++CurPtr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-25 08:20:22 +08:00
|
|
|
// Otherwise if we have something other than whitespace, we're done.
|
2006-06-18 13:43:12 +08:00
|
|
|
if (Char != '\n' && Char != '\r')
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
if (ParsingPreprocessorDirective) {
|
|
|
|
// End of preprocessor directive line, let LexTokenInternal handle this.
|
|
|
|
BufferPtr = CurPtr;
|
2008-10-12 12:05:48 +08:00
|
|
|
return false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// ok, but handle newline.
|
|
|
|
// The returned token is at the start of the line.
|
2007-07-21 00:59:19 +08:00
|
|
|
Result.setFlag(Token::StartOfLine);
|
2006-06-18 13:43:12 +08:00
|
|
|
// No leading whitespace seen so far.
|
2007-07-21 00:59:19 +08:00
|
|
|
Result.clearFlag(Token::LeadingSpace);
|
2006-06-18 13:43:12 +08:00
|
|
|
Char = *++CurPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this isn't immediately after a newline, there is leading space.
|
|
|
|
char PrevChar = CurPtr[-1];
|
|
|
|
if (PrevChar != '\n' && PrevChar != '\r')
|
2007-07-21 00:59:19 +08:00
|
|
|
Result.setFlag(Token::LeadingSpace);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2008-10-12 12:05:48 +08:00
|
|
|
// If the client wants us to return whitespace, return it now.
|
|
|
|
if (isKeepWhitespaceMode()) {
|
2008-10-12 12:51:35 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr, tok::unknown);
|
2008-10-12 12:05:48 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
BufferPtr = CurPtr;
|
2008-10-12 12:05:48 +08:00
|
|
|
return false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SkipBCPLComment - We have just read the // characters from input. Skip until
|
|
|
|
// we find the newline character thats terminate the comment. Then update
|
2010-01-19 06:35:47 +08:00
|
|
|
/// BufferPtr and return.
|
|
|
|
///
|
|
|
|
/// If we're in KeepCommentMode or any CommentHandler has inserted
|
|
|
|
/// some tokens, this will store the first token and return true.
|
2007-07-21 00:59:19 +08:00
|
|
|
bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// If BCPL comments aren't explicitly enabled for this language, emit an
|
|
|
|
// extension warning.
|
2008-11-22 10:02:22 +08:00
|
|
|
if (!Features.BCPLComment && !isLexingRawMode()) {
|
2006-06-19 00:22:51 +08:00
|
|
|
Diag(BufferPtr, diag::ext_bcpl_comment);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Mark them enabled so we only emit one warning for this translation
|
|
|
|
// unit.
|
|
|
|
Features.BCPLComment = true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Scan over the body of the comment. The common case, when scanning, is that
|
|
|
|
// the comment contains normal ascii characters with nothing interesting in
|
|
|
|
// them. As such, optimize for this case with the inner loop.
|
|
|
|
char C;
|
|
|
|
do {
|
|
|
|
C = *CurPtr;
|
2006-07-03 08:55:48 +08:00
|
|
|
// FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
|
|
|
|
// If we find a \n character, scan backwards, checking to see if it's an
|
|
|
|
// escaped newline, like we do for block comments.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Skip over characters in the fast loop.
|
|
|
|
while (C != 0 && // Potentially EOF.
|
|
|
|
C != '\\' && // Potentially escaped newline.
|
|
|
|
C != '?' && // Potentially trigraph.
|
|
|
|
C != '\n' && C != '\r') // Newline or DOS-style newline.
|
|
|
|
C = *++CurPtr;
|
|
|
|
|
|
|
|
// If this is a newline, we're done.
|
|
|
|
if (C == '\n' || C == '\r')
|
|
|
|
break; // Found the newline? Break out!
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
|
2008-12-12 15:34:39 +08:00
|
|
|
// properly decode the character. Read it in raw mode to avoid emitting
|
|
|
|
// diagnostics about things like trigraphs. If we see an escaped newline,
|
|
|
|
// we'll handle it below.
|
2006-06-18 13:43:12 +08:00
|
|
|
const char *OldPtr = CurPtr;
|
2008-12-12 15:34:39 +08:00
|
|
|
bool OldRawMode = isLexingRawMode();
|
|
|
|
LexingRawMode = true;
|
2006-06-18 13:43:12 +08:00
|
|
|
C = getAndAdvanceChar(CurPtr, Result);
|
2008-12-12 15:34:39 +08:00
|
|
|
LexingRawMode = OldRawMode;
|
2009-04-05 08:26:41 +08:00
|
|
|
|
|
|
|
// If the char that we finally got was a \n, then we must have had something
|
|
|
|
// like \<newline><newline>. We don't want to have consumed the second
|
|
|
|
// newline, we want CurPtr, to end up pointing to it down below.
|
|
|
|
if (C == '\n' || C == '\r') {
|
|
|
|
--CurPtr;
|
|
|
|
C = 'x'; // doesn't matter what this is.
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// If we read multiple characters, and one of those characters was a \r or
|
2007-06-09 14:07:22 +08:00
|
|
|
// \n, then we had an escaped newline within the comment. Emit diagnostic
|
|
|
|
// unless the next line is also a // comment.
|
|
|
|
if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
|
2006-06-18 13:43:12 +08:00
|
|
|
for (; OldPtr != CurPtr; ++OldPtr)
|
|
|
|
if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
|
2007-06-09 14:07:22 +08:00
|
|
|
// Okay, we found a // comment that ends in a newline, if the next
|
|
|
|
// line is also a // comment, but has spaces, don't emit a diagnostic.
|
|
|
|
if (isspace(C)) {
|
|
|
|
const char *ForwardPtr = CurPtr;
|
|
|
|
while (isspace(*ForwardPtr)) // Skip whitespace.
|
|
|
|
++ForwardPtr;
|
|
|
|
if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-22 10:02:22 +08:00
|
|
|
if (!isLexingRawMode())
|
|
|
|
Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
|
2006-06-18 14:48:37 +08:00
|
|
|
break;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-29 14:30:25 +08:00
|
|
|
if (CurPtr == BufferEnd+1) { --CurPtr; break; }
|
2006-06-18 13:43:12 +08:00
|
|
|
} while (C != '\n' && C != '\r');
|
|
|
|
|
2010-02-04 05:06:21 +08:00
|
|
|
// Found but did not consume the newline. Notify comment handlers about the
|
|
|
|
// comment unless we're in a #if 0 block.
|
|
|
|
if (PP && !isLexingRawMode() &&
|
|
|
|
PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
|
|
|
|
getSourceLocation(CurPtr)))) {
|
2010-01-19 06:35:47 +08:00
|
|
|
BufferPtr = CurPtr;
|
|
|
|
return true; // A token has to be returned.
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-29 14:30:25 +08:00
|
|
|
// If we are returning comments as tokens, return this comment as a token.
|
2008-10-12 11:22:02 +08:00
|
|
|
if (inKeepCommentMode())
|
2006-07-29 14:30:25 +08:00
|
|
|
return SaveBCPLComment(Result, CurPtr);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// If we are inside a preprocessor directive and we see the end of line,
|
|
|
|
// return immediately, so that the lexer can return this as an EOM token.
|
2006-07-29 14:30:25 +08:00
|
|
|
if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
|
2006-06-18 13:43:12 +08:00
|
|
|
BufferPtr = CurPtr;
|
2008-10-12 12:15:42 +08:00
|
|
|
return false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Otherwise, eat the \n character. We don't care if this is a \n\r or
|
2008-10-12 08:23:07 +08:00
|
|
|
// \r\n sequence. This is an efficiency hack (because we know the \n can't
|
2008-10-12 12:05:48 +08:00
|
|
|
// contribute to another token), it isn't needed for correctness. Note that
|
|
|
|
// this is ok even in KeepWhitespaceMode, because we would have returned the
|
|
|
|
/// comment above in that mode.
|
2006-06-18 13:43:12 +08:00
|
|
|
++CurPtr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// The next returned token is at the start of the line.
|
2007-07-21 00:59:19 +08:00
|
|
|
Result.setFlag(Token::StartOfLine);
|
2006-06-18 13:43:12 +08:00
|
|
|
// No leading whitespace seen so far.
|
2007-07-21 00:59:19 +08:00
|
|
|
Result.clearFlag(Token::LeadingSpace);
|
2006-06-18 13:43:12 +08:00
|
|
|
BufferPtr = CurPtr;
|
2008-10-12 12:15:42 +08:00
|
|
|
return false;
|
2006-07-29 14:30:25 +08:00
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-07-29 14:30:25 +08:00
|
|
|
/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
|
|
|
|
/// an appropriate way and return it.
|
2007-07-21 00:59:19 +08:00
|
|
|
bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
|
2008-10-12 12:51:35 +08:00
|
|
|
// If we're not in a preprocessor directive, just return the // comment
|
|
|
|
// directly.
|
|
|
|
FormTokenWithChars(Result, CurPtr, tok::comment);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-12 12:51:35 +08:00
|
|
|
if (!ParsingPreprocessorDirective)
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-12 12:51:35 +08:00
|
|
|
// If this BCPL-style comment is in a macro definition, transmogrify it into
|
|
|
|
// a C-style block comment.
|
|
|
|
std::string Spelling = PP->getSpelling(Result);
|
|
|
|
assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
|
|
|
|
Spelling[1] = '*'; // Change prefix to "/*".
|
|
|
|
Spelling += "*/"; // add suffix.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-12 12:51:35 +08:00
|
|
|
Result.setKind(tok::comment);
|
2009-01-27 03:29:26 +08:00
|
|
|
PP->CreateString(&Spelling[0], Spelling.size(), Result,
|
|
|
|
Result.getLocation());
|
2008-10-12 12:15:42 +08:00
|
|
|
return true;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2006-06-18 14:48:37 +08:00
|
|
|
/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
|
|
|
|
/// character (either \n or \r) is part of an escaped newline sequence. Issue a
|
2008-12-12 15:14:34 +08:00
|
|
|
/// diagnostic if so. We know that the newline is inside of a block comment.
|
2009-09-09 23:08:12 +08:00
|
|
|
static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
|
2006-06-18 14:53:56 +08:00
|
|
|
Lexer *L) {
|
2006-06-18 13:43:12 +08:00
|
|
|
assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Back up off the newline.
|
|
|
|
--CurPtr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// If this is a two-character newline sequence, skip the other character.
|
|
|
|
if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
|
|
|
|
// \n\n or \r\r -> not escaped newline.
|
|
|
|
if (CurPtr[0] == CurPtr[1])
|
|
|
|
return false;
|
|
|
|
// \n\r or \r\n -> skip the newline.
|
|
|
|
--CurPtr;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// If we have horizontal whitespace, skip over it. We allow whitespace
|
|
|
|
// between the slash and newline.
|
|
|
|
bool HasSpace = false;
|
|
|
|
while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
|
|
|
|
--CurPtr;
|
|
|
|
HasSpace = true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// If we have a slash, we know this is an escaped newline.
|
|
|
|
if (*CurPtr == '\\') {
|
2006-06-18 14:48:37 +08:00
|
|
|
if (CurPtr[-1] != '*') return false;
|
2006-06-18 13:43:12 +08:00
|
|
|
} else {
|
|
|
|
// It isn't a slash, is it the ?? / trigraph?
|
2006-06-18 14:48:37 +08:00
|
|
|
if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
|
|
|
|
CurPtr[-3] != '*')
|
2006-06-18 13:43:12 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 14:48:37 +08:00
|
|
|
// This is the trigraph ending the comment. Emit a stern warning!
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr -= 2;
|
|
|
|
|
|
|
|
// If no trigraphs are enabled, warn that we ignored this trigraph and
|
|
|
|
// ignore this * character.
|
2006-06-18 14:53:56 +08:00
|
|
|
if (!L->getFeatures().Trigraphs) {
|
2008-11-22 10:02:22 +08:00
|
|
|
if (!L->isLexingRawMode())
|
|
|
|
L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
|
2006-06-18 14:48:37 +08:00
|
|
|
return false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2008-11-22 10:02:22 +08:00
|
|
|
if (!L->isLexingRawMode())
|
|
|
|
L->Diag(CurPtr, diag::trigraph_ends_block_comment);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Warn about having an escaped newline between the */ characters.
|
2008-11-22 10:02:22 +08:00
|
|
|
if (!L->isLexingRawMode())
|
|
|
|
L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// If there was space between the backslash and newline, warn about it.
|
2008-11-22 10:02:22 +08:00
|
|
|
if (HasSpace && !L->isLexingRawMode())
|
|
|
|
L->Diag(CurPtr, diag::backslash_newline_space);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 14:48:37 +08:00
|
|
|
return true;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2006-10-27 12:42:31 +08:00
|
|
|
#ifdef __SSE2__
|
|
|
|
#include <emmintrin.h>
|
2006-10-31 04:01:22 +08:00
|
|
|
#elif __ALTIVEC__
|
|
|
|
#include <altivec.h>
|
|
|
|
#undef bool
|
2006-10-27 12:42:31 +08:00
|
|
|
#endif
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// SkipBlockComment - We have just read the /* characters from input. Read
|
|
|
|
/// until we find the */ characters that terminate the comment. Note that we
|
|
|
|
/// don't bother decoding trigraphs or escaped newlines in block comments,
|
|
|
|
/// because they cannot cause the comment to end. The only thing that can
|
|
|
|
/// happen is the comment could end with an escaped newline between the */ end
|
|
|
|
/// of comment.
|
2008-10-12 12:15:42 +08:00
|
|
|
///
|
2010-01-19 06:35:47 +08:00
|
|
|
/// If we're in KeepCommentMode or any CommentHandler has inserted
|
|
|
|
/// some tokens, this will store the first token and return true.
|
2007-07-21 00:59:19 +08:00
|
|
|
bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// Scan one character past where we should, looking for a '/' character. Once
|
|
|
|
// we find it, check to see if it was preceeded by a *. This common
|
|
|
|
// optimization helps people who like to put a lot of * characters in their
|
|
|
|
// comments.
|
2007-07-22 07:43:37 +08:00
|
|
|
|
|
|
|
// The first character we get with newlines and trigraphs skipped to handle
|
|
|
|
// the degenerate /*/ case below correctly if the * has an escaped newline
|
|
|
|
// after it.
|
|
|
|
unsigned CharSize;
|
|
|
|
unsigned char C = getCharAndSize(CurPtr, CharSize);
|
|
|
|
CurPtr += CharSize;
|
2006-06-18 13:43:12 +08:00
|
|
|
if (C == 0 && CurPtr == BufferEnd+1) {
|
2008-11-22 10:02:22 +08:00
|
|
|
if (!isLexingRawMode())
|
2008-10-12 09:31:51 +08:00
|
|
|
Diag(BufferPtr, diag::err_unterminated_block_comment);
|
2008-10-12 12:19:49 +08:00
|
|
|
--CurPtr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-12 12:19:49 +08:00
|
|
|
// KeepWhitespaceMode should return this broken comment as a token. Since
|
|
|
|
// it isn't a well formed comment, just return it as an 'unknown' token.
|
|
|
|
if (isKeepWhitespaceMode()) {
|
2008-10-12 12:51:35 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr, tok::unknown);
|
2008-10-12 12:19:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-12 12:19:49 +08:00
|
|
|
BufferPtr = CurPtr;
|
2008-10-12 12:15:42 +08:00
|
|
|
return false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-07-22 07:43:37 +08:00
|
|
|
// Check to see if the first character after the '/*' is another /. If so,
|
|
|
|
// then this slash does not end the block comment, it is part of it.
|
|
|
|
if (C == '/')
|
|
|
|
C = *CurPtr++;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
while (1) {
|
2006-10-27 12:12:35 +08:00
|
|
|
// Skip over all non-interesting characters until we find end of buffer or a
|
|
|
|
// (probably ending) '/' character.
|
|
|
|
if (CurPtr + 24 < BufferEnd) {
|
|
|
|
// While not aligned to a 16-byte boundary.
|
|
|
|
while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
|
|
|
|
C = *CurPtr++;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-27 12:12:35 +08:00
|
|
|
if (C == '/') goto FoundSlash;
|
2006-10-27 12:42:31 +08:00
|
|
|
|
|
|
|
#ifdef __SSE2__
|
|
|
|
__m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
|
|
|
|
'/', '/', '/', '/', '/', '/', '/', '/');
|
|
|
|
while (CurPtr+16 <= BufferEnd &&
|
|
|
|
_mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
|
|
|
|
CurPtr += 16;
|
2006-10-31 04:01:22 +08:00
|
|
|
#elif __ALTIVEC__
|
|
|
|
__vector unsigned char Slashes = {
|
2009-09-09 23:08:12 +08:00
|
|
|
'/', '/', '/', '/', '/', '/', '/', '/',
|
2006-10-31 04:01:22 +08:00
|
|
|
'/', '/', '/', '/', '/', '/', '/', '/'
|
|
|
|
};
|
|
|
|
while (CurPtr+16 <= BufferEnd &&
|
|
|
|
!vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
|
|
|
|
CurPtr += 16;
|
2009-09-09 23:08:12 +08:00
|
|
|
#else
|
2006-10-27 12:42:31 +08:00
|
|
|
// Scan for '/' quickly. Many block comments are very large.
|
2006-10-27 12:12:35 +08:00
|
|
|
while (CurPtr[0] != '/' &&
|
|
|
|
CurPtr[1] != '/' &&
|
|
|
|
CurPtr[2] != '/' &&
|
|
|
|
CurPtr[3] != '/' &&
|
|
|
|
CurPtr+4 < BufferEnd) {
|
|
|
|
CurPtr += 4;
|
|
|
|
}
|
2006-10-27 12:42:31 +08:00
|
|
|
#endif
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-27 12:42:31 +08:00
|
|
|
// It has to be one of the bytes scanned, increment to it and read one.
|
2006-10-27 12:12:35 +08:00
|
|
|
C = *CurPtr++;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-27 12:42:31 +08:00
|
|
|
// Loop to scan the remainder.
|
2006-06-18 13:43:12 +08:00
|
|
|
while (C != '/' && C != '\0')
|
|
|
|
C = *CurPtr++;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-27 12:12:35 +08:00
|
|
|
FoundSlash:
|
2006-06-18 13:43:12 +08:00
|
|
|
if (C == '/') {
|
|
|
|
if (CurPtr[-2] == '*') // We found the final */. We're done!
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
|
2006-06-18 14:53:56 +08:00
|
|
|
if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// We found the final */, though it had an escaped newline between the
|
|
|
|
// * and /. We're done!
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (CurPtr[0] == '*' && CurPtr[1] != '/') {
|
|
|
|
// If this is a /* inside of the comment, emit a warning. Don't do this
|
|
|
|
// if this is a /*/, which will end the comment. This misses cases with
|
|
|
|
// embedded escaped newlines, but oh well.
|
2008-11-22 10:02:22 +08:00
|
|
|
if (!isLexingRawMode())
|
|
|
|
Diag(CurPtr-1, diag::warn_nested_block_comment);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
} else if (C == 0 && CurPtr == BufferEnd+1) {
|
2008-11-22 10:02:22 +08:00
|
|
|
if (!isLexingRawMode())
|
|
|
|
Diag(BufferPtr, diag::err_unterminated_block_comment);
|
2006-06-18 13:43:12 +08:00
|
|
|
// Note: the user probably forgot a */. We could continue immediately
|
|
|
|
// after the /*, but this would involve lexing a lot of what really is the
|
|
|
|
// comment, which surely would confuse the parser.
|
2008-10-12 12:19:49 +08:00
|
|
|
--CurPtr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-12 12:19:49 +08:00
|
|
|
// KeepWhitespaceMode should return this broken comment as a token. Since
|
|
|
|
// it isn't a well formed comment, just return it as an 'unknown' token.
|
|
|
|
if (isKeepWhitespaceMode()) {
|
2008-10-12 12:51:35 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr, tok::unknown);
|
2008-10-12 12:19:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-12 12:19:49 +08:00
|
|
|
BufferPtr = CurPtr;
|
2008-10-12 12:15:42 +08:00
|
|
|
return false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
C = *CurPtr++;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-04 05:06:21 +08:00
|
|
|
// Notify comment handlers about the comment unless we're in a #if 0 block.
|
|
|
|
if (PP && !isLexingRawMode() &&
|
|
|
|
PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
|
|
|
|
getSourceLocation(CurPtr)))) {
|
2010-01-19 06:35:47 +08:00
|
|
|
BufferPtr = CurPtr;
|
|
|
|
return true; // A token has to be returned.
|
|
|
|
}
|
Add support for retrieving the Doxygen comment associated with a given
declaration in the AST.
The new ASTContext::getCommentForDecl function searches for a comment
that is attached to the given declaration, and returns that comment,
which may be composed of several comment blocks.
Comments are always available in an AST. However, to avoid harming
performance, we don't actually parse the comments. Rather, we keep the
source ranges of all of the comments within a large, sorted vector,
then lazily extract comments via a binary search in that vector only
when needed (which never occurs in a "normal" compile).
Comments are written to a precompiled header/AST file as a blob of
source ranges. That blob is only lazily loaded when one requests a
comment for a declaration (this never occurs in a "normal" compile).
The indexer testbed now supports comment extraction. When the
-point-at location points to a declaration with a Doxygen-style
comment, the indexer testbed prints the associated comment
block(s). See test/Index/comments.c for an example.
Some notes:
- We don't actually attempt to parse the comment blocks themselves,
beyond identifying them as Doxygen comment blocks to associate them
with a declaration.
- We won't find comment blocks that aren't adjacent to the
declaration, because we start our search based on the location of
the declaration.
- We don't go through the necessary hops to find, for example,
whether some redeclaration of a declaration has comments when our
current declaration does not. Similarly, we don't attempt to
associate a \param Foo marker in a function body comment with the
parameter named Foo (although that is certainly possible).
- Verification of my "no performance impact" claims is still "to be
done".
llvm-svn: 74704
2009-07-03 01:08:52 +08:00
|
|
|
|
2006-07-29 14:30:25 +08:00
|
|
|
// If we are returning comments as tokens, return this comment as a token.
|
2008-10-12 11:22:02 +08:00
|
|
|
if (inKeepCommentMode()) {
|
2008-10-12 12:51:35 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr, tok::comment);
|
2008-10-12 12:15:42 +08:00
|
|
|
return true;
|
2006-07-29 14:30:25 +08:00
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// It is common for the tokens immediately after a /**/ comment to be
|
|
|
|
// whitespace. Instead of going through the big switch, handle it
|
2008-10-12 12:05:48 +08:00
|
|
|
// efficiently now. This is safe even in KeepWhitespaceMode because we would
|
|
|
|
// have already returned above with the comment as a token.
|
2006-06-18 13:43:12 +08:00
|
|
|
if (isHorizontalWhitespace(*CurPtr)) {
|
2007-07-21 00:59:19 +08:00
|
|
|
Result.setFlag(Token::LeadingSpace);
|
2006-07-29 14:30:25 +08:00
|
|
|
SkipWhitespace(Result, CurPtr+1);
|
2008-10-12 12:15:42 +08:00
|
|
|
return false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, just return so that the next character will be lexed as a token.
|
|
|
|
BufferPtr = CurPtr;
|
2007-07-21 00:59:19 +08:00
|
|
|
Result.setFlag(Token::LeadingSpace);
|
2008-10-12 12:15:42 +08:00
|
|
|
return false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Primary Lexing Entry Points
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
|
|
|
|
/// uninterpreted string. This switches the lexer out of directive mode.
|
|
|
|
std::string Lexer::ReadToEndOfLine() {
|
|
|
|
assert(ParsingPreprocessorDirective && ParsingFilename == false &&
|
|
|
|
"Must be in a preprocessing directive!");
|
|
|
|
std::string Result;
|
2007-07-21 00:59:19 +08:00
|
|
|
Token Tmp;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// CurPtr - Cache BufferPtr in an automatic variable.
|
|
|
|
const char *CurPtr = BufferPtr;
|
|
|
|
while (1) {
|
|
|
|
char Char = getAndAdvanceChar(CurPtr, Tmp);
|
|
|
|
switch (Char) {
|
|
|
|
default:
|
|
|
|
Result += Char;
|
|
|
|
break;
|
|
|
|
case 0: // Null.
|
|
|
|
// Found end of file?
|
|
|
|
if (CurPtr-1 != BufferEnd) {
|
|
|
|
// Nope, normal character, continue.
|
|
|
|
Result += Char;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FALL THROUGH.
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
// Okay, we found the end of the line. First, back up past the \0, \r, \n.
|
|
|
|
assert(CurPtr[-1] == Char && "Trigraphs for newline?");
|
|
|
|
BufferPtr = CurPtr-1;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Next, lex the character, which should handle the EOM transition.
|
2006-06-18 14:48:37 +08:00
|
|
|
Lex(Tmp);
|
2007-10-10 02:02:16 +08:00
|
|
|
assert(Tmp.is(tok::eom) && "Unexpected token!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Finally, we're done, return the string we found.
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LexEndOfFile - CurPtr points to the end of this file. Handle this
|
|
|
|
/// condition, reporting diagnostics and handling other edge cases as required.
|
2006-07-18 14:36:12 +08:00
|
|
|
/// This returns true if Result contains a token, false if PP.Lex should be
|
|
|
|
/// called again.
|
2007-07-21 00:59:19 +08:00
|
|
|
bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// If we hit the end of the file while parsing a preprocessor directive,
|
|
|
|
// end the preprocessor directive first. The next token returned will
|
|
|
|
// then be the end of file.
|
|
|
|
if (ParsingPreprocessorDirective) {
|
|
|
|
// Done parsing the "line".
|
|
|
|
ParsingPreprocessorDirective = false;
|
2006-06-19 00:22:51 +08:00
|
|
|
// Update the location of token as well as BufferPtr.
|
2008-10-12 12:51:35 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr, tok::eom);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-29 14:30:25 +08:00
|
|
|
// Restore comment saving mode, in case it was disabled for directive.
|
2008-10-12 11:27:19 +08:00
|
|
|
SetCommentRetentionState(PP->getCommentRetentionState());
|
2006-07-18 14:36:12 +08:00
|
|
|
return true; // Have a token.
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
// If we are in raw mode, return this event as an EOF token. Let the caller
|
|
|
|
// that put us in raw mode handle the event.
|
|
|
|
if (isLexingRawMode()) {
|
|
|
|
Result.startToken();
|
|
|
|
BufferPtr = BufferEnd;
|
|
|
|
FormTokenWithChars(Result, BufferEnd, tok::eof);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, check if we are code-completing, then issue diagnostics for
|
|
|
|
// unterminated #if and missing newline.
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2009-12-02 14:49:09 +08:00
|
|
|
if (PP && PP->isCodeCompletionFile(FileLoc)) {
|
|
|
|
// We're at the end of the file, but we've been asked to consider the
|
|
|
|
// end of the file to be a code-completion token. Return the
|
|
|
|
// code-completion token.
|
|
|
|
Result.startToken();
|
|
|
|
FormTokenWithChars(Result, CurPtr, tok::code_completion);
|
2009-09-22 00:56:56 +08:00
|
|
|
|
2009-12-02 14:49:09 +08:00
|
|
|
// Only do the eof -> code_completion translation once.
|
|
|
|
PP->SetCodeCompletionPoint(0, 0, 0);
|
|
|
|
return true;
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
llvm-svn: 82166
2009-09-18 05:32:03 +08:00
|
|
|
}
|
|
|
|
|
2006-07-19 14:31:49 +08:00
|
|
|
// If we are in a #if directive, emit an error.
|
|
|
|
while (!ConditionalStack.empty()) {
|
2008-11-22 14:22:39 +08:00
|
|
|
PP->Diag(ConditionalStack.back().IfLoc,
|
|
|
|
diag::err_pp_unterminated_conditional);
|
2006-07-19 14:31:49 +08:00
|
|
|
ConditionalStack.pop_back();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-12 13:54:25 +08:00
|
|
|
// C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
|
|
|
|
// a pedwarn.
|
|
|
|
if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
|
2009-04-02 10:29:42 +08:00
|
|
|
Diag(BufferEnd, diag::ext_no_newline_eof)
|
|
|
|
<< CodeModificationHint::CreateInsertion(getSourceLocation(BufferEnd),
|
|
|
|
"\n");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
BufferPtr = CurPtr;
|
2006-07-19 14:31:49 +08:00
|
|
|
|
|
|
|
// Finally, let the preprocessor handle this.
|
2007-10-18 04:41:00 +08:00
|
|
|
return PP->HandleEndOfFile(Result);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2006-07-11 13:46:12 +08:00
|
|
|
/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
|
|
|
|
/// the specified lexer will return a tok::l_paren token, 0 if it is something
|
|
|
|
/// else and 2 if there are no more tokens in the buffer controlled by the
|
|
|
|
/// lexer.
|
|
|
|
unsigned Lexer::isNextPPTokenLParen() {
|
|
|
|
assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-11 13:46:12 +08:00
|
|
|
// Switch to 'skipping' mode. This will ensure that we can lex a token
|
|
|
|
// without emitting diagnostics, disables macro expansion, and will cause EOF
|
|
|
|
// to return an EOF token instead of popping the include stack.
|
|
|
|
LexingRawMode = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-11 13:46:12 +08:00
|
|
|
// Save state that can be changed while lexing so that we can restore it.
|
|
|
|
const char *TmpBufferPtr = BufferPtr;
|
2009-04-24 15:15:46 +08:00
|
|
|
bool inPPDirectiveMode = ParsingPreprocessorDirective;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-07-21 00:59:19 +08:00
|
|
|
Token Tok;
|
2006-10-14 13:19:21 +08:00
|
|
|
Tok.startToken();
|
2006-07-11 13:46:12 +08:00
|
|
|
LexTokenInternal(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-11 13:46:12 +08:00
|
|
|
// Restore state that may have changed.
|
|
|
|
BufferPtr = TmpBufferPtr;
|
2009-04-24 15:15:46 +08:00
|
|
|
ParsingPreprocessorDirective = inPPDirectiveMode;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-11 13:46:12 +08:00
|
|
|
// Restore the lexer back to non-skipping mode.
|
|
|
|
LexingRawMode = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-10 02:02:16 +08:00
|
|
|
if (Tok.is(tok::eof))
|
2006-07-11 13:46:12 +08:00
|
|
|
return 2;
|
2007-10-10 02:02:16 +08:00
|
|
|
return Tok.is(tok::l_paren);
|
2006-07-11 13:46:12 +08:00
|
|
|
}
|
|
|
|
|
2009-12-14 14:16:57 +08:00
|
|
|
/// FindConflictEnd - Find the end of a version control conflict marker.
|
|
|
|
static const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd) {
|
|
|
|
llvm::StringRef RestOfBuffer(CurPtr+7, BufferEnd-CurPtr-7);
|
|
|
|
size_t Pos = RestOfBuffer.find(">>>>>>>");
|
|
|
|
while (Pos != llvm::StringRef::npos) {
|
|
|
|
// Must occur at start of line.
|
|
|
|
if (RestOfBuffer[Pos-1] != '\r' &&
|
|
|
|
RestOfBuffer[Pos-1] != '\n') {
|
|
|
|
RestOfBuffer = RestOfBuffer.substr(Pos+7);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return RestOfBuffer.data()+Pos;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// IsStartOfConflictMarker - If the specified pointer is the start of a version
|
|
|
|
/// control conflict marker like '<<<<<<<', recognize it as such, emit an error
|
|
|
|
/// and recover nicely. This returns true if it is a conflict marker and false
|
|
|
|
/// if not.
|
|
|
|
bool Lexer::IsStartOfConflictMarker(const char *CurPtr) {
|
|
|
|
// Only a conflict marker if it starts at the beginning of a line.
|
|
|
|
if (CurPtr != BufferStart &&
|
|
|
|
CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check to see if we have <<<<<<<.
|
|
|
|
if (BufferEnd-CurPtr < 8 ||
|
|
|
|
llvm::StringRef(CurPtr, 7) != "<<<<<<<")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If we have a situation where we don't care about conflict markers, ignore
|
|
|
|
// it.
|
|
|
|
if (IsInConflictMarker || isLexingRawMode())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check to see if there is a >>>>>>> somewhere in the buffer at the start of
|
|
|
|
// a line to terminate this conflict marker.
|
|
|
|
if (FindConflictEnd(CurPtr+7, BufferEnd)) {
|
|
|
|
// We found a match. We are really in a conflict marker.
|
|
|
|
// Diagnose this, and ignore to the end of line.
|
|
|
|
Diag(CurPtr, diag::err_conflict_marker);
|
|
|
|
IsInConflictMarker = true;
|
|
|
|
|
|
|
|
// Skip ahead to the end of line. We know this exists because the
|
|
|
|
// end-of-conflict marker starts with \r or \n.
|
|
|
|
while (*CurPtr != '\r' && *CurPtr != '\n') {
|
|
|
|
assert(CurPtr != BufferEnd && "Didn't find end of line");
|
|
|
|
++CurPtr;
|
|
|
|
}
|
|
|
|
BufferPtr = CurPtr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No end of conflict marker found.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// HandleEndOfConflictMarker - If this is a '=======' or '|||||||' or '>>>>>>>'
|
|
|
|
/// marker, then it is the end of a conflict marker. Handle it by ignoring up
|
|
|
|
/// until the end of the line. This returns true if it is a conflict marker and
|
|
|
|
/// false if not.
|
|
|
|
bool Lexer::HandleEndOfConflictMarker(const char *CurPtr) {
|
|
|
|
// Only a conflict marker if it starts at the beginning of a line.
|
|
|
|
if (CurPtr != BufferStart &&
|
|
|
|
CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If we have a situation where we don't care about conflict markers, ignore
|
|
|
|
// it.
|
|
|
|
if (!IsInConflictMarker || isLexingRawMode())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check to see if we have the marker (7 characters in a row).
|
|
|
|
for (unsigned i = 1; i != 7; ++i)
|
|
|
|
if (CurPtr[i] != CurPtr[0])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If we do have it, search for the end of the conflict marker. This could
|
|
|
|
// fail if it got skipped with a '#if 0' or something. Note that CurPtr might
|
|
|
|
// be the end of conflict marker.
|
|
|
|
if (const char *End = FindConflictEnd(CurPtr, BufferEnd)) {
|
|
|
|
CurPtr = End;
|
|
|
|
|
|
|
|
// Skip ahead to the end of line.
|
|
|
|
while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n')
|
|
|
|
++CurPtr;
|
|
|
|
|
|
|
|
BufferPtr = CurPtr;
|
|
|
|
|
|
|
|
// No longer in the conflict marker.
|
|
|
|
IsInConflictMarker = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
/// LexTokenInternal - This implements a simple C family lexer. It is an
|
|
|
|
/// extremely performance critical piece of code. This assumes that the buffer
|
2009-07-07 13:05:42 +08:00
|
|
|
/// has a null character at the end of the file. This returns a preprocessing
|
|
|
|
/// token, not a normal token, as such, it is an internal interface. It assumes
|
|
|
|
/// that the Flags of result have been cleared before calling this.
|
2007-07-21 00:59:19 +08:00
|
|
|
void Lexer::LexTokenInternal(Token &Result) {
|
2006-06-18 13:43:12 +08:00
|
|
|
LexNextToken:
|
|
|
|
// New token, can't need cleaning yet.
|
2007-07-21 00:59:19 +08:00
|
|
|
Result.clearFlag(Token::NeedsCleaning);
|
2006-10-14 13:19:21 +08:00
|
|
|
Result.setIdentifierInfo(0);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// CurPtr - Cache BufferPtr in an automatic variable.
|
|
|
|
const char *CurPtr = BufferPtr;
|
|
|
|
|
2006-07-10 14:34:27 +08:00
|
|
|
// Small amounts of horizontal whitespace is very common between tokens.
|
|
|
|
if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
|
|
|
|
++CurPtr;
|
|
|
|
while ((*CurPtr == ' ') || (*CurPtr == '\t'))
|
|
|
|
++CurPtr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-12 12:05:48 +08:00
|
|
|
// If we are keeping whitespace and other tokens, just return what we just
|
|
|
|
// skipped. The next lexer invocation will return the token after the
|
|
|
|
// whitespace.
|
|
|
|
if (isKeepWhitespaceMode()) {
|
2008-10-12 12:51:35 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr, tok::unknown);
|
2008-10-12 12:05:48 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-10 14:34:27 +08:00
|
|
|
BufferPtr = CurPtr;
|
2007-07-21 00:59:19 +08:00
|
|
|
Result.setFlag(Token::LeadingSpace);
|
2006-07-10 14:34:27 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Read a character, advancing over it.
|
|
|
|
char Char = getAndAdvanceChar(CurPtr, Result);
|
2008-10-12 12:51:35 +08:00
|
|
|
tok::TokenKind Kind;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
switch (Char) {
|
|
|
|
case 0: // Null.
|
|
|
|
// Found end of file?
|
2006-07-18 14:36:12 +08:00
|
|
|
if (CurPtr-1 == BufferEnd) {
|
|
|
|
// Read the PP instance variable into an automatic variable, because
|
|
|
|
// LexEndOfFile will often delete 'this'.
|
2007-10-18 04:41:00 +08:00
|
|
|
Preprocessor *PPCache = PP;
|
2006-07-18 14:36:12 +08:00
|
|
|
if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
|
|
|
|
return; // Got a token to return.
|
2007-10-18 04:41:00 +08:00
|
|
|
assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
|
|
|
|
return PPCache->Lex(Result);
|
2006-07-18 14:36:12 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-22 10:02:22 +08:00
|
|
|
if (!isLexingRawMode())
|
|
|
|
Diag(CurPtr-1, diag::null_in_file);
|
2007-07-21 00:59:19 +08:00
|
|
|
Result.setFlag(Token::LeadingSpace);
|
2008-10-12 12:05:48 +08:00
|
|
|
if (SkipWhitespace(Result, CurPtr))
|
|
|
|
return; // KeepWhitespaceMode
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
goto LexNextToken; // GCC isn't tail call eliminating.
|
2009-12-17 13:29:40 +08:00
|
|
|
|
|
|
|
case 26: // DOS & CP/M EOF: "^Z".
|
|
|
|
// If we're in Microsoft extensions mode, treat this as end of file.
|
|
|
|
if (Features.Microsoft) {
|
|
|
|
// Read the PP instance variable into an automatic variable, because
|
|
|
|
// LexEndOfFile will often delete 'this'.
|
|
|
|
Preprocessor *PPCache = PP;
|
|
|
|
if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
|
|
|
|
return; // Got a token to return.
|
|
|
|
assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
|
|
|
|
return PPCache->Lex(Result);
|
|
|
|
}
|
|
|
|
// If Microsoft extensions are disabled, this is just random garbage.
|
|
|
|
Kind = tok::unknown;
|
|
|
|
break;
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
// If we are inside a preprocessor directive and we see the end of line,
|
|
|
|
// we know we are done with the directive, so return an EOM token.
|
|
|
|
if (ParsingPreprocessorDirective) {
|
|
|
|
// Done parsing the "line".
|
|
|
|
ParsingPreprocessorDirective = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-29 14:30:25 +08:00
|
|
|
// Restore comment saving mode, in case it was disabled for directive.
|
2008-10-12 11:27:19 +08:00
|
|
|
SetCommentRetentionState(PP->getCommentRetentionState());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Since we consumed a newline, we are back at the start of a line.
|
|
|
|
IsAtStartOfLine = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::eom;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// The returned token is at the start of the line.
|
2007-07-21 00:59:19 +08:00
|
|
|
Result.setFlag(Token::StartOfLine);
|
2006-06-18 13:43:12 +08:00
|
|
|
// No leading whitespace seen so far.
|
2007-07-21 00:59:19 +08:00
|
|
|
Result.clearFlag(Token::LeadingSpace);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-12 12:05:48 +08:00
|
|
|
if (SkipWhitespace(Result, CurPtr))
|
|
|
|
return; // KeepWhitespaceMode
|
2006-06-18 13:43:12 +08:00
|
|
|
goto LexNextToken; // GCC isn't tail call eliminating.
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\f':
|
|
|
|
case '\v':
|
2007-07-22 14:29:05 +08:00
|
|
|
SkipHorizontalWhitespace:
|
2007-07-21 00:59:19 +08:00
|
|
|
Result.setFlag(Token::LeadingSpace);
|
2008-10-12 12:05:48 +08:00
|
|
|
if (SkipWhitespace(Result, CurPtr))
|
|
|
|
return; // KeepWhitespaceMode
|
2007-07-22 14:29:05 +08:00
|
|
|
|
|
|
|
SkipIgnoredUnits:
|
|
|
|
CurPtr = BufferPtr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-07-22 14:29:05 +08:00
|
|
|
// If the next token is obviously a // or /* */ comment, skip it efficiently
|
|
|
|
// too (without going through the big switch stmt).
|
2009-01-17 06:39:25 +08:00
|
|
|
if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
|
|
|
|
Features.BCPLComment) {
|
2010-01-19 06:35:47 +08:00
|
|
|
if (SkipBCPLComment(Result, CurPtr+2))
|
|
|
|
return; // There is a token to return.
|
2007-07-22 14:29:05 +08:00
|
|
|
goto SkipIgnoredUnits;
|
2008-10-12 11:22:02 +08:00
|
|
|
} else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
|
2010-01-19 06:35:47 +08:00
|
|
|
if (SkipBlockComment(Result, CurPtr+2))
|
|
|
|
return; // There is a token to return.
|
2007-07-22 14:29:05 +08:00
|
|
|
goto SkipIgnoredUnits;
|
|
|
|
} else if (isHorizontalWhitespace(*CurPtr)) {
|
|
|
|
goto SkipHorizontalWhitespace;
|
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
goto LexNextToken; // GCC isn't tail call eliminating.
|
2009-12-17 13:29:40 +08:00
|
|
|
|
2008-01-04 01:58:54 +08:00
|
|
|
// C99 6.4.4.1: Integer Constants.
|
|
|
|
// C99 6.4.4.2: Floating Constants.
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
// Notify MIOpt that we read a non-whitespace/non-comment token.
|
|
|
|
MIOpt.ReadToken();
|
|
|
|
return LexNumericConstant(Result, CurPtr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-04 01:58:54 +08:00
|
|
|
case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
|
2006-07-04 15:11:10 +08:00
|
|
|
// Notify MIOpt that we read a non-whitespace/non-comment token.
|
|
|
|
MIOpt.ReadToken();
|
2006-06-18 13:43:12 +08:00
|
|
|
Char = getCharAndSize(CurPtr, SizeTmp);
|
|
|
|
|
|
|
|
// Wide string literal.
|
|
|
|
if (Char == '"')
|
2006-10-06 13:22:26 +08:00
|
|
|
return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
|
|
|
|
true);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Wide character constant.
|
|
|
|
if (Char == '\'')
|
|
|
|
return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
|
|
|
|
// FALL THROUGH, treating L like the start of an identifier.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// C99 6.4.2: Identifiers.
|
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
|
|
|
|
case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
|
|
|
|
case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
|
|
|
|
case 'V': case 'W': case 'X': case 'Y': case 'Z':
|
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
|
|
|
|
case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
|
|
|
|
case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
|
|
|
|
case 'v': case 'w': case 'x': case 'y': case 'z':
|
|
|
|
case '_':
|
2006-07-04 15:11:10 +08:00
|
|
|
// Notify MIOpt that we read a non-whitespace/non-comment token.
|
|
|
|
MIOpt.ReadToken();
|
2006-06-18 13:43:12 +08:00
|
|
|
return LexIdentifier(Result, CurPtr);
|
2008-01-04 01:58:54 +08:00
|
|
|
|
|
|
|
case '$': // $ in identifiers.
|
|
|
|
if (Features.DollarIdents) {
|
2008-11-22 10:02:22 +08:00
|
|
|
if (!isLexingRawMode())
|
|
|
|
Diag(CurPtr-1, diag::ext_dollar_in_identifier);
|
2008-01-04 01:58:54 +08:00
|
|
|
// Notify MIOpt that we read a non-whitespace/non-comment token.
|
|
|
|
MIOpt.ReadToken();
|
|
|
|
return LexIdentifier(Result, CurPtr);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::unknown;
|
2008-01-04 01:58:54 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// C99 6.4.4: Character Constants.
|
|
|
|
case '\'':
|
2006-07-04 15:11:10 +08:00
|
|
|
// Notify MIOpt that we read a non-whitespace/non-comment token.
|
|
|
|
MIOpt.ReadToken();
|
2006-06-18 13:43:12 +08:00
|
|
|
return LexCharConstant(Result, CurPtr);
|
|
|
|
|
|
|
|
// C99 6.4.5: String Literals.
|
|
|
|
case '"':
|
2006-07-04 15:11:10 +08:00
|
|
|
// Notify MIOpt that we read a non-whitespace/non-comment token.
|
|
|
|
MIOpt.ReadToken();
|
2006-10-06 13:22:26 +08:00
|
|
|
return LexStringLiteral(Result, CurPtr, false);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// C99 6.4.6: Punctuators.
|
|
|
|
case '?':
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::question;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case '[':
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::l_square;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case ']':
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::r_square;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case '(':
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::l_paren;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case ')':
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::r_paren;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case '{':
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::l_brace;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case '}':
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::r_brace;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
Char = getCharAndSize(CurPtr, SizeTmp);
|
|
|
|
if (Char >= '0' && Char <= '9') {
|
2006-07-04 15:11:10 +08:00
|
|
|
// Notify MIOpt that we read a non-whitespace/non-comment token.
|
|
|
|
MIOpt.ReadToken();
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
|
|
|
|
} else if (Features.CPlusPlus && Char == '*') {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::periodstar;
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr += SizeTmp;
|
|
|
|
} else if (Char == '.' &&
|
|
|
|
getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::ellipsis;
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
|
|
|
|
SizeTmp2, Result);
|
|
|
|
} else {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::period;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
Char = getCharAndSize(CurPtr, SizeTmp);
|
|
|
|
if (Char == '&') {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::ampamp;
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
|
|
|
} else if (Char == '=') {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::ampequal;
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
|
|
|
} else {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::amp;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
case '*':
|
2006-06-18 13:43:12 +08:00
|
|
|
if (getCharAndSize(CurPtr, SizeTmp) == '=') {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::starequal;
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
|
|
|
} else {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::star;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
Char = getCharAndSize(CurPtr, SizeTmp);
|
|
|
|
if (Char == '+') {
|
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::plusplus;
|
2006-06-18 13:43:12 +08:00
|
|
|
} else if (Char == '=') {
|
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::plusequal;
|
2006-06-18 13:43:12 +08:00
|
|
|
} else {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::plus;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
Char = getCharAndSize(CurPtr, SizeTmp);
|
2008-10-12 12:51:35 +08:00
|
|
|
if (Char == '-') { // --
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::minusminus;
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (Char == '>' && Features.CPlusPlus &&
|
2008-10-12 12:51:35 +08:00
|
|
|
getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
|
|
|
|
SizeTmp2, Result);
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::arrowstar;
|
|
|
|
} else if (Char == '>') { // ->
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::arrow;
|
|
|
|
} else if (Char == '=') { // -=
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::minusequal;
|
2006-06-18 13:43:12 +08:00
|
|
|
} else {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::minus;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '~':
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::tilde;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
if (getCharAndSize(CurPtr, SizeTmp) == '=') {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::exclaimequal;
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
|
|
|
} else {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::exclaim;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
// 6.4.9: Comments
|
|
|
|
Char = getCharAndSize(CurPtr, SizeTmp);
|
|
|
|
if (Char == '/') { // BCPL comment.
|
2009-01-17 06:39:25 +08:00
|
|
|
// Even if BCPL comments are disabled (e.g. in C89 mode), we generally
|
|
|
|
// want to lex this as a comment. There is one problem with this though,
|
|
|
|
// that in one particular corner case, this can change the behavior of the
|
|
|
|
// resultant program. For example, In "foo //**/ bar", C89 would lex
|
|
|
|
// this as "foo / bar" and langauges with BCPL comments would lex it as
|
|
|
|
// "foo". Check to see if the character after the second slash is a '*'.
|
|
|
|
// If so, we will lex that as a "/" instead of the start of a comment.
|
|
|
|
if (Features.BCPLComment ||
|
|
|
|
getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
|
|
|
|
if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
|
2010-01-19 06:35:47 +08:00
|
|
|
return; // There is a token to return.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 06:39:25 +08:00
|
|
|
// It is common for the tokens immediately after a // comment to be
|
|
|
|
// whitespace (indentation for the next line). Instead of going through
|
|
|
|
// the big switch, handle it efficiently now.
|
|
|
|
goto SkipIgnoredUnits;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 06:39:25 +08:00
|
|
|
if (Char == '*') { // /**/ comment.
|
2006-07-29 14:30:25 +08:00
|
|
|
if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
|
2010-01-19 06:35:47 +08:00
|
|
|
return; // There is a token to return.
|
2008-10-12 12:15:42 +08:00
|
|
|
goto LexNextToken; // GCC isn't tail call eliminating.
|
2009-01-17 06:39:25 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 06:39:25 +08:00
|
|
|
if (Char == '=') {
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::slashequal;
|
2006-06-18 13:43:12 +08:00
|
|
|
} else {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::slash;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
Char = getCharAndSize(CurPtr, SizeTmp);
|
|
|
|
if (Char == '=') {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::percentequal;
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
|
|
|
} else if (Features.Digraphs && Char == '>') {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::r_brace; // '%>' -> '}'
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
|
|
|
} else if (Features.Digraphs && Char == ':') {
|
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2006-07-15 13:41:09 +08:00
|
|
|
Char = getCharAndSize(CurPtr, SizeTmp);
|
|
|
|
if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::hashhash; // '%:%:' -> '##'
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
|
|
|
|
SizeTmp2, Result);
|
2006-07-15 13:41:09 +08:00
|
|
|
} else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
|
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2008-11-22 10:02:22 +08:00
|
|
|
if (!isLexingRawMode())
|
|
|
|
Diag(BufferPtr, diag::charize_microsoft_ext);
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::hashat;
|
2009-03-19 04:58:27 +08:00
|
|
|
} else { // '%:' -> '#'
|
2006-06-18 13:43:12 +08:00
|
|
|
// We parsed a # character. If this occurs at the start of the line,
|
|
|
|
// it's actually the start of a preprocessing directive. Callback to
|
|
|
|
// the preprocessor to handle it.
|
|
|
|
// FIXME: -fpreprocessed mode??
|
2009-05-13 14:10:29 +08:00
|
|
|
if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
|
2009-03-19 04:58:27 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr, tok::hash);
|
2007-10-18 04:41:00 +08:00
|
|
|
PP->HandleDirective(Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// As an optimization, if the preprocessor didn't switch lexers, tail
|
|
|
|
// recurse.
|
2007-10-18 04:41:00 +08:00
|
|
|
if (PP->isCurrentLexer(this)) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// Start a new token. If this is a #include or something, the PP may
|
|
|
|
// want us starting at the beginning of the line again. If so, set
|
|
|
|
// the StartOfLine flag.
|
|
|
|
if (IsAtStartOfLine) {
|
2007-07-21 00:59:19 +08:00
|
|
|
Result.setFlag(Token::StartOfLine);
|
2006-06-18 13:43:12 +08:00
|
|
|
IsAtStartOfLine = false;
|
|
|
|
}
|
|
|
|
goto LexNextToken; // GCC isn't tail call eliminating.
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-18 04:41:00 +08:00
|
|
|
return PP->Lex(Result);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-19 04:58:27 +08:00
|
|
|
Kind = tok::hash;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
} else {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::percent;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
Char = getCharAndSize(CurPtr, SizeTmp);
|
|
|
|
if (ParsingFilename) {
|
2009-04-18 07:56:52 +08:00
|
|
|
return LexAngledStringLiteral(Result, CurPtr);
|
2006-06-18 13:43:12 +08:00
|
|
|
} else if (Char == '<') {
|
2009-12-14 14:16:57 +08:00
|
|
|
char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
|
|
|
|
if (After == '=') {
|
|
|
|
Kind = tok::lesslessequal;
|
|
|
|
CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
|
|
|
|
SizeTmp2, Result);
|
|
|
|
} else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) {
|
|
|
|
// If this is actually a '<<<<<<<' version control conflict marker,
|
|
|
|
// recognize it as such and recover nicely.
|
|
|
|
goto LexNextToken;
|
|
|
|
} else {
|
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
|
|
|
Kind = tok::lessless;
|
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
} else if (Char == '=') {
|
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::lessequal;
|
|
|
|
} else if (Features.Digraphs && Char == ':') { // '<:' -> '['
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::l_square;
|
|
|
|
} else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::l_brace;
|
2006-06-18 13:43:12 +08:00
|
|
|
} else {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::less;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
Char = getCharAndSize(CurPtr, SizeTmp);
|
|
|
|
if (Char == '=') {
|
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::greaterequal;
|
2006-06-18 13:43:12 +08:00
|
|
|
} else if (Char == '>') {
|
2009-12-14 14:16:57 +08:00
|
|
|
char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
|
|
|
|
if (After == '=') {
|
|
|
|
CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
|
|
|
|
SizeTmp2, Result);
|
|
|
|
Kind = tok::greatergreaterequal;
|
|
|
|
} else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) {
|
|
|
|
// If this is '>>>>>>>' and we're in a conflict marker, ignore it.
|
|
|
|
goto LexNextToken;
|
|
|
|
} else {
|
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
|
|
|
Kind = tok::greatergreater;
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
} else {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::greater;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
Char = getCharAndSize(CurPtr, SizeTmp);
|
|
|
|
if (Char == '=') {
|
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::caretequal;
|
2006-06-18 13:43:12 +08:00
|
|
|
} else {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::caret;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '|':
|
|
|
|
Char = getCharAndSize(CurPtr, SizeTmp);
|
|
|
|
if (Char == '=') {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::pipeequal;
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
|
|
|
} else if (Char == '|') {
|
2009-12-14 14:16:57 +08:00
|
|
|
// If this is '|||||||' and we're in a conflict marker, ignore it.
|
|
|
|
if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1))
|
|
|
|
goto LexNextToken;
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::pipepipe;
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
|
|
|
} else {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::pipe;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
Char = getCharAndSize(CurPtr, SizeTmp);
|
|
|
|
if (Features.Digraphs && Char == '>') {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::r_square; // ':>' -> ']'
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
|
|
|
} else if (Features.CPlusPlus && Char == ':') {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::coloncolon;
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
} else {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::colon;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ';':
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::semi;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
Char = getCharAndSize(CurPtr, SizeTmp);
|
|
|
|
if (Char == '=') {
|
2009-12-14 14:16:57 +08:00
|
|
|
// If this is '=======' and we're in a conflict marker, ignore it.
|
|
|
|
if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1))
|
|
|
|
goto LexNextToken;
|
|
|
|
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::equalequal;
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
} else {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::equal;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ',':
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::comma;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case '#':
|
|
|
|
Char = getCharAndSize(CurPtr, SizeTmp);
|
|
|
|
if (Char == '#') {
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::hashhash;
|
2006-06-18 13:43:12 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2006-07-15 13:41:09 +08:00
|
|
|
} else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::hashat;
|
2008-11-22 10:02:22 +08:00
|
|
|
if (!isLexingRawMode())
|
|
|
|
Diag(BufferPtr, diag::charize_microsoft_ext);
|
2006-07-15 13:41:09 +08:00
|
|
|
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
|
2006-06-18 13:43:12 +08:00
|
|
|
} else {
|
|
|
|
// We parsed a # character. If this occurs at the start of the line,
|
|
|
|
// it's actually the start of a preprocessing directive. Callback to
|
|
|
|
// the preprocessor to handle it.
|
2006-07-03 08:55:48 +08:00
|
|
|
// FIXME: -fpreprocessed mode??
|
2009-05-13 14:10:29 +08:00
|
|
|
if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
|
2009-03-19 04:58:27 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr, tok::hash);
|
2007-10-18 04:41:00 +08:00
|
|
|
PP->HandleDirective(Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// As an optimization, if the preprocessor didn't switch lexers, tail
|
|
|
|
// recurse.
|
2007-10-18 04:41:00 +08:00
|
|
|
if (PP->isCurrentLexer(this)) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// Start a new token. If this is a #include or something, the PP may
|
|
|
|
// want us starting at the beginning of the line again. If so, set
|
|
|
|
// the StartOfLine flag.
|
|
|
|
if (IsAtStartOfLine) {
|
2007-07-21 00:59:19 +08:00
|
|
|
Result.setFlag(Token::StartOfLine);
|
2006-06-18 13:43:12 +08:00
|
|
|
IsAtStartOfLine = false;
|
|
|
|
}
|
|
|
|
goto LexNextToken; // GCC isn't tail call eliminating.
|
|
|
|
}
|
2007-10-18 04:41:00 +08:00
|
|
|
return PP->Lex(Result);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-19 04:58:27 +08:00
|
|
|
Kind = tok::hash;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-01-04 01:58:54 +08:00
|
|
|
case '@':
|
|
|
|
// Objective C support.
|
|
|
|
if (CurPtr[-1] == '@' && Features.ObjC1)
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::at;
|
2008-01-04 01:58:54 +08:00
|
|
|
else
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::unknown;
|
2008-01-04 01:58:54 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
case '\\':
|
2006-07-03 08:55:48 +08:00
|
|
|
// FIXME: UCN's.
|
2006-06-18 13:43:12 +08:00
|
|
|
// FALL THROUGH.
|
|
|
|
default:
|
2008-10-12 12:51:35 +08:00
|
|
|
Kind = tok::unknown;
|
2006-07-11 13:52:53 +08:00
|
|
|
break;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-04 15:11:10 +08:00
|
|
|
// Notify MIOpt that we read a non-whitespace/non-comment token.
|
|
|
|
MIOpt.ReadToken();
|
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
// Update the location of token as well as BufferPtr.
|
2008-10-12 12:51:35 +08:00
|
|
|
FormTokenWithChars(Result, CurPtr, Kind);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|