[cfe-commits] r136389 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/FixIt/fixit.c

Anna Zaks ganna at apple.com
Thu Jul 28 13:52:06 PDT 2011


Author: zaks
Date: Thu Jul 28 15:52:06 2011
New Revision: 136389

URL: http://llvm.org/viewvc/llvm-project?rev=136389&view=rev
Log:
Add a fixit for removal of unused label.

Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/FixIt/fixit.c

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=136389&r1=136388&r2=136389&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Jul 28 15:52:06 2011
@@ -1086,12 +1086,28 @@
   return true;
 }
 
+static void GenerateFixForUnusedDecl(const NamedDecl *D, ASTContext &Ctx,
+                                     FixItHint &Hint) {
+  if (isa<LabelDecl>(D)) {
+    SourceLocation AfterColon = Lexer::findLocationAfterToken(D->getLocEnd(),
+                tok::colon, Ctx.getSourceManager(), Ctx.getLangOptions(), true);
+    if (AfterColon.isInvalid())
+      return;
+    Hint = FixItHint::CreateRemoval(CharSourceRange::
+                                    getCharRange(D->getLocStart(), AfterColon));
+  }
+  return;
+}
+
 /// DiagnoseUnusedDecl - Emit warnings about declarations that are not used
 /// unless they are marked attr(unused).
 void Sema::DiagnoseUnusedDecl(const NamedDecl *D) {
+  FixItHint Hint;
   if (!ShouldDiagnoseUnusedDecl(D))
     return;
   
+  GenerateFixForUnusedDecl(D, Context, Hint);
+
   unsigned DiagID;
   if (isa<VarDecl>(D) && cast<VarDecl>(D)->isExceptionVariable())
     DiagID = diag::warn_unused_exception_param;
@@ -1100,7 +1116,7 @@
   else
     DiagID = diag::warn_unused_variable;
 
-  Diag(D->getLocation(), DiagID) << D->getDeclName();
+  Diag(D->getLocation(), DiagID) << D->getDeclName() << Hint;
 }
 
 static void CheckPoppedLabel(LabelDecl *L, Sema &S) {

Modified: cfe/trunk/test/FixIt/fixit.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/FixIt/fixit.c?rev=136389&r1=136388&r2=136389&view=diff
==============================================================================
--- cfe/trunk/test/FixIt/fixit.c (original)
+++ cfe/trunk/test/FixIt/fixit.c Thu Jul 28 15:52:06 2011
@@ -1,7 +1,7 @@
 // RUN: cp %s %t
-// RUN: %clang_cc1 -pedantic -fixit -x c %t || true
+// RUN: %clang_cc1 -pedantic -Wunused-label -fixit -x c %t || true
 // RUN: grep -v CHECK %t > %t2
-// RUN: %clang_cc1 -pedantic -Werror -x c %t
+// RUN: %clang_cc1 -pedantic -Wunused-label -Werror -x c %t
 // RUN: FileCheck -input-file=%t2 %t
 
 /* This is a test of the various code modification hints that are
@@ -59,3 +59,14 @@
   // CHECK: struct test_struct *struct_ptr;
   test_struct *struct_ptr; // expected-error {{must use 'struct' tag to refer to type 'test_struct'}}
 };
+
+void removeUnusedLabels(char c) {
+  L0 /*removed comment*/:        c++;
+  removeUnusedLabels(c);
+  L1:
+  c++;
+  /*preserved comment*/ L2  :        c++;
+  LL
+  : c++;
+  c = c + 3; L4: return;
+}





More information about the cfe-commits mailing list