[cfe-commits] r97322 - in /cfe/trunk: include/clang/Lex/Preprocessor.h lib/Frontend/PrintPreprocessedOutput.cpp lib/Lex/PPDirectives.cpp lib/Lex/PPExpressions.cpp lib/Lex/PPMacroExpansion.cpp lib/Lex/Pragma.cpp lib/Lex/Preprocessor.cpp lib/Parse/ParseDeclCXX.cpp lib/Sema/SemaExpr.cpp

Benjamin Kramer benny.kra at googlemail.com
Sat Feb 27 05:44:12 PST 2010


Author: d0k
Date: Sat Feb 27 07:44:12 2010
New Revision: 97322

URL: http://llvm.org/viewvc/llvm-project?rev=97322&view=rev
Log:
Add an overload of Preprocessor::getSpelling which takes a SmallVector and
returns a StringRef. Use it to simplify some repetitive code.

Modified:
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp
    cfe/trunk/lib/Lex/PPDirectives.cpp
    cfe/trunk/lib/Lex/PPExpressions.cpp
    cfe/trunk/lib/Lex/PPMacroExpansion.cpp
    cfe/trunk/lib/Lex/Pragma.cpp
    cfe/trunk/lib/Lex/Preprocessor.cpp
    cfe/trunk/lib/Parse/ParseDeclCXX.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=97322&r1=97321&r2=97322&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Sat Feb 27 07:44:12 2010
@@ -570,6 +570,24 @@
   /// if an internal buffer is returned.
   unsigned getSpelling(const Token &Tok, const char *&Buffer) const;
 
+  /// 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
+  /// supplied buffer if a copy can be avoided.
+  llvm::StringRef getSpelling(const Token &Tok,
+                              llvm::SmallVectorImpl<char> &Buffer) const {
+    // Try the fast path.
+    if (const IdentifierInfo *II = Tok.getIdentifierInfo())
+      return II->getName();
+
+    // Resize the buffer if we need to copy into it.
+    if (!Tok.needsCleaning())
+      Buffer.resize(Tok.getLength());
+
+    const char *Ptr = Buffer.data();
+    unsigned Len = getSpelling(Tok, Ptr);
+    return llvm::StringRef(Ptr, Len);
+  }
+
   /// getSpellingOfSingleCharacterNumericConstant - Tok is a numeric constant
   /// with length 1, return the character.
   char getSpellingOfSingleCharacterNumericConstant(const Token &Tok) const {

Modified: cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp?rev=97322&r1=97321&r2=97322&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp (original)
+++ cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp Sat Feb 27 07:44:12 2010
@@ -67,12 +67,7 @@
     if (I->hasLeadingSpace())
       OS << ' ';
 
-    // Make sure we have enough space in the spelling buffer.
-    if (I->getLength() > SpellingBuffer.size())
-      SpellingBuffer.resize(I->getLength());
-    const char *Buffer = SpellingBuffer.data();
-    unsigned SpellingLen = PP.getSpelling(*I, Buffer);
-    OS.write(Buffer, SpellingLen);
+    OS << PP.getSpelling(*I, SpellingBuffer);
   }
 }
 

Modified: cfe/trunk/lib/Lex/PPDirectives.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPDirectives.cpp?rev=97322&r1=97321&r2=97322&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Sat Feb 27 07:44:12 2010
@@ -1024,13 +1024,9 @@
     return;
 
   case tok::angle_string_literal:
-  case tok::string_literal: {
-    FilenameBuffer.resize(FilenameTok.getLength());
-    const char *FilenameStart = &FilenameBuffer[0];
-    unsigned Len = getSpelling(FilenameTok, FilenameStart);
-    Filename = llvm::StringRef(FilenameStart, Len);
+  case tok::string_literal:
+    Filename = getSpelling(FilenameTok, FilenameBuffer);
     break;
-  }
 
   case tok::less:
     // This could be a <foo/bar.h> file coming from a macro expansion.  In this

Modified: cfe/trunk/lib/Lex/PPExpressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPExpressions.cpp?rev=97322&r1=97321&r2=97322&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPExpressions.cpp (original)
+++ cfe/trunk/lib/Lex/PPExpressions.cpp Sat Feb 27 07:44:12 2010
@@ -218,10 +218,9 @@
   }
   case tok::char_constant: {   // 'x'
     llvm::SmallString<32> CharBuffer;
-    CharBuffer.resize(PeekTok.getLength());
-    const char *ThisTokBegin = &CharBuffer[0];
-    unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
-    CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
+    llvm::StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer);
+
+    CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
                               PeekTok.getLocation(), PP);
     if (Literal.hadError())
       return true;  // A diagnostic was already emitted.

