]> granicus.if.org Git - clang/commitdiff
Add an overload of Preprocessor::getSpelling which takes a SmallVector and
authorBenjamin Kramer <benny.kra@googlemail.com>
Sat, 27 Feb 2010 13:44:12 +0000 (13:44 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Sat, 27 Feb 2010 13:44:12 +0000 (13:44 +0000)
returns a StringRef. Use it to simplify some repetitive code.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97322 91177308-0d34-0410-b5e6-96231b3b80d8

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

index dedbbd868a99e3f502c100e3ed4272051f51b99e..f7e83d587fed650bba3bc009340ee5d3c325a152 100644 (file)
@@ -570,6 +570,24 @@ public:
   /// 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 {
index 43deaee8c1db3bbfee04cc55e30e13478789825a..774372c86934e344896408cc163d57d7e31eeb92 100644 (file)
@@ -67,12 +67,7 @@ static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI,
     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);
   }
 }
 
index 4803c5ab85d58e4ffa4ea69e8230569103401997..976c94eda364dc20c1c3947d19ef522a6091c583 100644 (file)
@@ -1024,13 +1024,9 @@ void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
     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
index 3b620d09345a19ffe0b7c5ac5e014f9ec2e12174..6c9212632f197b8a226e4545fad413a8bac22907 100644 (file)
@@ -218,10 +218,9 @@ static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
   }
   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.
index fd67f4c912475249278b5db052c8e95589863f9b..d60cf0804f536563846bc104b33d4f5a465533d2 100644 (file)
@@ -541,13 +541,9 @@ static bool EvaluateHasIncludeCommon(bool &Result, Token &Tok,
     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
index 63b23b6d5c474bc25ab9709379729dd68dd9a5e5..654d4606a9599824c57fef109bc29c378e96893d 100644 (file)
@@ -287,11 +287,8 @@ void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
 
   // 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
index df0e702ab447b7e6e9edbe4835e79196d0628c58..84ce062a6ab9e488d1d3a8ae61ee426db89bf665 100644 (file)
@@ -503,10 +503,8 @@ IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
   } 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;
index dbbfb9518b33ffacdacbd26294f641669461224e..cb0e5a7f876a56c9fcc803bb44dfea3a44a6d486 100644 (file)
@@ -167,9 +167,7 @@ Parser::DeclPtrTy Parser::ParseLinkage(ParsingDeclSpec &DS,
   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 @@ Parser::DeclPtrTy Parser::ParseLinkage(ParsingDeclSpec &DS,
   DeclPtrTy LinkageSpec
     = Actions.ActOnStartLinkageSpecification(CurScope,
                                              /*FIXME: */SourceLocation(),
-                                             Loc, LangBufPtr, StrSize,
+                                             Loc, Lang.data(), Lang.size(),
                                        Tok.is(tok::l_brace)? Tok.getLocation()
                                                            : SourceLocation());
 
index c25d1195d8bff02d20cb8d649de1619885cdae54..6c1377f3cb04d6703059247e3e2a77fdd4c802b7 100644 (file)
@@ -1664,12 +1664,10 @@ Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc,
 
 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();