blob: eb0188a0db3d29cffd10555b3b28c8770e4dd594 [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()) {
Michael Han6880f492012-10-03 01:56:22 +000051 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;
Sean Hunt8e083e72012-06-19 23:57:03 +000056 case AttributeList::AT_FallThrough:
Richard Smithe0d3b4c2012-05-03 18:27:39 +000057 return handleFallThroughAttr(S, St, A, Range);
Richard Smith534986f2012-04-14 00:33:13 +000058 default:
Michael Han6880f492012-10-03 01:56:22 +000059 // if we're here, then we parsed a known attribute, but didn't recognize
60 // it as a statement attribute => it is declaration attribute
Richard Smith05321402013-02-19 23:47:15 +000061 S.Diag(A.getRange().getBegin(), diag::err_attribute_invalid_on_stmt)
62 << A.getName() << St->getLocStart();
Richard Smith534986f2012-04-14 00:33:13 +000063 return 0;
64 }
65}
66
67StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
68 SourceRange Range) {
Alexander Kornienko49908902012-07-09 10:04:07 +000069 SmallVector<const Attr*, 8> Attrs;
Richard Smith534986f2012-04-14 00:33:13 +000070 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Richard Smithe0d3b4c2012-05-03 18:27:39 +000071 if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range))
Richard Smith534986f2012-04-14 00:33:13 +000072 Attrs.push_back(a);
73 }
74
75 if (Attrs.empty())
76 return S;
77
78 return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
79}