Richard Smith | c202b28 | 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" |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
| 17 | #include "clang/Sema/DelayedDiagnostic.h" |
| 18 | #include "clang/Sema/Lookup.h" |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ScopeInfo.h" |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 21 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace sema; |
| 24 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 25 | static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const AttributeList &A, |
| 26 | SourceRange Range) { |
| 27 | if (!isa<NullStmt>(St)) { |
| 28 | S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_wrong_target) |
| 29 | << St->getLocStart(); |
| 30 | if (isa<SwitchCase>(St)) { |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 31 | SourceLocation L = S.getLocForEndOfToken(Range.getEnd()); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 32 | S.Diag(L, diag::note_fallthrough_insert_semi_fixit) |
| 33 | << FixItHint::CreateInsertion(L, ";"); |
| 34 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 35 | return nullptr; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 36 | } |
| 37 | if (S.getCurFunction()->SwitchStack.empty()) { |
| 38 | S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 39 | return nullptr; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 40 | } |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 41 | return ::new (S.Context) FallThroughAttr(A.getRange(), S.Context, |
| 42 | A.getAttributeSpellingListIndex()); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 43 | } |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 44 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 45 | |
| 46 | static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A, |
| 47 | SourceRange Range) { |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 48 | switch (A.getKind()) { |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 49 | case AttributeList::UnknownAttribute: |
| 50 | S.Diag(A.getLoc(), A.isDeclspecAttribute() ? |
| 51 | diag::warn_unhandled_ms_attribute_ignored : |
| 52 | diag::warn_unknown_attribute_ignored) << A.getName(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 53 | return nullptr; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 54 | case AttributeList::AT_FallThrough: |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 55 | return handleFallThroughAttr(S, St, A, Range); |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 56 | default: |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 57 | // if we're here, then we parsed a known attribute, but didn't recognize |
| 58 | // it as a statement attribute => it is declaration attribute |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 59 | S.Diag(A.getRange().getBegin(), diag::err_attribute_invalid_on_stmt) |
| 60 | << A.getName() << St->getLocStart(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 61 | return nullptr; |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList, |
| 66 | SourceRange Range) { |
Alexander Kornienko | 20f6fc6 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 67 | SmallVector<const Attr*, 8> Attrs; |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 68 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 69 | if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range)) |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 70 | Attrs.push_back(a); |
| 71 | } |
| 72 | |
| 73 | if (Attrs.empty()) |
| 74 | return S; |
| 75 | |
| 76 | return ActOnAttributedStmt(Range.getBegin(), Attrs, S); |
| 77 | } |