Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1 | //===--- SemaStmtAttr.cpp - Statement Attribute Handling ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements stmt-related attribute processing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Sema/SemaInternal.h" |
| 15 | #include "TargetAttributesSema.h" |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/Basic/SourceManager.h" |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Lexer.h" |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 19 | #include "clang/Sema/DelayedDiagnostic.h" |
| 20 | #include "clang/Sema/Lookup.h" |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 21 | #include "clang/Sema/ScopeInfo.h" |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringExtras.h" |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 23 | |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace sema; |
| 26 | |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 27 | static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const AttributeList &A, |
| 28 | SourceRange Range) { |
| 29 | if (!isa<NullStmt>(St)) { |
| 30 | S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_wrong_target) |
| 31 | << St->getLocStart(); |
| 32 | if (isa<SwitchCase>(St)) { |
| 33 | SourceLocation L = Lexer::getLocForEndOfToken(Range.getEnd(), 0, |
| 34 | S.getSourceManager(), S.getLangOpts()); |
| 35 | S.Diag(L, diag::note_fallthrough_insert_semi_fixit) |
| 36 | << FixItHint::CreateInsertion(L, ";"); |
| 37 | } |
| 38 | return 0; |
| 39 | } |
| 40 | if (S.getCurFunction()->SwitchStack.empty()) { |
| 41 | S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch); |
| 42 | return 0; |
| 43 | } |
| 44 | return ::new (S.Context) FallThroughAttr(A.getRange(), S.Context); |
| 45 | } |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 46 | |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 47 | |
| 48 | static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A, |
| 49 | SourceRange Range) { |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 50 | switch (A.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 51 | case AttributeList::AT_FallThrough: |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 52 | return handleFallThroughAttr(S, St, A, Range); |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 53 | default: |
| 54 | // if we're here, then we parsed an attribute, but didn't recognize it as a |
| 55 | // statement attribute => it is declaration attribute |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 56 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_invalid_on_stmt) |
| 57 | << A.getName()->getName() << St->getLocStart(); |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList, |
| 63 | SourceRange Range) { |
Alexander Kornienko | 4990890 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 64 | SmallVector<const Attr*, 8> Attrs; |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 65 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 66 | if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range)) |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 67 | Attrs.push_back(a); |
| 68 | } |
| 69 | |
| 70 | if (Attrs.empty()) |
| 71 | return S; |
| 72 | |
| 73 | return ActOnAttributedStmt(Range.getBegin(), Attrs, S); |
| 74 | } |