Modified: cfe/trunk/lib/Lex/PPMacroExpansion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPMacroExpansion.cpp?rev=97322&r1=97321&r2=97322&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPMacroExpansion.cpp (original)
+++ cfe/trunk/lib/Lex/PPMacroExpansion.cpp Sat Feb 27 07:44:12 2010
@@ -541,13 +541,9 @@
     return false;
 
   case tok::angle_string_literal:
-  case tok::string_literal: {
-    FilenameBuffer.resize(Tok.getLength());
-    const char *FilenameStart = &FilenameBuffer[0];
-    unsigned Len = PP.getSpelling(Tok, FilenameStart);
-    Filename = llvm::StringRef(FilenameStart, Len);
+  case tok::string_literal:
+    Filename = PP.getSpelling(Tok, FilenameBuffer);
     break;
-  }
 
   case tok::less:
     // This could be a <foo/bar.h> file coming from a macro expansion.  In this

Modified: cfe/trunk/lib/Lex/Pragma.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Pragma.cpp?rev=97322&r1=97321&r2=97322&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Pragma.cpp (original)
+++ cfe/trunk/lib/Lex/Pragma.cpp Sat Feb 27 07:44:12 2010
@@ -287,11 +287,8 @@
 
   // Reserve a buffer to get the spelling.
   llvm::SmallString<128> FilenameBuffer;
-  FilenameBuffer.resize(FilenameTok.getLength());
+  llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer);
 
-  const char *FilenameStart = &FilenameBuffer[0];
-  unsigned Len = getSpelling(FilenameTok, FilenameStart);
-  llvm::StringRef Filename(FilenameStart, Len);
   bool isAngled =
     GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
   // If GetIncludeFilenameSpelling set the start ptr to null, there was an

Modified: cfe/trunk/lib/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Preprocessor.cpp?rev=97322&r1=97321&r2=97322&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/lib/Lex/Preprocessor.cpp Sat Feb 27 07:44:12 2010
@@ -503,10 +503,8 @@
   } else {
     // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
     llvm::SmallVector<char, 64> IdentifierBuffer;
-    IdentifierBuffer.resize(Identifier.getLength());
-    const char *TmpBuf = &IdentifierBuffer[0];
-    unsigned Size = getSpelling(Identifier, TmpBuf);
-    II = getIdentifierInfo(llvm::StringRef(TmpBuf, Size));
+    llvm::StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer);
+    II = getIdentifierInfo(CleanedStr);
   }
   Identifier.setIdentifierInfo(II);
   return II;

Modified: cfe/trunk/lib/Parse/ParseDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDeclCXX.cpp?rev=97322&r1=97321&r2=97322&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseDeclCXX.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDeclCXX.cpp Sat Feb 27 07:44:12 2010
@@ -167,9 +167,7 @@
   assert(Tok.is(tok::string_literal) && "Not a string literal!");
   llvm::SmallVector<char, 8> LangBuffer;
   // LangBuffer is guaranteed to be big enough.
-  LangBuffer.resize(Tok.getLength());
-  const char *LangBufPtr = &LangBuffer[0];
-  unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
+  llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer);
 
   SourceLocation Loc = ConsumeStringToken();
 
@@ -177,7 +175,7 @@
   DeclPtrTy LinkageSpec
     = Actions.ActOnStartLinkageSpecification(CurScope,
                                              /*FIXME: */SourceLocation(),
-                                             Loc, LangBufPtr, StrSize,
+                                             Loc, Lang.data(), Lang.size(),
                                        Tok.is(tok::l_brace)? Tok.getLocation()
                                                            : SourceLocation());
 

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=97322&r1=97321&r2=97322&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sat Feb 27 07:44:12 2010
@@ -1664,12 +1664,10 @@
 
 Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
   llvm::SmallString<16> CharBuffer;
-  CharBuffer.resize(Tok.getLength());
-  const char *ThisTokBegin = &CharBuffer[0];
-  unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
+  llvm::StringRef ThisTok = PP.getSpelling(Tok, CharBuffer);
 
-  CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
-                            Tok.getLocation(), PP);
+  CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(),
+                            PP);
   if (Literal.hadError())
     return ExprError();
 





More information about the cfe-commits mailing list