r188863 - Issue fixits replacing invalid character literals with the equivalent \xNN

Nick Lewycky nicholas at mxc.ca
Tue Aug 20 21:10:58 PDT 2013


Author: nicholas
Date: Tue Aug 20 23:10:58 2013
New Revision: 188863

URL: http://llvm.org/viewvc/llvm-project?rev=188863&view=rev
Log:
Issue fixits replacing invalid character literals with the equivalent \xNN
escape code.

Added:
    cfe/trunk/test/Lexer/char-literal-encoding-fixit.c
Modified:
    cfe/trunk/lib/Lex/LiteralSupport.cpp

Modified: cfe/trunk/lib/Lex/LiteralSupport.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/LiteralSupport.cpp?rev=188863&r1=188862&r2=188863&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/LiteralSupport.cpp (original)
+++ cfe/trunk/lib/Lex/LiteralSupport.cpp Tue Aug 20 23:10:58 2013
@@ -978,7 +978,7 @@ CharLiteralParser::CharLiteralParser(con
   uint32_t largest_character_for_kind;
   if (tok::wide_char_constant == Kind) {
     largest_character_for_kind =
-        0xFFFFFFFFu >> (32-PP.getTargetInfo().getWCharWidth());
+        0xFFFFFFFFu >> (32 - PP.getTargetInfo().getWCharWidth());
   } else if (tok::utf16_char_constant == Kind) {
     largest_character_for_kind = 0xFFFF;
   } else if (tok::utf32_char_constant == Kind) {
@@ -1009,7 +1009,13 @@ CharLiteralParser::CharLiteralParser(con
         unsigned Msg = diag::err_bad_character_encoding;
         if (NoErrorOnBadEncoding)
           Msg = diag::warn_bad_character_encoding;
-        PP.Diag(Loc, Msg);
+        std::string escaped = llvm::utohexstr(static_cast<uint8_t>(*start));
+        FullSourceLoc SourceLoc(Loc, PP.getSourceManager());
+        PP.Diag(Loc, Msg) << FixItHint::CreateReplacement(
+                                 MakeCharSourceRange(PP.getLangOpts(),
+                                                     SourceLoc, TokBegin, start,
+                                                     start + 1),
+                                 "\\x" + escaped);
         if (NoErrorOnBadEncoding) {
           start = tmp_in_start;
           buffer_begin = tmp_out_start;
@@ -1047,7 +1053,7 @@ CharLiteralParser::CharLiteralParser(con
     unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo());
     uint64_t result =
       ProcessCharEscape(TokBegin, begin, end, HadError,
-                        FullSourceLoc(Loc,PP.getSourceManager()),
+                        FullSourceLoc(Loc, PP.getSourceManager()),
                         CharWidth, &PP.getDiagnostics(), PP.getLangOpts());
     *buffer_begin++ = result;
   }

Added: cfe/trunk/test/Lexer/char-literal-encoding-fixit.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/char-literal-encoding-fixit.c?rev=188863&view=auto
==============================================================================
--- cfe/trunk/test/Lexer/char-literal-encoding-fixit.c (added)
+++ cfe/trunk/test/Lexer/char-literal-encoding-fixit.c Tue Aug 20 23:10:58 2013
@@ -0,0 +1,11 @@
+// RUN: cp %s %t
+// RUN: %clang_cc1 -fixit -x c %t
+// RUN: FileCheck -input-file=%t %t
+
+// Note that this file is not valid UTF-8.
+
+int test1 = 'ˆ';
+// CHECK: int test1 = '\x88';
+
+int test2 = 'abˆc';
+// CHECK: int test2 = 'ab\x88c';





More information about the cfe-commits mailing list