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"
18 #include "clang/Lex/Lexer.h"
19 #include "clang/Sema/DelayedDiagnostic.h"
20 #include "clang/Sema/Lookup.h"
21 #include "clang/Sema/ScopeInfo.h"
22 #include "llvm/ADT/StringExtras.h"
23
24 using namespace clang;
25 using namespace sema;
26
handleFallThroughAttr(Sema & S,Stmt * St,const AttributeList & A,SourceRange Range)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 }
46
47
ProcessStmtAttribute(Sema & S,Stmt * St,const AttributeList & A,SourceRange Range)48 static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
49 SourceRange Range) {
50 switch (A.getKind()) {
51 case AttributeList::UnknownAttribute:
52 S.Diag(A.getLoc(), A.isDeclspecAttribute() ?
53 diag::warn_unhandled_ms_attribute_ignored :
54 diag::warn_unknown_attribute_ignored) << A.getName();
55 return 0;
56 case AttributeList::AT_FallThrough:
57 return handleFallThroughAttr(S, St, A, Range);
58 default:
59 // if we're here, then we parsed a known attribute, but didn't recognize
60 // it as a statement attribute => it is declaration attribute
61 S.Diag(A.getRange().getBegin(), diag::err_attribute_invalid_on_stmt)
62 << A.getName() << St->getLocStart();
63 return 0;
64 }
65 }
66
ProcessStmtAttributes(Stmt * S,AttributeList * AttrList,SourceRange Range)67 StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
68 SourceRange Range) {
69 SmallVector<const Attr*, 8> Attrs;
70 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
71 if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range))
72 Attrs.push_back(a);
73 }
74
75 if (Attrs.empty())
76 return S;
77
78 return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
79 }
80