Stmt *SubStmt;
SourceLocation IdentLoc;
bool Used : 1;
+ bool HasUnusedAttr : 1;
public:
- LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt)
+ LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt,
+ bool hasUnusedAttr = false)
: Stmt(LabelStmtClass), Label(label),
- SubStmt(substmt), IdentLoc(IL), Used(false) {}
+ SubStmt(substmt), IdentLoc(IL), Used(false),
+ HasUnusedAttr(hasUnusedAttr) {}
// \brief Build an empty label statement.
explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
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; }
+ bool isUsed(bool CheckUnusedAttr = true) const {
+ return Used || (CheckUnusedAttr && HasUnusedAttr);
+ }
void setUsed(bool U = true) { Used = U; }
+ bool HasUnusedAttribute() const { return HasUnusedAttr; }
+ void setUnusedAttribute(bool U) { HasUnusedAttr = U; }
+
virtual SourceRange getSourceRange() const {
return SourceRange(IdentLoc, SubStmt->getLocEnd());
}
"attribute was previously declared %plural{0:without the regparm|1:"
"with the regparm(1)|2:with the regparm(2)|3:with the regparm(3)|:with the"
"regparm}1 attribute">;
+def warn_label_attribute_not_unused : Warning<
+ "The only valid attribute for labels is 'unused'">;
def warn_impcast_vector_scalar : Warning<
"implicit conversion turns vector to scalar: %0 to %1">,
StmtResult ActOnDefaultStmt(SourceLocation DefaultLoc,
SourceLocation ColonLoc,
Stmt *SubStmt, Scope *CurScope);
- StmtResult ActOnLabelStmt(SourceLocation IdentLoc,
- IdentifierInfo *II,
- SourceLocation ColonLoc,
- Stmt *SubStmt);
+ StmtResult ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
+ SourceLocation ColonLoc, Stmt *SubStmt,
+ const AttributeList *Attr);
+ StmtResult ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
+ SourceLocation ColonLoc, Stmt *SubStmt,
+ bool HasUnusedAttr);
StmtResult ActOnIfStmt(SourceLocation IfLoc,
FullExprArg CondVal, Decl *CondVar,
Stmt *ThenVal,
if (SubStmt.isInvalid())
SubStmt = Actions.ActOnNullStmt(ColonLoc);
- // FIXME: use attributes?
return Actions.ActOnLabelStmt(IdentTok.getLocation(),
IdentTok.getIdentifierInfo(),
- ColonLoc, SubStmt.get());
+ ColonLoc, SubStmt.get(), AttrList.take());
}
/// ParseCaseStatement
StmtResult
Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
- SourceLocation ColonLoc, Stmt *SubStmt) {
+ SourceLocation ColonLoc, Stmt *SubStmt,
+ const AttributeList *Attr) {
+ // According to GCC docs, "the only attribute that makes sense after a label
+ // is 'unused'".
+ bool HasUnusedAttr = false;
+ llvm::OwningPtr<const AttributeList> AttrList(Attr);
+ for (const AttributeList* a = AttrList.get(); a; a = a->getNext()) {
+ if (a->getKind() == AttributeList::AT_unused) {
+ HasUnusedAttr = true;
+ } else {
+ Diag(a->getLoc(), diag::warn_label_attribute_not_unused);
+ a->setInvalid(true);
+ }
+ }
+
+ return ActOnLabelStmt(IdentLoc, II, ColonLoc, SubStmt, HasUnusedAttr);
+}
+
+StmtResult
+Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
+ SourceLocation ColonLoc, Stmt *SubStmt,
+ bool HasUnusedAttr) {
// Look up the record for this label identifier.
LabelStmt *&LabelDecl = getCurFunction()->LabelMap[II];
// If not forward referenced or defined already, just create a new LabelStmt.
if (LabelDecl == 0)
- return Owned(LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt));
+ return Owned(LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt,
+ HasUnusedAttr));
assert(LabelDecl->getID() == II && "Label mismatch!");
// definition. Fill in the forward definition and return it.
LabelDecl->setIdentLoc(IdentLoc);
LabelDecl->setSubStmt(SubStmt);
+ LabelDecl->setUnusedAttribute(HasUnusedAttr);
return Owned(LabelDecl);
}
StmtResult RebuildLabelStmt(SourceLocation IdentLoc,
IdentifierInfo *Id,
SourceLocation ColonLoc,
- Stmt *SubStmt) {
- return SemaRef.ActOnLabelStmt(IdentLoc, Id, ColonLoc, SubStmt);
+ Stmt *SubStmt, bool HasUnusedAttr) {
+ return SemaRef.ActOnLabelStmt(IdentLoc, Id, ColonLoc, SubStmt,
+ HasUnusedAttr);
}
/// \brief Build a new "if" statement.
// FIXME: Pass the real colon location in.
SourceLocation ColonLoc = SemaRef.PP.getLocForEndOfToken(S->getIdentLoc());
return getDerived().RebuildLabelStmt(S->getIdentLoc(), S->getID(), ColonLoc,
- SubStmt.get());
+ SubStmt.get(), S->HasUnusedAttribute());
}
template<typename Derived>
S->setSubStmt(Reader.ReadSubStmt());
S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
S->setUsed(Record[Idx++]);
+ S->setUnusedAttribute(Record[Idx++]);
Reader.RecordLabelStmt(S, Record[Idx++]);
}
Writer.AddStmt(S->getSubStmt());
Writer.AddSourceLocation(S->getIdentLoc(), Record);
Record.push_back(S->isUsed());
+ Record.push_back(S->HasUnusedAttribute());
Record.push_back(Writer.GetLabelID(S));
Code = serialization::STMT_LABEL;
}
a:
goto a;
b: // expected-warning{{unused}}
+ c: __attribute__((unused));
+ d: __attribute__((noreturn)); // expected-warning {{The only valid attribute for labels is 'unused'}}
+ goto d;
return;
}