[cfe-commits] r159458 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaDecl.cpp test/SemaCXX/warn-unique-enum.cpp
Richard Trieu
rtrieu at google.com
Fri Jun 29 13:36:14 PDT 2012
Author: rtrieu
Date: Fri Jun 29 15:36:14 2012
New Revision: 159458
URL: http://llvm.org/viewvc/llvm-project?rev=159458&view=rev
Log:
Add a fix-it hint note to -Wunique-enum to suggest that the last element gets
initialized with the next to last element to silence the warning.
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaDecl.cpp
cfe/trunk/test/SemaCXX/warn-unique-enum.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=159458&r1=159457&r2=159458&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Jun 29 15:36:14 2012
@@ -23,6 +23,9 @@
def warn_identical_enum_values : Warning<
"all elements of %0 are initialized with literals to value %1">,
InGroup<DiagGroup<"unique-enum">>;
+def note_identical_enum_values : Note<
+ "initialize the last element with the previous element to silence "
+ "this warning">;
// Constant expressions
def err_expr_not_ice : Error<
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=159458&r1=159457&r2=159458&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Jun 29 15:36:14 2012
@@ -10364,6 +10364,13 @@
S.Diag(Enum->getLocation(), diag::warn_identical_enum_values)
<< EnumType << FirstVal.toString(10)
<< Enum->getSourceRange();
+
+ EnumConstantDecl *Last = cast<EnumConstantDecl>(Elements[NumElements - 1]),
+ *Next = cast<EnumConstantDecl>(Elements[NumElements - 2]);
+
+ S.Diag(Last->getLocation(), diag::note_identical_enum_values)
+ << FixItHint::CreateReplacement(Last->getInitExpr()->getSourceRange(),
+ Next->getName());
}
void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc,
Modified: cfe/trunk/test/SemaCXX/warn-unique-enum.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unique-enum.cpp?rev=159458&r1=159457&r2=159458&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unique-enum.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unique-enum.cpp Fri Jun 29 15:36:14 2012
@@ -1,8 +1,13 @@
// RUN: %clang_cc1 %s -fsyntax-only -verify -Wunique-enum
-enum A { A1 = 1, A2 = 1, A3 = 1 }; // expected-warning {{all elements of 'A' are initialized with literals to value 1}}
+enum A { A1 = 1, A2 = 1, A3 = 1 }; // expected-warning {{all elements of 'A' are initialized with literals to value 1}} \
+// expected-note {{initialize the last element with the previous element to silence this warning}}
enum { B1 = 1, B2 = 1, B3 = 1 }; // no warning
-enum C { C1 = true, C2 = true}; // expected-warning {{all elements of 'C' are initialized with literals to value 1}}
-enum D { D1 = 5, D2 = 5L, D3 = 5UL, D4 = 5LL, D5 = 5ULL }; // expected-warning {{all elements of 'D' are initialized with literals to value 5}}
+enum C { // expected-warning {{all elements of 'C' are initialized with literals to value 1}}
+ C1 = true,
+ C2 = true // expected-note {{initialize the last element with the previous element to silence this warning}}
+};
+enum D { D1 = 5, D2 = 5L, D3 = 5UL, D4 = 5LL, D5 = 5ULL }; // expected-warning {{all elements of 'D' are initialized with literals to value 5}} \
+// expected-note {{initialize the last element with the previous element to silence this warning}}
// Don't warn on enums with less than 2 elements.
enum E { E1 = 4 };
More information about the cfe-commits
mailing list