]> granicus.if.org Git - clang/commitdiff
Introduce Lexer::makeFileCharRange() that accepts a token source range
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 19 Jan 2012 15:59:14 +0000 (15:59 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 19 Jan 2012 15:59:14 +0000 (15:59 +0000)
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

include/clang/Lex/Lexer.h
lib/Lex/Lexer.cpp
unittests/Lex/LexerTest.cpp

index 99c16d292afa783c0081044cc5fb3e20b8a66acf..eb8ad347d62f112312f168e50ebaed7dcbb43efe 100644 (file)
@@ -331,6 +331,14 @@ public:
                                       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
index ddb5eccff5ee9742e6735997bcc8f4ecff286700..1a469bef488e0ad8e6dd7845061aa95e5571c97c 100644 (file)
@@ -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) {
index e63f31a705edd31c0e3c3f715c9000a7bf2f2386..05478eef9f43915c0afdccd7b30fe0fd1e5163a7 100644 (file)
@@ -90,14 +90,30 @@ TEST_F(LexerTest, LexAPI) {
   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