[cfe-commits] r127216 - in /cfe/trunk: include/clang/Lex/Preprocessor.h lib/Lex/Preprocessor.cpp

John McCall rjmccall at apple.com
Mon Mar 7 20:06:57 PST 2011


Author: rjmccall
Date: Mon Mar  7 22:06:57 2011
New Revision: 127216

URL: http://llvm.org/viewvc/llvm-project?rev=127216&view=rev
Log:
Add an API call to retrieve the spelling data of a token from its SourceLocation.


Modified:
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/lib/Lex/Preprocessor.cpp

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=127216&r1=127215&r2=127216&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Mon Mar  7 22:06:57 2011
@@ -644,13 +644,18 @@
     return Diags->Report(Tok.getLocation(), DiagID);
   }
 
+  /// getSpelling() - Return the 'spelling' of the token at the given location.
+  ///
+  /// \param invalid If non-null, will be set \c true if an error occurs.
+  llvm::StringRef getSpelling(SourceLocation loc, bool *invalid = 0) const;
+
   /// getSpelling() - Return the 'spelling' of the Tok token.  The spelling of a
   /// token is the characters used to represent the token in the source file
   /// after trigraph expansion and escaped-newline folding.  In particular, this
   /// wants to get the true, uncanonicalized, spelling of things like digraphs
   /// UCNs, etc.
   ///
-  /// \param Invalid If non-NULL, will be set \c true if an error occurs.
+  /// \param Invalid If non-null, will be set \c true if an error occurs.
   std::string getSpelling(const Token &Tok, bool *Invalid = 0) const {
     return Lexer::getSpelling(Tok, SourceMgr, Features, Invalid);
   }

Modified: cfe/trunk/lib/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Preprocessor.cpp?rev=127216&r1=127215&r2=127216&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/lib/Lex/Preprocessor.cpp Mon Mar  7 22:06:57 2011
@@ -278,6 +278,16 @@
     CodeComplete->CodeCompleteNaturalLanguage();
 }
 
+llvm::StringRef Preprocessor::getSpelling(SourceLocation loc,
+                                          bool *invalid) const {
+  bool invalidTemp = false;
+  if (!invalid) invalid = &invalidTemp;
+  const char *begin = SourceMgr.getCharacterData(loc, invalid);
+  if (*invalid) return llvm::StringRef();
+
+  unsigned length = Lexer::MeasureTokenLength(loc, SourceMgr, Features);
+  return llvm::StringRef(begin, length);
+}
 
 /// getSpelling - This method is used to get the spelling of a token into a
 /// SmallVector. Note that the returned StringRef may not point to the





More information about the cfe-commits mailing list