From: Argyrios Kyrtzidis Date: Sat, 5 May 2012 04:20:28 +0000 (+0000) Subject: Make BuiltinType::getName return a StringRef and introduce BuiltinType::getNameAsCString X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=27a00970bf4ababdc115e54383e6252cc3276dfa;p=clang Make BuiltinType::getName return a StringRef and introduce BuiltinType::getNameAsCString 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 --- diff --git a/include/clang/AST/Type.h b/include/clang/AST/Type.h index 50f30c232a..32e55e9ef6 100644 --- a/include/clang/AST/Type.h +++ b/include/clang/AST/Type.h @@ -1757,7 +1757,13 @@ public: } Kind getKind() const { return static_cast(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); } diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp index fa71ecf454..42673e8e7d 100644 --- a/lib/AST/Type.cpp +++ b/lib/AST/Type.cpp @@ -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"; diff --git a/lib/AST/TypePrinter.cpp b/lib/AST/TypePrinter.cpp index 3bf80e7972..5ec548f3f9 100644 --- a/lib/AST/TypePrinter.cpp +++ b/lib/AST/TypePrinter.cpp @@ -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; } } diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp index b3fc0a45ce..0567bf10e4 100644 --- a/lib/CodeGen/CGDebugInfo.cpp +++ b/lib/CodeGen/CGDebugInfo.cpp @@ -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) \ diff --git a/lib/Sema/SemaCodeComplete.cpp b/lib/Sema/SemaCodeComplete.cpp index 6536d9f522..24f536fa06 100644 --- a/lib/Sema/SemaCodeComplete.cpp +++ b/lib/Sema/SemaCodeComplete.cpp @@ -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(T)) - return BT->getName(Policy); + return BT->getNameAsCString(Policy); // Anonymous tag types are constant strings. if (const TagType *TagT = dyn_cast(T))