From: Jordan Rose Date: Thu, 24 Jan 2013 20:50:52 +0000 (+0000) Subject: Add a fixit for \U1234 -> \u1234. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b87672b124ab4fbe6f2cabc2ad71655fc71230ea;p=clang Add a fixit for \U1234 -> \u1234. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173371 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticLexKinds.td b/include/clang/Basic/DiagnosticLexKinds.td index 00b385ef12..e3bff51b8e 100644 --- a/include/clang/Basic/DiagnosticLexKinds.td +++ b/include/clang/Basic/DiagnosticLexKinds.td @@ -114,12 +114,13 @@ def err_ucn_escape_incomplete : Error< def warn_ucn_escape_incomplete : Warning< "incomplete universal character name; " "treating as '\\' followed by identifier">, InGroup; -def err_ucn_escape_invalid : Error<"invalid universal character">; +def note_ucn_four_not_eight : Note<"did you mean to use '\\u'?">; def err_ucn_escape_basic_scs : Error< "character '%0' cannot be specified by a universal character name">; def err_ucn_control_character : Error< "universal character name refers to a control character">; +def err_ucn_escape_invalid : Error<"invalid universal character">; def warn_cxx98_compat_literal_ucn_escape_basic_scs : Warning< "specifying character '%0' with a universal character name " "is incompatible with C++98">, InGroup, DefaultIgnore; diff --git a/lib/Lex/Lexer.cpp b/lib/Lex/Lexer.cpp index 2a57e6fced..a4d6a2e8f7 100644 --- a/lib/Lex/Lexer.cpp +++ b/lib/Lex/Lexer.cpp @@ -2725,8 +2725,16 @@ uint32_t Lexer::tryReadUCN(const char *&StartPtr, const char *SlashLoc, Diag(BufferPtr, diag::warn_ucn_escape_no_digits) << StringRef(KindLoc, 1); } else { - // FIXME: if i == 4 and NumHexDigits == 8, suggest a fixit to \u. Diag(BufferPtr, diag::warn_ucn_escape_incomplete); + + // If the user wrote \U1234, suggest a fixit to \u. + if (i == 4 && NumHexDigits == 8) { + CharSourceRange URange = + CharSourceRange::getCharRange(getSourceLocation(KindLoc), + getSourceLocation(KindLoc + 1)); + Diag(KindLoc, diag::note_ucn_four_not_eight) + << FixItHint::CreateReplacement(URange, "u"); + } } } diff --git a/test/Preprocessor/ucn-pp-identifier.c b/test/Preprocessor/ucn-pp-identifier.c index f4afa91ed3..b6e46516d8 100644 --- a/test/Preprocessor/ucn-pp-identifier.c +++ b/test/Preprocessor/ucn-pp-identifier.c @@ -1,5 +1,6 @@ // RUN: %clang_cc1 %s -fsyntax-only -std=c99 -pedantic -verify -Wundef // RUN: %clang_cc1 %s -fsyntax-only -x c++ -pedantic -verify -Wundef +// RUN: %clang_cc1 %s -fsyntax-only -std=c99 -pedantic -fdiagnostics-parseable-fixits -Wundef 2>&1 | FileCheck -strict-whitespace %s #define \u00FC #define a\u00FD() 0 @@ -95,3 +96,11 @@ C 1 #else #error "Line splicing failed to produce UCNs" #endif + + +#define capital_u_\U00FC +// expected-warning@-1 {{incomplete universal character name}} expected-note@-1 {{did you mean to use '\u'?}} expected-warning@-1 {{whitespace}} +// CHECK: note: did you mean to use '\u'? +// CHECK-NEXT: #define capital_u_\U00FC +// CHECK-NEXT: {{^ \^}} +// CHECK-NEXT: {{^ u}}