From d5612a235fdd5df145c485c6ac489fcfbf7120d3 Mon Sep 17 00:00:00 2001 From: Anna Zaks Date: Thu, 28 Jul 2011 20:52:06 +0000 Subject: [PATCH] Add a fixit for removal of unused label. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@136389 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDecl.cpp | 18 +++++++++++++++++- test/FixIt/fixit.c | 15 +++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 321ab780d4..4988323f41 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -1086,12 +1086,28 @@ static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) { return true; } +static void GenerateFixForUnusedDecl(const NamedDecl *D, ASTContext &Ctx, + FixItHint &Hint) { + if (isa(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(D) && cast(D)->isExceptionVariable()) DiagID = diag::warn_unused_exception_param; @@ -1100,7 +1116,7 @@ void Sema::DiagnoseUnusedDecl(const NamedDecl *D) { 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) { diff --git a/test/FixIt/fixit.c b/test/FixIt/fixit.c index ba45cf28e6..2dbaab1c9f 100644 --- a/test/FixIt/fixit.c +++ b/test/FixIt/fixit.c @@ -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 @@ struct test_struct { // 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; +} -- 2.40.0