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) { }
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());
}
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<
// 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();
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)));
// 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();
if (LabelDecl == 0)
LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0);
+ LabelDecl->setUsed();
return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc));
}
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++]);
}
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;
}
--- /dev/null
+// RUN: %clang_cc1 -fsyntax-only -Wunused-label -verify %s
+
+void f() {
+ a:
+ goto a;
+ b: // expected-warning{{unused}}
+ return;
+}
-// 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
*pi; // expected-warning {{expression result unused}}
*pj;
- foo_label:
+ foo_label: // expected-warning {{unused label}}
i; // expected-warning {{expression result unused}}
}