diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2012-01-19 15:59:14 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2012-01-19 15:59:14 +0000 |
commit | 11b652d41d0d97380ab321a1dba48ecb044f9de8 (patch) | |
tree | 94aa7ff36d083794630aa801f3166d96c80993bb /lib/Lex/Lexer.cpp | |
parent | 69bda4c027671df7163619f215209529eb236620 (diff) |
Introduce Lexer::makeFileCharRange() that accepts a token source range
and returns a character range with file locations.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@148480 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Lex/Lexer.cpp')
-rw-r--r-- | lib/Lex/Lexer.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/Lex/Lexer.cpp b/lib/Lex/Lexer.cpp index ddb5eccff5..1a469bef48 100644 --- a/lib/Lex/Lexer.cpp +++ b/lib/Lex/Lexer.cpp @@ -792,6 +792,35 @@ bool Lexer::isAtEndOfMacroExpansion(SourceLocation loc, return isAtEndOfMacroExpansion(expansionLoc, SM, LangOpts, MacroEnd); } +/// \brief Accepts a token source range and returns a character range with +/// file locations. +/// Returns a null range if a part of the range resides inside a macro +/// expansion or the range does not reside on the same FileID. +CharSourceRange Lexer::makeFileCharRange(SourceRange TokenRange, + const SourceManager &SM, + const LangOptions &LangOpts) { + SourceLocation Begin = TokenRange.getBegin(); + if (Begin.isInvalid()) + return CharSourceRange(); + + if (Begin.isMacroID()) + if (!isAtStartOfMacroExpansion(Begin, SM, LangOpts, &Begin)) + return CharSourceRange(); + + SourceLocation End = getLocForEndOfToken(TokenRange.getEnd(), 0, SM,LangOpts); + if (End.isInvalid()) + return CharSourceRange(); + + // Break down the source locations. + std::pair<FileID, unsigned> beginInfo = SM.getDecomposedLoc(Begin); + unsigned EndOffs; + if (!SM.isInFileID(End, beginInfo.first, &EndOffs) || + beginInfo.second > EndOffs) + return CharSourceRange(); + + return CharSourceRange::getCharRange(Begin, End); +} + StringRef Lexer::getImmediateMacroName(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts) { |