]> granicus.if.org Git - clang/commitdiff
Make BuiltinType::getName return a StringRef and introduce BuiltinType::getNameAsCString
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sat, 5 May 2012 04:20:28 +0000 (04:20 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sat, 5 May 2012 04:20:28 +0000 (04:20 +0000)
to get a const char* if necessary.

This avoids unnecessary conversions when we want to use the result of getName as
a StringRef.

Part of rdar://10796159

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

include/clang/AST/Type.h
lib/AST/Type.cpp
lib/AST/TypePrinter.cpp
lib/CodeGen/CGDebugInfo.cpp
lib/Sema/SemaCodeComplete.cpp

index 50f30c232af8efe6c5906d72b1f5f90d48d2d04b..32e55e9ef6bb8aa9fc747bef7bb1a0e6afd865a4 100644 (file)
@@ -1757,7 +1757,13 @@ public:
   }
 
   Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); }
-  const char *getName(const PrintingPolicy &Policy) const;
+  StringRef getName(const PrintingPolicy &Policy) const;
+  const char *getNameAsCString(const PrintingPolicy &Policy) const {
+    // The StringRef is null-terminated.
+    StringRef str = getName(Policy);
+    assert(!str.empty() && str.data()[str.size()] == '\0');
+    return str.data();
+  }
 
   bool isSugared() const { return false; }
   QualType desugar() const { return QualType(this, 0); }
index fa71ecf454595e9a233eeca581f61490245e45c8..42673e8e7d04fa6529ddca31ea7875aa331c7318 100644 (file)
@@ -1425,7 +1425,7 @@ const char *Type::getTypeClassName() const {
   llvm_unreachable("Invalid type class.");
 }
 
-const char *BuiltinType::getName(const PrintingPolicy &Policy) const {
+StringRef BuiltinType::getName(const PrintingPolicy &Policy) const {
   switch (getKind()) {
   case Void:              return "void";
   case Bool:              return Policy.Bool ? "bool" : "_Bool";
index 3bf80e7972447d171f5f11b38b9758a6bcdd988e..5ec548f3f9a076bd6781c673102f4d32a4e7d2d4 100644 (file)
@@ -210,7 +210,7 @@ void TypePrinter::printBuiltin(const BuiltinType *T, std::string &S) {
   } else {
     // Prefix the basic type, e.g. 'int X'.
     S = ' ' + S;
-    S = T->getName(Policy) + S;
+    S = T->getNameAsCString(Policy) + S;
   }
 }
 
index b3fc0a45cefdb06bf0d9515bc669cb458f48bfa8..0567bf10e43fd765029ca3dcbe9d84c389deb5b0 100644 (file)
@@ -335,7 +335,7 @@ void CGDebugInfo::CreateCompileUnit() {
 /// one if necessary.
 llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
   unsigned Encoding = 0;
-  const char *BTName = NULL;
+  StringRef BTName;
   switch (BT->getKind()) {
 #define BUILTIN_TYPE(Id, SingletonId)
 #define PLACEHOLDER_TYPE(Id, SingletonId) \
index 6536d9f522ba0561bb9b28faccddc75dc850fa04..24f536fa06b2da816faf9f83904e276e797990a1 100644 (file)
@@ -1414,7 +1414,7 @@ static const char *GetCompletionTypeString(QualType T,
   if (!T.getLocalQualifiers()) {
     // Built-in type names are constant strings.
     if (const BuiltinType *BT = dyn_cast<BuiltinType>(T))
-      return BT->getName(Policy);
+      return BT->getNameAsCString(Policy);
     
     // Anonymous tag types are constant strings.
     if (const TagType *TagT = dyn_cast<TagType>(T))