summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Lewycky <nicholas@mxc.ca>2013-08-21 04:10:58 +0000
committerNick Lewycky <nicholas@mxc.ca>2013-08-21 04:10:58 +0000
commit3151d7c76a641b61347b667d1e6f757b6007f548 (patch)
tree9d656bcbc02a782b81d2fe5e57119d089d7fc621
parentcee61339305cc00900ea7346656032447a756281 (diff)
downloadbcm5719-llvm-3151d7c76a641b61347b667d1e6f757b6007f548.tar.gz
bcm5719-llvm-3151d7c76a641b61347b667d1e6f757b6007f548.zip
Issue fixits replacing invalid character literals with the equivalent \xNN
escape code. llvm-svn: 188863
-rw-r--r--clang/lib/Lex/LiteralSupport.cpp12
-rw-r--r--clang/test/Lexer/char-literal-encoding-fixit.c11
2 files changed, 20 insertions, 3 deletions
diff --git a/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp
index 08d6c438d32..2301aaed821 100644
--- a/clang/lib/Lex/LiteralSupport.cpp
+++ b/clang/lib/Lex/LiteralSupport.cpp
@@ -978,7 +978,7 @@ CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
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(const char *begin, const char *end,
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(const char *begin, const char *end,
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;
}
diff --git a/clang/test/Lexer/char-literal-encoding-fixit.c b/clang/test/Lexer/char-literal-encoding-fixit.c
new file mode 100644
index 00000000000..ab5d28aa53f
--- /dev/null
+++ b/clang/test/Lexer/char-literal-encoding-fixit.c
@@ -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';
OpenPOWER on IntegriCloud