]> granicus.if.org Git - clang/commitdiff
Add an API call to retrieve the spelling data of a token from its SourceLocation.
authorJohn McCall <rjmccall@apple.com>
Tue, 8 Mar 2011 04:06:57 +0000 (04:06 +0000)
committerJohn McCall <rjmccall@apple.com>
Tue, 8 Mar 2011 04:06:57 +0000 (04:06 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@127216 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Lex/Preprocessor.h
lib/Lex/Preprocessor.cpp

index ec04838af2983e19f2415906011a1c93b0982374..0b01710abfc6bb8d62dd86469f76c48b1839e6a6 100644 (file)
@@ -644,13 +644,18 @@ public:
     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);
   }
index 6fe414b66414bd704aabb220319b59f598e52840..40227dc5e678b84ead8bcc40bdd05c0a93e7279e 100644 (file)
@@ -278,6 +278,16 @@ void Preprocessor::CodeCompleteNaturalLanguage() {
     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