[cfe-commits] r114315 - in /cfe/trunk: include/clang/AST/Stmt.h include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaDecl.cpp lib/Sema/SemaExpr.cpp lib/Sema/SemaStmt.cpp lib/Serialization/ASTReaderStmt.cpp lib/Serialization/ASTWriterStmt.cpp test/Sema/warn-unused-label.c test/Sema/warn-unused-value.c

Argyrios Kyrtzidis akyrtzi at gmail.com
Sun Sep 19 14:21:25 PDT 2010


Author: akirtzidis
Date: Sun Sep 19 16:21:25 2010
New Revision: 114315

URL: http://llvm.org/viewvc/llvm-project?rev=114315&view=rev
Log:
Implement -Wunused-label.

Added:
    cfe/trunk/test/Sema/warn-unused-label.c
Modified:
    cfe/trunk/include/clang/AST/Stmt.h
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/lib/Serialization/ASTReaderStmt.cpp
    cfe/trunk/lib/Serialization/ASTWriterStmt.cpp
    cfe/trunk/test/Sema/warn-unused-value.c

Modified: cfe/trunk/include/clang/AST/Stmt.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Stmt.h?rev=114315&r1=114314&r2=114315&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Stmt.h (original)
+++ cfe/trunk/include/clang/AST/Stmt.h Sun Sep 19 16:21:25 2010
@@ -561,10 +561,11 @@
   IdentifierInfo *Label;
   Stmt *SubStmt;
   SourceLocation IdentLoc;
+  bool Used : 1;
 public:
   LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt)
     : Stmt(LabelStmtClass), Label(label),
-      SubStmt(substmt), IdentLoc(IL) {}
+      SubStmt(substmt), IdentLoc(IL), Used(false) {}
 
   // \brief Build an empty label statement.
   explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
@@ -578,6 +579,11 @@
   void setIdentLoc(SourceLocation L) { IdentLoc = L; }
   void setSubStmt(Stmt *SS) { SubStmt = SS; }
 
+  /// \brief Whether this label was used.
+  /// FIXME: Check "used" attribute (requires storing label attributes).
+  bool isUsed() const { return Used; }
+  void setUsed(bool U = true) { Used = U; }
+
   virtual SourceRange getSourceRange() const {
     return SourceRange(IdentLoc, SubStmt->getLocEnd());
   }

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=114315&r1=114314&r2=114315&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Sun Sep 19 16:21:25 2010
@@ -1899,6 +1899,8 @@
 
 def err_redefinition_of_label : Error<"redefinition of label '%0'">;
 def err_undeclared_label_use : Error<"use of undeclared label '%0'">;
+def warn_unused_label : Warning<"unused label '%0'">,
+  InGroup<UnusedLabel>, DefaultIgnore;
 
 def err_goto_into_protected_scope : Error<"goto into protected scope">;
 def err_switch_into_protected_scope : Error<

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=114315&r1=114314&r2=114315&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sun Sep 19 16:21:25 2010
@@ -5028,8 +5028,11 @@
     // Verify that we have no forward references left.  If so, there was a goto
     // or address of a label taken, but no definition of it.  Label fwd
     // definitions are indicated with a null substmt.
-    if (L->getSubStmt() != 0)
+    if (L->getSubStmt() != 0) {
+      if (!L->isUsed())
+        Diag(L->getIdentLoc(), diag::warn_unused_label) << L->getName();
       continue;
+    }
 
     // Emit error.
     Diag(L->getIdentLoc(), diag::err_undeclared_label_use) << L->getName();

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=114315&r1=114314&r2=114315&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sun Sep 19 16:21:25 2010
@@ -6871,6 +6871,7 @@
   if (LabelDecl == 0)
     LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0);
 
+  LabelDecl->setUsed();
   // Create the AST node.  The address of a label always has type 'void*'.
   return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
                                        Context.getPointerType(Context.VoidTy)));
@@ -7355,8 +7356,11 @@
 
     // Verify that we have no forward references left.  If so, there was a goto
     // or address of a label taken, but no definition of it.
-    if (L->getSubStmt() != 0)
+    if (L->getSubStmt() != 0) {
+      if (!L->isUsed())
+        Diag(L->getIdentLoc(), diag::warn_unused_label) << L->getName();
       continue;
+    }
 
     // Emit error.
     Diag(L->getIdentLoc(), diag::err_undeclared_label_use) << L->getName();

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=114315&r1=114314&r2=114315&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Sun Sep 19 16:21:25 2010
@@ -994,6 +994,7 @@
   if (LabelDecl == 0)
     LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0);
 
+  LabelDecl->setUsed();
   return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc));
 }
 

Modified: cfe/trunk/lib/Serialization/ASTReaderStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReaderStmt.cpp?rev=114315&r1=114314&r2=114315&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReaderStmt.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReaderStmt.cpp Sun Sep 19 16:21:25 2010
@@ -219,6 +219,7 @@
   S->setID(Reader.GetIdentifierInfo(Record, Idx));
   S->setSubStmt(Reader.ReadSubStmt());
   S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
+  S->setUsed(Record[Idx++]);
   Reader.RecordLabelStmt(S, Record[Idx++]);
 }
 

Modified: cfe/trunk/lib/Serialization/ASTWriterStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriterStmt.cpp?rev=114315&r1=114314&r2=114315&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriterStmt.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriterStmt.cpp Sun Sep 19 16:21:25 2010
@@ -213,6 +213,7 @@
   Writer.AddIdentifierRef(S->getID(), Record);
   Writer.AddStmt(S->getSubStmt());
   Writer.AddSourceLocation(S->getIdentLoc(), Record);
+  Record.push_back(S->isUsed());
   Record.push_back(Writer.GetLabelID(S));
   Code = serialization::STMT_LABEL;
 }

Added: cfe/trunk/test/Sema/warn-unused-label.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unused-label.c?rev=114315&view=auto
==============================================================================
--- cfe/trunk/test/Sema/warn-unused-label.c (added)
+++ cfe/trunk/test/Sema/warn-unused-label.c Sun Sep 19 16:21:25 2010
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -Wunused-label -verify %s
+
+void f() {
+  a:
+  goto a;
+  b: // expected-warning{{unused}}
+  return;
+}

Modified: cfe/trunk/test/Sema/warn-unused-value.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unused-value.c?rev=114315&r1=114314&r2=114315&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-unused-value.c (original)
+++ cfe/trunk/test/Sema/warn-unused-value.c Sun Sep 19 16:21:25 2010
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value -Wunused-label %s
 // RUN: %clang_cc1 -fsyntax-only -verify -Wunused %s
 // RUN: %clang_cc1 -fsyntax-only -verify -Wall %s
 
@@ -53,7 +53,7 @@
   *pi;              // expected-warning {{expression result unused}}
   *pj;
 
-  foo_label:
+  foo_label:        // expected-warning {{unused label}}
   i;                // expected-warning {{expression result unused}}
 }
 





More information about the cfe-commits mailing list