From: Bruno Ricci Date: Thu, 15 Nov 2018 16:42:14 +0000 (+0000) Subject: [AST][NFC] Various NFCs in StringLiteral X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b9e6abd1ea926a4d0b2395a4e6e1a1f2f4fe1f7b;p=clang [AST][NFC] Various NFCs in StringLiteral Factored out of D54166 ([AST] Store the string data in StringLiteral in a trailing array of chars): * For-range loops in containsNonAscii and containsNonAsciiOrNull. * Comments and style fixes. * int -> unsigned in mapCharByteWidth since TargetInfo::getCharWidth and friends return an unsigned, and StringLiteral manipulates and stores CharByteWidth as an unsigned. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@346967 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 93798d034b..b2dcad3b63 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -1552,14 +1552,15 @@ public: }; /// StringLiteral - This represents a string literal expression, e.g. "foo" -/// or L"bar" (wide strings). The actual string is returned by getBytes() -/// is NOT null-terminated, and the length of the string is determined by -/// calling getByteLength(). The C type for a string is always a -/// ConstantArrayType. In C++, the char type is const qualified, in C it is -/// not. +/// or L"bar" (wide strings). The actual string data can be obtained with +/// getBytes() and is NOT null-terminated. The length of the string data is +/// determined by calling getByteLength(). +/// +/// The C type for a string is always a ConstantArrayType. In C++, the char +/// type is const qualified, in C it is not. /// /// Note that strings in C can be formed by concatenation of multiple string -/// literal pptokens in translation phase #6. This keeps track of the locations +/// literal pptokens in translation phase #6. This keeps track of the locations /// of each of these pieces. /// /// Strings in C can also be truncated and extended by assigning into arrays, @@ -1569,13 +1570,7 @@ public: /// have type "char[2]". class StringLiteral : public Expr { public: - enum StringKind { - Ascii, - Wide, - UTF8, - UTF16, - UTF32 - }; + enum StringKind { Ascii, Wide, UTF8, UTF16, UTF32 }; private: friend class ASTStmtReader; @@ -1596,7 +1591,8 @@ private: Expr(StringLiteralClass, Ty, VK_LValue, OK_Ordinary, false, false, false, false) {} - static int mapCharByteWidth(TargetInfo const &target,StringKind k); + /// Map a target and string kind to the appropriate character width. + static unsigned mapCharByteWidth(TargetInfo const &Target, StringKind SK); public: /// This is the "fully general" constructor that allows representation of @@ -1666,17 +1662,15 @@ public: bool isPascal() const { return IsPascal; } bool containsNonAscii() const { - StringRef Str = getString(); - for (unsigned i = 0, e = Str.size(); i != e; ++i) - if (!isASCII(Str[i])) + for (auto c : getString()) + if (!isASCII(c)) return true; return false; } bool containsNonAsciiOrNull() const { - StringRef Str = getString(); - for (unsigned i = 0, e = Str.size(); i != e; ++i) - if (!isASCII(Str[i]) || !Str[i]) + for (auto c : getString()) + if (!isASCII(c) || !c) return true; return false; } diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index b915bb58f5..37575e7ba2 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -887,27 +887,28 @@ double FloatingLiteral::getValueAsApproximateDouble() const { return V.convertToDouble(); } -int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) { - int CharByteWidth = 0; - switch(k) { - case Ascii: - case UTF8: - CharByteWidth = target.getCharWidth(); - break; - case Wide: - CharByteWidth = target.getWCharWidth(); - break; - case UTF16: - CharByteWidth = target.getChar16Width(); - break; - case UTF32: - CharByteWidth = target.getChar32Width(); - break; +unsigned StringLiteral::mapCharByteWidth(TargetInfo const &Target, + StringKind SK) { + unsigned CharByteWidth = 0; + switch (SK) { + case Ascii: + case UTF8: + CharByteWidth = Target.getCharWidth(); + break; + case Wide: + CharByteWidth = Target.getWCharWidth(); + break; + case UTF16: + CharByteWidth = Target.getChar16Width(); + break; + case UTF32: + CharByteWidth = Target.getChar32Width(); + break; } assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple"); CharByteWidth /= 8; - assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4) - && "character byte widths supported are 1, 2, and 4 only"); + assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth == 4) && + "The only supported character byte widths are 1,2 and 4!"); return CharByteWidth; } @@ -1102,7 +1103,8 @@ StringLiteral::getLocationOfByte(unsigned ByteNo, const SourceManager &SM, const LangOptions &Features, const TargetInfo &Target, unsigned *StartToken, unsigned *StartTokenByteOffset) const { - assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) && + assert((getKind() == StringLiteral::Ascii || + getKind() == StringLiteral::UTF8) && "Only narrow string literals are currently supported"); // Loop over all of the tokens in this string until we find the one that @@ -1170,8 +1172,6 @@ StringLiteral::getLocationOfByte(unsigned ByteNo, const SourceManager &SM, } } - - /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it /// corresponds to, e.g. "sizeof" or "[pre]++". StringRef UnaryOperator::getOpcodeStr(Opcode Op) {