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

Chris Lattner sabre at nondot.org
Sun Jul 15 23:48:38 PDT 2007


Author: lattner
Date: Mon Jul 16 01:48:38 2007
New Revision: 39905

URL: http://llvm.org/viewvc/llvm-project?rev=39905&view=rev
Log:
Add a new Preprocessor::AdvanceToTokenCharacter method which, given a sloc
specifying the start of a token and a logical (phase 3) character number,
returns a sloc representing the input character corresponding to it.


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

Modified: cfe/trunk/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Lex/Preprocessor.cpp?rev=39905&r1=39904&r2=39905&view=diff

==============================================================================
--- cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/Lex/Preprocessor.cpp Mon Jul 16 01:48:38 2007
@@ -36,6 +36,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/TargetInfo.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include <iostream>
 using namespace clang;
 
@@ -253,6 +254,42 @@
 }
 
 
+/// AdvanceToTokenCharacter - Given a location that specifies the start of a
+/// token, return a new location that specifies a character within the token.
+SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart, 
+                                                     unsigned CharNo) {
+  // If they request the first char of the token, we're trivially done.
+  if (CharNo == 0) return TokStart;
+  
+  // Figure out how many physical characters away the specified logical
+  // character is.  This needs to take into consideration newlines and
+  // trigraphs.
+  const char *TokStartPtr = SourceMgr.getCharacterData(TokStart);
+  const char *TokPtr = TokStartPtr;
+  
+  // The usual case is that tokens don't contain anything interesting.  Skip
+  // over the uninteresting characters.  If a token only consists of simple
+  // chars, this method is extremely fast.
+  while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr))
+    ++TokPtr, --CharNo;
+  
+  // If we have a character that may be a trigraph or escaped newline, create a
+  // lexer to parse it correctly.
+  unsigned FileID = TokStart.getFileID();
+  const llvm::MemoryBuffer *SrcBuf = SourceMgr.getBuffer(FileID);
+  if (CharNo != 0) {
+    // Create a lexer starting at this token position.
+    Lexer TheLexer(SrcBuf, FileID, *this, TokPtr);
+    LexerToken Tok;
+    // Skip over characters the remaining characters.
+    for (; CharNo; --CharNo)
+      TheLexer.getAndAdvanceChar(TokPtr, Tok);
+  }
+  return SourceLocation(FileID, TokPtr-SrcBuf->getBufferStart());
+}
+
+
+
 //===----------------------------------------------------------------------===//
 // Source File Location Methods.
 //===----------------------------------------------------------------------===//

Modified: cfe/trunk/include/clang/Lex/Lexer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Lexer.h?rev=39905&r1=39904&r2=39905&view=diff

==============================================================================
--- cfe/trunk/include/clang/Lex/Lexer.h (original)
+++ cfe/trunk/include/clang/Lex/Lexer.h Mon Jul 16 01:48:38 2007
@@ -200,6 +200,7 @@
 
   //===--------------------------------------------------------------------===//
   // Lexer character reading interfaces.
+public:
   
   // This lexer is built on two interfaces for reading characters, both of which
   // automatically provide phase 1/2 translation.  getAndAdvanceChar is used
@@ -239,6 +240,7 @@
     return C;
   }
   
+private:
   /// ConsumeChar - When a character (identified by PeekCharAndSize) is consumed
   /// and added to a given token, check to see if there are diagnostics that
   /// need to be emitted or flags that need to be set on the token.  If so, do

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=39905&r1=39904&r2=39905&view=diff

==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Mon Jul 16 01:48:38 2007
@@ -279,6 +279,10 @@
   void DumpToken(const LexerToken &Tok, bool DumpFlags = false) const;
   void DumpMacro(const MacroInfo &MI) const;
   
+  /// AdvanceToTokenCharacter - Given a location that specifies the start of a
+  /// token, return a new location that specifies a character within the token.
+  SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,unsigned Char);
+  
   /// IncrementPasteCounter - Increment the counters for the number of token
   /// paste operations performed.  If fast was specified, this is a 'fast paste'
   /// case we handled.





More information about the cfe-commits mailing list