summaryrefslogtreecommitdiffstats
path: root/clang/lib
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2011-02-18 02:08:43 +0000
committerChris Lattner <sabre@nondot.org>2011-02-18 02:08:43 +0000
commit43e7f31f11ab8a8ce941a720c8198c9094bea4ea (patch)
tree10a4fed274fe204f3b7c11910a663c60f9724795 /clang/lib
parent4ba77fa9761179098fe7d98141530f5f9f6863ee (diff)
downloadbcm5719-llvm-43e7f31f11ab8a8ce941a720c8198c9094bea4ea.tar.gz
bcm5719-llvm-43e7f31f11ab8a8ce941a720c8198c9094bea4ea.zip
implement basic support for __label__. I wouldn't be shocked if there are
bugs from other clients that don't expect to see a LabelDecl in a DeclStmt, but if so they should be easy to fix. This implements most of PR3429 and rdar://8287027 llvm-svn: 125817
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/AST/StmtDumper.cpp2
-rw-r--r--clang/lib/CodeGen/CGDecl.cpp2
-rw-r--r--clang/lib/Parse/ParseStmt.cpp37
-rw-r--r--clang/lib/Sema/SemaLookup.cpp17
4 files changed, 49 insertions, 9 deletions
diff --git a/clang/lib/AST/StmtDumper.cpp b/clang/lib/AST/StmtDumper.cpp
index 490e2eea6e3..846bd4ce144 100644
--- a/clang/lib/AST/StmtDumper.cpp
+++ b/clang/lib/AST/StmtDumper.cpp
@@ -282,6 +282,8 @@ void StmtDumper::DumpDeclarator(Decl *D) {
UD->getTargetNestedNameDecl()->print(OS,
PrintingPolicy(UD->getASTContext().getLangOptions()));
OS << ";\"";
+ } else if (LabelDecl *LD = dyn_cast<LabelDecl>(D)) {
+ OS << "label " << LD->getNameAsString();
} else {
assert(0 && "Unexpected decl");
}
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index 97dbf0ba316..22f28197c60 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -70,7 +70,6 @@ void CodeGenFunction::EmitDecl(const Decl &D) {
case Decl::Friend:
case Decl::FriendTemplate:
case Decl::Block:
- case Decl::Label:
assert(0 && "Declaration not should not be in declstmts!");
case Decl::Function: // void X();
case Decl::Record: // struct/union/class X;
@@ -82,6 +81,7 @@ void CodeGenFunction::EmitDecl(const Decl &D) {
case Decl::UsingDirective: // using namespace X; [C++]
case Decl::NamespaceAlias:
case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
+ case Decl::Label: // __label__ x;
// None of these decls require codegen support.
return;
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index 2c4ab655aec..2d9758333f0 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -476,12 +476,41 @@ StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
- // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
- // only allowed at the start of a compound stmt regardless of the language.
-
StmtVector Stmts(Actions);
- while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
+ // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
+ // only allowed at the start of a compound stmt regardless of the language.
+ while (Tok.is(tok::kw___label__)) {
+ SourceLocation LabelLoc = ConsumeToken();
+ Diag(LabelLoc, diag::ext_gnu_local_label);
+
+ llvm::SmallVector<Decl *, 8> DeclsInGroup;
+ while (1) {
+ if (Tok.isNot(tok::identifier)) {
+ Diag(Tok, diag::err_expected_ident);
+ break;
+ }
+
+ IdentifierInfo *II = Tok.getIdentifierInfo();
+ SourceLocation IdLoc = ConsumeToken();
+ DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, true));
+
+ if (!Tok.is(tok::comma))
+ break;
+ ConsumeToken();
+ }
+
+ DeclSpec DS;
+ DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
+ DeclsInGroup.data(), DeclsInGroup.size());
+ StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
+
+ ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
+ if (R.isUsable())
+ Stmts.push_back(R.release());
+ }
+
+ while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
if (Tok.is(tok::annot_pragma_unused)) {
HandlePragmaUnused();
continue;
diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp
index b7740bf5c08..789f9c9f4ff 100644
--- a/clang/lib/Sema/SemaLookup.cpp
+++ b/clang/lib/Sema/SemaLookup.cpp
@@ -2760,10 +2760,18 @@ void Sema::LookupVisibleDecls(DeclContext *Ctx, LookupNameKind Kind,
/*InBaseClass=*/false, Consumer, Visited);
}
-LabelDecl *Sema::LookupOrCreateLabel(IdentifierInfo *II, SourceLocation Loc) {
+/// LookupOrCreateLabel - Do a name lookup of a label with the specified name.
+/// If isLocalLabel is true, then this is a definition of an __label__ label
+/// name, otherwise it is a normal label definition or use.
+LabelDecl *Sema::LookupOrCreateLabel(IdentifierInfo *II, SourceLocation Loc,
+ bool isLocalLabel) {
// Do a lookup to see if we have a label with this name already.
- NamedDecl *Res = LookupSingleName(CurScope, II, Loc, LookupLabel,
- NotForRedeclaration);
+ NamedDecl *Res = 0;
+
+ // Local label definitions always shadow existing labels.
+ if (!isLocalLabel)
+ Res = LookupSingleName(CurScope, II, Loc, LookupLabel, NotForRedeclaration);
+
// If we found a label, check to see if it is in the same context as us. When
// in a Block, we don't want to reuse a label in an enclosing function.
if (Res && Res->getDeclContext() != CurContext)
@@ -2772,7 +2780,8 @@ LabelDecl *Sema::LookupOrCreateLabel(IdentifierInfo *II, SourceLocation Loc) {
if (Res == 0) {
// If not forward referenced or defined already, create the backing decl.
Res = LabelDecl::Create(Context, CurContext, Loc, II);
- PushOnScopeChains(Res, CurScope->getFnParent(), true);
+ PushOnScopeChains(Res, isLocalLabel ? CurScope : CurScope->getFnParent(),
+ true);
}
return cast<LabelDecl>(Res);
OpenPOWER on IntegriCloud