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 | a99e02d0191ab7a89b66b45cb0ac02630edf8998 (patch) | |
tree | 4b424d55e58124ef545cb26dec7bb42b3d36dd56 /clang/lib/Lex/Lexer.cpp | |
parent | 1b07c344b4bcaacf2bc950a511283be0422c3fe8 (diff) | |
download | bcm5719-llvm-a99e02d0191ab7a89b66b45cb0ac02630edf8998.tar.gz bcm5719-llvm-a99e02d0191ab7a89b66b45cb0ac02630edf8998.zip |
Introduce Lexer::makeFileCharRange() that accepts a token source range
and returns a character range with file locations.
llvm-svn: 148480
Diffstat (limited to 'clang/lib/Lex/Lexer.cpp')
-rw-r--r-- | clang/lib/Lex/Lexer.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp index ddb5eccff5e..1a469bef488 100644 --- a/clang/lib/Lex/Lexer.cpp +++ b/clang/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) { |