[cfe-commits] r114954 - in /cfe/trunk: include/clang/AST/Stmt.h include/clang/Basic/DiagnosticSemaKinds.td include/clang/Sema/Sema.h lib/Parse/ParseStmt.cpp lib/Sema/SemaStmt.cpp lib/Sema/TreeTransform.h lib/Serialization/ASTReaderStmt.cpp lib/Serialization/ASTWriterStmt.cpp test/Sema/warn-unused-label.c
Argyrios Kyrtzidis
akyrtzi at gmail.com
Tue Sep 28 07:54:07 PDT 2010
Author: akirtzidis
Date: Tue Sep 28 09:54:07 2010
New Revision: 114954
URL: http://llvm.org/viewvc/llvm-project?rev=114954&view=rev
Log:
Don't warn for an unused label if it has 'unused' attribute. Fixes rdar://8483139.
Modified:
cfe/trunk/include/clang/AST/Stmt.h
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/include/clang/Sema/Sema.h
cfe/trunk/lib/Parse/ParseStmt.cpp
cfe/trunk/lib/Sema/SemaStmt.cpp
cfe/trunk/lib/Sema/TreeTransform.h
cfe/trunk/lib/Serialization/ASTReaderStmt.cpp
cfe/trunk/lib/Serialization/ASTWriterStmt.cpp
cfe/trunk/test/Sema/warn-unused-label.c
Modified: cfe/trunk/include/clang/AST/Stmt.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Stmt.h?rev=114954&r1=114953&r2=114954&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Stmt.h (original)
+++ cfe/trunk/include/clang/AST/Stmt.h Tue Sep 28 09:54:07 2010
@@ -562,10 +562,13 @@
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) { }
@@ -580,10 +583,14 @@
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());
}
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=114954&r1=114953&r2=114954&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Sep 28 09:54:07 2010
@@ -962,6 +962,8 @@
"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">,
Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=114954&r1=114953&r2=114954&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Tue Sep 28 09:54:07 2010
@@ -1540,10 +1540,12 @@
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,
Modified: cfe/trunk/lib/Parse/ParseStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseStmt.cpp?rev=114954&r1=114953&r2=114954&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseStmt.cpp (original)
+++ cfe/trunk/lib/Parse/ParseStmt.cpp Tue Sep 28 09:54:07 2010
@@ -241,10 +241,9 @@
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
Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=114954&r1=114953&r2=114954&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Tue Sep 28 09:54:07 2010
@@ -227,13 +227,35 @@
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!");
@@ -249,6 +271,7 @@
// definition. Fill in the forward definition and return it.
LabelDecl->setIdentLoc(IdentLoc);
LabelDecl->setSubStmt(SubStmt);
+ LabelDecl->setUnusedAttribute(HasUnusedAttr);
return Owned(LabelDecl);
}
Modified: cfe/trunk/lib/Sema/TreeTransform.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/TreeTransform.h?rev=114954&r1=114953&r2=114954&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/TreeTransform.h (original)
+++ cfe/trunk/lib/Sema/TreeTransform.h Tue Sep 28 09:54:07 2010
@@ -758,8 +758,9 @@
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.
@@ -3562,7 +3563,7 @@
// 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>
Modified: cfe/trunk/lib/Serialization/ASTReaderStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReaderStmt.cpp?rev=114954&r1=114953&r2=114954&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReaderStmt.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReaderStmt.cpp Tue Sep 28 09:54:07 2010
@@ -220,6 +220,7 @@
S->setSubStmt(Reader.ReadSubStmt());
S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
S->setUsed(Record[Idx++]);
+ S->setUnusedAttribute(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=114954&r1=114953&r2=114954&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriterStmt.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriterStmt.cpp Tue Sep 28 09:54:07 2010
@@ -214,6 +214,7 @@
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;
}
Modified: cfe/trunk/test/Sema/warn-unused-label.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unused-label.c?rev=114954&r1=114953&r2=114954&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-unused-label.c (original)
+++ cfe/trunk/test/Sema/warn-unused-label.c Tue Sep 28 09:54:07 2010
@@ -4,5 +4,8 @@
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;
}
More information about the cfe-commits
mailing list