diff options
| author | Chris Lattner <sabre@nondot.org> | 2007-10-17 21:18:47 +0000 |
|---|---|---|
| committer | Chris Lattner <sabre@nondot.org> | 2007-10-17 21:18:47 +0000 |
| commit | 8e129c23c8a303dfabdb46a46a71ef194d8f0f49 (patch) | |
| tree | 163a20f4764276d58203c50cd289ac0b42596cbd /clang/Lex/Lexer.cpp | |
| parent | dbc8e043c2f734754020e275c10c7b917f3183c8 (diff) | |
| download | bcm5719-llvm-8e129c23c8a303dfabdb46a46a71ef194d8f0f49.tar.gz bcm5719-llvm-8e129c23c8a303dfabdb46a46a71ef194d8f0f49.zip | |
Move token length calculation out of the diagnostics machinery into
the lexer, where it can be shared.
llvm-svn: 43090
Diffstat (limited to 'clang/Lex/Lexer.cpp')
| -rw-r--r-- | clang/Lex/Lexer.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/clang/Lex/Lexer.cpp b/clang/Lex/Lexer.cpp index c7f54ca8ff3..19dcfe2f0c6 100644 --- a/clang/Lex/Lexer.cpp +++ b/clang/Lex/Lexer.cpp @@ -163,6 +163,39 @@ void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) { } +/// 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, + const SourceManager &SM) { + // If this comes from a macro expansion, we really do want the macro name, not + // the token this macro expanded to. + Loc = SM.getLogicalLoc(Loc); + + const char *StrData = SM.getCharacterData(Loc); + + // 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 + // all obviously single-char tokens. This could use + // Lexer::isObviouslySimpleCharacter for example to handle identifiers or + // something. + + + const char *BufEnd = SM.getBufferData(Loc.getFileID()).second; + + // Create a langops struct and enable trigraphs. This is sufficient for + // measuring tokens. + LangOptions LangOpts; + LangOpts.Trigraphs = true; + + // Create a lexer starting at the beginning of this token. + Lexer TheLexer(Loc, LangOpts, StrData, BufEnd); + Token TheTok; + TheLexer.LexRawToken(TheTok); + return TheTok.getLength(); +} + //===----------------------------------------------------------------------===// // Character information. //===----------------------------------------------------------------------===// |

