[cfe-commits] r148481 - 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:19 PST 2012


Author: akirtzidis
Date: Thu Jan 19 09:59:19 2012
New Revision: 148481

URL: http://llvm.org/viewvc/llvm-project?rev=148481&view=rev
Log:
Introduce Lexer::getSourceText() that returns a string for the source
that the given source range encompasses.

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=148481&r1=148480&r2=148481&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Lexer.h (original)
+++ cfe/trunk/include/clang/Lex/Lexer.h Thu Jan 19 09:59:19 2012
@@ -339,6 +339,12 @@
                                            const SourceManager &SM,
                                            const LangOptions &LangOpts);
 
+  /// \brief Returns a string for the source that the range encompasses.
+  static StringRef getSourceText(CharSourceRange Range,
+                                 const SourceManager &SM,
+                                 const LangOptions &LangOpts,
+                                 bool *Invalid = 0);
+
   /// \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=148481&r1=148480&r2=148481&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Lexer.cpp (original)
+++ cfe/trunk/lib/Lex/Lexer.cpp Thu Jan 19 09:59:19 2012
@@ -813,6 +813,9 @@
 
   // Break down the source locations.
   std::pair<FileID, unsigned> beginInfo = SM.getDecomposedLoc(Begin);
+  if (beginInfo.first.isInvalid())
+    return CharSourceRange();
+
   unsigned EndOffs;
   if (!SM.isInFileID(End, beginInfo.first, &EndOffs) ||
       beginInfo.second > EndOffs)
@@ -821,6 +824,45 @@
   return CharSourceRange::getCharRange(Begin, End);
 }
 
+StringRef Lexer::getSourceText(CharSourceRange Range,
+                               const SourceManager &SM,
+                               const LangOptions &LangOpts,
+                               bool *Invalid) {
+  if (Range.isTokenRange())
+    Range = makeFileCharRange(Range.getAsRange(), SM, LangOpts);
+
+  if (Range.isInvalid() ||
+      Range.getBegin().isMacroID() || Range.getEnd().isMacroID()) {
+    if (Invalid) *Invalid = true;
+    return StringRef();
+  }
+
+  // Break down the source location.
+  std::pair<FileID, unsigned> beginInfo = SM.getDecomposedLoc(Range.getBegin());
+  if (beginInfo.first.isInvalid()) {
+    if (Invalid) *Invalid = true;
+    return StringRef();
+  }
+
+  unsigned EndOffs;
+  if (!SM.isInFileID(Range.getEnd(), beginInfo.first, &EndOffs) ||
+      beginInfo.second > EndOffs) {
+    if (Invalid) *Invalid = true;
+    return StringRef();
+  }
+
+  // Try to the load the file buffer.
+  bool invalidTemp = false;
+  StringRef file = SM.getBufferData(beginInfo.first, &invalidTemp);
+  if (invalidTemp) {
+    if (Invalid) *Invalid = true;
+    return StringRef();
+  }
+
+  if (Invalid) *Invalid = false;
+  return file.substr(beginInfo.second, EndOffs - beginInfo.second);
+}
+
 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=148481&r1=148480&r2=148481&view=diff
==============================================================================
--- cfe/trunk/unittests/Lex/LexerTest.cpp (original)
+++ cfe/trunk/unittests/Lex/LexerTest.cpp Thu Jan 19 09:59:19 2012
@@ -114,6 +114,11 @@
   EXPECT_EQ(range.getAsRange(),
             SourceRange(macroRange.getBegin(),
                         macroRange.getEnd().getLocWithOffset(1)));
+
+  StringRef text = Lexer::getSourceText(
+                  CharSourceRange::getTokenRange(SourceRange(lsqrLoc, rsqrLoc)),
+                  SourceMgr, LangOpts);
+  EXPECT_EQ(text, "M(foo)");
 }
 
 } // anonymous namespace





More information about the cfe-commits mailing list