[cfe-commits] r38672 - in /cfe/cfe/trunk: Basic/SourceManager.cpp Lex/Preprocessor.cpp include/clang/Basic/SourceManager.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:23:41 PDT 2007


Author: sabre
Date: Wed Jul 11 11:23:41 2007
New Revision: 38672

URL: http://llvm.org/viewvc/llvm-project?rev=38672&view=rev
Log:
Add a fast-path in getSpelling for identifiers.

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

Modified: cfe/cfe/trunk/Basic/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Basic/SourceManager.cpp?rev=38672&r1=38671&r2=38672&view=diff

==============================================================================
--- cfe/cfe/trunk/Basic/SourceManager.cpp (original)
+++ cfe/cfe/trunk/Basic/SourceManager.cpp Wed Jul 11 11:23:41 2007
@@ -128,12 +128,14 @@
 
 
 /// getCharacterData - Return a pointer to the start of the specified location
-/// in the appropriate SourceBuffer.  This returns null if it cannot be
-/// computed (e.g. invalid SourceLocation).
+/// in the appropriate SourceBuffer.
 const char *SourceManager::getCharacterData(SourceLocation SL) const {
-  if (unsigned FileID = SL.getFileID())
-    return getFileInfo(FileID)->Buffer->getBufferStart() + getFilePos(SL);
-  return 0;
+  // Note that this is a hot function in the getSpelling() path, which is
+  // heavily used by -E mode.
+  unsigned FileID = SL.getFileID();
+  assert(FileID && "Invalid source location!");
+  
+  return getFileInfo(FileID)->Buffer->getBufferStart() + getFilePos(SL);
 }
 
 /// getIncludeLoc - Return the location of the #include for the specified

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

==============================================================================
--- cfe/cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/cfe/trunk/Lex/Preprocessor.cpp Wed Jul 11 11:23:41 2007
@@ -214,7 +214,6 @@
   
   // If this token contains nothing interesting, return it directly.
   const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
-  assert(TokStart && "Token has invalid location!");
   if (!Tok.needsCleaning())
     return std::string(TokStart, TokStart+Tok.getLength());
   
@@ -247,8 +246,15 @@
                                    const char *&Buffer) const {
   assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
   
+  // If this token is an identifier, just return the string from the identifier
+  // table, which is very quick.
+  if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
+    Buffer = II->getName();
+    return Tok.getLength();
+  }
+  
+  // Otherwise, compute the start of the token in the input lexer buffer.
   const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
-  assert(TokStart && "Token has invalid location!");
 
   // If this token contains nothing interesting, return it directly.
   if (!Tok.needsCleaning()) {

Modified: cfe/cfe/trunk/include/clang/Basic/SourceManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Basic/SourceManager.h?rev=38672&r1=38671&r2=38672&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/SourceManager.h (original)
+++ cfe/cfe/trunk/include/clang/Basic/SourceManager.h Wed Jul 11 11:23:41 2007
@@ -217,8 +217,7 @@
   }
   
   /// getCharacterData - Return a pointer to the start of the specified location
-  /// in the appropriate SourceBuffer.  This returns null if it cannot be
-  /// computed (e.g. invalid SourceLocation).
+  /// in the appropriate SourceBuffer.
   const char *getCharacterData(SourceLocation SL) const;
   
   /// getColumnNumber - Return the column # for the specified include position.





More information about the cfe-commits mailing list