[cfe-commits] r148480 - in /cfe/trunk: include/clang/Lex/Lexer.h lib/Lex/Lexer.cpp unittests/Lex/LexerTest.cpp
Argyrios Kyrtzidis
akyrtzi at gmail.com
Thu Jan 19 07:59:14 PST 2012
Author: akirtzidis
Date: Thu Jan 19 09:59:14 2012
New Revision: 148480
URL: http://llvm.org/viewvc/llvm-project?rev=148480&view=rev
Log:
Introduce Lexer::makeFileCharRange() that accepts a token source range
and returns a character range with file locations.
Modified:
cfe/trunk/include/clang/Lex/Lexer.h
cfe/trunk/lib/Lex/Lexer.cpp
cfe/trunk/unittests/Lex/LexerTest.cpp
Modified: cfe/trunk/include/clang/Lex/Lexer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Lexer.h?rev=148480&r1=148479&r2=148480&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Lexer.h (original)
+++ cfe/trunk/include/clang/Lex/Lexer.h Thu Jan 19 09:59:14 2012
@@ -331,6 +331,14 @@
const LangOptions &LangOpts,
SourceLocation *MacroEnd = 0);
+ /// \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.
+ static CharSourceRange makeFileCharRange(SourceRange TokenRange,
+ const SourceManager &SM,
+ const LangOptions &LangOpts);
+
/// \brief Retrieve the name of the immediate macro expansion.
///
/// This routine starts from a source location, and finds the name of the macro
Modified: cfe/trunk/lib/Lex/Lexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Lexer.cpp?rev=148480&r1=148479&r2=148480&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Lexer.cpp (original)
+++ cfe/trunk/lib/Lex/Lexer.cpp Thu Jan 19 09:59:14 2012
@@ -792,6 +792,35 @@
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) {
Modified: cfe/trunk/unittests/Lex/LexerTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Lex/LexerTest.cpp?rev=148480&r1=148479&r2=148480&view=diff
==============================================================================
--- cfe/trunk/unittests/Lex/LexerTest.cpp (original)
+++ cfe/trunk/unittests/Lex/LexerTest.cpp Thu Jan 19 09:59:14 2012
@@ -90,14 +90,30 @@
SourceLocation lsqrLoc = toks[0].getLocation();
SourceLocation idLoc = toks[1].getLocation();
SourceLocation rsqrLoc = toks[2].getLocation();
-
+ std::pair<SourceLocation,SourceLocation>
+ macroPair = SourceMgr.getExpansionRange(lsqrLoc);
+ SourceRange macroRange = SourceRange(macroPair.first, macroPair.second);
+
SourceLocation Loc;
EXPECT_TRUE(Lexer::isAtStartOfMacroExpansion(lsqrLoc, SourceMgr, LangOpts, &Loc));
- EXPECT_EQ(SourceMgr.getExpansionLoc(lsqrLoc), Loc);
+ EXPECT_EQ(Loc, macroRange.getBegin());
EXPECT_FALSE(Lexer::isAtStartOfMacroExpansion(idLoc, SourceMgr, LangOpts));
EXPECT_FALSE(Lexer::isAtEndOfMacroExpansion(idLoc, SourceMgr, LangOpts));
EXPECT_TRUE(Lexer::isAtEndOfMacroExpansion(rsqrLoc, SourceMgr, LangOpts, &Loc));
- EXPECT_EQ(SourceMgr.getExpansionRange(rsqrLoc).second, Loc);
+ EXPECT_EQ(Loc, macroRange.getEnd());
+
+ CharSourceRange range = Lexer::makeFileCharRange(SourceRange(lsqrLoc, idLoc),
+ SourceMgr, LangOpts);
+ EXPECT_TRUE(range.isInvalid());
+ range = Lexer::makeFileCharRange(SourceRange(idLoc, rsqrLoc),
+ SourceMgr, LangOpts);
+ EXPECT_TRUE(range.isInvalid());
+ range = Lexer::makeFileCharRange(SourceRange(lsqrLoc, rsqrLoc),
+ SourceMgr, LangOpts);
+ EXPECT_TRUE(!range.isTokenRange());
+ EXPECT_EQ(range.getAsRange(),
+ SourceRange(macroRange.getBegin(),
+ macroRange.getEnd().getLocWithOffset(1)));
}
} // anonymous namespace
More information about the cfe-commits
mailing list