Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 025c3a6

Browse files
committed
add a simplified lexer ctor that sets up the lexer to raw-lex an
entire file. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@62414 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 22d91ca commit 025c3a6

File tree

6 files changed

+25
-17
lines changed

6 files changed

+25
-17
lines changed

Driver/CacheTokens.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,7 @@ void PTHWriter::GeneratePTH() {
486486
if (!B) continue;
487487

488488
FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
489-
Lexer L(SM.getLocForStartOfFile(FID), LOpts,
490-
B->getBufferStart(), B->getBufferEnd(), B);
489+
Lexer L(FID, SM, LOpts);
491490
PM[FE] = LexTokens(L);
492491
}
493492

Driver/clang.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -1332,15 +1332,11 @@ static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF,
13321332

13331333
case DumpRawTokens: {
13341334
SourceManager &SM = PP.getSourceManager();
1335-
std::pair<const char*,const char*> File =
1336-
SM.getBufferData(SM.getMainFileID());
13371335
// Start lexing the specified input file.
1338-
Lexer RawLex(SM.getLocForStartOfFile(SM.getMainFileID()),
1339-
PP.getLangOptions(), File.first, File.second);
1336+
Lexer RawLex(SM.getMainFileID(), SM, PP.getLangOptions());
13401337
RawLex.SetKeepWhitespaceMode(true);
13411338

13421339
Token RawTok;
1343-
13441340
RawLex.LexFromRawLexer(RawTok);
13451341
while (RawTok.isNot(tok::eof)) {
13461342
PP.DumpToken(RawTok, true);

include/clang/Lex/Lexer.h

+6
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ class Lexer : public PreprocessorLexer {
8787
const char *BufStart, const char *BufEnd,
8888
const llvm::MemoryBuffer *FromFile = 0);
8989

90+
/// Lexer constructor - Create a new raw lexer object. This object is only
91+
/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
92+
/// range will outlive it, so it doesn't take ownership of it.
93+
Lexer(FileID FID, const SourceManager &SM, const LangOptions &Features);
94+
9095
/// getFeatures - Return the language features currently enabled. NOTE: this
9196
/// lexer modifies features as a file is parsed!
9297
const LangOptions &getFeatures() const { return Features; }
@@ -166,6 +171,7 @@ class Lexer : public PreprocessorLexer {
166171
ExtendedTokenMode = Mode ? 1 : 0;
167172
}
168173

174+
const char *getBufferStart() const { return BufferStart; }
169175

170176
/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
171177
/// uninterpreted string. This switches the lexer out of directive mode.

lib/Lex/Lexer.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
127127
const char *BufPtr, const char *BufEnd,
128128
const llvm::MemoryBuffer *FromFile)
129129
: FileLoc(fileloc), Features(features) {
130-
131130

132131
// If a MemoryBuffer was specified, use its start as BufferStart. This affects
133132
// the source location objects produced by this lexer.
@@ -140,6 +139,20 @@ Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
140139
LexingRawMode = true;
141140
}
142141

142+
/// Lexer constructor - Create a new raw lexer object. This object is only
143+
/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
144+
/// range will outlive it, so it doesn't take ownership of it.
145+
Lexer::Lexer(FileID FID, const SourceManager &SM, const LangOptions &features)
146+
: FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
147+
const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
148+
149+
InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
150+
FromFile->getBufferEnd());
151+
152+
// We *are* in raw mode.
153+
LexingRawMode = true;
154+
}
155+
143156

144157
/// Stringify - Convert the specified string into a C string, with surrounding
145158
/// ""'s, and with escaped \ and " characters.

lib/Rewrite/HTMLRewrite.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,8 @@ void html::SyntaxHighlight(Rewriter &R, FileID FID, Preprocessor &PP) {
344344
RewriteBuffer &RB = R.getEditBuffer(FID);
345345

346346
const SourceManager &SourceMgr = PP.getSourceManager();
347-
std::pair<const char*, const char*> File = SourceMgr.getBufferData(FID);
348-
const char *BufferStart = File.first;
349-
350-
Lexer L(SourceMgr.getLocForStartOfFile(FID),
351-
PP.getLangOptions(), File.first, File.second);
347+
Lexer L(FID, SourceMgr, PP.getLangOptions());
348+
const char *BufferStart = L.getBufferStart();
352349

353350
// Inform the preprocessor that we want to retain comments as tokens, so we
354351
// can highlight them.

lib/Rewrite/TokenRewriter.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ TokenRewriter::TokenRewriter(FileID FID, SourceManager &SM,
2222
const LangOptions &LangOpts) {
2323
ScratchBuf.reset(new ScratchBuffer(SM));
2424

25-
std::pair<const char*,const char*> File = SM.getBufferData(FID);
26-
2725
// Create a lexer to lex all the tokens of the main file in raw mode.
28-
Lexer RawLex(SM.getLocForStartOfFile(FID),
29-
LangOpts, File.first, File.second);
26+
Lexer RawLex(FID, SM, LangOpts);
3027

3128
// Return all comments and whitespace as tokens.
3229
RawLex.SetKeepWhitespaceMode(true);

0 commit comments

Comments
 (0)