[cfe-commits] [PATCH] StringRef'ize API: clang::Token::getName()

Zach Wheeler zachw.foss at gmail.com
Sat Jun 25 14:55:34 PDT 2011


This patch is intended to change clang::Token::getName()  so that it returns
llvm::StringRef instead of const char*.

This is my first patch (ever), so if I did something I wasn't supposed to,
just scream at me and I'll try to fix it. :-)

This turned out to be an easy place to start; the doxygen reference
indicates that this method isn't referenced at all. Clang built fine with
these changes.

Regards,
ZJ



Index: include/clang/Basic/TokenKinds.h
===================================================================
--- TokenKinds.h (revision 133818)
+++ TokenKinds.h (working copy)
@@ -14,6 +14,10 @@
 #ifndef LLVM_CLANG_TOKENKINDS_H
 #define LLVM_CLANG_TOKENKINDS_H

+namespace llvm {
+  class StringRef;
+}
+
 namespace clang {

 namespace tok {
@@ -53,7 +57,7 @@
 ///
 /// The name of a token will be an internal name (such as "l_square")
 /// and should not be used as part of diagnostic messages.
-const char *getTokenName(enum TokenKind Kind);
+llvm::StringRef getTokenName(enum TokenKind Kind);

 /// \brief Determines the spelling of simple punctuation tokens like
 /// '!' or '%', and returns NULL for literal and annotation tokens.

Index: lib/Basic/TokenKinds.cpp
===================================================================
--- TokenKinds.cpp (revision 133818)
+++ TokenKinds.cpp (working copy)
@@ -12,7 +12,7 @@
 //===----------------------------------------------------------------------===//

 #include "clang/Basic/TokenKinds.h"
-
+#include "llvm/ADT/StringRef.h"
 #include <cassert>
 using namespace clang;

@@ -23,9 +23,9 @@
   0
 };

-const char *tok::getTokenName(enum TokenKind Kind) {
+llvm::StringRef tok::getTokenName(enum TokenKind Kind) {
   assert(Kind < tok::NUM_TOKENS);
-  return TokNames[Kind];
+  return llvm::StringRef(TokNames[Kind]);
 }

 const char *tok::getTokenSimpleSpelling(enum TokenKind Kind) {

Index: include/clang/Lex/Token.h
===================================================================
--- Token.h (revision 133818)
+++ Token.h (working copy)
@@ -18,6 +18,7 @@
 #include "clang/Basic/TokenKinds.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/OperatorKinds.h"
+#include "llvm/ADT/StringRef.h"
 #include <cstdlib>

 namespace clang {
@@ -145,7 +146,7 @@
     setAnnotationEndLoc(R.getEnd());
   }

-  const char *getName() const {
+  llvm::StringRef getName() const {
     return tok::getTokenName( (tok::TokenKind) Kind);
   }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20110625/65dbd8c0/attachment.html>


More information about the cfe-commits mailing list