blob: 3c15b7a8afadcfb65f654cbeb32d858fe6be99e5 [file] [log] [blame]
Richard Smith534986f2012-04-14 00:33:13 +00001//===--- 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 Smithe0d3b4c2012-05-03 18:27:39 +000018#include "clang/Lex/Lexer.h"
Richard Smith534986f2012-04-14 00:33:13 +000019#include "clang/Sema/DelayedDiagnostic.h"
20#include "clang/Sema/Lookup.h"
Richard Smithe0d3b4c2012-05-03 18:27:39 +000021#include "clang/Sema/ScopeInfo.h"
Richard Smith534986f2012-04-14 00:33:13 +000022#include "llvm/ADT/StringExtras.h"
Richard Smithe0d3b4c2012-05-03 18:27:39 +000023
Richard Smith534986f2012-04-14 00:33:13 +000024using namespace clang;
25using namespace sema;
26
Richard Smithe0d3b4c2012-05-03 18:27:39 +000027static 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 Smith534986f2012-04-14 00:33:13 +000046
Richard Smithe0d3b4c2012-05-03 18:27:39 +000047
48static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
49 SourceRange Range) {
Richard Smith534986f2012-04-14 00:33:13 +000050 switch (A.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +000051 case AttributeList::AT_FallThrough:
Richard Smithe0d3b4c2012-05-03 18:27:39 +000052 return handleFallThroughAttr(S, St, A, Range);
Richard Smith534986f2012-04-14 00:33:13 +000053 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 Smithe0d3b4c2012-05-03 18:27:39 +000056 S.Diag(A.getRange().getBegin(), diag::warn_attribute_invalid_on_stmt)
57 << A.getName()->getName() << St->getLocStart();
Richard Smith534986f2012-04-14 00:33:13 +000058 return 0;
59 }
60}
61
62StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
63 SourceRange Range) {
Alexander Kornienko49908902012-07-09 10:04:07 +000064 SmallVector<const Attr*, 8> Attrs;
Richard Smith534986f2012-04-14 00:33:13 +000065 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Richard Smithe0d3b4c2012-05-03 18:27:39 +000066 if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range))
Richard Smith534986f2012-04-14 00:33:13 +000067 Attrs.push_back(a);
68 }
69
70 if (Attrs.empty())
71 return S;
72
73 return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
74}